-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMembershipManager.cpp
141 lines (113 loc) · 3.46 KB
/
MembershipManager.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include"MembershipManager/MembershipManager.h"
#define LATE_CHECKIN_DEADLINE_SECONDS 3
// --------------------------------------------------------
// class MembershipManager
// implements IntactMiddleware/MembershipManagerInterface.h
// -> Which are RMIs that are called from clients
//
// MembershipManager is centralized
// 1. Hosts log in
// 2. Host log out or crash
// 3. Hosts report "I am alive"
// --------------------------------------------------------
MembershipManager::MembershipManager(bool autoRun) : runThread_(true)
{
if(autoRun) start();
}
MembershipManager::~MembershipManager()
{
// TODO
}
// --------------------------------------------------------
// main thread function
// --------------------------------------------------------
void MembershipManager::run()
{
ASSERT(runThread_ == true);
IWARNING() << " running! ";
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)";
IWARNING() << " " << it->second << " <= " << time(NULL) - LATE_CHECKIN_DEADLINE_SECONDS;
mapHostInformation_[it->first].SetOnlineStatus(HostInformation::OFFLINE);
}
}
}
}
catch(Exception ex)
{
IWARNING() << "Exception caught " << ex.msg();
unlock();
}
}
// --------------------------------------------------------
// class MembershipManager
// --------------------------------------------------------
int MembershipManager::Login(HostInformation &info)
{
MutexLocker lock(&mutexUpdate_);
if(mapHostInformation_.count(info.GetComponentName()))
{
IDEBUG() << "WARNING! " << info.GetComponentName() << " is already logged in!" << endl;
}
IDEBUG() << " " << info.GetComponentName() << " is logged in!" << endl;
info.SetOnlineStatus(HostInformation::ONLINE);
mapHostInformation_[info.GetComponentName()] = info;
createOrUpdateTimeStamp(info.GetComponentName());
waitForChanges_.wakeAll();
return 1;
}
int MembershipManager::Logout(string componentName)
{
MutexLocker lock(&mutexUpdate_);
if(mapHostInformation_.count(componentName) <= 0)
{
IDEBUG() << "WARNING! " << componentName << " is already logged out!";
}
else
{
mapHostInformation_.erase(componentName);
waitForChanges_.wakeAll();
}
return 1;
}
int MembershipManager::AliveChecker(string componentName)
{
MutexLocker lock(&mutexUpdate_);
ASSERT(componentName.empty() == false);
createOrUpdateTimeStamp(componentName);
return 1;
}
map<string, HostInformation> MembershipManager::GetMembershipMap()
{
MutexLocker lock(&mutexUpdate_);
return mapHostInformation_;
}
//-------------------------------------------------------------------
// private functions
//-------------------------------------------------------------------
void MembershipManager::createOrUpdateTimeStamp(string componentName)
{
if(mapAliveCheck_.count(componentName) <= 0)
mapAliveCheck_.insert(pair<string, int64>(componentName, time(NULL)));
else
mapAliveCheck_[componentName] = time(NULL);
}