/
packet.cpp
executable file
·242 lines (164 loc) · 4.68 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
#include "packet.h"
// Constructs a new packet object from the parameters
// type - The type of packet
// lifeTime - The number of seconds before the packet will expire
// packetNumber - The number of this packet
// sourceAddress - The source address of the packet
// destinationAddress - The destination address of the packet
// payload - The payload of the packet
Packet::Packet(int packetNumberParam, int lifeTimeParam, string sourceAddressParam, string destinationAddressParam, string payloadParam){
time_t tempTime;
time(&tempTime);
setTimeSent(tempTime);
setHopCount(0);
lifeTime = lifeTimeParam;
packetNumber = packetNumberParam;
sourceAddress = sourceAddressParam;
destinationAddress = destinationAddressParam;
payload = payloadParam;
}
Packet::Packet(string packetData){
// Parse the packet into the object
stringstream deserializer(packetData);
string type;
deserializer >> type;
string d;
string t;
string dateTime;
deserializer >> d;
deserializer >> t;
stringstream dateTimeStream;
dateTimeStream << d << " " << t;
dateTime = dateTimeStream.str();
const char * timeBuffer =dateTime.c_str();
tm date;
strptime(timeBuffer, "%Y-%m-%d %H:%M:%S", &date);
time_t packetTime = mktime(&date);
setTimeSent(packetTime);
int life;
deserializer >> life;
setLifeTime(life);
int pno;
deserializer >> pno;
setPacketNumber(pno);
int hop;
deserializer >> hop;
hop++;
setHopCount(hop);
string src;
deserializer >> src;
setSourceAddress(src);
string dest;
deserializer >> dest;
setDestinationAddress(dest);
stringstream payloadStream;
payloadStream << deserializer.rdbuf();
string pay = payloadStream.str();
pay = pay.substr(1, pay.length());
setPayload(pay);
}
// Returns the type of packet represented by this object
string Packet::getType() const{
return string("Packet");
}
// Returns whether or not this packet is expired by looking at the associated time of expiry
bool Packet::isExpired() const{
/*
time_t now;
time_t then = getTimeSent();
time(&now);
double difference=difftime(now, then);
if(difference > getLifeTime()){
return true;
}
return false;
*/
if(hopCount > 30){
return true;
}
return false;
}
// Returns the number of the packet represented by this object
int Packet::getPacketNumber() const{
return packetNumber;
}
// Returns the source address of the packet represented by this object
string Packet::getSourceAddress() const{
return sourceAddress;
}
// Returns the destination address of the packet represented by this object
string Packet::getDestinationAddress() const{
return destinationAddress;
}
// Returns the payload contained within the packet represented by this object
string Packet::getPayload() const{
return payload;
}
// Returns the number of hops that this packet has made
int Packet::getHopCount() const{
return hopCount;
}
string Packet::toString() const{
// Serialize this object for the network
string delimiter = "\n";
stringstream serialization;
serialization << getType();
serialization << delimiter;
struct tm *nowtm;
char tmbuf[64] ;
nowtm = localtime(&timeSent);
strftime(tmbuf, sizeof tmbuf, "%Y-%m-%d %H:%M:%S", nowtm);
string timeString (tmbuf);
serialization << timeString;
serialization << delimiter;
serialization << getLifeTime();
serialization << delimiter;
serialization << getPacketNumber();
serialization << delimiter;
serialization << getHopCount();
serialization << delimiter;
serialization << getSourceAddress();
serialization << delimiter;
serialization << getDestinationAddress();
serialization << delimiter;
serialization << getPayload();
return serialization.str();
}
time_t Packet::getTimeSent() const{
return timeSent;
}
/*
Private Methods
*/
// Sets the time at which this packet was sent
void Packet::setTimeSent(time_t newTimeSent){
timeSent = newTimeSent;
}
// Sets the number of this packet
void Packet::setPacketNumber(int newPacketNumber){
packetNumber = newPacketNumber;
}
// Sets the source address of the packet represented by this object
void Packet::setSourceAddress(string newSourceAddress){
sourceAddress = newSourceAddress;
}
// Sets the destination address of the packet represented by this object
void Packet::setDestinationAddress(string newDestinationAddress){
destinationAddress = newDestinationAddress;
}
// Sets the payload of this packet
void Packet::setPayload(string newPayload){
payload = newPayload;
}
// Sets the hop count of this packet
void Packet::setHopCount(int newHopCount){
hopCount = newHopCount;
}
// Sets the lifetime of this packet
void Packet::setLifeTime(int newLifeTime){
lifeTime = newLifeTime;
}
// Returns the lifetime of this packet
int Packet::getLifeTime() const{
return lifeTime;
}