-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstarterCode.cpp
120 lines (101 loc) · 2.45 KB
/
starterCode.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
#include <iostream>
#include <string>
#include <map>
#include <utility>
#include <vector>
using namespace std;
/**
* Giving Higher priority to
* higher numbered process
* Each group has the highest number
* process as its leader
* It waits for 2*TM+TP(see readme!) before declaring timeout on each id
* node id's start from 1-n(considering n processes)
*/
long long TM,TP;
/**
* Simulate the network(Assume and handle failures randomly make node sleep for some time doing nothing)
* for this generate a random number between 0-100 if number <10 make the node sleep
*/
double failueProbability = 0.1;
/**
* Randomly set it to be 4 sec(maybe anything)
*/
long long sleepingTime = 4;
/**
* Store ID of network with IP,port
*/
#define pair<string,int> ps
std::map<int,ps> map;
/**
* Keeping track of nodeid of current node and total number of nodes
*/
myId = -1;
N_nodes = -1;
class invitation
{
private:
int leader = -1;
public:
std::vector<int> group; //only if needed!
invitation(int nodeId);
~invitation();
void sendMessage(int id);
void recieveMessage();
void merge();
void checkIfLeaderIsAlive();
void announceLeaderChange();
};
invitation::invitation(int nodeId){
leader = nodeId;
}
void invitation::announceLeaderChange(){
/**
* create socket and send the message to the whole group telling them their leader
* after that clear the nodes under it mind all messages should again have a probability of being sent
*/
group.clear();
}
void invitation::checkIfLeaderIsAlive(){
/**
* if no message is recieved from the leader from some given time
* assume leader not alive(assume time to be 3*(2*TM+Tp) similar to 3RTT)
* anounce itself to be leader
* keep a lock on each function
*/
group.clear();
group.push_back(myId);
leader = myId;
}
void invitation::mergeGroups(int _leader,string s){
/**
*still check the fact that more than two leaders respond with higher priority
*check what to do in such a case
*If leader not equal to myId
*/
if(myId == _leader){
/**
* Update the group by converting string to vector
*/
}
else{
/**
* in this case inform the group mates of leader change
* and update leader
*/
leader = _leader;
sendMessage(leaderChange);
group.clear();
}
}
int main(int argc, char const *argv[])
{
if (argc != 3){
cout<<"Error Enter node ID,Number of nodes as command line Args"<<endl;
exit(1);
}
myId = atoi(argv[1]);
N_nodes = atoi(argv[2]);
invitation node(myId);
return 0;
}