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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
|
// **********************************************************************
//
// Copyright (c) 2003-2012 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#include <Glacier2/Application.h>
#include <IceUtil/IceUtil.h>
#include <IceUtil/ArgVector.h>
using namespace std;
using namespace Ice;
Ice::ObjectAdapterPtr Glacier2::Application::_adapter;
Glacier2::RouterPrx Glacier2::Application::_router;
Glacier2::SessionPrx Glacier2::Application::_session;
bool Glacier2::Application::_createdSession = false;
string Glacier2::Application::_category;
namespace
{
class SessionPingThread : virtual public IceUtil::Shared
{
public:
virtual void done() = 0;
};
typedef IceUtil::Handle<SessionPingThread> SessionPingThreadPtr;
class AMI_Router_refreshSessionI : public Glacier2::AMI_Router_refreshSession
{
public:
AMI_Router_refreshSessionI(Glacier2::Application* app, const SessionPingThreadPtr& pinger) :
_app(app),
_pinger(pinger)
{
}
void
ice_response()
{
}
void
ice_exception(const Ice::Exception& ex)
{
//
// Here the session has gone. The thread
// terminates, and we notify the
// application that the session has been
// destroyed.
//
_pinger->done();
_app->sessionDestroyed();
}
private:
Glacier2::Application* _app;
SessionPingThreadPtr _pinger;
};
class SessionPingThreadI : virtual public IceUtil::Thread, virtual public SessionPingThread
{
public:
SessionPingThreadI(Glacier2::Application* app, const Glacier2::RouterPrx& router, IceUtil::Int64 period) :
_app(app),
_router(router),
_period(period),
_done(false)
{
assert(_period);
}
void
run()
{
IceUtil::Monitor<IceUtil::Mutex>::Lock lock(_monitor);
while(true)
{
try
{
_router->refreshSession_async(new AMI_Router_refreshSessionI(_app, this));
}
catch(const Ice::CommunicatorDestroyedException&)
{
//
// AMI requests can raise CommunicatorDestroyedException directly.
//
break;
}
if(!_done)
{
_monitor.timedWait(IceUtil::Time::milliSeconds(_period));
}
if(_done)
{
break;
}
}
}
void
done()
{
IceUtil::Monitor<IceUtil::Mutex>::Lock lock(_monitor);
if(!_done)
{
_done = true;
_monitor.notify();
}
}
private:
Glacier2::Application* _app;
Glacier2::RouterPrx _router;
IceUtil::Int64 _period;
bool _done;
IceUtil::Monitor<IceUtil::Mutex> _monitor;
};
typedef IceUtil::Handle<SessionPingThreadI> SessionPingThreadIPtr;
}
string
Glacier2::RestartSessionException::ice_name() const
{
return "RestartSessionException";
}
Exception*
Glacier2::RestartSessionException::ice_clone() const
{
return new RestartSessionException(*this);
}
void
Glacier2::RestartSessionException::ice_throw() const
{
throw *this;
}
Ice::ObjectAdapterPtr
Glacier2::Application::objectAdapter()
{
if(!_router)
{
SessionNotExistException ex;
throw ex;
}
IceUtil::Mutex::Lock lock(*IceInternal::Application::mutex);
if(!_adapter)
{
_adapter = communicator()->createObjectAdapterWithRouter("", _router);
_adapter->activate();
}
return _adapter;
}
Ice::ObjectPrx
Glacier2::Application::addWithUUID(const Ice::ObjectPtr& servant)
{
return objectAdapter()->add(servant, createCallbackIdentity(IceUtil::generateUUID()));
}
Ice::Identity
Glacier2::Application::createCallbackIdentity(const string& name)
{
Ice::Identity id;
id.name = name;
id.category = categoryForClient();
return id;
}
std::string
Glacier2::Application::categoryForClient()
{
if(!_router)
{
SessionNotExistException ex;
throw ex;
}
return _category;
}
int
Glacier2::Application::doMain(int argc, char* argv[], const Ice::InitializationData& initData)
{
// Set the default properties for all Glacier2 applications.
initData.properties->setProperty("Ice.ACM.Client", "0");
initData.properties->setProperty("Ice.RetryIntervals", "-1");
bool restart;
int ret = 0;
do
{
//
// A copy of the initialization data and the string seq
// needs to be passed to doMainInternal, as these can be
// changed by the application.
//
Ice::InitializationData id(initData);
id.properties = id.properties->clone();
Ice::StringSeq args = Ice::argsToStringSeq(argc, argv);
restart = doMain(args, id, ret);
}
while(restart);
return ret;
}
bool
Glacier2::Application::doMain(Ice::StringSeq& args, const Ice::InitializationData& initData, int& status)
{
//
// Reset internal state variables from Ice.Application. The
// remainder are reset at the end of this method.
//
IceInternal::Application::_callbackInProgress = false;
IceInternal::Application::_destroyed = false;
IceInternal::Application::_interrupted = false;
bool restart = false;
status = 0;
SessionPingThreadIPtr ping;
try
{
IceInternal::Application::_communicator = Ice::initialize(args, initData);
_router = Glacier2::RouterPrx::uncheckedCast(communicator()->getDefaultRouter());
if(!_router)
{
Error out(getProcessLogger());
out << IceInternal::Application::_appName << ": no glacier2 router configured";
status = 1;
}
else
{
//
// The default is to destroy when a signal is received.
//
if(IceInternal::Application::_signalPolicy == Ice::HandleSignals)
{
destroyOnInterrupt();
}
// If createSession throws, we're done.
try
{
_session = createSession();
_createdSession = true;
}
catch(const Ice::LocalException& ex)
{
Error out(getProcessLogger());
out << IceInternal::Application::_appName << ": " << ex;
status = 1;
}
if(_createdSession)
{
IceUtil::Int64 timeout = _router->getSessionTimeout();
if(timeout > 0)
{
ping = new SessionPingThreadI(this, _router, (timeout * 1000) / 2);
ping->start();
}
_category = _router->getCategoryForClient();
IceUtilInternal::ArgVector a(args);
status = runWithSession(a.argc, a.argv);
}
}
}
// We want to restart on those exceptions which indicate a
// break down in communications, but not those exceptions that
// indicate a programming logic error (ie: marshal, protocol
// failure, etc).
catch(const RestartSessionException&)
{
restart = true;
}
catch(const Ice::ConnectionRefusedException& ex)
{
Error out(getProcessLogger());
out << IceInternal::Application::_appName << ": " << ex;
restart = true;
}
catch(const Ice::ConnectionLostException& ex)
{
Error out(getProcessLogger());
out << IceInternal::Application::_appName << ": " << ex;
restart = true;
}
catch(const Ice::UnknownLocalException& ex)
{
Error out(getProcessLogger());
out << IceInternal::Application::_appName << ": " << ex;
restart = true;
}
catch(const Ice::RequestFailedException& ex)
{
Error out(getProcessLogger());
out << IceInternal::Application::_appName << ": " << ex;
restart = true;
}
catch(const Ice::TimeoutException& ex)
{
Error out(getProcessLogger());
out << IceInternal::Application::_appName << ": " << ex;
restart = true;
}
catch(const Ice::LocalException& ex)
{
Error out(getProcessLogger());
out << IceInternal::Application::_appName << ": " << ex;
status = 1;
}
catch(const std::exception& ex)
{
Error out(getProcessLogger());
out << IceInternal::Application::_appName << ": std::exception " << ex;
status = 1;
}
catch(const std::string& ex)
{
Error out(getProcessLogger());
out << IceInternal::Application::_appName << ": c++ exception " << ex;
status = 1;
}
catch(const char* ex)
{
Error out(getProcessLogger());
out << IceInternal::Application::_appName << ": char* exception " << ex;
status = 1;
}
catch(...)
{
Error out(getProcessLogger());
out << IceInternal::Application::_appName << ": unknown exception";
status = 1;
}
//
// Don't want any new interrupt and at this point (post-run),
// it would not make sense to release a held signal to run
// shutdown or destroy.
//
if(IceInternal::Application::_signalPolicy == HandleSignals)
{
ignoreInterrupt();
}
{
IceUtil::Mutex::Lock lock(*IceInternal::Application::mutex);
while(IceInternal::Application::_callbackInProgress)
{
IceInternal::Application::_condVar->wait(lock);
}
if(IceInternal::Application::_destroyed)
{
IceInternal::Application::_communicator = 0;
}
else
{
IceInternal::Application::_destroyed = true;
//
// And _communicator != 0, meaning will be destroyed
// next, _destroyed = true also ensures that any
// remaining callback won't do anything
//
}
IceInternal::Application::_application = 0;
}
if(ping)
{
ping->done();
while(true)
{
ping->getThreadControl().join();
break;
}
ping = 0;
}
if(_createdSession && _router)
{
try
{
_router->destroySession();
}
catch(const Ice::ConnectionLostException&)
{
// Expected if another thread invoked on an object from the session concurrently.
}
catch(const Glacier2::SessionNotExistException&)
{
// This can also occur.
}
catch(const exception& ex)
{
// Not expected.
Error out(getProcessLogger());
out << "unexpected exception when destroying the session:\n" << ex;
}
_router = 0;
}
if(IceInternal::Application::_communicator)
{
try
{
IceInternal::Application::_communicator->destroy();
}
catch(const Ice::LocalException& ex)
{
Error out(getProcessLogger());
out << IceInternal::Application::_appName << ": " << ex;
status = 1;
}
catch(const exception& ex)
{
Error out(getProcessLogger());
out << "unknown exception:\n" << ex;
status = 1;
}
IceInternal::Application::_communicator = 0;
}
//
// Reset internal state. We cannot reset the Application state
// here, since _destroyed must remain true until we re-run
// this method.
//
_adapter = 0;
_router = 0;
_session = 0;
_createdSession = false;
_category.clear();
return restart;
}
|