-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcomm.cc
248 lines (213 loc) · 5.62 KB
/
comm.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
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
#include "comm.hh"
CommMessage::CommMessage(){
message = "";
command = "";
clientID = "";
serverID = "";
timestamp = "";
version = "1.0";
count = 0;
size = 0;
}
void CommMessage::updateMessage(std::string msg){
message = msg;
}
bool CommMessage::parse(){
char tempa[30],tempb[30];
deviceIDs.clear();
std::stringstream ss(message);
std::string line;
while(std::getline(ss,line)){
if(sscanf(line.c_str(),"%s IoTPS\\%s",tempa,tempb) == 2){
command = tempa;
version = tempb;
continue;
}
else if(sscanf(line.c_str(),"ClientID: %s",tempa) == 1){
clientID = tempa;
continue;
}
else if(sscanf(line.c_str(),"ServerID: %s",tempa) == 1){
serverID = tempa;
continue;
}
else if(sscanf(line.c_str(),"DeviceID: %s",tempa) == 1){
deviceIDs.push_back(tempa);
continue;
}
else if(sscanf(line.c_str(),"Count: %s",tempa) == 1){
count = (size_t)strtol(tempa,NULL,10);
continue;
}
else if(sscanf(line.c_str(),"Size: %s",tempa) == 1){
size = (size_t)strtol(tempa,NULL,10);
continue;
}
else if(sscanf(line.c_str(),"SeqNumber: %s",tempa) == 1){
seqno = (size_t)strtol(tempa,NULL,10);
continue;
}
else if(sscanf(line.c_str(),"TimeStamp: %s",tempa) == 1){
timestamp = tempa;
continue;
}
}
return true;
}
bool CommMessage::sanityCheck(){
// check for errors
}
void CommMessage::print(){
//std::vector<std::string>::iterator itr;
//std::cout<<"Command: "<<command<<std::endl<<"Version: "<<version<<std::endl<<"ClientId: "<<clientID<<std::endl \
<<"ServerID: "<<serverID<<std::endl<<"Count: "<<count<<std::endl<<"Size: "<<size<<std::endl;
//for(itr = deviceIDs.begin();itr != deviceIDs.end();itr++)
//std::cout<<*itr<<std::endl;
std::cout<<message<<std::endl;
}
// Get
std::string CommMessage::getCommand(){
return command;
}
std::string CommMessage::getVersion(){
return version;
}
std::string CommMessage::getClientID(){
return clientID;
}
std::string CommMessage::getServerID(){
return serverID;
}
size_t CommMessage::getSeqNumber(){
return seqno;
}
std::string CommMessage::getTimeStamp(){
return timestamp;
}
size_t CommMessage::getCount(){
return count;
}
size_t CommMessage::getSize(){
return size;
}
std::vector<std::string> CommMessage::getDeviceIDs(){
return deviceIDs;
}
std::string CommMessage::getMessage(){
return message;
}
//Updates
void CommMessage::updateVersion(std::string ver){
version = ver;
}
void CommMessage::updateClientID(std::string cID){
clientID = cID;
}
void CommMessage::updateServerID(std::string sID){
serverID = sID;
}
void CommMessage::updateCount(size_t c){
count = c;
}
void CommMessage::updateSize(size_t s){
size = s;
}
void CommMessage::updateSeqNo(size_t sn){
seqno = sn;
}
void CommMessage::updateTimeStamp(std::string t){
timestamp = t;
}
void CommMessage::updateDeviceIDs(std::vector<std::string> dIDs){
std::vector<std::string>::iterator itr;
deviceIDs.clear();
for(itr = dIDs.begin();itr != dIDs.end();itr++)
deviceIDs.push_back(*itr);
}
//Requests
std::string CommMessage::createListRequest(){
std::string str;
str = "LIST IoTPS\\"+version+"\r\nClientID: "+clientID+"\r\n\r\n";
return str;
}
std::string CommMessage::createSubsRequest(){
std::string str;
std::vector<std::string>::iterator itr;
std::stringstream cc;
cc << count;
str = "SUBSCRIBE IoTPS\\"+version+"\r\nCount: "+cc.str()+"\r\n";
for(itr = deviceIDs.begin();itr != deviceIDs.end();itr++)
str+="DeviceID: "+(*itr)+"\r\n";
str+="ClientID: "+clientID+"\r\n\r\n";
return str;
}
std::string CommMessage::createUnsubsRequest(){
std::string str;
std::vector<std::string>::iterator itr;
std::stringstream cc;
cc << count;
str = "UNSUBSCRIBE IoTPS\\"+version+"\r\nCount: "+cc.str()+"\r\n";
for(itr = deviceIDs.begin();itr != deviceIDs.end();itr++)
str+="DeviceID: "+(*itr)+"\r\n";
str+="ClientID: "+clientID+"\r\n\r\n";
return str;
}
//Good Replies
std::string CommMessage::createListReply(){
std::string str;
std::vector<std::string>::iterator itr;
str = "OK IoTPS\\"+version+"\r\n";
for(itr = deviceIDs.begin();itr != deviceIDs.end();itr++)
str+="DeviceID: "+(*itr)+"\r\n";
str += "ServerID: "+serverID+"\r\n\r\n";
return str;
}
std::string CommMessage::createSubscribeReply(){
std::string str;
std::stringstream ss,cc;
ss << size;
cc << count;
std::vector<std::string>::iterator itr;
str = "OK IoTPS\\"+version+"\r\nCount: "+cc.str()+"\r\n";
for(itr = deviceIDs.begin();itr != deviceIDs.end();itr++)
str+="DeviceID: "+(*itr)+"\r\n";
str+="ServerID: "+serverID+"\r\n\r\n";
return str;
}
std::string CommMessage::createUnsubscribeReply(){
std::string str;
std::vector<std::string>::iterator itr;
std::stringstream cc;
cc << count;
str = "OK IoTPS\\"+version+"\r\nCount: "+cc.str()+"\r\n";
for(itr = deviceIDs.begin();itr != deviceIDs.end();itr++)
str+="DeviceID: "+(*itr)+"\r\n";
str+="ServerID: "+serverID+"\r\n\r\n";
return str;
}
std::string CommMessage::createUpdatesMessage(){
std::string str;
std::vector<std::string>::iterator itr;
std::stringstream ss,cc,snsn;
ss << size;
cc << count;
snsn << seqno;
str = "UPDATES IoTPS\\"+version+"\r\nCount: "+cc.str()+"\r\nSize: "+ss.str()+"\r\n";
str += "SeqNumber: "+snsn.str()+"\r\n";
str += "TimeStamp: "+timestamp+"\r\n";
for(itr = deviceIDs.begin();itr != deviceIDs.end();itr++)
str+="DeviceID: "+(*itr)+"\r\n";
str+="ServerID: "+serverID+"\r\n\r\n";
return str;
}
//Bad Replies
std::string CommMessage::createErrorReply(){
std::string str;
str = "ERROR IoTPS\\"+version+"\r\nServerID: "+serverID+"\r\n\r\n";
return str;
}
std::string CommMessage::createInvalidReply(){
std::string str;
str = "INVALID IoTPS\\"+version+"\r\nServerID: "+serverID+"\r\n\r\n";
return str;
}