-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientNetwork.cpp
294 lines (248 loc) · 14 KB
/
ClientNetwork.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
#include <vector>
#include <algorithm> // For find
#include "ClientNetwork.hpp"
#include "libraries/Message.hpp"
#include "libraries/UserEvent.hpp"
#include "libraries/utils.hpp"
ClientNetwork::ClientNetwork() : cSimpleModule(), personalRoomId(0), personalMessageId(0), client(nullptr), timeToLive(-1) {}
void ClientNetwork::initialize()
{
this->createRoomMinTime = getParentModule()->par("createRoomMinTime").doubleValue();
this->createRoomMaxTime = getParentModule()->par("createRoomMaxTime").doubleValue();
this->createRoomProbability = getParentModule()->par("createRoomProbability").doubleValue();
this->sendMessageMinTime = getParentModule()->par("sendMessageMinTime").doubleValue();
this->sendMessageMaxTime = getParentModule()->par("sendMessageMaxTime").doubleValue();
this->sendMessageProbability = getParentModule()->par("sendMessageProbability").doubleValue();
this->resendCreationTime = getParentModule()->par("resendCreationTime").doubleValue();
this->askMessagesMinTime = getParentModule()->par("askMessagesMinTime").doubleValue();
this->askMessagesMaxTime = getParentModule()->par("askMessagesMaxTime").doubleValue();
this->deleteRoomMinTime = getParentModule()->par("deleteRoomMinTime").doubleValue();
this->deleteRoomMaxTime = getParentModule()->par("deleteRoomMaxTime").doubleValue();
this->deleteRoomProbability = getParentModule()->par("deleteRoomProbability").doubleValue();
this->stopEventTime = getParentModule()->par("stopEventTime").doubleValue();
this->client = new Client(std::string(this->getFullName()));
this->timeToLive = getParentModule()->par("numClients").intValue();
scheduleAt(simTime() + uniform(createRoomMinTime, createRoomMaxTime), new cMessage(ue_toString(UserEvent::CREATE_ROOM).c_str()));
scheduleAt(simTime() + uniform(sendMessageMinTime, sendMessageMaxTime), new cMessage(ue_toString(UserEvent::SEND_MESSAGE).c_str()));
scheduleAt(simTime() + resendCreationTime, new cMessage(ue_toString(UserEvent::RESEND_CREATION).c_str()));
scheduleAt(simTime() + uniform(askMessagesMinTime, askMessagesMaxTime), new cMessage(ue_toString(UserEvent::ASK_MESSAGES).c_str()));
}
void ClientNetwork::handleMessage(cMessage *msg)
{
if(msg == nullptr) {
return;
}
if(msg->isSelfMessage()) {
handleUserEvent(msg);
delete msg;
return;
}
handleReceivedMessage(msg);
forwardMessage(msg);
cancelAndDelete(msg);
return;
}
void ClientNetwork::handleUserEvent(cMessage *msg)
{
UserEvent re = ue_fromString(msg->getName());
switch(re) {
case UserEvent::CREATE_ROOM:
if(uniform(0, 1) < createRoomProbability) {
handleEvent_RoomCreation();
}
if(simTime() < this->stopEventTime) {
scheduleAt(simTime() + uniform(createRoomMinTime, createRoomMaxTime), new cMessage(ue_toString(UserEvent::CREATE_ROOM).c_str()));
scheduleAt(simTime() + uniform(deleteRoomMinTime, deleteRoomMaxTime), new cMessage(ue_toString(UserEvent::DELETE_ROOM).c_str()));
}
break;
case UserEvent::SEND_MESSAGE:
if(uniform(0, 1) < sendMessageProbability) {
handleEvent_SendMessage();
}
if(simTime() < this->stopEventTime) {
scheduleAt(simTime() + uniform(sendMessageMinTime, sendMessageMaxTime), new cMessage(ue_toString(UserEvent::SEND_MESSAGE).c_str()));
}
break;
case UserEvent::RESEND_CREATION:
handleEvent_ResendCreation();
scheduleAt(simTime() + resendCreationTime, new cMessage(ue_toString(UserEvent::RESEND_CREATION).c_str()));
break;
case UserEvent::ASK_MESSAGES:
scheduleAt(simTime() + uniform(askMessagesMinTime, askMessagesMaxTime), new cMessage(ue_toString(UserEvent::ASK_MESSAGES).c_str()));
handleEvent_AskMessages();
break;
case UserEvent::DELETE_ROOM:
if(uniform(0, 1) < deleteRoomProbability) {
handleEvent_deleteFirstRoom();
}
break;
default:
break;
}
return;
}
void ClientNetwork::handleEvent_deleteFirstRoom()
{
std::vector<std::string> rooms = client->getManagedNonDeletedRooms();
if(rooms.empty()) {
EV << this->getFullName() << " - No rooms available" << endl;
std::cout << this->getFullName() << " - No rooms available" << std::endl;
return;
}
std::string roomId = rooms[0];
RoomDeletionMessage *msg = client->getRoomDeletionMessage(roomId);
client->deleteRoom(msg);
cMessage *cMsg = msg->getCmessage();
cMsg->addPar("timeToLive").setLongValue(this->timeToLive);
sendToAll(cMsg);
EV << this->getFullName() << " - Deleting room: " << roomId << endl;
std::cout << this->getFullName() << " - Deleting room: " << roomId << std::endl;
cancelAndDelete(cMsg);
delete msg;
return;
}
void ClientNetwork::handleEvent_RoomCreation()
{
int numClients = getParentModule()->par("numClients").intValue();
int numMembers = intuniform(2, numClients);
std::vector<std::string> members;
members.push_back(this->getFullName());
while(members.size() < numMembers) {
std::string clientName = "client[" + std::to_string(intuniform(0, numClients-1)) + "]";
if(std::find(members.begin(), members.end(), clientName) == members.end()) {
members.push_back(clientName);
}
}
std::string roomId = "stanza(" + std::string(this->getFullName()) + ", " + std::to_string(this->personalRoomId++) + ")";
RoomCreationMessage *msg = client->createRoom(roomId, members);
cMessage *cMsg = msg->getCmessage();
cMsg->addPar("timeToLive").setLongValue(this->timeToLive);
sendToAll(cMsg);
EV << this->getFullName() << " - Sent room creation: " << roomId << " with: " << vectorOfStrings_to_String(members) << endl;
std::cout << this->getFullName() << " - Sent room creation: " << roomId << " with: " << vectorOfStrings_to_String(members) << std::endl;
cancelAndDelete(cMsg);
delete msg;
return;
}
void ClientNetwork::handleEvent_SendMessage()
{
std::vector<std::string> rooms = client->getNonDeletedRooms();
if(rooms.empty()) {
EV << this->getFullName() << " - No rooms available" << endl;
std::cout << this->getFullName() << " - No rooms available" << std::endl;
return;
}
std::string roomId = rooms[intuniform(0, rooms.size()-1)];
ChatMessage *msg = client->getMessage("msg("+std::to_string(this->personalMessageId++)+","+getFullName()+")", roomId);
cMessage *cMsg = msg->getCmessage();
cMsg->addPar("timeToLive").setLongValue(this->timeToLive);
sendToAll(cMsg);
EV << this->getFullName() << " - Sending message to room " << msg->getRoomId() << " - " << msg->getContent() << endl;
std::cout << this->getFullName() << " - Sending message to room " << msg->getRoomId() << " - " << msg->getContent() << std::endl;
cancelAndDelete(cMsg);
delete msg;
return;
}
void ClientNetwork::handleEvent_ResendCreation() {
std::set<RoomCreationMessage *> creationMsgs = client->creationToResend();
for(RoomCreationMessage * msg : creationMsgs) {
cMessage *cMsg = msg->getCmessage();
cMsg->addPar("timeToLive").setLongValue(this->timeToLive);
sendToAll(cMsg);
EV << this->getFullName() << " - Resending room creation: " << cMsg->par("roomId").stringValue() << endl;
std::cout << this->getFullName() << " - Resending room creation: " << cMsg->par("roomId").stringValue() << std::endl;
cancelAndDelete(cMsg);
}
}
void ClientNetwork::handleEvent_AskMessages() {
std::list<AskMessage> messages = client->askMessages();
EV << this->getFullName() << " - Start asking for messages" << endl;
std::cout << this->getFullName() << " - Start asking for messages" << std::endl;
for(AskMessage am : messages) {
cMessage *cMsg = am.getCmessage();
cMsg->addPar("timeToLive").setLongValue(this->timeToLive);
sendToAll(cMsg);
EV << this->getFullName() << " - Asked for message: " << vectorOfInts_to_String(am.getMissingVectorClock()) << " - Room: " << am.getRoomId() << endl;
std::cout << this->getFullName() << " - Asked for message: " << vectorOfInts_to_String(am.getMissingVectorClock()) << " - Room: " << am.getRoomId() << std::endl;
cancelAndDelete(cMsg);
}
}
void ClientNetwork::handleReceivedMessage(cMessage *msg)
{
std::pair<ActionPerformed, std::vector<BaseMessage*>> *result = client->handleMessage(Message::createMessage(*msg));
ActionPerformed ap = result->first;
if(ap == ActionPerformed::CREATED_ROOM) {
std::vector<BaseMessage*> ackList = (std::vector<BaseMessage *>)result->second;
AckMessage *ack = (AckMessage *) ackList[0];
cMessage *cMsg = ack->getCmessage();
cMsg->addPar("timeToLive").setLongValue(this->timeToLive);
sendToAll(cMsg);
EV << this->getFullName() << " - Room created: " << msg->par("roomId").stringValue() << endl;
std::cout << this->getFullName() << " - Room created: " << msg->par("roomId").stringValue() << std::endl;
EV << this->getFullName() << " - Ack sent: " << cMsg->par("roomId").stringValue() << endl;
std::cout << this->getFullName() << " - Ack sent: " << cMsg->par("roomId").stringValue() << std::endl;
cancelAndDelete(cMsg);
} else if(ap == ActionPerformed::DISCARDED_ALREADY_RECEIVED_ROOM_CREATION) {
std::vector<BaseMessage*> ackList = (std::vector<BaseMessage *>)result->second;
AckMessage *ack = (AckMessage *) ackList[0];
cMessage *cMsg = ack->getCmessage();
cMsg->addPar("timeToLive").setLongValue(this->timeToLive);
sendToAll(cMsg);
EV << this->getFullName() << " - Room already created: " << msg->par("roomId").stringValue() << endl;
std::cout << this->getFullName() << " - Room already created: " << msg->par("roomId").stringValue() << std::endl;
EV << this->getFullName() << " - Ack sent: " << cMsg->par("roomId").stringValue() << endl;
std::cout << this->getFullName() << " - Ack sent: " << cMsg->par("roomId").stringValue() << std::endl;
cancelAndDelete(cMsg);
} else if(ap == ActionPerformed::RECEIVED_CHAT_MESSAGE) {
EV << this->getFullName() << " - Room: " << msg->par("roomId").stringValue() << " - Message received: " << msg->par("message").stringValue() << endl;
std::cout << this->getFullName() << " - Room: " << msg->par("roomId").stringValue() << " - Message received: " << msg->par("message").stringValue() << std::endl;
} else if(ap == ActionPerformed::DISCARDED_ALREADY_RECEIVED_MESSAGE) {
EV << this->getFullName() << " - Room: " << msg->par("roomId").stringValue() << " - Message already received: " << endl;
std::cout << this->getFullName() << " - Room: " << msg->par("roomId").stringValue() << " - Message already received: " << std::endl;
} else if(ap == ActionPerformed::DISCARDED_NON_RECIPIENT_MESSAGE) {
EV << this->getFullName() << " - Room: " << msg->par("roomId").stringValue() << " - Message discarded for I'm not a recipient: " << endl;
std::cout << this->getFullName() << " - Room: " << msg->par("roomId").stringValue() << " - Message discarded for I'm not a recipient: " << std::endl;
} else if(ap == ActionPerformed::ANSWERED_ASK_FOR_MESSAGE) {
EV << this->getFullName() << " - Room: " << msg->par("roomId").stringValue() << " - Asked for message" << endl;
std::cout << this->getFullName() << " - Room: " << msg->par("roomId").stringValue() << " - Asked for message" << std::endl;
std::vector<BaseMessage *> messages = result->second;
for (BaseMessage *bm : messages) {
Message *msg = (Message *) bm;
if (msg->getType() == MessageType::CHAT) {
ChatMessage *cm = (ChatMessage *) bm;
cMessage *cMess = cm->getCmessage();
cMess->addPar("timeToLive").setLongValue(this->timeToLive);
sendToAll(cMess);
EV << this->getFullName() << " - Replayed message: " << cMess->par("message").stringValue() << " - Room: " << cMess->par("roomId").stringValue() << endl;
std::cout << this->getFullName() << " - Replayed message: " << cMess->par("message").stringValue() << " - Room: " << cMess->par("roomId").stringValue() << std::endl;
cancelAndDelete(cMess);
} else if (msg->getType() == MessageType::DELETE_ROOM) {
RoomDeletionMessage *rdm = (RoomDeletionMessage *) bm;
cMessage *cMess = rdm->getCmessage();
cMess->addPar("timeToLive").setLongValue(this->timeToLive);
sendToAll(cMess);
EV << this->getFullName() << " - Resending deletion: " << cMess->par("roomId").stringValue() << endl;
std::cout << this->getFullName() << " - Resending deletion: " << cMess->par("roomId").stringValue() << std::endl;
cancelAndDelete(cMess);
}
}
} else if(ap == ActionPerformed::ROOM_DELETED) {
EV << this->getFullName() << " - Room scheduled for deletion: " << msg->par("roomId").stringValue() << endl;
std::cout << this->getFullName() << " - Room scheduled for deletion: " << msg->par("roomId").stringValue() << std::endl;
}
return;
}
void ClientNetwork::forwardMessage(cMessage *msg)
{
if(msg->par("timeToLive").longValue() > 1) {
msg->par("timeToLive").setLongValue(msg->par("timeToLive").longValue() - 1);
forward(msg);
EV << this->getFullName() << " - Message forwarded: " << msg->getName() << " - Time to live: " << msg->par("timeToLive").longValue() << endl;
std::cout << this->getFullName() << " - Message forwarded: " << msg->getName() << " - Time to live: " << msg->par("timeToLive").longValue() << std::endl;
}
else {
EV << this->getFullName() << " - time to live = 0 for message: " << msg->getName() << endl;
std::cout << this->getFullName() << " - time to live = 0 for message: " << msg->getName() << std::endl;
}
return;
}