-
Notifications
You must be signed in to change notification settings - Fork 6
/
context.cc
354 lines (322 loc) · 10.1 KB
/
context.cc
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <map>
#include "context.h"
SimulationContext::SimulationContext()
{}
SimulationContext::~SimulationContext()
{}
void SimulationContext::LoadEvents(const string &file)
{
FILE *in = fopen(file.c_str(),"r");
if (in==0) {
cerr << "Can't read events from "<<file<<endl;
exit(-1);
}
while (!feof(in)) {
char b[1024];
char *buf=b;
char cmd[1024];
unsigned num;
unsigned src,dest;
double lat, bw;
double timestamp;
if (!fgets(buf,1024,in)) {
break;
}
if (strlen(buf)==0){
continue;
}
while (isspace(*buf)) {
buf++;
}
if (*buf==0) {
continue;
}
if (toupper(buf[0])=='#') {
continue;
}
//fprintf(stderr,"%s",buf);
sscanf(buf,"%lf %s",×tamp,cmd);
if (!strcasecmp(cmd,"DRAW_TOPOLOGY")) {
sscanf(buf,"%lf %s",×tamp,cmd);
PostEvent(new Event(timestamp,DRAW_TOPOLOGY,this,0));
continue;
}
if (!strcasecmp(cmd,"DUMP_TABLE")) {
sscanf(buf,"%lf %s %u",×tamp,cmd,&num);
PostEvent(new Event(timestamp,DUMP_TABLE,this,new Node(num,this,0,0)));
continue;
}
if (!strcasecmp(cmd,"DRAW_TREE")) {
sscanf(buf,"%lf %s %u",×tamp,cmd,&num);
PostEvent(new Event(timestamp,DRAW_TREE,this,new Node(num,this,0,0)));
continue;
}
if (!strcasecmp(cmd,"DRAW_PATH")) {
sscanf(buf,"%lf %s %u %u",×tamp,cmd,&src,&dest);
PostEvent(new Event(timestamp,DRAW_PATH,this,new Link(src,dest,this,0,0)));
continue;
}
#if 0
if (!strcasecmp(cmd,"WRITE_TOPOLOGY")) {
char file[1024];
sscanf(buf,"%lf %s %s",×tamp,cmd,file);
PostEvent(new Event(timestamp,WRITE_TOPOLOGY,this,new string(file)));
continue;
}
if (!strcasecmp(cmd,"WRITE_TREE")) {
char file[1024];
sscanf(buf,"%lf %s %s",×tamp,cmd,file);
PostEvent(new Event(timestamp,WRITE_TREE,this,new string(file)));
continue;
}
#endif
if (!strcasecmp(cmd,"ADD_NODE")) {
sscanf(buf,"%lf %s %u %lf %lf",×tamp,cmd,&num,&lat,&bw);
PostEvent(new Event(timestamp,ADD_NODE,this,new Node(num,this,bw,lat)));
continue;
}
if (!strcasecmp(cmd,"DELETE_NODE")) {
sscanf(buf,"%lf %s %u %lf %lf",×tamp,cmd,&num,&lat,&bw);
PostEvent(new Event(timestamp,DELETE_NODE,this,new Node(num,this,bw,lat)));
continue;
}
if (!strcasecmp(cmd,"CHANGE_NODE")) {
sscanf(buf,"%lf %s %u %lf %lf",×tamp,cmd,&num,&lat,&bw);
PostEvent(new Event(timestamp,CHANGE_NODE,this,new Node(num,this,bw,lat)));
continue;
}
if (!strcasecmp(cmd,"ADD_LINK")) {
sscanf(buf,"%lf %s %u %u %lf %lf",×tamp,cmd,&src,&dest,&lat,&bw);
PostEvent(new Event(timestamp,ADD_LINK,this,new Link(src,dest,this,bw,lat)));
continue;
}
if (!strcasecmp(cmd,"DELETE_LINK")) {
sscanf(buf,"%lf %s %u %u %lf %lf",×tamp,cmd,&src,&dest,&lat,&bw);
PostEvent(new Event(timestamp,DELETE_LINK,this,new Link(src,dest,this,bw,lat)));
continue;
}
if (!strcasecmp(cmd,"CHANGE_LINK")) {
sscanf(buf,"%lf %s %u %u %lf %lf",×tamp,cmd,&src,&dest,&lat,&bw);
PostEvent(new Event(timestamp,CHANGE_LINK,this,new Link(src,dest,this,bw,lat)));
continue;
}
if (!strcasecmp(cmd,"PRINT")) {
sscanf(buf,"%lf %s",×tamp,cmd);
char *start;
char *data = new char [1024];
start=strstr(buf,"PRINT")+5;
while (*start!=0 && isspace(*start)) {
start++;
}
if (*start==0) {
strncpy(data,"Nothing to print!",1024);
} else {
strncpy(data,start,1024);
}
PostEvent(new Event(timestamp,PRINT,this,data));
continue;
}
}
fclose(in);
}
void SimulationContext::Init()
{
for (deque<Link*>::const_iterator i=links.begin();i!=links.end();++i) {
//PostEvent(new Event(0x80000000, CHANGE_LINK,this,new Link(**i)));
PostEvent(new Event(-100, CHANGE_LINK,this,new Link(**i)));
}
}
void SimulationContext::LoadTopology(const string &file)
{
LoadEvents(file);
Event *e;
while ((e=GetEarliestEvent())) {
e->Dispatch();
e->Disassociate();
delete e;
}
}
ostream & SimulationContext::Print(ostream &os)
{
os << "SimulationContext(topology=";
Topology::Print(os);
os <<", eventqueue=";
EventQueue::Print(os);
os<<")";
return os;
}
void SimulationContext::DrawShortestPathTree(const Node *node) const
{
WriteShortestPathTreeDot(node,"_tree.in");
system("dot _tree.in > _tree.out");
system("dotty _tree.out");
}
void SimulationContext::DispatchEvent(Event *e)
{
e->Dispatch();
e->Disassociate();
delete e;
}
// This is just used by the map container to build a search tree.
// It defines a consistent "total ordering" of the links.
struct link_compare {
bool operator() ( const Link &l, const Link &r) const {
return (l.GetSrc()<r.GetSrc()) || (l.GetSrc()==r.GetSrc() && l.GetDest()<r.GetDest());
}
};
void SimulationContext::WriteShortestPathTreeDot(const Node *src, const string &s) const
{
cerr << "********BEGIN WRITE TREE"<<endl;
FILE *out = fopen(s.c_str(),"w");
if (out==0) {
return;
}
// Yes, this is hideously slow
map<Link, int, link_compare> treelinks;
deque<Link> path;
for (deque<Node*>::const_iterator i=nodes.begin();i!=nodes.end();++i) {
path.clear();
CollectPathLinks(*src,**i,path);
for (deque<Link>::const_iterator j=path.begin();j!=path.end();++j) {
treelinks[*j]=1;
}
}
deque<Link> realtree;
((SimulationContext*)this)->CollectShortestPathTreeLinks(*src,realtree);
fprintf(out,"digraph tree {\n");
for (deque<Node*>::const_iterator i=nodes.begin(); i!=nodes.end();++i) {
fprintf(out,"%u\n",(*i)->GetNumber());
}
for (deque<Link*>::const_iterator i=links.begin(); i!=links.end();++i) {
fprintf(out,"%u -> %u [ label=\"%5.1lf\" ];\n",(*i)->GetSrc(),(*i)->GetDest(), (*i)->GetLatency());
}
for (deque<Link>::const_iterator i=realtree.begin(); i!=realtree.end();++i) {
fprintf(out,"%u -> %u [ color=blue ];\n",(*i).GetSrc(),(*i).GetDest());
}
for (map<Link,int,link_compare>::const_iterator i=treelinks.begin();i!=treelinks.end();++i) {
Link l = (*i).first;
fprintf(out,"%u -> %u [ color=red ];\n",l.GetSrc(),l.GetDest());
bool found=false;
for (deque<Link>::const_iterator j=realtree.begin(); j!=realtree.end();++j) {
if ((*j).GetSrc()==l.GetSrc() && (*j).GetDest()==l.GetDest()) {
found=true;
break;
}
}
if (!found) {
cout << "SUSPICIOUS: "<<l.GetSrc()<<" -> "<<l.GetDest()<<" not found in actual shortest paths tree"<<endl;
}
}
fprintf(out,"}\n");
fclose(out);
cerr << "********END WRITE TREE"<<endl;
}
void SimulationContext::WritePathDot(const Node &src, const Node &dest, const string &s) const
{
cerr << "********BEGIN WRITE PATH"<<endl;
FILE *out = fopen(s.c_str(),"w");
if (out==0) {
return;
}
deque<Link> path;
CollectPathLinks(src,dest,path);
deque<Link> realpath;
((SimulationContext*)this)->CollectShortestPathLinks(src,dest,realpath);
fprintf(out,"digraph path {\n");
for (deque<Node*>::const_iterator i=nodes.begin(); i!=nodes.end();++i) {
fprintf(out,"%u\n",(*i)->GetNumber());
}
for (deque<Link*>::const_iterator i=links.begin(); i!=links.end();++i) {
fprintf(out,"%u -> %u [ label=\"%5.1lf\" ];\n",(*i)->GetSrc(),(*i)->GetDest(), (*i)->GetLatency());
}
for (deque<Link>::const_iterator i=realpath.begin();i!=realpath.end();++i) {
fprintf(out,"%u -> %u [ color=blue ];\n",(*i).GetSrc(),(*i).GetDest());
}
for (deque<Link>::const_iterator i=path.begin();i!=path.end();++i) {
fprintf(out,"%u -> %u [ color=red ];\n",(*i).GetSrc(),(*i).GetDest());
Link l =(*i);
bool found=false;
for (deque<Link>::const_iterator j=realpath.begin(); j!=realpath.end();++j) {
if ((*j).GetSrc()==l.GetSrc() && (*j).GetDest()==l.GetDest()) {
found=true;
break;
}
}
if (!found) {
cout << "SUSPICIOUS: "<<l.GetSrc()<<" -> "<<l.GetDest()<<" not found in actual shortest paths tree"<<endl;
}
}
fprintf(out,"}\n");
fclose(out);
cerr << "********END WRITE PATH"<<endl;
}
void SimulationContext::DrawPath(const Link *p) const
{
WritePathDot(Node(p->GetSrc(),0,0,0),Node(p->GetDest(),0,0,0),string("_path.in"));
system("dot _path.in > _path.out");
system("dotty _path.out");
}
void SimulationContext::CollectPathLinks(const Node &src, const Node &dest, deque<Link> &path) const
{
Node *n=((SimulationContext*)this)->FindMatchingNode(&src);
if (n==0) {
return;
}
unsigned last=n->GetNumber();
unsigned count=0;
while (n->GetNumber()!=dest.GetNumber()) {
Node *next_node=n->GetNextHop(&dest);
if (next_node==0) {
break;
}
n=((SimulationContext *)this)->FindMatchingNode(next_node);
if (n==0) {
delete next_node;
break;
}
// cerr << last <<" -> " << n->GetNumber()<<endl;
path.push_back(Link(last,n->GetNumber(),0,0,0));
last=n->GetNumber();
delete next_node;
count++;
if (count>(nodes.size()*2)) {
cerr << "SimulationContext::CollectPathLinks terminating prematurely due to suspected routing loop!\n";
break;
}
}
}
void SimulationContext::DumpTable(const Node *src)
{
cout <<*(FindMatchingNode(src))<< endl;
}
void SimulationContext::TimeOut(const Node *src, const double timefromnow)
{
PostEvent(new Event(GetTime()+timefromnow,
TIMEOUT,
FindMatchingNode(src),
0));
}
void SimulationContext::SendToNeighbors(const Node *src, const RoutingMessage *m)
{
deque<Link*> *ll=GetOutgoingLinks(src);
for (deque<Link*>::const_iterator i=ll->begin();i!=ll->end();++i) {
Node x = Node((*i)->GetDest(),0,0,0);
PostEvent(new Event(GetTime()+(*i)->GetLatency(),
ROUTING_MESSAGE_ARRIVAL,
FindMatchingNode(&x),
(void*)m));
}
delete ll;
}
void SimulationContext::SendToNeighbor(const Node *src, const Node *dest, const RoutingMessage *m)
{
Link x = Link(src->GetNumber(),dest->GetNumber(),0,0,0);
Link *l = FindMatchingLink(&x);
PostEvent(new Event(GetTime()+l->GetLatency(),
ROUTING_MESSAGE_ARRIVAL,
FindMatchingNode(dest),
(void*)m));
}