-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChaosManager.cc
150 lines (128 loc) · 4.43 KB
/
ChaosManager.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
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see http://www.gnu.org/licenses/.
//
#include <list>
#include <vector>
#include <algorithm>
#include <time.h>
#include <string>
#include <cstring>
#include <map>
#include <iostream>
#include <regex>
//#include "inet/routing/leach/Leach.h"
#include "ChaosManager.h"
#include "inet/common/lifecycle/LifecycleOperation.h"
#include "inet/common/lifecycle/ModuleOperations.h"
#include "inet/common/lifecycle/NodeStatus.h"
#include "inet/common/chaos/ChaosEvent_m.h"
using namespace std;
namespace inet {
struct eventObject {
int command;
int time;
int nodeNum;
};
vector<eventObject> eventList;
Define_Module(ChaosManager);
void ChaosManager::initialize()
{
numNodes = par("numNodes");
faultPerc = par("faultPerc");
generateChaos(faultPerc, numNodes);
// iterate through list and generate self messages
for (auto &itr : eventList) {
simtime_t t = itr.time;
auto msg = new ChaosEvent("chaos-event");
msg->setNodeNum(itr.nodeNum);
msg->setCommand(itr.command);
scheduleAt(t, msg);
}
}
void ChaosManager::handleMessage(cMessage *msg)
{
int retrievedCommand = check_and_cast<ChaosEvent *>(msg)->getCommand();
int retrievedNodeNum = check_and_cast<ChaosEvent *>(msg)->getNodeNum();
delete msg;
// processCommands as they come in
processCommand(retrievedCommand, retrievedNodeNum);
}
void ChaosManager::generateChaos(int faultPerc, int numNodes) {
srand(time(0));
int faultyNodes = numNodes * faultPerc / 100;
for (int counter = 0; counter < faultyNodes; counter++) {
eventObject event;
event.nodeNum = getUniqueNodeId(numNodes);
event.time = rand()%getResolvedSimTimeLimit();
// event.time = 6;
event.command = 1 + (rand()%2);
eventList.push_back(event);
}
}
bool ChaosManager::eventExist(int nodeId) {
for (auto& it : eventList) {
if (it.nodeNum == nodeId) {
return true;
}
}
return false;
}
int ChaosManager::getUniqueNodeId(int numNodes) {
int uniqueNodeId;
for (int counter = 0; counter <= numNodes; counter++) {
srand((unsigned) time(NULL));
uniqueNodeId = rand() % (numNodes + 1);
if (eventExist(uniqueNodeId)) {
continue;
} else {
break;
}
}
return uniqueNodeId;
}
void ChaosManager::processCommand(int command, int nodeNum) {
// resolve target node
string targetString = "host[" + std::to_string(nodeNum) + "]"; // string "host[" needs to be updated with respective node naming convention used in network.
const char *target = &targetString[0];
cModule *module = getModuleByPath(target);
NodeStatus *nodeStatus = dynamic_cast<NodeStatus *>(module->getSubmodule("status"));
if (!module)
throw cRuntimeError("Module '%s' not found", target);
if (!nodeStatus)
throw cRuntimeError("Cannot find node status");
// resolve command
LifecycleOperation *operation;
if (nodeStatus->getState() == NodeStatus::UP) {
if (command == 1) {
operation = new ModuleStopOperation;
} else {
operation = new ModuleCrashOperation;
}
map<string, string> params; // empty string to fill parameters
operation->initialize(module, params);
// perform operation
lifecycleController.initiateOperation(operation);
}
}
// Resolve simulation time limit from ini file.
int ChaosManager::getResolvedSimTimeLimit() {
int timeLimit = 0;
cConfiguration *config = getEnvir()->getConfig();
string simTimeLimit = config->getConfigValue("sim-time-limit");
string simTimeLimitResolved = std::regex_replace(simTimeLimit,std::regex("[^0-9]*([0-9]+).*"),
std::string("$1"));
timeLimit = std::stoi(simTimeLimitResolved);
return timeLimit;
}
} //namespace