-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataSegment.cpp
More file actions
78 lines (68 loc) · 2.89 KB
/
DataSegment.cpp
File metadata and controls
78 lines (68 loc) · 2.89 KB
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
#include "DataSegment.hpp"
// Overloaded constructor to create data segment
// status = 'D' for data packet
// status = 'C' for control packet
DataSegment::DataSegment(char status, int sourcePort, int destPort, int length, string payload){
header.sourcePort = sourcePort;
header.destPort = destPort;
header.status = status;
header.payloadLength = length;
this->payload = payload;
return;
}
// Overloaded constructor which takes a string and parses it into a data segment object
DataSegment::DataSegment(string dataSegment){
// parse the string
char delimitter = ' ';
unsigned long position = string::npos;
// set status
if ( (position = dataSegment.find(delimitter)) != string::npos ){
header.status = dataSegment.substr(0, position)[0]; // index 0 because I am assinging string type to character
dataSegment.erase(0, position + 1);
}
// set sourcePort
if ( (position = dataSegment.find(delimitter)) != string::npos ){
header.sourcePort = stoi(dataSegment.substr(0, position)); // convert string representation of int to int type
dataSegment.erase(0, position + 1);
}
// set destPort
if ( (position = dataSegment.find(delimitter)) != string::npos ){
header.destPort = stoi(dataSegment.substr(0, position)); // convert string representation of int to int type
dataSegment.erase(0, position + 1);
}
// set length
delimitter = '\n';
if ( (position = dataSegment.find(delimitter)) != string::npos ){
header.payloadLength = stoi(dataSegment.substr(0, position)); // convert string representation of int to int type
dataSegment.erase(0, position + 1);
}
// set payload
if ( (position = dataSegment.find(delimitter)) != string::npos ){
this->payload = dataSegment.substr(0, dataSegment.length() -1); // convert string representation of int to int type
dataSegment.erase(0, position + 1);
}
}
string DataSegment::constructDataSegment(){
string out = "";
out.push_back(header.status);
out.append(" ");
out.append(to_string(header.sourcePort));
out.append(" ");
out.append(to_string(header.destPort));
out.append(" ");
out.append(to_string(header.payloadLength));
out.push_back('\n');
out.append(payload);
out.push_back('\n');
return out;
}
void DataSegment::printDataSegment(){
cout << " ---------------------------------------------------------------------------" << endl;
cout << "| Status " << header.status
<< " | SourcePort " << header.sourcePort
<< " | DestinationPort " << header.destPort
<< " | Length " << header.payloadLength << " |" << endl
<< " ---------------------------------------------------------------------------" << endl
<< "| " << payload << endl
<< " ---------------------------------------------------------------------------" << endl;
}