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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//
#include <Ice/UUID.h>
#include <Ice/LocalException.h>
#include <Ice/ObjectAdapter.h>
#include <IceGrid/SessionServantManager.h>
#include <iterator>
using namespace std;
using namespace IceGrid;
SessionServantManager::SessionServantManager(const shared_ptr<Ice::ObjectAdapter>& adapter,
const string& instanceName,
bool checkConnection,
const string& serverAdminCategory,
const shared_ptr<Ice::Object>& serverAdminRouter,
const string& nodeAdminCategory,
const shared_ptr<Ice::Object>& nodeAdminRouter,
const string& replicaAdminCategory,
const shared_ptr<Ice::Object>& replicaAdminRouter,
const shared_ptr<AdminCallbackRouter>& adminCallbackRouter) :
_adapter(adapter),
_instanceName(instanceName),
_checkConnection(checkConnection),
_serverAdminCategory(serverAdminCategory),
_serverAdminRouter(serverAdminRouter),
_nodeAdminCategory(nodeAdminCategory),
_nodeAdminRouter(nodeAdminRouter),
_replicaAdminCategory(replicaAdminCategory),
_replicaAdminRouter(replicaAdminRouter),
_adminCallbackRouter(adminCallbackRouter)
{
}
shared_ptr<Ice::Object>
SessionServantManager::locate(const Ice::Current& current, shared_ptr<void>&)
{
lock_guard lock(_mutex);
shared_ptr<Ice::Object> servant;
bool plainServant = false;
if(_serverAdminRouter && current.id.category == _serverAdminCategory)
{
servant = _serverAdminRouter;
}
else if(_nodeAdminRouter && current.id.category == _nodeAdminCategory)
{
servant = _nodeAdminRouter;
}
else if(_replicaAdminRouter && current.id.category == _replicaAdminCategory)
{
servant = _replicaAdminRouter;
}
else
{
plainServant = true;
auto p = _servants.find(current.id);
if(p == _servants.end() || (_checkConnection && p->second.connection != current.con))
{
servant = 0;
}
else
{
servant = p->second.servant;
}
}
if(!plainServant && servant && _checkConnection &&
_adminConnections.find(current.con) == _adminConnections.end())
{
servant = 0;
}
return servant;
}
void
SessionServantManager::finished(const Ice::Current&, const shared_ptr<Ice::Object>&, const shared_ptr<void>&)
{
}
void
SessionServantManager::deactivate(const std::string&)
{
lock_guard lock(_mutex);
assert(_servants.empty());
assert(_sessions.empty());
assert(_adminConnections.empty());
}
shared_ptr<Ice::ObjectPrx>
SessionServantManager::addSession(const shared_ptr<Ice::Object>& session,
const shared_ptr<Ice::Connection>& con, const string& category)
{
lock_guard lock(_mutex);
_sessions.insert({ session, SessionInfo(con, category) });
//
// Keep track of all the connections which have an admin session to allow access
// to server admin objects.
//
if(!category.empty() && con != 0)
{
_adminConnections.insert(con);
if(_adminCallbackRouter != 0)
{
_adminCallbackRouter->addMapping(category, con);
}
}
return addImpl(session, session); // Register a servant for the session and return its proxy.
}
void
SessionServantManager::setSessionControl(const shared_ptr<Ice::Object>& session,
const shared_ptr<Glacier2::SessionControlPrx>& ctl,
const Ice::IdentitySeq& ids)
{
lock_guard lock(_mutex);
auto p = _sessions.find(session);
assert(p != _sessions.end());
p->second.sessionControl = ctl;
p->second.identitySet = ctl->identities();
//
// Allow invocations on the session servants and the given objects.
//
Ice::IdentitySeq allIds = ids;
copy(p->second.identities.begin(), p->second.identities.end(), back_inserter(allIds));
p->second.identitySet->add(allIds);
//
// Allow invocations on server admin objects.
//
if(!p->second.category.empty() && _serverAdminRouter)
{
Ice::StringSeq seq;
seq.push_back(_serverAdminCategory);
ctl->categories()->add(seq);
}
}
shared_ptr<Glacier2::IdentitySetPrx>
SessionServantManager::getGlacier2IdentitySet(const shared_ptr<Ice::Object>& session)
{
lock_guard lock(_mutex);
auto p = _sessions.find(session);
if(p != _sessions.end() && p->second.sessionControl)
{
if(!p->second.identitySet) // Cache the identity set proxy
{
p->second.identitySet = p->second.sessionControl->identities();
}
return p->second.identitySet;
}
else
{
return nullptr;
}
}
shared_ptr<Glacier2::StringSetPrx>
SessionServantManager::getGlacier2AdapterIdSet(const shared_ptr<Ice::Object>& session)
{
lock_guard lock(_mutex);
auto p = _sessions.find(session);
if(p != _sessions.end() && p->second.sessionControl)
{
if(!p->second.adapterIdSet) // Cache the adapterId set proxy
{
p->second.adapterIdSet = p->second.sessionControl->adapterIds();
}
return p->second.adapterIdSet;
}
else
{
return nullptr;
}
}
void
SessionServantManager::removeSession(const shared_ptr<Ice::Object>& session)
{
lock_guard lock(_mutex);
auto p = _sessions.find(session);
assert(p != _sessions.end());
//
// Remove all the servants associated with the session.
//
for(auto q = p->second.identities.cbegin(); q != p->second.identities.cend(); ++q)
{
_servants.erase(*q);
}
//
// If this is an admin session, remove its connection from the admin connections.
//
if(!p->second.category.empty() && p->second.connection)
{
assert(_adminConnections.find(p->second.connection) != _adminConnections.end());
_adminConnections.erase(_adminConnections.find(p->second.connection));
if(_adminCallbackRouter != nullptr)
{
_adminCallbackRouter->removeMapping(p->second.category);
}
}
_sessions.erase(p);
}
shared_ptr<Ice::ObjectPrx>
SessionServantManager::add(const shared_ptr<Ice::Object>& servant, const shared_ptr<Ice::Object>& session)
{
lock_guard lock(_mutex);
return addImpl(servant, session);
}
void
SessionServantManager::remove(const Ice::Identity& id)
{
lock_guard lock(_mutex);
map<Ice::Identity, ServantInfo>::iterator p = _servants.find(id);
assert(p != _servants.end());
//
// Find the session associated to the servant and remove the servant identity from the
// session identities.
//
map<shared_ptr<Ice::Object>, SessionInfo>::iterator q = _sessions.find(p->second.session);
assert(q != _sessions.end());
q->second.identities.erase(id);
//
// Remove the identity from the Glacier2 identity set.
//
if(q->second.identitySet)
{
try
{
q->second.identitySet->remove({id});
}
catch(const Ice::LocalException&)
{
}
}
//
// Remove the servant from the servant map.
//
_servants.erase(p);
}
shared_ptr<Ice::ObjectPrx>
SessionServantManager::addImpl(const shared_ptr<Ice::Object>& servant, const shared_ptr<Ice::Object>& session)
{
auto p = _sessions.find(session);
assert(p != _sessions.end());
Ice::Identity id;
id.name = Ice::generateUUID();
id.category = _instanceName;
//
// Add the identity to the session identities.
//
p->second.identities.insert(id);
//
// Add the identity to the Glacier2 identity set.
//
if(p->second.identitySet)
{
try
{
p->second.identitySet->add({id});
}
catch(const Ice::LocalException&)
{
}
}
//
// Add the servant to the servant map and return its proxy.
//
_servants.insert(make_pair(id, ServantInfo(servant, p->second.connection, session)));
return _adapter->createProxy(id);
}
|