-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroupManager.cpp
More file actions
266 lines (214 loc) · 7.21 KB
/
Copy pathGroupManager.cpp
File metadata and controls
266 lines (214 loc) · 7.21 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
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
#include"MembershipManager/GroupManager.h"
#define LATE_CHECKIN_DEADLINE_SECONDS 2
// --------------------------------------------------------
// class GroupManager
// implements MicroMiddleware/GroupManagerInterface.h
// -> Which are RMIs that are called from clients
//
// GroupManager is centralized
//
// 1. Update group view
// 2. Update group overlay
// 3. Respond to client joining/leaving
// 4. Respond to clients that have changed neighbors.
// --------------------------------------------------------
GroupManager::GroupManager(bool autoStart) : runThread_(true)
{
if(autoStart) start();
}
GroupManager::~GroupManager()
{
// TODO
}
// --------------------------------------------------------
// main thread function
// --------------------------------------------------------
void GroupManager::run()
{
IWARNING() << " running! " << endl;
try
{
lock();
while(runThread_)
{
waitForChanges_.wait(&mutexUpdate_, 1000);
if(runThread_ == false)
{
unlock();
break;
}
for(MapAliveCheck::iterator it = mapAliveCheck_.begin(), it_end = mapAliveCheck_.end(); it != it_end; ++it)
{
if(it->second > time(NULL) - LATE_CHECKIN_DEADLINE_SECONDS)
{
ASSERT(mapHostInformation_.count(it->first));
mapHostInformation_[it->first].SetOnlineStatus(HostInformation::ONLINE);
}
else if(mapHostInformation_[it->first].GetOnlineStatus() == HostInformation::ONLINE)
{
IWARNING() << " " << it->first << " is assumed OFFLINE (missed several alive messages)" << endl;
IWARNING() << " " << it->second << " <= " << time(NULL) - LATE_CHECKIN_DEADLINE_SECONDS << endl;
mapHostInformation_[it->first].SetOnlineStatus(HostInformation::OFFLINE);
}
addOrUpdateHostInfo(mapHostInformation_[it->first]);
}
}
}
catch(Exception ex)
{
IWARNING() << "Exception caught " << ex.msg() << endl;
unlock();
}
}
void GroupManager::StopGroupManager(bool waitForTermination)
{
lock();
runThread_ = false;
wakeAll();
unlock();
if(isRunning() && waitForTermination)
while(wait() == false)
{
IWARNING() << "Wait() for thread termination timed out!" << endl;
ASSERT(isRunning());
}
}
// --------------------------------------------------------
// class GroupManager
// --------------------------------------------------------
GroupInformation GroupManager::GroupChange(HostInformation hostInfo, string currentGroupName, string newGroupName)
{
MutexLocker lock(&mutexUpdate_);
if(addOrUpdateHostInfo(hostInfo) == 0)
{
IWARNING() << "WARNING! " << hostInfo.GetComponentName() << " Changing groups but not registered!" << endl;
}
removeHostFromGroup(hostInfo, currentGroupName);
addHostToGroup(hostInfo, newGroupName);
createOrUpdateTimeStamp(hostInfo.GetComponentName());
wakeAll();
return mapGroupInformation_[newGroupName];
}
GroupInformation GroupManager::GetGroupInformation(string groupName)
{
MutexLocker lock(&mutexUpdate_);
if(mapGroupInformation_.count(groupName) <= 0)
return GroupInformation();
return mapGroupInformation_[groupName];
}
GroupInformation GroupManager::AliveChecker(HostInformation hostInfo, string currentGroupName)
{
MutexLocker lock(&mutexUpdate_);
if(mapGroupInformation_.count(currentGroupName) <= 0)
{
IWARNING() << "WARNING! group not found! " << currentGroupName << endl;
return GroupInformation();
}
GroupInformation &groupInfo = mapGroupInformation_[currentGroupName];
bool ret = groupInfo.UpdateHost(hostInfo);
ASSERT(ret == true);
createOrUpdateTimeStamp(hostInfo.GetComponentName());
wakeAll();
return groupInfo;
}
int GroupManager::LeaveGroup(HostInformation hostInfo, string groupName)
{
MutexLocker lock(&mutexUpdate_);
if(addOrUpdateHostInfo(hostInfo) == 0)
IWARNING() << "WARNING! " << hostInfo.GetComponentName() << " removing from group " << groupName << " but not registered!" << endl;
wakeAll();
return (int)removeHostFromGroup(hostInfo, groupName);
}
GroupInformation GroupManager::JoinGroup(HostInformation hostInfo, string groupName)
{
MutexLocker lock(&mutexUpdate_);
IWARNING() << " Joining group name : " << groupName << endl;
if(addOrUpdateHostInfo(hostInfo) == 0)
{
IWARNING() << "WARNING! Adding " << hostInfo.GetComponentName() << " to group " << groupName << " but component is not registered!" << endl;
}
addHostToGroup(hostInfo, groupName);
createOrUpdateTimeStamp(hostInfo.GetComponentName());
wakeAll();
return mapGroupInformation_[groupName];
}
map<string, GroupInformation> GroupManager::GetAllGroupInformation()
{
MutexLocker lock(&mutexUpdate_);
return mapGroupInformation_;
}
// --------------------------------------------------------
// private functions
// --------------------------------------------------------
void GroupManager::createOrUpdateTimeStamp(string componentName)
{
if(mapAliveCheck_.count(componentName) <= 0)
mapAliveCheck_.insert(pair<string, int64>(componentName, time(NULL)));
else
mapAliveCheck_[componentName] = time(NULL);
}
bool GroupManager::removeHostFromGroup(const HostInformation &hostInfo, const string &groupName)
{
if(mapGroupInformation_.count(groupName) <= 0)
{
IWARNING() << "WARNING! " << groupName<< " is not registered!" << endl;
return false;
}
GroupInformation &groupInfo = mapGroupInformation_[groupName];
if(groupInfo.IsMember(hostInfo.GetComponentName()))
{
mapComponentNameGroups_[hostInfo.GetComponentName()].erase(groupName);
groupInfo.RemoveHost(hostInfo.GetComponentName());
return true;
}
else
{
IWARNING() << "WARNING! " << hostInfo.GetComponentName() << " is not a member of " << groupName << endl;
return false;
}
return false;
}
bool GroupManager::addHostToGroup(const HostInformation &hostInfo, const string &groupName)
{
if(mapGroupInformation_.count(groupName) <= 0)
{
IWARNING() << "WARNING! " << groupName << " is not registered. Adding it!" << endl;
mapGroupInformation_.insert(pair<string, GroupInformation>(groupName, GroupInformation(groupName)));
}
GroupInformation &groupInfo = mapGroupInformation_[groupName];
if(!groupInfo.IsMember(hostInfo.GetComponentName()))
{
mapComponentNameGroups_[hostInfo.GetComponentName()].insert(groupName);
groupInfo.AddHost(hostInfo);
return true;
}
else
{
IWARNING() << "WARNING! " << hostInfo.GetComponentName() << " is already a member of " << groupName << endl;
return false;
}
return false;
}
int GroupManager::addOrUpdateHostInfo(const HostInformation &hostInfo)
{
if(mapHostInformation_.count(hostInfo.GetComponentName()) <= 0)
{
mapHostInformation_.insert(pair<string, HostInformation>(hostInfo.GetComponentName(), hostInfo));
return 0;
}
mapHostInformation_[hostInfo.GetComponentName()] = hostInfo;
ASSERT(mapComponentNameGroups_.count(hostInfo.GetComponentName()));
//IWARNING() << " " << hostInfo << endl;
const SetGroup &setGroup = mapComponentNameGroups_[hostInfo.GetComponentName()];
for(SetGroup::const_iterator it = setGroup.begin(), it_end = setGroup.end(); it != it_end; ++it)
{
string groupName = *it;
ASSERT(mapGroupInformation_.count(groupName));
mapGroupInformation_[groupName].UpdateHost(hostInfo);
GroupInformation &groupInfo = mapGroupInformation_[groupName];
groupInfo.UpdateHost(hostInfo);
//IWARNING() << " updating group " << groupName << endl;
//groupInfo.Print();
}
return 1;
}