-
Notifications
You must be signed in to change notification settings - Fork 1
/
EmulNet.cpp
220 lines (186 loc) · 4.85 KB
/
EmulNet.cpp
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
/**********************************
* FILE NAME: EmulNet.cpp
*
* DESCRIPTION: Emulated Network classes definition
**********************************/
#include "EmulNet.h"
/**
* Constructor
*/
EmulNet::EmulNet(Params *p)
{
//trace.funcEntry("EmulNet::EmulNet");
int i,j;
par = p;
emulnet.setNextId(1);
emulnet.settCurrBuffSize(0);
enInited=0;
for ( i = 0; i < MAX_NODES; i++ ) {
for ( j = 0; j < MAX_TIME; j++ ) {
sent_msgs[i][j] = 0;
recv_msgs[i][j] = 0;
}
}
//trace.funcExit("EmulNet::EmulNet", SUCCESS);
}
/**
* Copy constructor
*/
EmulNet::EmulNet(EmulNet &anotherEmulNet) {
int i, j;
this->par = anotherEmulNet.par;
this->enInited = anotherEmulNet.enInited;
for ( i = 0; i < MAX_NODES; i++ ) {
for ( j = 0; j < MAX_TIME; j++ ) {
this->sent_msgs[i][j] = anotherEmulNet.sent_msgs[i][j];
this->recv_msgs[i][j] = anotherEmulNet.recv_msgs[i][j];
}
}
this->emulnet = anotherEmulNet.emulnet;
}
/**
* Assignment operator overloading
*/
EmulNet& EmulNet::operator =(EmulNet &anotherEmulNet) {
int i, j;
this->par = anotherEmulNet.par;
this->enInited = anotherEmulNet.enInited;
for ( i = 0; i < MAX_NODES; i++ ) {
for ( j = 0; j < MAX_TIME; j++ ) {
this->sent_msgs[i][j] = anotherEmulNet.sent_msgs[i][j];
this->recv_msgs[i][j] = anotherEmulNet.recv_msgs[i][j];
}
}
this->emulnet = anotherEmulNet.emulnet;
return *this;
}
/**
* Destructor
*/
EmulNet::~EmulNet() {}
/**
* FUNCTION NAME: ENinit
*
* DESCRIPTION: Init the emulnet for this node
*/
void *EmulNet::ENinit(Address *myaddr, short port) {
// Initialize data structures for this member
*(int *)(myaddr->addr) = emulnet.nextid++;
*(short *)(&myaddr->addr[4]) = 0;
return myaddr;
}
/**
* FUNCTION NAME: ENsend
*
* DESCRIPTION: EmulNet send function
*
* RETURNS:
* size
*/
int EmulNet::ENsend(Address *myaddr, Address *toaddr, char *data, int size) {
en_msg *em;
static char temp[2048];
int sendmsg = rand() % 100;
if( (emulnet.currbuffsize >= ENBUFFSIZE) || (size + (int)sizeof(en_msg) >= par->MAX_MSG_SIZE) || (par->dropmsg && sendmsg < (int) (par->MSG_DROP_PROB * 100)) ) {
return 0;
}
em = (en_msg *)malloc(sizeof(en_msg) + size);
em->size = size;
memcpy(&(em->from.addr), &(myaddr->addr), sizeof(em->from.addr));
memcpy(&(em->to.addr), &(toaddr->addr), sizeof(em->from.addr));
memcpy(em + 1, data, size);
emulnet.buff[emulnet.currbuffsize++] = em;
int src = *(int *)(myaddr->addr);
int time = par->getcurrtime();
assert(src <= MAX_NODES);
assert(time < MAX_TIME);
sent_msgs[src][time]++;
#ifdef DEBUGLOG
sprintf(temp, "Sending 4+%d B msg type %d to %d.%d.%d.%d:%d ", size-4, *(int *)data, toaddr->addr[0], toaddr->addr[1], toaddr->addr[2], toaddr->addr[3], *(short *)&toaddr->addr[4]);
#endif
return size;
}
/**
* FUNCTION NAME: ENsend
*
* DESCRIPTION: EmulNet send function
*
* RETURNS:
* size
*/
int EmulNet::ENsend(Address *myaddr, Address *toaddr, string data) {
char * str = (char *) malloc(data.length() * sizeof(char));
memcpy(str, data.c_str(), data.size());
int ret = this->ENsend(myaddr, toaddr, str, (data.length() * sizeof(char)));
free(str);
return ret;
}
/**
* FUNCTION NAME: ENrecv
*
* DESCRIPTION: EmulNet receive function
*
* RETURN:
* 0
*/
int EmulNet::ENrecv(Address *myaddr, int (* enq)(void *, char *, int), struct timeval *t, int times, void *queue){
// times is always assumed to be 1
int i;
char* tmp;
int sz;
en_msg *emsg;
for( i = emulnet.currbuffsize - 1; i >= 0; i-- ) {
emsg = emulnet.buff[i];
if ( 0 == strcmp(emsg->to.addr, myaddr->addr) ) {
sz = emsg->size;
tmp = (char *) malloc(sz * sizeof(char));
memcpy(tmp, (char *)(emsg+1), sz);
emulnet.buff[i] = emulnet.buff[emulnet.currbuffsize-1];
emulnet.currbuffsize--;
(*enq)(queue, (char *)tmp, sz);
free(emsg);
int dst = *(int *)(myaddr->addr);
int time = par->getcurrtime();
assert(dst <= MAX_NODES);
assert(time < MAX_TIME);
recv_msgs[dst][time]++;
}
}
return 0;
}
/**
* FUNCTION NAME: ENcleanup
*
* DESCRIPTION: Cleanup the EmulNet. Called exactly once at the end of the program.
*/
int EmulNet::ENcleanup() {
emulnet.nextid=0;
int i, j;
int sent_total, recv_total;
FILE* file = fopen("msgcount.log", "w+");
while(emulnet.currbuffsize > 0) {
free(emulnet.buff[--emulnet.currbuffsize]);
}
for ( i = 1; i <= par->EN_GPSZ; i++ ) {
fprintf(file, "node %3d ", i);
sent_total = 0;
recv_total = 0;
for (j = 0; j < par->getcurrtime(); j++) {
sent_total += sent_msgs[i][j];
recv_total += recv_msgs[i][j];
if (i != 67) {
fprintf(file, " (%4d, %4d)", sent_msgs[i][j], recv_msgs[i][j]);
if (j % 10 == 9) {
fprintf(file, "\n ");
}
}
else {
fprintf(file, "special %4d %4d %4d\n", j, sent_msgs[i][j], recv_msgs[i][j]);
}
}
fprintf(file, "\n");
fprintf(file, "node %3d sent_total %6u recv_total %6u\n\n", i, sent_total, recv_total);
}
fclose(file);
return 0;
}