-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkFullyBehavior.cpp
More file actions
76 lines (57 loc) · 3.4 KB
/
NetworkFullyBehavior.cpp
File metadata and controls
76 lines (57 loc) · 3.4 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
#include "NetworkBehavior.hpp"
using namespace omnetpp;
class NetworkFullyBehavior : public NetworkBehavior
{
public:
NetworkFullyBehavior();
protected:
virtual std::set<std::tuple<std::string,std::string>> handleEvent_partition() override;
virtual void handleEvent_endPartition(std::set<std::tuple<std::string,std::string>> gates) override;
};
Define_Module(NetworkFullyBehavior);
NetworkFullyBehavior::NetworkFullyBehavior() : NetworkBehavior() {}
std::set<std::tuple<std::string,std::string>> NetworkFullyBehavior::handleEvent_partition() {
std::set<std::tuple<std::string, std::string>> gates;
std::set<int> partitionA;
std::set<int> partitionB;
int numClients = this->getParentModule()->par("numClients");
for(int i = 0; i < numClients; i++) {
if(uniform(0, 1) < 0.5) {
partitionA.insert(i);
} else {
partitionB.insert(i);
}
}
for(int i : partitionA) {
for(int j : partitionB) {
cGate *a_in = this->getParentModule()->getSubmodule("client", i)->gate("in", j);
cGate *a_out = this->getParentModule()->getSubmodule("client", i)->gate("out", j);
gates.insert(std::make_tuple(a_out->getFullPath(), a_out->getNextGate()->getFullPath()));
gates.insert(std::make_tuple(a_in->getPreviousGate()->getFullPath(), a_in->getFullPath()));
}
}
for(std::tuple<std::string,std::string> gate : gates) {
std::string modulePath0 = std::get<0>(gate).substr(0, std::get<0>(gate).rfind("."));
std::string gateName0 = std::get<0>(gate).substr(std::get<0>(gate).rfind(".") + 1, std::get<0>(gate).rfind("[") - std::get<0>(gate).rfind(".") - 1);
int index0 = std::stoi(std::get<0>(gate).substr(std::get<0>(gate).rfind("[") + 1, std::get<0>(gate).rfind("]") - std::get<0>(gate).rfind("[") - 1));
cGate *gate0 = this->getParentModule()->getModuleByPath(modulePath0.c_str())->gate(gateName0.c_str(), index0);
gate0->disconnect();
}
return gates;
}
void NetworkFullyBehavior::handleEvent_endPartition(std::set<std::tuple<std::string, std::string>> gates) {
for(std::tuple<std::string,std::string> gate : gates) {
std::string modulePath0 = std::get<0>(gate).substr(0, std::get<0>(gate).rfind("."));
std::string gateName0 = std::get<0>(gate).substr(std::get<0>(gate).rfind(".") + 1, std::get<0>(gate).rfind("[") - std::get<0>(gate).rfind(".") - 1);
int index0 = std::stoi(std::get<0>(gate).substr(std::get<0>(gate).rfind("[") + 1, std::get<0>(gate).rfind("]") - std::get<0>(gate).rfind("[") - 1));
std::string modulePath1 = std::get<1>(gate).substr(0, std::get<1>(gate).rfind("."));
std::string gateName1 = std::get<1>(gate).substr(std::get<1>(gate).rfind(".") + 1, std::get<1>(gate).rfind("[") - std::get<1>(gate).rfind(".") - 1);
int index1 = std::stoi(std::get<1>(gate).substr(std::get<1>(gate).rfind("[") + 1, std::get<1>(gate).rfind("]") - std::get<1>(gate).rfind("[") - 1));
cChannel *channel = cDelayChannel::create("channel");
channel->par("delay").setDoubleValue(this->linkDelay);
cGate *gate0 = this->getParentModule()->getModuleByPath(modulePath0.c_str())->gate(gateName0.c_str(), index0);
cGate *gate1 = this->getParentModule()->getModuleByPath(modulePath1.c_str())->gate(gateName1.c_str(), index1);
gate0->connectTo(gate1, channel);
}
return;
}