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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
|
// **********************************************************************
//
// 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 <IceUtil/DisableWarnings.h>
#include <Glacier2/SessionHelper.h>
#include <IceUtil/UUID.h>
#include <IceUtil/CountDownLatch.h>
#include <Ice/Ice.h>
#include <algorithm> // required by max
#include <memory> // required by auto_ptr
using namespace std;
namespace
{
class ConnectStrategy : public IceUtil::Shared
{
public:
virtual Glacier2::SessionPrx connect(const Glacier2::RouterPrx& router) = 0;
};
typedef IceUtil::Handle< ConnectStrategy> ConnectStrategyPtr;
class Disconnected : public Ice::DispatcherCall
{
public:
Disconnected(const Glacier2::SessionHelperPtr& session, const Glacier2::SessionCallbackPtr& callback) :
_session(session),
_callback(callback)
{
}
virtual
void run()
{
_callback->disconnected(_session);
}
private:
const Glacier2::SessionHelperPtr _session;
const Glacier2::SessionCallbackPtr _callback;
};
class SessionRefreshThread : public IceUtil::Thread
{
public:
SessionRefreshThread(const Glacier2::SessionHelperPtr&, const Glacier2::RouterPrx&, Ice::Long);
virtual void run();
void done();
void success();
void failure(const Ice::Exception&);
private:
const Glacier2::Callback_Router_refreshSessionPtr _cb;
const Glacier2::SessionHelperPtr _session;
const Glacier2::RouterPrx _router;
Ice::Long _period;
bool _done;
IceUtil::Monitor<IceUtil::Mutex> _monitor;
};
typedef IceUtil::Handle<SessionRefreshThread> SessionRefreshThreadPtr;
class SessionHelperI : public Glacier2::SessionHelper
{
public:
SessionHelperI(const Glacier2::SessionCallbackPtr&, const Ice::InitializationData&);
void destroy();
Ice::CommunicatorPtr communicator() const;
std::string categoryForClient() const;
Ice::ObjectPrx addWithUUID(const Ice::ObjectPtr&);
Glacier2::SessionPrx session() const;
bool isConnected() const;
Ice::ObjectAdapterPtr objectAdapter();
friend class DestroyInternal;
friend class ConnectThread;
friend class DispatchCallThread;
friend class Glacier2::SessionFactoryHelper;
private:
void destroy(const IceUtil::ThreadPtr&);
Ice::ObjectAdapterPtr internalObjectAdapter();
void connected(const Glacier2::RouterPrx&, const Glacier2::SessionPrx&);
void destroyInternal(const Ice::DispatcherCallPtr&);
void connectFailed();
void connect(const std::map<std::string, std::string>&);
void connect(const std::string&, const std::string&, const std::map<std::string, std::string>&);
void connectImpl(const ConnectStrategyPtr&);
void dispatchCallback(const Ice::DispatcherCallPtr&, const Ice::ConnectionPtr&);
void dispatchCallbackAndWait(const Ice::DispatcherCallPtr&, const Ice::ConnectionPtr&);
IceUtil::Mutex _mutex;
Ice::CommunicatorPtr _communicator;
Ice::ObjectAdapterPtr _adapter;
Glacier2::RouterPrx _router;
Glacier2::SessionPrx _session;
SessionRefreshThreadPtr _refreshThread;
std::string _category;
bool _connected;
bool _destroy;
const Ice::InitializationData _initData;
const Glacier2::SessionCallbackPtr _callback;
};
typedef IceUtil::Handle<SessionHelperI> SessionHelperIPtr;
SessionRefreshThread::SessionRefreshThread(const Glacier2::SessionHelperPtr& session,
const Glacier2::RouterPrx& router, Ice::Long period) :
_cb(Glacier2::newCallback_Router_refreshSession(this, &SessionRefreshThread::success,
&SessionRefreshThread::failure)),
_session(session),
_router(router),
_period(period),
_done(false)
{
}
void
SessionRefreshThread::run()
{
IceUtil::Monitor<IceUtil::Mutex>::Lock lock(_monitor);
while(true)
{
try
{
_router->begin_refreshSession(_cb);
}
catch(const Ice::CommunicatorDestroyedException&)
{
//
// AMI requests can raise CommunicatorDestroyedException directly.
//
break;
}
if(!_done)
{
_monitor.timedWait(IceUtil::Time::seconds(_period));
}
if(_done)
{
break;
}
}
}
void
SessionRefreshThread::done()
{
IceUtil::Monitor<IceUtil::Mutex>::Lock lock(_monitor);
if(!_done)
{
_done = true;
_monitor.notify();
}
}
void
SessionRefreshThread::success()
{
}
void
SessionRefreshThread::failure(const Ice::Exception&)
{
done();
_session->destroy();
}
class DestroyInternal : public IceUtil::Thread
{
public:
DestroyInternal(const SessionHelperIPtr& session, const Glacier2::SessionCallbackPtr& callback) :
_session(session),
_disconnected(new Disconnected(session, callback))
{
}
virtual void run()
{
_session->destroyInternal(_disconnected);
}
private:
const SessionHelperIPtr _session;
const Ice::DispatcherCallPtr _disconnected;
};
}
SessionHelperI::SessionHelperI(const Glacier2::SessionCallbackPtr& callback,
const Ice::InitializationData& initData) :
_connected(false),
_destroy(false),
_initData(initData),
_callback(callback)
{
}
void
SessionHelperI::destroy()
{
IceUtil::Mutex::Lock sync(_mutex);
destroy(new DestroyInternal(this, _callback));
}
void
SessionHelperI::destroy(const IceUtil::ThreadPtr& destroyInternal)
{
if(_destroy)
{
return;
}
_destroy = true;
if(!_connected)
{
//
// In this case a connecting session is being
// destroyed. The communicator and session will be
// destroyed when the connection establishment has
// completed.
//
return;
}
_session = 0;
_connected = false;
//
// Run the destroyInternal in a thread. This is because it
// destroyInternal makes remote invocations.
//
destroyInternal->start();
}
Ice::CommunicatorPtr
SessionHelperI::communicator() const
{
IceUtil::Mutex::Lock sync(_mutex);
return _communicator;
}
string
SessionHelperI::categoryForClient() const
{
IceUtil::Mutex::Lock sync(_mutex);
if(!_router)
{
throw Glacier2::SessionNotExistException();
}
return _category;
}
Ice::ObjectPrx
SessionHelperI::addWithUUID(const Ice::ObjectPtr& servant)
{
IceUtil::Mutex::Lock sync(_mutex);
if(!_router)
{
throw Glacier2::SessionNotExistException();
}
Ice::Identity id;
id.name = IceUtil::generateUUID();
id.category = _category;
return internalObjectAdapter()->add(servant, id);
}
Glacier2::SessionPrx
SessionHelperI::session() const
{
IceUtil::Mutex::Lock sync(_mutex);
if(!_session)
{
throw new Glacier2::SessionNotExistException();
}
return _session;
}
bool
SessionHelperI::isConnected() const
{
IceUtil::Mutex::Lock sync(_mutex);
return _connected;
}
Ice::ObjectAdapterPtr
SessionHelperI::objectAdapter()
{
IceUtil::Mutex::Lock sync(_mutex);
return internalObjectAdapter();
}
bool
Glacier2::SessionHelper::operator==(const Glacier2::SessionHelper& other) const
{
return this == &other;
}
bool
Glacier2::SessionHelper::operator!=(const Glacier2::SessionHelper& other) const
{
return this != &other;
}
Ice::ObjectAdapterPtr
SessionHelperI::internalObjectAdapter()
{
if(!_router)
{
throw Glacier2::SessionNotExistException();
}
if(!_adapter)
{
_adapter = _communicator->createObjectAdapterWithRouter("", _router);
_adapter->activate();
}
return _adapter;
}
namespace
{
class ConnectStrategySecureConnection : public ConnectStrategy
{
public:
ConnectStrategySecureConnection(const map<string, string>& context) :
_context(context)
{
}
virtual Glacier2::SessionPrx
connect(const Glacier2::RouterPrx& router)
{
return router->createSessionFromSecureConnection(_context);
}
private:
const map<string, string> _context;
};
class ConnectStrategyUserPassword : public ConnectStrategy
{
public:
ConnectStrategyUserPassword(const string& user, const string& password, const map<string, string>& context) :
_user(user),
_password(password),
_context(context)
{
}
virtual Glacier2::SessionPrx
connect(const Glacier2::RouterPrx& router)
{
return router->createSession(_user, _password, _context);
}
private:
const string _user;
const string _password;
const map<string, string> _context;
};
}
void
SessionHelperI::connect(const map<string, string>& context)
{
IceUtil::Mutex::Lock sync(_mutex);
connectImpl(new ConnectStrategySecureConnection(context));
}
void
SessionHelperI::connect(const string& user, const string& password, const map<string, string>& context)
{
IceUtil::Mutex::Lock sync(_mutex);
connectImpl(new ConnectStrategyUserPassword(user, password, context));
}
void
SessionHelperI::destroyInternal(const Ice::DispatcherCallPtr& disconnected)
{
assert(_destroy);
Ice::CommunicatorPtr communicator;
Glacier2::RouterPrx router;
SessionRefreshThreadPtr refreshThread;
{
IceUtil::Mutex::Lock sync(_mutex);
router = _router;
_router = 0;
_connected = false;
refreshThread = _refreshThread;
_refreshThread = 0;
communicator = _communicator;
}
if(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 Ice::Exception& ex)
{
//
// Not expected.
//
ostringstream os;
os << "SessionHelper: unexpected exception when destroying the session:\n";
os << ex;
if(communicator)
{
communicator->getLogger()->warning(os.str());
}
}
}
if(refreshThread)
{
refreshThread->done();
refreshThread->getThreadControl().join();
refreshThread = 0;
}
if(communicator)
{
try
{
communicator->destroy();
}
catch(...)
{
}
communicator = 0;
}
dispatchCallback(disconnected, 0);
}
void
SessionHelperI::connectFailed()
{
Ice::CommunicatorPtr communicator;
{
IceUtil::Mutex::Lock sync(_mutex);
communicator = _communicator;
}
if(communicator)
{
try
{
communicator->destroy();
}
catch(...)
{
}
}
}
namespace
{
class ConnectFailed : public Ice::DispatcherCall
{
public:
ConnectFailed(const Glacier2::SessionCallbackPtr& callback, const Glacier2::SessionHelperPtr& session,
const Ice::Exception& ex) :
_callback(callback),
_session(session)
{
_ex.reset(ex.ice_clone());
}
virtual void
run()
{
const Ice::Exception* ex(_ex.get());
_callback->connectFailed(_session, *ex);
}
private:
const Glacier2::SessionCallbackPtr _callback;
const Glacier2::SessionHelperPtr _session;
std::auto_ptr<Ice::Exception> _ex;
};
class CreatedCommunicator : public Ice::DispatcherCall
{
public:
CreatedCommunicator(const Glacier2::SessionCallbackPtr& callback, const Glacier2::SessionHelperPtr& session) :
_callback(callback),
_session(session)
{
}
virtual void
run()
{
_callback->createdCommunicator(_session);
}
private:
const Glacier2::SessionCallbackPtr _callback;
const Glacier2::SessionHelperPtr _session;
};
class ConnectThread : public IceUtil::Thread
{
public:
ConnectThread(const Glacier2::SessionCallbackPtr& callback, const SessionHelperIPtr& session,
const ConnectStrategyPtr& factory, const Ice::CommunicatorPtr& communicator) :
_callback(callback),
_session(session),
_factory(factory),
_communicator(communicator)
{
}
virtual void
run()
{
try
{
_session->dispatchCallbackAndWait(new CreatedCommunicator(_callback, _session), 0);
Glacier2::RouterPrx routerPrx = Glacier2::RouterPrx::uncheckedCast(_communicator->getDefaultRouter());
Glacier2::SessionPrx session = _factory->connect(routerPrx);
_session->connected(routerPrx, session);
}
catch(const Ice::Exception& ex)
{
try
{
_session->connectFailed();
}
catch(...)
{
}
_session->dispatchCallback(new ConnectFailed(_callback, _session, ex), 0);
}
}
private:
const Glacier2::SessionCallbackPtr _callback;
const SessionHelperIPtr _session;
const ConnectStrategyPtr _factory;
const Ice::CommunicatorPtr _communicator;
};
class DispatchCallThread : public IceUtil::Thread
{
public:
DispatchCallThread(const SessionHelperIPtr& session, const Ice::DispatcherCallPtr& call,
const Ice::ConnectionPtr& conn) :
_session(session),
_call(call),
_conn(conn)
{
}
virtual void run()
{
_session->dispatchCallback(_call, _conn);
}
private:
const SessionHelperIPtr _session;
const Ice::DispatcherCallPtr _call;
const Ice::ConnectionPtr _conn;
};
}
void
SessionHelperI::connectImpl(const ConnectStrategyPtr& factory)
{
assert(!_destroy);
try
{
_communicator = Ice::initialize(_initData);
}
catch(const Ice::LocalException& ex)
{
_destroy = true;
IceUtil::ThreadPtr thread = new DispatchCallThread(this, new ConnectFailed(_callback, this, ex), 0);
thread->start();
return;
}
IceUtil::ThreadPtr connectThread = new ConnectThread(_callback, this, factory, _communicator);
connectThread->start();
}
namespace
{
class Connected : public Ice::DispatcherCall
{
public:
Connected(const Glacier2::SessionCallbackPtr& callback, const Glacier2::SessionHelperPtr& session) :
_callback(callback),
_session(session)
{
}
virtual void
run()
{
try
{
_callback->connected(_session);
}
catch(const Glacier2::SessionNotExistException&)
{
_session->destroy();
}
}
private:
const Glacier2::SessionCallbackPtr _callback;
const Glacier2::SessionHelperPtr _session;
};
}
void
SessionHelperI::connected(const Glacier2::RouterPrx& router, const Glacier2::SessionPrx& session)
{
//
// Remote invocation should be done without acquire a mutex lock.
//
assert(router);
Ice::ConnectionPtr conn = router->ice_getCachedConnection();
string category = router->getCategoryForClient();
Ice::Long timeout = router->getSessionTimeout();
{
IceUtil::Mutex::Lock sync(_mutex);
_router = router;
if(_destroy)
{
//
// Run the destroyInternal in a thread. This is because it
// destroyInternal makes remote invocations.
//
IceUtil::ThreadPtr thread = new DestroyInternal(this, _callback);
thread->start();
return;
}
//
// Cache the category.
//
_category = category;
//
// Assign the session after _destroy is checked.
//
_session = session;
_connected = true;
assert(!_refreshThread);
if(timeout > 0)
{
_refreshThread = new SessionRefreshThread(this, _router, (timeout)/2);
_refreshThread->start();
}
}
dispatchCallback(new Connected(_callback, this), conn);
}
void
SessionHelperI::dispatchCallback(const Ice::DispatcherCallPtr& call, const Ice::ConnectionPtr& conn)
{
if(_initData.dispatcher)
{
_initData.dispatcher->dispatch(call, conn);
}
else
{
call->run();
}
}
namespace
{
class DispatcherCallWait : public Ice::DispatcherCall
{
public:
DispatcherCallWait(IceUtilInternal::CountDownLatch& cdl, const Ice::DispatcherCallPtr& call) :
_cdl(cdl),
_call(call)
{
}
virtual void
run()
{
_call->run();
_cdl.countDown();
}
private:
IceUtilInternal::CountDownLatch& _cdl;
const Ice::DispatcherCallPtr _call;
};
}
void
SessionHelperI::dispatchCallbackAndWait(const Ice::DispatcherCallPtr& call, const Ice::ConnectionPtr& conn)
{
if(_initData.dispatcher)
{
IceUtilInternal::CountDownLatch cdl(1);
Ice::DispatcherCallPtr callWait = new DispatcherCallWait(cdl, call);
_initData.dispatcher->dispatch(callWait, conn);
cdl.await();
}
else
{
call->run();
}
}
Glacier2::SessionFactoryHelper::SessionFactoryHelper(const SessionCallbackPtr& callback) :
_routerHost("localhost"),
_secure(true),
_port(0),
_timeout(10000),
_callback(callback)
{
_identity.name = "router";
_identity.category = "Glacier2";
_initData.properties = Ice::createProperties();
setDefaultProperties();
}
Glacier2::SessionFactoryHelper::SessionFactoryHelper(const Ice::InitializationData& initData,
const SessionCallbackPtr& callback) :
_routerHost("localhost"),
_secure(true),
_port(0),
_timeout(10000),
_initData(initData),
_callback(callback)
{
_identity.name = "router";
_identity.category = "Glacier2";
if(!initData.properties)
{
_initData.properties = Ice::createProperties();
}
setDefaultProperties();
}
Glacier2::SessionFactoryHelper::SessionFactoryHelper(const Ice::PropertiesPtr& properties, const SessionCallbackPtr& callback) :
_routerHost("localhost"),
_secure(true),
_port(0),
_timeout(10000),
_callback(callback)
{
if(!properties)
{
throw Ice::InitializationException(
__FILE__, __LINE__, "Attempt to create a SessionFactoryHelper with a null Properties argument");
}
_identity.name = "router";
_identity.category = "Glacier2";
_initData.properties = properties;
setDefaultProperties();
}
void
Glacier2::SessionFactoryHelper::setRouterIdentity(const Ice::Identity& identity)
{
IceUtil::Mutex::Lock sync(_mutex);
_identity = identity;
}
Ice::Identity
Glacier2::SessionFactoryHelper::getRouterIdentity() const
{
IceUtil::Mutex::Lock sync(_mutex);
return _identity;
}
void
Glacier2::SessionFactoryHelper::setRouterHost(const string& hostname)
{
IceUtil::Mutex::Lock sync(_mutex);
_routerHost = hostname;
}
string
Glacier2::SessionFactoryHelper::getRouterHost() const
{
IceUtil::Mutex::Lock sync(_mutex);
return _routerHost;
}
void
Glacier2::SessionFactoryHelper::setSecure(bool secure)
{
IceUtil::Mutex::Lock sync(_mutex);
_secure = secure;
}
bool
Glacier2::SessionFactoryHelper::getSecure() const
{
IceUtil::Mutex::Lock sync(_mutex);
return _secure;
}
void
Glacier2::SessionFactoryHelper::setTimeout(int timeout)
{
IceUtil::Mutex::Lock sync(_mutex);
_timeout = timeout;
}
int
Glacier2::SessionFactoryHelper::getTimeout() const
{
IceUtil::Mutex::Lock sync(_mutex);
return _timeout;
}
void
Glacier2::SessionFactoryHelper::setPort(int port)
{
IceUtil::Mutex::Lock sync(_mutex);
_port = port;
}
int
Glacier2::SessionFactoryHelper::getPort() const
{
IceUtil::Mutex::Lock sync(_mutex);
return _port;
}
Ice::InitializationData
Glacier2::SessionFactoryHelper::getInitializationData() const
{
IceUtil::Mutex::Lock sync(_mutex);
return _initData;
}
void
Glacier2::SessionFactoryHelper::setConnectContext(map<string, string> context)
{
IceUtil::Mutex::Lock sync(_mutex);
_context = context;
}
Glacier2::SessionHelperPtr
Glacier2::SessionFactoryHelper::connect()
{
IceUtil::Mutex::Lock sync(_mutex);
SessionHelperIPtr session = new SessionHelperI(_callback, createInitData());
session->connect(_context);
return session;
}
Glacier2::SessionHelperPtr
Glacier2::SessionFactoryHelper::connect(const string& user, const string& password)
{
IceUtil::Mutex::Lock sync(_mutex);
SessionHelperIPtr session = new SessionHelperI(_callback, createInitData());
session->connect(user, password, _context);
return session;
}
Ice::InitializationData
Glacier2::SessionFactoryHelper::createInitData()
{
//
// Clone the initialization data and properties.
//
Ice::InitializationData initData = _initData;
initData.properties = initData.properties->clone();
if(initData.properties->getProperty("Ice.Default.Router").size() == 0)
{
ostringstream os;
os << "\"";
//
// TODO replace with identityToString, we cannot use the Communicator::identityToString
// current implementation because we need to do that before the communicator has been
// initialized.
//
if(!_identity.category.empty())
{
os << _identity.category << "/";
}
os << _identity.name;
os << "\"";
os << ":";
if(_secure)
{
os << "ssl -p ";
}
else
{
os << "tcp -p ";
}
if(_port != 0)
{
os << _port;
}
else
{
if(_secure)
{
os << GLACIER2_SSL_PORT;
}
else
{
os << GLACIER2_TCP_PORT;
}
}
os << " -h ";
os << _routerHost;
if(_timeout > 0)
{
os << " -t ";
os << _timeout;
}
initData.properties->setProperty("Ice.Default.Router", os.str());
#ifndef ICE_OS_WINRT
if(_secure)
{
initData.properties->setProperty("Ice.Plugin.IceSSL","IceSSL:createIceSSL");
}
#endif
}
return initData;
}
void
Glacier2::SessionFactoryHelper::setDefaultProperties()
{
assert(_initData.properties);
_initData.properties->setProperty("Ice.ACM.Client", "0");
_initData.properties->setProperty("Ice.RetryIntervals", "-1");
}
|