-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPacket.cpp
365 lines (313 loc) · 7.04 KB
/
Packet.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
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
355
356
357
358
359
360
361
362
363
364
365
/**
* @Author: Alec Thompson
*
* Packet.cpp
*
* Source file for packets
*/
#include <iostream>
#include <cstdlib>
#include "Node.h"
#include "Packet.h"
#include "Sender.h"
using std::cout;
using std::endl;
using std::exit;
using namespace std;
/**
* @Author: Alec Thompson
*
* Packet Constructor
*
* @param id ID of the source router
* @param s_time packet timestamp
* @param size packet size
* @param head pointer to the head of the SR list
*/
Packet::Packet() {
source_id = 0;
timestamp = 0;
pkt_size = 0;
headPtr = NULL;
tailPtr = NULL;
nextPtr = NULL;
}
Packet::Packet(int id, int s_time, int size) {
source_id = id;
timestamp = s_time;
pkt_size = size;
headPtr = NULL;
tailPtr = NULL;
nextPtr = NULL;
}
Packet::Packet(Sender *original, int t) {
#if DEBUG
cout << "Creating new packet based off of Sender " << original << endl;
original->printSender();
#endif
source_id = original->getID();
timestamp = t;
pkt_size = original->getPktSize();
headPtr = NULL;
tailPtr = NULL;
nextPtr = NULL;
#if DEBUG
cout << "Packet without SR queue" << endl;
printPacket();
cout << "Copying queue now" << endl;
#endif
// copy the Sender's SR queue to the packet
this->copyQueue(original);
#if DEBUG
cout << "Packet with SR queue" << endl;
printPacket();
#endif
}
/**
* @Author: Alec Thompson
*
* Destructor
*/
Packet::~Packet() {
Node *a, *b;
// destruct the list of nodes
a = headPtr;
while (a != NULL) {
b = a->nextPtr;
delete a;
a = b;
}
}
/**
* @Author: Alec Thompson
*
* Sets the packets ID
*/
void Packet::setID(int nID) {
source_id = nID;
}
/**
* @Author: Alec Thompson
*
* Gets the packets ID
*/
int Packet::getID() {
return source_id;
}
/** @Author: Alec Thompson */
/** Sets the packets timestamp */
void Packet::setTimestamp(int t) {
timestamp = t;
}
/** @Author: Alec Thompson */
/** Gets the packets timestamp */
int Packet::getTimestamp() {
return timestamp;
}
/** @Author: Alec Thompson */
/** Sets the packets size */
void Packet::setSize(int s) {
pkt_size = s;
}
/** @Author: Alec Thompson */
/** Gets the packets size */
int Packet::getSize() {
return pkt_size;
}
/** @Author: Alec Thompson */
/** Sets the packets propagation time (calculated in EventList) */
void Packet::setProp(int pTime) {
delay = pTime;
}
/**
* @Author: Alec Thompson
*
* Sets the delay (time of arrival at receiver - timestamp)
*
* @param t time of arrival at receiver
*/
void Packet::setDelay(int t) {
delay = t - timestamp;
}
/** @Author: Alec Thompson */
/** Gets the delay */
int Packet::getDelay() {
return delay;
}
/** @Author: Alec Thompson */
/** Gets the pointer to the head of the SR queue */
Node *Packet::getHead() {
return headPtr;
}
/** @Author: Alec Thompson */
/** Gets the pointer to the tail of the SR queue */
Node *Packet::getTail() {
return tailPtr;
}
/** @Author: Alec Thompson */
/** Gets the pointer to the next */
Packet *Packet::getNext() {
return this->nextPtr;
}
/** @Author: Alec Thompson */
/** Copies the node queue from a template packet */
void Packet::copyQueue(Packet original) {
Node *currentPtr = original.getHead();
while (currentPtr != NULL) {
this->enqueue(currentPtr->getData());
currentPtr = currentPtr->getNext();
}
}
/** @Author: Alec Thompson */
/** Copies the node queue from a template sender */
void Packet::copyQueue(Sender *original) {
Node *currentPtr = original->getSRHead();
while (currentPtr != NULL) {
#if DEBUG
cout << "---------------------------------------------" << endl;
original->printNodes();
cout << "---------------------------------------------" << endl;
cout << "Enqueuing " << currentPtr->getData() << endl;
currentPtr->printNode();
#endif
this->enqueue(currentPtr->getData());
#if DEBUG
cout << currentPtr->getData() << " enqueued" << endl;
currentPtr->printNode();
#endif
currentPtr = currentPtr->getNext();
#if DEBUG
cout << "---------------------------------------------" << endl;
original->printNodes();
cout << "---------------------------------------------" << endl;
#endif
}
}
/**
* @Author: Alec Thompson
*
* Enqueues the integer in the SR queue
*
* @param sr_id ID of the router to be enqueued
*/
void Packet::enqueue(int sr_id) {
#if DEBUG
cout << "Enquing SR ID " << sr_id << endl;
cout << "HeadPtr: " << headPtr << endl;
cout << "TailPtr: " << tailPtr << endl;
#endif
if (headPtr == NULL) {
#if DEBUG
cout << "SR List is empty" << endl;
#endif
// the router list is empty
headPtr = new Node(sr_id);
#if DEBUG
cout << "New HeadPtr: " << headPtr << endl;
#endif
// the memory was successfully allocated
#if DEBUG
cout << "New TailPtr: " << tailPtr << endl;
#endif
} else if (tailPtr == NULL) {
headPtr->nextPtr = new Node(sr_id);
tailPtr = headPtr->nextPtr;
#if DEBUG
cout << "New TailPtr: " << tailPtr << endl;
#endif
} else {
#if DEBUG
cout << "SR List is not empty" << endl;
#endif
// the router list is not empty
tailPtr->nextPtr = new Node(sr_id);
#if DEBUG
cout << "New TailPtr: " << tailPtr << endl;
#endif
if (tailPtr->nextPtr != NULL) {
// memory successfully allocated
tailPtr = tailPtr->nextPtr;
} else {
cout << "Memory Allocation Failed. Router " << sr_id;
cout << " Not Added." << endl;
}
}
}
/**
* @Author: Alec Thompson
*
* Dequeues the first router ID in the queue and returns it.
*
* @return Router ID
*/
int Packet::dequeue() {
Node *tempPtr;
int returnVal;
tempPtr = headPtr;
if (tempPtr != NULL) {
// the queue is not empty
returnVal = tempPtr->data;
headPtr = tempPtr->nextPtr;
if (headPtr == NULL) {
// the list is now empty
tailPtr = headPtr;
}
// deallocate the memory
delete tempPtr;
return returnVal;
} else {
cout << "List is empty. No items dequeued." << endl;
cout << "Quitting the program." << endl;
exit(1);
}
}
/**
* @Author: Alec Thompson
*
* Dequeues the first node in the queue and returns a pointer to it
*
* @return Pointer to the just dequeued node
*/
Node *Packet::dequeueNode() {
Node *tempPtr;
tempPtr = headPtr;
if (tempPtr != NULL) {
// the queue is not empty
headPtr = tempPtr->nextPtr;
if (headPtr == NULL) {
// the list is now empty
tailPtr = headPtr;
}
return tempPtr;
} else {
cout << "List is empty. No items dequeued." << endl;
cout << "Quitting the program." << endl;
exit(1);
}
}
/** @Author: Alec Thompson */
/** Finds the length of the node queue */
int Packet::nLength() {
Node *currentPtr = headPtr;
int length = 0;
while (currentPtr != NULL) {
++length;
currentPtr = currentPtr->nextPtr;
}
return length;
}
/** @Author: Alec Thompson */
/** Prints the packet queue */
void Packet::printPacket() {
Node *currentPtr = headPtr;
// print out the contents of the packet
cout << "Source ID: " << source_id << endl;
cout << "Timestamp: " << timestamp << endl;
cout << "Packet Size: " << pkt_size << endl;
cout << "Delay: " << delay << endl;
cout << "Nodes: " << endl;
while (currentPtr != NULL) {
currentPtr->printNode();
currentPtr = currentPtr->nextPtr;
}
}