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
|
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//
#include <Glacier2/FilterManager.h>
#include <Glacier2/RoutingTable.h>
#include <Glacier2/RouterI.h>
#include <Glacier2/Session.h>
#include<random>
using namespace std;
using namespace Ice;
using namespace Glacier2;
Glacier2::RouterI::RouterI(shared_ptr<Instance> instance, shared_ptr<Connection> connection,
const string& userId, shared_ptr<SessionPrx> session,
const Identity& controlId, shared_ptr<FilterManager> filters,
const Context& context) :
_instance(move(instance)),
_routingTable(make_shared<RoutingTable>(_instance->communicator(), _instance->proxyVerifier())),
_clientBlobject(make_shared<ClientBlobject>(_instance, move(filters), context, _routingTable)),
_clientBlobjectBuffered(_instance->clientRequestQueueThread()),
_serverBlobjectBuffered(_instance->serverRequestQueueThread()),
_connection(move(connection)),
_userId(userId),
_session(move(session)),
_controlId(controlId),
_context(context),
_timestamp(chrono::steady_clock::now())
{
//
// If Glacier2 will be used with pre 3.2 clients, then the client proxy must be set.
// Otherwise getClientProxy just needs to return a nil proxy.
//
if(_instance->properties()->getPropertyAsInt("Glacier2.ReturnClientProxy") > 0)
{
const_cast<shared_ptr<ObjectPrx>&>(_clientProxy) =
_instance->clientObjectAdapter()->createProxy(stringToIdentity("dummy"));
}
if(_instance->serverObjectAdapter())
{
Identity ident = { "dummy", "" };
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dist(33, 126); // We use ASCII 33-126 (from ! to ~, w/o space).
for(unsigned int i = 0; i < 20; ++i)
{
ident.category.push_back(static_cast<char>(dist(gen)));
}
const_cast<shared_ptr<ObjectPrx>&>(_serverProxy) = _instance->serverObjectAdapter()->createProxy(ident);
shared_ptr<ServerBlobject>& serverBlobject = const_cast<shared_ptr<ServerBlobject>&>(_serverBlobject);
serverBlobject = make_shared<ServerBlobject>(_instance, _connection);
}
if(_instance->getObserver())
{
updateObserver(_instance->getObserver());
}
}
void
Glacier2::RouterI::destroy(function<void(exception_ptr)> error)
{
if(_session)
{
if(_instance->serverObjectAdapter())
{
try
{
//
// Remove the session control object.
//
_instance->serverObjectAdapter()->remove(_controlId);
}
catch(const NotRegisteredException&)
{
}
catch(const ObjectAdapterDeactivatedException&)
{
//
// Expected if the router has been shutdown.
//
}
}
if(_context.size() > 0)
{
_session->destroyAsync(nullptr, move(error), nullptr, _context);
}
else
{
_session->destroyAsync(nullptr, move(error), nullptr);
}
}
_clientBlobject->destroy();
if(_serverBlobject)
{
_serverBlobject->destroy();
}
_routingTable->destroy();
}
shared_ptr<ObjectPrx>
Glacier2::RouterI::getClientProxy(Ice::optional<bool>& hasRoutingTable, const Current&) const
{
// No mutex lock necessary, _clientProxy is immutable and is never destroyed.
hasRoutingTable = true;
return _clientProxy;
}
shared_ptr<ObjectPrx>
Glacier2::RouterI::getServerProxy(const Current&) const
{
// No mutex lock necessary, _serverProxy is immutable and is never destroyed.
return _serverProxy;
}
ObjectProxySeq
Glacier2::RouterI::addProxies(ObjectProxySeq proxies, const Current& current)
{
return _routingTable->add(move(proxies), current);
}
string
Glacier2::RouterI::getCategoryForClient(const Current&) const
{
assert(false); // Must not be called in this router implementation.
return 0;
}
void
Glacier2::RouterI::createSessionAsync(string, string,
function<void(const shared_ptr<SessionPrx>& returnValue)>,
function<void(exception_ptr)>, const Current&)
{
assert(false); // Must not be called in this router implementation.
}
void
Glacier2::RouterI::createSessionFromSecureConnectionAsync(function<void(const shared_ptr<SessionPrx>& returnValue)>,
function<void(exception_ptr)>, const Current&)
{
assert(false); // Must not be called in this router implementation.
}
void
Glacier2::RouterI::refreshSessionAsync(function<void()>, function<void(exception_ptr)>, const Current&)
{
assert(false); // Must not be called in this router implementation.
}
void
Glacier2::RouterI::destroySession(const Current&)
{
assert(false); // Must not be called in this router implementation.
}
Long
Glacier2::RouterI::getSessionTimeout(const Current&) const
{
assert(false); // Must not be called in this router implementation.
return 0;
}
Int
Glacier2::RouterI::getACMTimeout(const Current&) const
{
assert(false); // Must not be called in this router implementation.
return 0;
}
shared_ptr<ClientBlobject>
Glacier2::RouterI::getClientBlobject() const
{
// Can only be called with the SessionRouterI mutex locked
if(!_clientBlobjectBuffered && _observer)
{
_observer->forwarded(true);
}
return _clientBlobject;
}
shared_ptr<ServerBlobject>
Glacier2::RouterI::getServerBlobject() const
{
// Can only be called with the SessionRouterI mutex locked
if(!_serverBlobjectBuffered && _observer)
{
_observer->forwarded(false);
}
return _serverBlobject;
}
shared_ptr<SessionPrx>
Glacier2::RouterI::getSession() const
{
return _session; // No mutex lock necessary, _session is immutable.
}
chrono::steady_clock::time_point
Glacier2::RouterI::getTimestamp() const
{
// Can only be called with the SessionRouterI mutex locked
return _timestamp;
}
void
Glacier2::RouterI::updateTimestamp() const
{
// Can only be called with the SessionRouterI mutex locked
_timestamp = chrono::steady_clock::now();
}
void
Glacier2::RouterI::updateObserver(const shared_ptr<Glacier2::Instrumentation::RouterObserver>& observer)
{
// Can only be called with the SessionRouterI mutex locked
_observer = _routingTable->updateObserver(observer, _userId, _connection);
_clientBlobject->updateObserver(_observer);
if(_serverBlobject)
{
_serverBlobject->updateObserver(_observer);
}
}
string
Glacier2::RouterI::toString() const
{
ostringstream out;
out << "id = " << _userId << '\n';
if(_serverProxy)
{
out << "category = " << _serverProxy->ice_getIdentity().category << '\n';
}
out << _connection->toString();
return out.str();
}
|