-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientTree.cpp
98 lines (87 loc) · 2.25 KB
/
ClientTree.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
#include <string>
#include <cmath>
#include "ClientNetwork.hpp"
using namespace omnetpp;
class ClientTree : public ClientNetwork
{
public:
ClientTree();
protected:
virtual void initialize() override;
virtual void sendToAll(cMessage *msg) override;
virtual void forward(cMessage *msg) override;
};
Define_Module(ClientTree);
ClientTree::ClientTree() : ClientNetwork() {}
void ClientTree::initialize()
{
ClientNetwork::initialize();
this->timeToLive = 2*(ceil(log(getParentModule()->par("numClients").intValue()+1)/log(2)));
}
void ClientTree::sendToAll(cMessage *msg)
{
cMessage *cMsg = msg->dup();
try{
send(cMsg, "out_parent");
} catch(cRuntimeError e) {
cancelAndDelete(cMsg);
}
cMsg = msg->dup();
try{
send(cMsg, "out_left");
} catch(cRuntimeError e) {
cancelAndDelete(cMsg);
}
cMsg = msg->dup();
try{
send(cMsg, "out_right");
} catch(cRuntimeError e) {
cancelAndDelete(cMsg);
}
return;
}
void ClientTree::forward(cMessage *msg)
{
cMessage *cMsg = nullptr;
if(std::string(msg->getArrivalGate()->getName()) == "in_parent") {
cMsg = msg->dup();
try{
send(cMsg, "out_left");
} catch(cRuntimeError e) {
cancelAndDelete(cMsg);
}
cMsg = msg->dup();
try{
send(cMsg, "out_right");
} catch(cRuntimeError e) {
cancelAndDelete(cMsg);
}
} else if(std::string(msg->getArrivalGate()->getName()) == "in_left") {
cMsg = msg->dup();
try{
send(cMsg, "out_parent");
} catch(cRuntimeError e) {
cancelAndDelete(cMsg);
}
cMsg = msg->dup();
try{
send(cMsg, "out_right");
} catch(cRuntimeError e) {
cancelAndDelete(cMsg);
}
} else if(std::string(msg->getArrivalGate()->getName()) == "in_right") {
cMsg = msg->dup();
try{
send(cMsg, "out_parent");
} catch(cRuntimeError e) {
cancelAndDelete(cMsg);
}
cMsg = msg->dup();
try{
send(cMsg, "out_left");
} catch(cRuntimeError e) {
cancelAndDelete(cMsg);
}
}
return;
}