-
Notifications
You must be signed in to change notification settings - Fork 6
/
event.cc
187 lines (171 loc) · 3.78 KB
/
event.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
#include <stdlib.h>
#include <iostream>
#include "event.h"
#include "context.h"
Event::Event(double t, EventType e, void *h, void *d) :
timestamp(t), etype(e), handler(h), data(d)
{}
void Event::Dispatch()
{
switch (etype) {
case DRAW_TOPOLOGY:
((Topology*)handler)->DrawTopology();
break;
case DRAW_TREE:
((SimulationContext*)handler)->DrawShortestPathTree((Node*)data);
break;
case DRAW_PATH:
((SimulationContext*)handler)->DrawPath((Link*)data);
break;
case DUMP_TABLE:
((SimulationContext*)handler)->DumpTable((Node*)data);
break;
#if 0
case WRITE_TOPOLOGY:
((Topology*)handler)->WriteDot(*(string*)data);
break;
case WRITE_TREE:
((SimulationContext*)handler)->WriteShortestPathTreeDot(*(string*)data);
break;
#endif
case ADD_NODE:
((Topology*)handler)->AddNode((Node*)data);
break;
case DELETE_NODE:
((Topology*)handler)->DeleteNode((Node*)data);
break;
case ADD_LINK:
((Topology*)handler)->AddLink((Link*)data);
break;
case DELETE_LINK:
((Topology*)handler)->DeleteLink((Link*)data);
break;
case CHANGE_NODE:
((Topology*)handler)->ChangeNode((Node*)data);
break;
case CHANGE_LINK:
((Topology*)handler)->ChangeLink((Link*)data);
break;
case TIMEOUT:
((Node*)handler)->TimeOut();
break;
case ROUTING_MESSAGE_ARRIVAL:
((Node*)handler)->ProcessIncomingRoutingMessage((RoutingMessage*)data);
break;
case PRINT:
cout << ((char*) data);
break;
default:
cerr <<"Unknown event type\n";
exit(-1);
}
}
ostream & Event::Print(ostream &os) const
{
os <<"Event(timestamp="<<timestamp<<", etype="<<
(etype==ADD_NODE ? "ADD_NODE" :
etype==DELETE_NODE ? "DELETE_NODE" :
etype==ADD_LINK ? "ADD_LINK" :
etype==DELETE_LINK ? "DELETE_LINK" :
etype==CHANGE_NODE ? "CHANGE_NODE" :
etype==CHANGE_LINK ? "CHANGE_LINK" :
#if 0
etype==WRITE_TOPOLOGY ? "WRITE_TOPOLOGY" :
etype==WRITE_TREE ? "WRITE_TREE" :
#endif
etype==DRAW_TOPOLOGY ? "DRAW_TOPOLOGY" :
etype==DRAW_TREE ? "DRAW_TREE" :
etype==DRAW_PATH ? "DRAW_PATH" :
etype==DUMP_TABLE ? "DUMP_TABLE" :
etype==TIMEOUT ? "TIMEOUT" :
etype==PRINT ? "PRINT" :
etype==ROUTING_MESSAGE_ARRIVAL ? "ROUTING_MESSAGE_ARRIVAL" :
"UNKNOWN") << ", ";
switch (etype) {
case DRAW_TOPOLOGY:
break;
case ADD_NODE:
case DELETE_NODE:
case CHANGE_NODE:
case DRAW_TREE:
case DUMP_TABLE:
case TIMEOUT:
os << *((Node*)data);
break;
case DRAW_PATH:
os << *((Link*)data);
break;
#if 0
case WRITE_TOPOLOGY:
case WRITE_TREE:
os <<*((string*)data);
break;
#endif
case ADD_LINK:
case DELETE_LINK:
case CHANGE_LINK:
os << *((Link*)data);
break;
case ROUTING_MESSAGE_ARRIVAL:
os <<*((RoutingMessage*)data);
break;
case PRINT:
os <<"\""<<((char*)data)<<"\"";
break;
default:
os << "Unknown()";
}
os <<")";
return os;
}
void Event::Disassociate()
{
handler=0;
data=0;
}
double Event::GetTimeStamp()
{
return timestamp;
}
Event::~Event()
{
if (data) {
switch (etype) {
case DRAW_TOPOLOGY:
break;
#if 0
case WRITE_TOPOLOGY:
case WRITE_TREE:
delete (string *)data;
break;
#endif
case DRAW_TREE:
case ADD_NODE:
case DELETE_NODE:
case CHANGE_NODE:
case DUMP_TABLE:
case TIMEOUT:
delete (Node *) data;
break;
case ADD_LINK:
case DELETE_LINK:
case CHANGE_LINK:
case DRAW_PATH:
delete (Link *) data;
break;
case ROUTING_MESSAGE_ARRIVAL:
delete (RoutingMessage*) data;
break;
case PRINT:
delete [] (char*) data;
break;
default:
cerr <<"Unknown event type\n";
exit(-1);
}
}
}
bool Event::IsLater(const Event *rhs) const
{
return timestamp>rhs->timestamp;
}