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
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
|
// **********************************************************************
//
// Copyright (c) 2003-2018 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/SessionHelper.h>
#include <IceUtil/IceUtil.h>
#include <IceUtil/CountDownLatch.h>
#include <Ice/Ice.h>
#include <algorithm> // required by max
using namespace std;
namespace Glacier2
{
class SessionThreadCallback :
#ifdef ICE_CPP11_MAPPING
public std::enable_shared_from_this<SessionThreadCallback>
#else
public virtual IceUtil::Shared
#endif
{
public:
SessionThreadCallback(const Glacier2::SessionFactoryHelperPtr& factory) :
_factory(factory)
{
}
IceUtil::ThreadPtr add(const SessionHelper* session, const IceUtil::ThreadPtr& thread)
{
return _factory->addThread(session, thread);
}
private:
const SessionFactoryHelperPtr _factory;
};
ICE_DEFINE_PTR(SessionThreadCallbackPtr, SessionThreadCallback);
};
namespace
{
class ConnectStrategy
#ifndef ICE_CPP11_MAPPING
: public Ice::LocalObject
#endif
{
public:
virtual Glacier2::SessionPrxPtr connect(const Glacier2::RouterPrxPtr& router) = 0;
};
ICE_DEFINE_PTR(ConnectStrategyPtr, ConnectStrategy);
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 SessionHelperI : public Glacier2::SessionHelper
#ifdef ICE_CPP11_MAPPING
, public std::enable_shared_from_this<SessionHelperI>
#endif
{
public:
SessionHelperI(const Glacier2::SessionThreadCallbackPtr&, const Glacier2::SessionCallbackPtr&,
const Ice::InitializationData&, const string&, bool);
void destroy();
Ice::CommunicatorPtr communicator() const;
std::string categoryForClient() const;
Ice::ObjectPrxPtr addWithUUID(const Ice::ObjectPtr&);
Glacier2::SessionPrxPtr session() const;
bool isConnected() const;
Ice::ObjectAdapterPtr objectAdapter();
friend class DestroyInternal;
friend class DestroyCommunicator;
friend class ConnectThread;
friend class DispatchCallThread;
friend class Glacier2::SessionFactoryHelper;
private:
Ice::ObjectAdapterPtr internalObjectAdapter();
void connected(const Glacier2::RouterPrxPtr&, const Glacier2::SessionPrxPtr&);
void destroyInternal(const Ice::DispatcherCallPtr&);
void destroyCommunicator();
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::RouterPrxPtr _router;
Glacier2::SessionPrxPtr _session;
std::string _category;
bool _connected;
bool _destroy;
const Ice::InitializationData _initData;
Glacier2::SessionThreadCallbackPtr _threadCB;
const Glacier2::SessionCallbackPtr _callback;
const string _finder;
const bool _useCallbacks;
};
ICE_DEFINE_PTR(SessionHelperIPtr, SessionHelperI);
class DestroyInternal : public IceUtil::Thread
{
public:
DestroyInternal(const SessionHelperIPtr& session,
const Glacier2::SessionCallbackPtr& callback,
const Glacier2::SessionThreadCallbackPtr& threadCB) :
_session(session), _disconnected(new Disconnected(session, callback))
{
_previous = threadCB->add(session.get(), this);
}
virtual void run()
{
_session->destroyInternal(_disconnected);
_session = ICE_NULLPTR;
//
// Join the connect thread to free resources.
//
if(_previous)
{
_previous->getThreadControl().join();
}
}
private:
SessionHelperIPtr _session;
IceUtil::ThreadPtr _previous;
const Ice::DispatcherCallPtr _disconnected;
};
class DestroyCommunicator : public IceUtil::Thread
{
public:
DestroyCommunicator(const SessionHelperIPtr& session, const Glacier2::SessionThreadCallbackPtr& threadCB) :
_session(session)
{
_previous = threadCB->add(session.get(), this);
}
virtual void run()
{
_session->destroyCommunicator();
_session = ICE_NULLPTR;
//
// Join the connect thread to free resources.
//
if(_previous)
{
_previous->getThreadControl().join();
}
}
private:
SessionHelperIPtr _session;
IceUtil::ThreadPtr _previous;
};
}
SessionHelperI::SessionHelperI(const Glacier2::SessionThreadCallbackPtr& threadCB,
const Glacier2::SessionCallbackPtr& callback,
const Ice::InitializationData& initData,
const string& finderStr,
bool useCallbacks) :
_connected(false),
_destroy(false),
_initData(initData),
_threadCB(threadCB),
_callback(callback),
_finder(finderStr),
_useCallbacks(useCallbacks)
{
}
void
SessionHelperI::destroy()
{
IceUtil::Mutex::Lock sync(_mutex);
if(_destroy)
{
return;
}
_destroy = true;
IceUtil::ThreadPtr destroyThread;
if(!_connected)
{
//
// In this case a connecting session is being destroyed.
// We destroy the communicator to trigger the immediate
// failure of the connection establishment.
//
destroyThread = new DestroyCommunicator(ICE_SHARED_FROM_THIS, _threadCB);
}
else
{
destroyThread = new DestroyInternal(ICE_SHARED_FROM_THIS, _callback, _threadCB);
_connected = false;
_session = ICE_NULLPTR;
}
_threadCB = ICE_NULLPTR;
//
// Run destroy in a thread because it can block.
//
destroyThread->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::ObjectPrxPtr
SessionHelperI::addWithUUID(const Ice::ObjectPtr& servant)
{
IceUtil::Mutex::Lock sync(_mutex);
if(!_router)
{
throw Glacier2::SessionNotExistException();
}
Ice::Identity id;
id.name = Ice::generateUUID();
id.category = _category;
return internalObjectAdapter()->add(servant, id);
}
Glacier2::SessionPrxPtr
SessionHelperI::session() const
{
IceUtil::Mutex::Lock sync(_mutex);
return _session;
}
bool
SessionHelperI::isConnected() const
{
IceUtil::Mutex::Lock sync(_mutex);
return _connected;
}
Ice::ObjectAdapterPtr
SessionHelperI::objectAdapter()
{
IceUtil::Mutex::Lock sync(_mutex);
return internalObjectAdapter();
}
Glacier2::SessionHelper::~SessionHelper()
{
// Out of line to avoid weak vtable
}
#ifndef ICE_CPP11_MAPPING
bool
Glacier2::SessionHelper::operator==(const Glacier2::SessionHelper& other) const
{
return this == &other;
}
bool
Glacier2::SessionHelper::operator!=(const Glacier2::SessionHelper& other) const
{
return this != &other;
}
#endif
Ice::ObjectAdapterPtr
SessionHelperI::internalObjectAdapter()
{
if(!_router)
{
throw Glacier2::SessionNotExistException();
}
if(!_useCallbacks)
{
throw Ice::InitializationException(__FILE__, __LINE__,
"Object adapter not available, call SessionFactoryHelper.setUseCallbacks(true)");
}
return _adapter;
}
Glacier2::SessionCallback::~SessionCallback()
{
// Out of line to avoid weak vtable
}
namespace
{
class ConnectStrategySecureConnection ICE_FINAL : public ConnectStrategy
{
public:
ConnectStrategySecureConnection(const map<string, string>& context) :
_context(context)
{
}
virtual Glacier2::SessionPrxPtr
connect(const Glacier2::RouterPrxPtr& router)
{
return router->createSessionFromSecureConnection(_context);
}
private:
const map<string, string> _context;
};
class ConnectStrategyUserPassword ICE_FINAL : public ConnectStrategy
{
public:
ConnectStrategyUserPassword(const string& user, const string& password, const map<string, string>& context) :
_user(user),
_password(password),
_context(context)
{
}
virtual Glacier2::SessionPrxPtr
connect(const Glacier2::RouterPrxPtr& 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(ICE_MAKE_SHARED(ConnectStrategySecureConnection, context));
}
void
SessionHelperI::connect(const string& user, const string& password, const map<string, string>& context)
{
IceUtil::Mutex::Lock sync(_mutex);
connectImpl(ICE_MAKE_SHARED(ConnectStrategyUserPassword, user, password, context));
}
void
SessionHelperI::destroyInternal(const Ice::DispatcherCallPtr& disconnected)
{
assert(_destroy);
Ice::CommunicatorPtr communicator;
Glacier2::RouterPrxPtr router;
{
IceUtil::Mutex::Lock sync(_mutex);
router = _router;
_router = ICE_NULLPTR;
_connected = false;
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 std::exception& ex)
{
//
// Not expected.
//
if(communicator)
{
Ice::Warning warn(communicator->getLogger());
warn << "SessionHelper: unexpected exception when destroying the session:\n" << ex;
}
}
}
if(communicator)
{
communicator->destroy();
}
dispatchCallback(disconnected, ICE_NULLPTR);
}
void
SessionHelperI::destroyCommunicator()
{
Ice::CommunicatorPtr communicator;
{
IceUtil::Mutex::Lock sync(_mutex);
communicator = _communicator;
}
if(communicator)
{
communicator->destroy();
}
}
void
SessionHelperI::connectFailed()
{
Ice::CommunicatorPtr communicator;
{
IceUtil::Mutex::Lock sync(_mutex);
communicator = _communicator;
}
if(communicator)
{
communicator->destroy();
}
}
namespace
{
class ConnectFailed : public Ice::DispatcherCall
{
public:
ConnectFailed(const Glacier2::SessionCallbackPtr& callback, const Glacier2::SessionHelperPtr& session,
const Ice::Exception& ex) :
_callback(callback),
_session(session)
{
ICE_SET_EXCEPTION_FROM_CLONE(_ex, 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;
IceInternal::UniquePtr<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 string& finder) :
_callback(callback),
_session(session),
_factory(factory),
_finder(finder)
{
}
virtual void
run()
{
Ice::CommunicatorPtr communicator;
try
{
IceUtil::Mutex::Lock sync(_session->_mutex);
communicator = Ice::initialize(_session->_initData);
_session->_communicator = communicator;
}
catch(const Ice::LocalException& ex)
{
{
IceUtil::Mutex::Lock sync(_session->_mutex);
_session->_destroy = true;
}
_session->dispatchCallback(new ConnectFailed(_callback, _session, ex), ICE_NULLPTR);
return;
}
try
{
if(!communicator->getDefaultRouter())
{
Ice::RouterFinderPrxPtr finder =
ICE_UNCHECKED_CAST(Ice::RouterFinderPrx, communicator->stringToProxy(_finder));
try
{
communicator->setDefaultRouter(finder->getRouter());
}
catch(const Ice::CommunicatorDestroyedException& ex)
{
_session->dispatchCallback(new ConnectFailed(_callback, _session, ex), 0);
return;
}
catch(const Ice::Exception&)
{
//
// In case of error getting router identity from RouterFinder use
// default identity.
//
Ice::Identity ident;
ident.category = "Glacier2";
ident.name = "router";
communicator->setDefaultRouter(ICE_UNCHECKED_CAST(Ice::RouterPrx, finder->ice_identity(ident)));
}
}
_session->dispatchCallbackAndWait(new CreatedCommunicator(_callback, _session), 0);
Glacier2::RouterPrxPtr routerPrx = ICE_UNCHECKED_CAST(Glacier2::RouterPrx, communicator->getDefaultRouter());
Glacier2::SessionPrxPtr 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);
}
_session = 0;
}
private:
const Glacier2::SessionCallbackPtr _callback;
SessionHelperIPtr _session;
const ConnectStrategyPtr _factory;
const string _finder;
};
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);
_session = ICE_NULLPTR;
}
private:
SessionHelperIPtr _session;
const Ice::DispatcherCallPtr _call;
const Ice::ConnectionPtr _conn;
};
}
void
SessionHelperI::connectImpl(const ConnectStrategyPtr& factory)
{
assert(!_destroy);
IceUtil::ThreadPtr thread = new ConnectThread(_callback, ICE_SHARED_FROM_THIS, factory, _finder);
_threadCB->add(this, thread);
thread->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;
};
#ifndef ICE_CPP11_MAPPING // C++98
class CloseCallbackI : public Ice::CloseCallback
{
public:
CloseCallbackI(const SessionHelperIPtr& sessionHelper) : _sessionHelper(sessionHelper)
{
}
virtual void
closed(const Ice::ConnectionPtr&)
{
_sessionHelper->destroy();
}
private:
SessionHelperIPtr _sessionHelper;
};
#endif
}
void
SessionHelperI::connected(const Glacier2::RouterPrxPtr& router, const Glacier2::SessionPrxPtr& session)
{
//
// Remote invocation should be done without acquiring a mutex lock.
//
assert(router);
Ice::ConnectionPtr conn = router->ice_getCachedConnection();
string category = router->getCategoryForClient();
Ice::Int acmTimeout = 0;
try
{
acmTimeout = router->getACMTimeout();
}
catch(const Ice::OperationNotExistException&)
{
}
if(acmTimeout <= 0)
{
acmTimeout = static_cast<Ice::Int>(router->getSessionTimeout());
}
//
// We create the callback object adapter here because createObjectAdapter internally
// makes synchronous RPCs to the router. We can't create the OA on-demand when the
// client calls objectAdapter() or addWithUUID() because they can be called from the
// GUI thread.
//
if(_useCallbacks)
{
_adapter = _communicator->createObjectAdapterWithRouter("", router);
_adapter->activate();
}
bool destroy;
{
IceUtil::Mutex::Lock sync(_mutex);
_router = router;
destroy = _destroy;
if(!_destroy)
{
//
// Cache the category.
//
_category = category;
//
// Assign the session after _destroy is checked.
//
_session = session;
_connected = true;
if(acmTimeout > 0)
{
Ice::ConnectionPtr connection = _router->ice_getCachedConnection();
assert(connection);
connection->setACM(acmTimeout, IceUtil::None, Ice::ICE_ENUM(ACMHeartbeat, HeartbeatAlways));
#ifdef ICE_CPP11_MAPPING
auto self = shared_from_this();
connection->setCloseCallback([self](Ice::ConnectionPtr)
{
self->destroy();
});
#else
connection->setCloseCallback(ICE_MAKE_SHARED(CloseCallbackI, this));
#endif
}
}
}
if(destroy)
{
//
// connected() is only called from the ConnectThread so it is ok to
// call destroyInternal here.
//
destroyInternal(new Disconnected(ICE_SHARED_FROM_THIS, _callback));
}
else
{
dispatchCallback(new Connected(_callback, ICE_SHARED_FROM_THIS), conn);
}
}
void
SessionHelperI::dispatchCallback(const Ice::DispatcherCallPtr& call, const Ice::ConnectionPtr& conn)
{
if(_initData.dispatcher)
{
#ifdef ICE_CPP11_MAPPING
_initData.dispatcher([call]()
{
call->run();
},
conn);
#else
_initData.dispatcher->dispatch(call, conn);
#endif
}
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);
#ifdef ICE_CPP11_MAPPING
_initData.dispatcher([callWait]()
{
callWait->run();
},
conn);
#else
_initData.dispatcher->dispatch(callWait, conn);
#endif
cdl.await();
}
else
{
call->run();
}
}
Glacier2::SessionFactoryHelper::SessionFactoryHelper(const SessionCallbackPtr& callback) :
_routerHost("localhost"),
_protocol("ssl"),
_port(0),
_timeout(10000),
_callback(callback),
_useCallbacks(true)
{
_initData.properties = Ice::createProperties();
setDefaultProperties();
}
Glacier2::SessionFactoryHelper::SessionFactoryHelper(const Ice::InitializationData& initData,
const SessionCallbackPtr& callback) :
_routerHost("localhost"),
_protocol("ssl"),
_port(0),
_timeout(10000),
_initData(initData),
_callback(callback),
_useCallbacks(true)
{
if(!initData.properties)
{
_initData.properties = Ice::createProperties();
}
setDefaultProperties();
}
Glacier2::SessionFactoryHelper::SessionFactoryHelper(const Ice::PropertiesPtr& properties,
const SessionCallbackPtr& callback) :
_routerHost("localhost"),
_protocol("ssl"),
_port(0),
_timeout(10000),
_callback(callback),
_useCallbacks(true)
{
if(!properties)
{
throw Ice::InitializationException(
__FILE__, __LINE__, "Attempt to create a SessionFactoryHelper with a null Properties argument");
}
_initData.properties = properties;
setDefaultProperties();
}
Glacier2::SessionFactoryHelper::~SessionFactoryHelper()
{
IceUtil::Mutex::Lock sync(_mutex);
if(!_threads.empty() && Ice::getProcessLogger())
{
Ice::Warning warn(Ice::getProcessLogger());
warn << "Glacier2::SessionFactoryHelper::destroy() has not been called, threads won't be joined";
}
}
IceUtil::ThreadPtr
Glacier2::SessionFactoryHelper::addThread(const SessionHelper* session, const IceUtil::ThreadPtr& thread)
{
//
// A SessionHelper can only ever have one thread running. Therefore any
// currently registered thread for the same session must be finished, so
// we just replace it. Caller must join returned thread.
//
IceUtil::Mutex::Lock sync(_mutex);
IceUtil::ThreadPtr previous;
map<const SessionHelper*, IceUtil::ThreadPtr>::iterator p = _threads.find(session);
if(p != _threads.end())
{
previous = p->second;
p->second = thread;
}
else
{
_threads.insert(make_pair(session, thread));
}
return previous;
}
void
Glacier2::SessionFactoryHelper::destroy()
{
IceUtil::Mutex::Lock sync(_mutex);
for(map<const SessionHelper*, IceUtil::ThreadPtr>::iterator p = _threads.begin(); p != _threads.end(); ++p)
{
p->second->getThreadControl().join();
}
_threads.clear();
}
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)
{
setProtocol(secure ? "ssl" : "tcp");
}
bool
Glacier2::SessionFactoryHelper::getSecure() const
{
return getProtocol() == "ssl";
}
void
Glacier2::SessionFactoryHelper::setProtocol(const string& protocol)
{
IceUtil::Mutex::Lock sync(_mutex);
if(protocol != "tcp" &&
protocol != "ssl" &&
protocol != "ws" &&
protocol != "wss")
{
#ifdef ICE_CPP11_MAPPING
throw invalid_argument("Unknown protocol `" + protocol + "'");
#else
throw IceUtil::IllegalArgumentException(__FILE__, __LINE__, "Unknown protocol `" + protocol + "'");
#endif
}
_protocol = protocol;
}
string
Glacier2::SessionFactoryHelper::getProtocol() const
{
IceUtil::Mutex::Lock sync(_mutex);
return _protocol;
}
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 getPortInternal();
}
int
Glacier2::SessionFactoryHelper::getPortInternal() const
{
// Must be called with the muext lock
return _port == 0 ? ((_protocol == "ssl" || _protocol == "wss") ? GLACIER2_SSL_PORT : GLACIER2_TCP_PORT) : _port;
}
Ice::InitializationData
Glacier2::SessionFactoryHelper::getInitializationData() const
{
IceUtil::Mutex::Lock sync(_mutex);
return _initData;
}
void
Glacier2::SessionFactoryHelper::setConnectContext(const map<string, string>& context)
{
IceUtil::Mutex::Lock sync(_mutex);
_context = context;
}
void
Glacier2::SessionFactoryHelper::setUseCallbacks(bool useCallbacks)
{
IceUtil::Mutex::Lock sync(_mutex);
_useCallbacks = useCallbacks;
}
bool
Glacier2::SessionFactoryHelper::getUseCallbacks() const
{
IceUtil::Mutex::Lock sync(_mutex);
return _useCallbacks;
}
Glacier2::SessionHelperPtr
Glacier2::SessionFactoryHelper::connect()
{
SessionHelperIPtr session;
map<string, string> context;
{
IceUtil::Mutex::Lock sync(_mutex);
session = ICE_MAKE_SHARED(SessionHelperI,
ICE_MAKE_SHARED(SessionThreadCallback, ICE_SHARED_FROM_THIS),
_callback,
createInitData(),
getRouterFinderStr(),
_useCallbacks);
context = _context;
}
session->connect(context);
return session;
}
Glacier2::SessionHelperPtr
Glacier2::SessionFactoryHelper::connect(const string& user, const string& password)
{
SessionHelperIPtr session;
map<string, string> context;
{
IceUtil::Mutex::Lock sync(_mutex);
session = ICE_MAKE_SHARED(SessionHelperI,
ICE_MAKE_SHARED(SessionThreadCallback, ICE_SHARED_FROM_THIS),
_callback,
createInitData(),
getRouterFinderStr(),
_useCallbacks);
context = _context;
}
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 && !_identity.name.empty())
{
initData.properties->setProperty("Ice.Default.Router", createProxyStr(_identity));
}
#ifndef ICE_OS_UWP
//
// If using a secure connection setup the IceSSL plug-in, if IceSSL
// plug-in has already been setup we don't want to override the
// configuration so it can be loaded from a custom location.
//
if((_protocol == "ssl" || _protocol == "wss") &&
initData.properties->getProperty("Ice.Plugin.IceSSL").empty())
{
initData.properties->setProperty("Ice.Plugin.IceSSL","IceSSL:createIceSSL");
}
#endif
return initData;
}
string
Glacier2::SessionFactoryHelper::getRouterFinderStr()
{
Ice::Identity ident;
ident.category = "Ice";
ident.name = "RouterFinder";
return createProxyStr(ident);
}
string
Glacier2::SessionFactoryHelper::createProxyStr(const Ice::Identity& ident)
{
ostringstream os;
os << "\"" << identityToString(ident, Ice::ICE_ENUM(ToStringMode, Unicode)) << "\":" << _protocol
<< " -p " << getPortInternal() << " -h \"" << _routerHost << "\"";
if(_timeout > 0)
{
os << " -t " << _timeout;
}
return os.str();
}
void
Glacier2::SessionFactoryHelper::setDefaultProperties()
{
assert(_initData.properties);
_initData.properties->setProperty("Ice.RetryIntervals", "-1");
}
|