-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.cpp
More file actions
143 lines (133 loc) · 3.93 KB
/
Graph.cpp
File metadata and controls
143 lines (133 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "Graph.h"
map<string,HostAddr> labels;
int numberOfNodes;
list<IP_Address> nodesList;
string currentHostName;
int **costMatrix;
void whoAmI(){
char *hostname = (char*)calloc(sizeof(char),1024);
gethostname(hostname,1024);
//cout << hostname;
//rootnode.name="bandicoot.soic.indiana.edu";
hostName = hostname;
labels[hostName] = *(getHostIp(hostName));
nodesList.push_back(hostName);
}
void createGraph(char *file){
numberOfNodes = 1;
fstream ifile;
string line, nodeName;
int position;
ifile.open(file,ios::in);
if (!ifile)
{
cout << "No config file found!! \n Program Terminating";
exit(0);
}
else
{
ifile.unsetf(ios_base::skipws);
while (getline(ifile,line))
{
// cout << line<<"\n";
if(line.empty()){continue;}
numberOfNodes++;
position = line.find(" ");
nodeName = line.substr(0,position);
nodesList.push_back(nodeName);
labels[nodeName] = *(getHostIp(nodeName));
if(strcasecmp(line.substr(position+1).c_str(),"yes")==0){
neighbors.push_back(nodeName);
}
}
}
initializeSingleSourceGraph();
}
void initializeSingleSourceGraph(){
int indexRow,indexColumn;
costMatrix = (int**)malloc(sizeof(int*)*numberOfNodes);
list<IP_Address>:: iterator colIterator,rowIterator;
for(rowIterator = nodesList.begin(),indexRow=0;rowIterator!=nodesList.end();++rowIterator,++indexRow){
string rowNode = *rowIterator;
costMatrix[indexRow] = (int*)malloc(sizeof(int)*numberOfNodes);
for(colIterator = nodesList.begin(),indexColumn=0;colIterator!=nodesList.end();++colIterator,++indexColumn){
string colNode = *colIterator;
costMatrix[indexRow][indexColumn] =infinity;
}
}
costMatrix[findIndex(hostName)][findIndex(hostName)]=0;
list<IP_Address>:: iterator iterator;
indexRow = findIndex(hostName);
for(iterator = neighbors.begin();iterator!=neighbors.end();++iterator){
string node = *iterator;
indexColumn = findIndex(node);
costMatrix[indexRow][indexColumn] = 1;
}
constructRoutingTable();
updateRoutingTable(0);
}
void printGraph(){
int indexRow,indexColumn;
list<IP_Address>:: iterator colIterator,rowIterator;
list<IP_Address>:: iterator it;
cout<<"\t";
for(it = nodesList.begin();it!=nodesList.end();++it){
string node = *it;
cout<<node.substr(0,2)<<"\t";
}
cout << "\n";
for(rowIterator = nodesList.begin(),indexRow=0;rowIterator!=nodesList.end();++rowIterator,++indexRow){
string rowNode = *rowIterator;
cout << rowNode.substr(0,2)<<"\t";
for(colIterator = nodesList.begin(),indexColumn=0;colIterator!=nodesList.end();++colIterator,++indexColumn){
string colNode = *colIterator;
cout<<costMatrix[indexRow][indexColumn]<<"\t";
}
cout<<"\n";
}
}
int findIndex(IP_Address host){
int index = -1;
list<IP_Address>:: iterator iterator;
for(iterator = nodesList.begin(),index=0;iterator!=nodesList.end();++iterator,++index){
string node = *iterator;
if(host.compare(node)==0){
return index;
}
}
return index;
}
void lostNode(IP_Address host){
int rowIndex = findIndex(host);
costMatrix[findIndex(hostName)][rowIndex] = infinity;
RouteEntry entry = RoutingTable[host];
entry.TTL = ttl;
entry.cost = infinity;
entry.nextHop = "NULL";
RoutingTable[host] = entry;
list<IP_Address>:: iterator it1;
for(it1 = nodesList.begin();it1!=nodesList.end();++it1){
string node = *it1;
costMatrix[rowIndex][findIndex(node)] = infinity;
}
}
int updateNode(AdversisementEntry tempNode,IP_Address host){
int rowIndex = findIndex(host);
int colIndex = findIndex(tempNode.ipAddr);
int oldCost;
costMatrix[findIndex(hostName)][rowIndex] =1;
//cout <<rowIndex<<" "<<colIndex <<" "<<tempNode.cost<<endl;
costMatrix[rowIndex][colIndex] = tempNode.cost;
RouteEntry tempEntry = RoutingTable[tempNode.ipAddr];
oldCost = tempEntry.cost;
if(tempEntry.nextHop.compare(host)==0){
if(tempNode.cost+1!=tempEntry.cost){
tempEntry.cost = infinity;
tempEntry.nextHop = "NULL";
RoutingTable[tempNode.ipAddr] = tempEntry;
if(oldCost != infinity)
return 1;
}
}
return 0;
}