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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
|
// **********************************************************************
//
// Copyright (c) 2003-2014 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 <IceUtil/UUID.h>
#include <IceUtil/Options.h>
#include <IceUtil/FileUtil.h>
#include <Ice/Service.h>
#include <Glacier2/Instance.h>
#include <Glacier2/RouterI.h>
#include <Glacier2/Session.h>
#include <Glacier2/SessionRouterI.h>
#include <Glacier2/NullPermissionsVerifier.h>
using namespace std;
using namespace Ice;
using namespace Glacier2;
namespace
{
class RouterService : public Service
{
public:
RouterService();
protected:
virtual bool start(int, char*[], int&);
virtual bool stop();
virtual CommunicatorPtr initializeCommunicator(int&, char*[], const InitializationData&);
private:
void usage(const std::string&);
InstancePtr _instance;
SessionRouterIPtr _sessionRouter;
};
class FinderI : public Ice::RouterFinder
{
public:
FinderI(const Glacier2::RouterPrx& router) : _router(router)
{
}
virtual Ice::RouterPrx
getRouter(const Ice::Current&)
{
return _router;
}
private:
const Glacier2::RouterPrx _router;
};
};
RouterService::RouterService()
{
}
bool
RouterService::start(int argc, char* argv[], int& status)
{
bool nowarn;
IceUtilInternal::Options opts;
opts.addOpt("h", "help");
opts.addOpt("v", "version");
opts.addOpt("", "nowarn");
vector<string> args;
try
{
args = opts.parse(argc, const_cast<const char**>(argv));
}
catch(const IceUtilInternal::BadOptException& e)
{
error(e.reason);
usage(argv[0]);
return false;
}
if(opts.isSet("help"))
{
usage(argv[0]);
status = EXIT_SUCCESS;
return false;
}
if(opts.isSet("version"))
{
print(ICE_STRING_VERSION);
status = EXIT_SUCCESS;
return false;
}
nowarn = opts.isSet("nowarn");
if(!args.empty())
{
cerr << argv[0] << ": too many arguments" << endl;
usage(argv[0]);
return false;
}
PropertiesPtr properties = communicator()->getProperties();
//
// Initialize the client object adapter.
//
const string clientEndpointsProperty = "Glacier2.Client.Endpoints";
if(properties->getProperty(clientEndpointsProperty).empty())
{
error("property `" + clientEndpointsProperty + "' is not set");
return false;
}
if(properties->getPropertyAsInt("Glacier2.SessionTimeout") > 0 &&
properties->getProperty("Glacier2.Client.ACM.Timeout").empty())
{
ostringstream os;
os << properties->getPropertyAsInt("Glacier2.SessionTimeout");
properties->setProperty("Glacier2.Client.ACM.Timeout", os.str());
}
if(properties->getProperty("Glacier2.Client.ACM.Close").empty())
{
properties->setProperty("Glacier2.Client.ACM.Close", "4"); // Forcefull close on invocation and idle.
}
ObjectAdapterPtr clientAdapter = communicator()->createObjectAdapter("Glacier2.Client");
//
// Initialize the server object adapter only if server endpoints
// are defined.
//
const string serverEndpointsProperty = "Glacier2.Server.Endpoints";
ObjectAdapterPtr serverAdapter;
if(!properties->getProperty(serverEndpointsProperty).empty())
{
serverAdapter = communicator()->createObjectAdapter("Glacier2.Server");
}
string instanceName = communicator()->getProperties()->getPropertyWithDefault("Glacier2.InstanceName", "Glacier2");
vector<string> verifierProperties;
verifierProperties.push_back("Glacier2.PermissionsVerifier");
verifierProperties.push_back("Glacier2.SSLPermissionsVerifier");
Glacier2Internal::setupNullPermissionsVerifier(communicator(), instanceName, verifierProperties);
string verifierProperty = verifierProperties[0];
PermissionsVerifierPrx verifier;
ObjectPrx obj;
try
{
//
// We use propertyToProxy instead of stringToProxy because the property
// can provide proxy attributes
//
obj = communicator()->propertyToProxy(verifierProperty);
}
catch(const std::exception& ex)
{
ServiceError err(this);
err << "permissions verifier `" << communicator()->getProperties()->getProperty(verifierProperty)
<< "' is invalid:\n" << ex;
return false;
}
if(obj)
{
try
{
verifier = PermissionsVerifierPrx::checkedCast(obj);
if(!verifier)
{
ServiceError err(this);
err << "permissions verifier `" << communicator()->getProperties()->getProperty(verifierProperty)
<< "' is invalid";
return false;
}
}
catch(const Ice::Exception& ex)
{
if(!nowarn)
{
ServiceWarning warn(this);
warn << "unable to contact permissions verifier `"
<< communicator()->getProperties()->getProperty(verifierProperty) << "'\n" << ex;
}
verifier = PermissionsVerifierPrx::uncheckedCast(obj);
}
}
//
// Get the session manager if specified.
//
string sessionManagerProperty = "Glacier2.SessionManager";
string sessionManagerPropertyValue = properties->getProperty(sessionManagerProperty);
SessionManagerPrx sessionManager;
if(!sessionManagerPropertyValue.empty())
{
try
{
obj = communicator()->propertyToProxy(sessionManagerProperty);
}
catch(const std::exception& ex)
{
ServiceError err(this);
err << "session manager `" << sessionManagerPropertyValue << "' is invalid\n:" << ex;
return false;
}
try
{
sessionManager = SessionManagerPrx::checkedCast(obj);
if(!sessionManager)
{
error("session manager `" + sessionManagerPropertyValue + "' is invalid");
return false;
}
}
catch(const std::exception& ex)
{
if(!nowarn)
{
ServiceWarning warn(this);
warn << "unable to contact session manager `" << sessionManagerPropertyValue << "'\n" << ex;
}
sessionManager = SessionManagerPrx::uncheckedCast(obj);
}
sessionManager =
SessionManagerPrx::uncheckedCast(sessionManager->ice_connectionCached(false)->ice_locatorCacheTimeout(
properties->getPropertyAsIntWithDefault("Glacier2.SessionManager.LocatorCacheTimeout", 600)));
}
//
// Check for an SSL permissions verifier.
//
string sslVerifierProperty = verifierProperties[1];
SSLPermissionsVerifierPrx sslVerifier;
try
{
obj = communicator()->propertyToProxy(sslVerifierProperty);
}
catch(const std::exception& ex)
{
ServiceError err(this);
err << "ssl permissions verifier `" << communicator()->getProperties()->getProperty(sslVerifierProperty)
<< "' is invalid:\n" << ex;
return false;
}
if(obj)
{
try
{
sslVerifier = SSLPermissionsVerifierPrx::checkedCast(obj);
if(!sslVerifier)
{
ServiceError err(this);
err << "ssl permissions verifier `"
<< communicator()->getProperties()->getProperty(sslVerifierProperty)
<< "' is invalid";
}
}
catch(const Ice::Exception& ex)
{
if(!nowarn)
{
ServiceWarning warn(this);
warn << "unable to contact ssl permissions verifier `"
<< communicator()->getProperties()->getProperty(sslVerifierProperty) << "'\n"
<< ex;
}
sslVerifier = SSLPermissionsVerifierPrx::uncheckedCast(obj);
}
}
//
// Get the SSL session manager if specified.
//
string sslSessionManagerProperty = "Glacier2.SSLSessionManager";
string sslSessionManagerPropertyValue = properties->getProperty(sslSessionManagerProperty);
SSLSessionManagerPrx sslSessionManager;
if(!sslSessionManagerPropertyValue.empty())
{
try
{
obj = communicator()->propertyToProxy(sslSessionManagerProperty);
}
catch(const std::exception& ex)
{
ServiceError err(this);
err << "ssl session manager `" << sslSessionManagerPropertyValue << "' is invalid:\n" << ex;
return false;
}
try
{
sslSessionManager = SSLSessionManagerPrx::checkedCast(obj);
if(!sslSessionManager)
{
error("ssl session manager `" + sslSessionManagerPropertyValue + "' is invalid");
return false;
}
}
catch(const Ice::Exception& ex)
{
if(!nowarn)
{
ServiceWarning warn(this);
warn << "unable to contact ssl session manager `" << sslSessionManagerPropertyValue
<< "'\n" << ex;
}
sslSessionManager = SSLSessionManagerPrx::uncheckedCast(obj);
}
sslSessionManager =
SSLSessionManagerPrx::uncheckedCast(sslSessionManager->ice_connectionCached(false)->ice_locatorCacheTimeout(
properties->getPropertyAsIntWithDefault("Glacier2.SSLSessionManager.LocatorCacheTimeout", 600)));
}
if(!verifier && !sslVerifier)
{
error("Glacier2 requires a permissions verifier or password file");
return false;
}
//
// Create the instance object.
//
try
{
_instance = new Instance(communicator(), clientAdapter, serverAdapter);
}
catch(const Ice::InitializationException& ex)
{
error("Glacier2 initialization failed:\n" + ex.reason);
return false;
}
//
// Create the session router. The session router registers itself
// and all required servant locators, so no registration has to be
// done here.
//
_sessionRouter = new SessionRouterI(_instance, verifier, sessionManager, sslVerifier, sslSessionManager);
//
// Th session router is used directly as servant for the main
// Glacier2 router Ice object.
//
Identity routerId;
routerId.category = _instance->properties()->getPropertyWithDefault("Glacier2.InstanceName", "Glacier2");
routerId.name = "router";
Glacier2::RouterPrx routerPrx = Glacier2::RouterPrx::uncheckedCast(clientAdapter->add(_sessionRouter, routerId));
//
// Add the Ice router finder object to allow retrieving the router
// proxy with just the endpoint information of the router.
//
Identity finderId;
finderId.category = "Ice";
finderId.name = "RouterFinder";
clientAdapter->add(new FinderI(routerPrx), finderId);
if(_instance->getObserver())
{
_instance->getObserver()->setObserverUpdater(_sessionRouter);
}
//
// Everything ok, let's go.
//
try
{
clientAdapter->activate();
if(serverAdapter)
{
serverAdapter->activate();
}
}
catch(const std::exception& ex)
{
{
ServiceError err(this);
err << "caught exception activating object adapters\n" << ex;
}
stop();
return false;
}
return true;
}
bool
RouterService::stop()
{
if(_sessionRouter)
{
_sessionRouter->destroy();
_sessionRouter = 0;
}
if(_instance)
{
if(_instance->getObserver())
{
_instance->getObserver()->setObserverUpdater(0);
}
_instance->destroy();
_instance = 0;
}
return true;
}
CommunicatorPtr
RouterService::initializeCommunicator(int& argc, char* argv[],
const InitializationData& initializationData)
{
InitializationData initData = initializationData;
initData.properties = createProperties(argc, argv, initializationData.properties);
//
// Make sure that Glacier2 doesn't use a router.
//
initData.properties->setProperty("Ice.Default.Router", "");
//
// If Glacier2.PermissionsVerifier is not set and Glacier2.CryptPasswords is set,
// load the Glacier2CryptPermissionsVerifier plug-in
//
string verifier = "Glacier2.PermissionsVerifier";
if(initData.properties->getProperty(verifier).empty())
{
string cryptPasswords = initData.properties->getProperty("Glacier2.CryptPasswords");
if(!cryptPasswords.empty())
{
initData.properties->setProperty("Ice.Plugin.Glacier2CryptPermissionsVerifier",
"Glacier2CryptPermissionsVerifier:createCryptPermissionsVerifier");
initData.properties->setProperty("Glacier2CryptPermissionsVerifier.Glacier2.PermissionsVerifier",
cryptPasswords);
}
}
//
// Active connection management is permitted with Glacier2. For
// the client object adapter, the ACM timeout is set to the
// session timeout to ensure client connections are not closed
// prematurely,
//
//initData.properties->setProperty("Ice.ACM.Client", "0");
//initData.properties->setProperty("Ice.ACM.Server", "0");
//
// We do not need to set Ice.RetryIntervals to -1, i.e., we do
// not have to disable connection retry. It is safe for
// Glacier2 to retry outgoing connections to servers. Retry
// for incoming connections from clients must be disabled in
// the clients.
//
return Service::initializeCommunicator(argc, argv, initData);
}
void
RouterService::usage(const string& appName)
{
string options =
"Options:\n"
"-h, --help Show this message.\n"
"-v, --version Display the Ice version.\n"
"--nowarn Suppress warnings.";
#ifndef _WIN32
options.append(
"\n"
"\n"
"--daemon Run as a daemon.\n"
"--pidfile FILE Write process ID into FILE.\n"
"--noclose Do not close open file descriptors.\n"
"--nochdir Do not change the current working directory.\n"
);
#endif
print("Usage: " + appName + " [options]\n" + options);
}
#ifdef _WIN32
int
wmain(int argc, wchar_t* argv[])
#else
int
main(int argc, char* argv[])
#endif
{
RouterService svc;
return svc.main(argc, argv);
}
|