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
|
// **********************************************************************
//
// Copyright (c) 2003-present ZeroC, Inc. All rights reserved.
//
// **********************************************************************
package IceInternal;
import java.util.concurrent.Callable;
public final class OutgoingConnectionFactory
{
//
// Helper class to multi hash map.
//
private static class MultiHashMap<K, V> extends java.util.HashMap<K, java.util.List<V>>
{
public void
putOne(K key, V value)
{
java.util.List<V> list = this.get(key);
if(list == null)
{
list = new java.util.LinkedList<V>();
this.put(key, list);
}
list.add(value);
}
public boolean
removeElementWithValue(K key, V value)
{
java.util.List<V> list = this.get(key);
assert(list != null);
boolean v = list.remove(value);
if(list.isEmpty())
{
this.remove(key);
}
return v;
}
}
interface CreateConnectionCallback
{
void setConnection(Ice.ConnectionI connection, boolean compress);
void setException(Ice.LocalException ex);
}
public synchronized void
destroy()
{
if(_destroyed)
{
return;
}
for(java.util.List<Ice.ConnectionI> connectionList : _connections.values())
{
for(Ice.ConnectionI connection : connectionList)
{
connection.destroy(Ice.ConnectionI.CommunicatorDestroyed);
}
}
_destroyed = true;
_communicator = null;
notifyAll();
}
public synchronized void
updateConnectionObservers()
{
for(java.util.List<Ice.ConnectionI> connectionList : _connections.values())
{
for(Ice.ConnectionI connection : connectionList)
{
connection.updateObserver();
}
}
}
// Called from Instance.destroy().
public void
waitUntilFinished()
{
java.util.Map<Connector, java.util.List<Ice.ConnectionI> > connections = null;
synchronized(this)
{
//
// First we wait until the factory is destroyed. We also
// wait until there are no pending connections
// anymore. Only then we can be sure the _connections
// contains all connections.
//
while(!_destroyed || !_pending.isEmpty() || _pendingConnectCount > 0)
{
try
{
wait();
}
catch(InterruptedException ex)
{
throw new Ice.OperationInterruptedException();
}
}
//
// We want to wait until all connections are finished outside the
// thread synchronization.
//
connections = new java.util.HashMap<Connector, java.util.List<Ice.ConnectionI> >(_connections);
}
//
// Now we wait until the destruction of each connection is finished.
//
for(java.util.List<Ice.ConnectionI> connectionList : connections.values())
{
for(Ice.ConnectionI connection : connectionList)
{
try
{
connection.waitUntilFinished();
}
catch(InterruptedException e)
{
//
// Force close all of the connections.
//
for(java.util.List<Ice.ConnectionI> l : connections.values())
{
for(Ice.ConnectionI c : l)
{
c.close(Ice.ConnectionClose.Forcefully);
}
}
throw new Ice.OperationInterruptedException();
}
}
}
synchronized(this)
{
// Ensure all the connections are finished and reapable at this point.
java.util.List<Ice.ConnectionI> cons = _monitor.swapReapedConnections();
if(cons != null)
{
int size = 0;
for(java.util.List<Ice.ConnectionI> connectionList : _connections.values())
{
size += connectionList.size();
}
assert(cons.size() == size);
_connections.clear();
_connectionsByEndpoint.clear();
}
else
{
assert(_connections.isEmpty());
assert(_connectionsByEndpoint.isEmpty());
}
}
//
// Must be destroyed outside the synchronization since this might block waiting for
// a timer task to complete.
//
_monitor.destroy();
}
public void
create(EndpointI[] endpts, boolean hasMore, Ice.EndpointSelectionType selType, CreateConnectionCallback callback)
{
assert(endpts.length > 0);
//
// Apply the overrides.
//
java.util.List<EndpointI> endpoints = applyOverrides(endpts);
//
// Try to find a connection to one of the given endpoints.
//
try
{
Ice.Holder<Boolean> compress = new Ice.Holder<Boolean>();
Ice.ConnectionI connection = findConnectionByEndpoint(endpoints, compress);
if(connection != null)
{
callback.setConnection(connection, compress.value);
return;
}
}
catch(Ice.LocalException ex)
{
callback.setException(ex);
return;
}
final ConnectCallback cb = new ConnectCallback(this, endpoints, hasMore, callback, selType);
//
// Calling cb.getConnectors() can eventually result in a call to connect() on a socket, which is not
// allowed while in Android's main thread (with a dispatcher installed).
//
if(_instance.queueRequests())
{
_instance.getQueueExecutor().executeNoThrow(new Callable<Void>()
{
@Override
public Void call()
throws Exception
{
cb.getConnectors();
return null;
}
});
}
else
{
cb.getConnectors();
}
}
public void
setRouterInfo(IceInternal.RouterInfo routerInfo)
{
assert(routerInfo != null);
Ice.ObjectAdapter adapter = routerInfo.getAdapter();
EndpointI[] endpoints = routerInfo.getClientEndpoints(); // Must be called outside the synchronization
synchronized(this)
{
if(_destroyed)
{
throw new Ice.CommunicatorDestroyedException();
}
//
// Search for connections to the router's client proxy
// endpoints, and update the object adapter for such
// connections, so that callbacks from the router can be
// received over such connections.
//
DefaultsAndOverrides defaultsAndOverrides = _instance.defaultsAndOverrides();
for(EndpointI endpoint : endpoints)
{
//
// Modify endpoints with overrides.
//
if(defaultsAndOverrides.overrideTimeout)
{
endpoint = endpoint.timeout(defaultsAndOverrides.overrideTimeoutValue);
}
//
// The Connection object does not take the compression flag of
// endpoints into account, but instead gets the information
// about whether messages should be compressed or not from
// other sources. In order to allow connection sharing for
// endpoints that differ in the value of the compression flag
// only, we always set the compression flag to false here in
// this connection factory.
//
endpoint = endpoint.compress(false);
for(java.util.List<Ice.ConnectionI> connectionList : _connections.values())
{
for(Ice.ConnectionI connection : connectionList)
{
if(connection.endpoint() == endpoint)
{
connection.setAdapter(adapter);
}
}
}
}
}
}
public synchronized void
removeAdapter(Ice.ObjectAdapter adapter)
{
if(_destroyed)
{
return;
}
for(java.util.List<Ice.ConnectionI> connectionList : _connections.values())
{
for(Ice.ConnectionI connection : connectionList)
{
if(connection.getAdapter() == adapter)
{
connection.setAdapter(null);
}
}
}
}
public void
flushAsyncBatchRequests(Ice.CompressBatch compressBatch, CommunicatorFlushBatch outAsync)
{
java.util.List<Ice.ConnectionI> c = new java.util.LinkedList<Ice.ConnectionI>();
synchronized(this)
{
if(!_destroyed)
{
for(java.util.List<Ice.ConnectionI> connectionList : _connections.values())
{
for(Ice.ConnectionI connection : connectionList)
{
if(connection.isActiveOrHolding())
{
c.add(connection);
}
}
}
}
}
for(Ice.ConnectionI conn : c)
{
try
{
outAsync.flushConnection(conn, compressBatch);
}
catch(Ice.LocalException ex)
{
// Ignore.
}
}
}
//
// Only for use by Instance.
//
OutgoingConnectionFactory(Ice.Communicator communicator, Instance instance)
{
_communicator = communicator;
_instance = instance;
_monitor = new FactoryACMMonitor(instance, instance.clientACM());
_destroyed = false;
}
@SuppressWarnings("deprecation")
@Override
protected synchronized void
finalize()
throws Throwable
{
try
{
IceUtilInternal.Assert.FinalizerAssert(_destroyed);
IceUtilInternal.Assert.FinalizerAssert(_connections.isEmpty());
IceUtilInternal.Assert.FinalizerAssert(_connectionsByEndpoint.isEmpty());
IceUtilInternal.Assert.FinalizerAssert(_pendingConnectCount == 0);
IceUtilInternal.Assert.FinalizerAssert(_pending.isEmpty());
}
catch(java.lang.Exception ex)
{
}
finally
{
super.finalize();
}
}
private java.util.List<EndpointI>
applyOverrides(EndpointI[] endpts)
{
DefaultsAndOverrides defaultsAndOverrides = _instance.defaultsAndOverrides();
java.util.List<EndpointI> endpoints = new java.util.ArrayList<EndpointI>();
for(EndpointI endpoint : endpts)
{
//
// Modify endpoints with overrides.
//
if(defaultsAndOverrides.overrideTimeout)
{
endpoints.add(endpoint.timeout(defaultsAndOverrides.overrideTimeoutValue));
}
else
{
endpoints.add(endpoint);
}
}
return endpoints;
}
synchronized private Ice.ConnectionI
findConnectionByEndpoint(java.util.List<EndpointI> endpoints, Ice.Holder<Boolean> compress)
{
if(_destroyed)
{
throw new Ice.CommunicatorDestroyedException();
}
DefaultsAndOverrides defaultsAndOverrides = _instance.defaultsAndOverrides();
assert(!endpoints.isEmpty());
for(EndpointI endpoint : endpoints)
{
java.util.List<Ice.ConnectionI> connectionList = _connectionsByEndpoint.get(endpoint);
if(connectionList == null)
{
continue;
}
for(Ice.ConnectionI connection : connectionList)
{
if(connection.isActiveOrHolding()) // Don't return destroyed or un-validated connections
{
if(defaultsAndOverrides.overrideCompress)
{
compress.value = defaultsAndOverrides.overrideCompressValue;
}
else
{
compress.value = endpoint.compress();
}
return connection;
}
}
}
return null;
}
//
// Must be called while synchronized.
//
private Ice.ConnectionI
findConnection(java.util.List<ConnectorInfo> connectors, Ice.Holder<Boolean> compress)
{
DefaultsAndOverrides defaultsAndOverrides = _instance.defaultsAndOverrides();
for(ConnectorInfo ci : connectors)
{
if(_pending.containsKey(ci.connector))
{
continue;
}
java.util.List<Ice.ConnectionI> connectionList = _connections.get(ci.connector);
if(connectionList == null)
{
continue;
}
for(Ice.ConnectionI connection : connectionList)
{
if(connection.isActiveOrHolding()) // Don't return destroyed or un-validated connections
{
if(defaultsAndOverrides.overrideCompress)
{
compress.value = defaultsAndOverrides.overrideCompressValue;
}
else
{
compress.value = ci.endpoint.compress();
}
return connection;
}
}
}
return null;
}
synchronized private void
incPendingConnectCount()
{
//
// Keep track of the number of pending connects. The outgoing connection factory
// waitUntilFinished() method waits for all the pending connects to terminate before
// to return. This ensures that the communicator client thread pool isn't destroyed
// too soon and will still be available to execute the ice_exception() callbacks for
// the asynchronous requests waiting on a connection to be established.
//
if(_destroyed)
{
throw new Ice.CommunicatorDestroyedException();
}
++_pendingConnectCount;
}
synchronized private void
decPendingConnectCount()
{
--_pendingConnectCount;
assert(_pendingConnectCount >= 0);
if(_destroyed && _pendingConnectCount == 0)
{
notifyAll();
}
}
private Ice.ConnectionI
getConnection(java.util.List<ConnectorInfo> connectors, ConnectCallback cb, Ice.Holder<Boolean> compress)
{
assert(cb != null);
synchronized(this)
{
if(_destroyed)
{
throw new Ice.CommunicatorDestroyedException();
}
//
// Reap closed connections
//
java.util.List<Ice.ConnectionI> cons = _monitor.swapReapedConnections();
if(cons != null)
{
for(Ice.ConnectionI c : cons)
{
_connections.removeElementWithValue(c.connector(), c);
_connectionsByEndpoint.removeElementWithValue(c.endpoint(), c);
_connectionsByEndpoint.removeElementWithValue(c.endpoint().compress(true), c);
}
}
//
// Try to get the connection. We may need to wait for other threads to
// finish if one of them is currently establishing a connection to one
// of our connectors.
//
while(true)
{
if(_destroyed)
{
throw new Ice.CommunicatorDestroyedException();
}
//
// Search for a matching connection. If we find one, we're done.
//
Ice.ConnectionI connection = findConnection(connectors, compress);
if(connection != null)
{
return connection;
}
if(addToPending(cb, connectors))
{
return null;
}
else
{
//
// If no thread is currently establishing a connection to one of our connectors,
// we get out of this loop and start the connection establishment to one of the
// given connectors.
//
break;
}
}
}
//
// At this point, we're responsible for establishing the connection to one of
// the given connectors. If it's a non-blocking connect, calling nextConnector
// will start the connection establishment. Otherwise, we return null to get
// the caller to establish the connection.
//
if(cb != null)
{
cb.nextConnector();
}
return null;
}
private synchronized Ice.ConnectionI
createConnection(Transceiver transceiver, ConnectorInfo ci)
{
assert(_pending.containsKey(ci.connector) && transceiver != null);
//
// Create and add the connection to the connection map. Adding the connection to the map
// is necessary to support the interruption of the connection initialization and validation
// in case the communicator is destroyed.
//
Ice.ConnectionI connection = null;
try
{
if(_destroyed)
{
throw new Ice.CommunicatorDestroyedException();
}
connection = new Ice.ConnectionI(_communicator, _instance, _monitor, transceiver, ci.connector,
ci.endpoint.compress(false), null);
}
catch(Ice.LocalException ex)
{
try
{
transceiver.close();
}
catch(Ice.LocalException exc)
{
// Ignore
}
throw ex;
}
_connections.putOne(ci.connector, connection);
_connectionsByEndpoint.putOne(connection.endpoint(), connection);
_connectionsByEndpoint.putOne(connection.endpoint().compress(true), connection);
return connection;
}
private void
finishGetConnection(java.util.List<ConnectorInfo> connectors,
ConnectorInfo ci,
Ice.ConnectionI connection,
ConnectCallback cb)
{
java.util.Set<ConnectCallback> connectionCallbacks = new java.util.HashSet<ConnectCallback>();
if(cb != null)
{
connectionCallbacks.add(cb);
}
java.util.Set<ConnectCallback> callbacks = new java.util.HashSet<ConnectCallback>();
synchronized(this)
{
for(ConnectorInfo c : connectors)
{
java.util.Set<ConnectCallback> cbs = _pending.remove(c.connector);
if(cbs != null)
{
for(ConnectCallback cc : cbs)
{
if(cc.hasConnector(ci))
{
connectionCallbacks.add(cc);
}
else
{
callbacks.add(cc);
}
}
}
}
for(ConnectCallback cc : connectionCallbacks)
{
cc.removeFromPending();
callbacks.remove(cc);
}
for(ConnectCallback cc : callbacks)
{
cc.removeFromPending();
}
notifyAll();
}
boolean compress;
DefaultsAndOverrides defaultsAndOverrides = _instance.defaultsAndOverrides();
if(defaultsAndOverrides.overrideCompress)
{
compress = defaultsAndOverrides.overrideCompressValue;
}
else
{
compress = ci.endpoint.compress();
}
for(ConnectCallback cc : callbacks)
{
cc.getConnection();
}
for(ConnectCallback cc : connectionCallbacks)
{
cc.setConnection(connection, compress);
}
}
private void
finishGetConnection(java.util.List<ConnectorInfo> connectors, Ice.LocalException ex, ConnectCallback cb)
{
java.util.Set<ConnectCallback> failedCallbacks = new java.util.HashSet<ConnectCallback>();
if(cb != null)
{
failedCallbacks.add(cb);
}
java.util.Set<ConnectCallback> callbacks = new java.util.HashSet<ConnectCallback>();
synchronized(this)
{
for(ConnectorInfo c : connectors)
{
java.util.Set<ConnectCallback> cbs = _pending.remove(c.connector);
if(cbs != null)
{
for(ConnectCallback cc : cbs)
{
if(cc.removeConnectors(connectors))
{
failedCallbacks.add(cc);
}
else
{
callbacks.add(cc);
}
}
}
}
for(ConnectCallback cc : callbacks)
{
assert(!failedCallbacks.contains(cc));
cc.removeFromPending();
}
notifyAll();
}
for(ConnectCallback cc : callbacks)
{
cc.getConnection();
}
for(ConnectCallback cc : failedCallbacks)
{
cc.setException(ex);
}
}
private boolean
addToPending(ConnectCallback cb, java.util.List<ConnectorInfo> connectors)
{
//
// Add the callback to each connector pending list.
//
boolean found = false;
for(ConnectorInfo p : connectors)
{
java.util.Set<ConnectCallback> cbs = _pending.get(p.connector);
if(cbs != null)
{
found = true;
if(cb != null)
{
cbs.add(cb); // Add the callback to each pending connector.
}
}
}
if(found)
{
return true;
}
//
// If there's no pending connection for the given connectors, we're
// responsible for its establishment. We add empty pending lists,
// other callbacks to the same connectors will be queued.
//
for(ConnectorInfo p : connectors)
{
if(!_pending.containsKey(p.connector))
{
_pending.put(p.connector, new java.util.HashSet<ConnectCallback>());
}
}
return false;
}
private void
removeFromPending(ConnectCallback cb, java.util.List<ConnectorInfo> connectors)
{
for(ConnectorInfo p : connectors)
{
java.util.Set<ConnectCallback> cbs = _pending.get(p.connector);
if(cbs != null)
{
cbs.remove(cb);
}
}
}
private void
handleConnectionException(Ice.LocalException ex, boolean hasMore)
{
TraceLevels traceLevels = _instance.traceLevels();
if(traceLevels.network >= 2)
{
StringBuilder s = new StringBuilder(128);
s.append("connection to endpoint failed");
if(ex instanceof Ice.CommunicatorDestroyedException)
{
s.append("\n");
}
else
{
if(hasMore)
{
s.append(", trying next endpoint\n");
}
else
{
s.append(" and no more endpoints to try\n");
}
}
s.append(ex.toString());
_instance.initializationData().logger.trace(traceLevels.networkCat, s.toString());
}
}
private void
handleException(Ice.LocalException ex, boolean hasMore)
{
TraceLevels traceLevels = _instance.traceLevels();
if(traceLevels.network >= 2)
{
StringBuilder s = new StringBuilder(128);
s.append("couldn't resolve endpoint host");
if(ex instanceof Ice.CommunicatorDestroyedException)
{
s.append("\n");
}
else
{
if(hasMore)
{
s.append(", trying next endpoint\n");
}
else
{
s.append(" and no more endpoints to try\n");
}
}
s.append(ex.toString());
_instance.initializationData().logger.trace(traceLevels.networkCat, s.toString());
}
}
private static class ConnectorInfo
{
public ConnectorInfo(Connector c, EndpointI e)
{
connector = c;
endpoint = e;
}
@Override
public boolean
equals(Object obj)
{
ConnectorInfo r = (ConnectorInfo)obj;
return connector.equals(r.connector);
}
@Override
public int
hashCode()
{
return connector.hashCode();
}
public Connector connector;
public EndpointI endpoint;
}
private static class ConnectCallback implements Ice.ConnectionI.StartCallback, EndpointI_connectors
{
ConnectCallback(OutgoingConnectionFactory f, java.util.List<EndpointI> endpoints, boolean more,
CreateConnectionCallback cb, Ice.EndpointSelectionType selType)
{
_factory = f;
_endpoints = endpoints;
_hasMore = more;
_callback = cb;
_selType = selType;
_endpointsIter = _endpoints.iterator();
}
//
// Methods from ConnectionI.StartCallback
//
@Override
public void
connectionStartCompleted(Ice.ConnectionI connection)
{
if(_observer != null)
{
_observer.detach();
}
connection.activate();
_factory.finishGetConnection(_connectors, _current, connection, this);
}
@Override
public void
connectionStartFailed(Ice.ConnectionI connection, Ice.LocalException ex)
{
assert(_current != null);
if(connectionStartFailedImpl(ex))
{
nextConnector();
}
}
//
// Methods from EndpointI_connectors
//
@Override
public void
connectors(java.util.List<Connector> cons)
{
for(Connector p : cons)
{
_connectors.add(new ConnectorInfo(p, _currentEndpoint));
}
if(_endpointsIter.hasNext())
{
nextEndpoint();
}
else
{
assert(!_connectors.isEmpty());
//
// We now have all the connectors for the given endpoints. We can try to obtain the
// connection.
//
_iter = _connectors.iterator();
getConnection();
}
}
@Override
public void
exception(Ice.LocalException ex)
{
_factory.handleException(ex, _hasMore || _endpointsIter.hasNext());
if(_endpointsIter.hasNext())
{
nextEndpoint();
}
else if(!_connectors.isEmpty())
{
//
// We now have all the connectors for the given endpoints. We can try to obtain the
// connection.
//
_iter = _connectors.iterator();
getConnection();
}
else
{
_callback.setException(ex);
_factory.decPendingConnectCount(); // Must be called last.
}
}
void
setConnection(Ice.ConnectionI connection, boolean compress)
{
//
// Callback from the factory: the connection to one of the callback
// connectors has been established.
//
_callback.setConnection(connection, compress);
_factory.decPendingConnectCount(); // Must be called last.
}
void
setException(Ice.LocalException ex)
{
//
// Callback from the factory: connection establishment failed.
//
_callback.setException(ex);
_factory.decPendingConnectCount(); // Must be called last.
}
boolean
hasConnector(ConnectorInfo ci)
{
return _connectors.contains(ci);
}
boolean
removeConnectors(java.util.List<ConnectorInfo> connectors)
{
_connectors.removeAll(connectors);
_iter = _connectors.iterator();
return _connectors.isEmpty();
}
void
removeFromPending()
{
_factory.removeFromPending(this, _connectors);
}
private void
getConnectors()
{
try
{
//
// Notify the factory that there's an async connect pending. This is necessary
// to prevent the outgoing connection factory to be destroyed before all the
// pending asynchronous connects are finished.
//
_factory.incPendingConnectCount();
}
catch(Ice.LocalException ex)
{
_callback.setException(ex);
return;
}
nextEndpoint();
}
private void
nextEndpoint()
{
try
{
assert(_endpointsIter.hasNext());
_currentEndpoint = _endpointsIter.next();
_currentEndpoint.connectors_async(_selType, this);
}
catch(Ice.LocalException ex)
{
exception(ex);
}
}
private void
getConnection()
{
try
{
//
// If all the connectors have been created, we ask the factory to get a
// connection.
//
Ice.Holder<Boolean> compress = new Ice.Holder<Boolean>();
Ice.ConnectionI connection = _factory.getConnection(_connectors, this, compress);
if(connection == null)
{
//
// A null return value from getConnection indicates that the connection
// is being established and that everything has been done to ensure that
// the callback will be notified when the connection establishment is
// done.
//
return;
}
_callback.setConnection(connection, compress.value);
_factory.decPendingConnectCount(); // Must be called last.
}
catch(Ice.LocalException ex)
{
_callback.setException(ex);
_factory.decPendingConnectCount(); // Must be called last.
}
}
private void
nextConnector()
{
while(true)
{
try
{
assert(_iter.hasNext());
_current = _iter.next();
Ice.Instrumentation.CommunicatorObserver obsv = _factory._instance.initializationData().observer;
if(obsv != null)
{
_observer = obsv.getConnectionEstablishmentObserver(_current.endpoint,
_current.connector.toString());
if(_observer != null)
{
_observer.attach();
}
}
if(_factory._instance.traceLevels().network >= 2)
{
StringBuffer s = new StringBuffer("trying to establish ");
s.append(_current.endpoint.protocol());
s.append(" connection to ");
s.append(_current.connector.toString());
_factory._instance.initializationData().logger.trace(_factory._instance.traceLevels().networkCat,
s.toString());
}
Ice.ConnectionI connection = _factory.createConnection(_current.connector.connect(), _current);
connection.start(this);
}
catch(Ice.LocalException ex)
{
if(_factory._instance.traceLevels().network >= 2)
{
StringBuffer s = new StringBuffer("failed to establish ");
s.append(_current.endpoint.protocol());
s.append(" connection to ");
s.append(_current.connector.toString());
s.append("\n");
s.append(ex);
_factory._instance.initializationData().logger.trace(_factory._instance.traceLevels().networkCat,
s.toString());
}
if(connectionStartFailedImpl(ex))
{
continue;
}
}
break;
}
}
private boolean
connectionStartFailedImpl(Ice.LocalException ex)
{
if(_observer != null)
{
_observer.failed(ex.ice_id());
_observer.detach();
}
_factory.handleConnectionException(ex, _hasMore || _iter.hasNext());
if(ex instanceof Ice.CommunicatorDestroyedException) // No need to continue.
{
_factory.finishGetConnection(_connectors, ex, this);
}
else if(_iter.hasNext()) // Try the next connector.
{
return true;
}
else
{
_factory.finishGetConnection(_connectors, ex, this);
}
return false;
}
private final OutgoingConnectionFactory _factory;
private final boolean _hasMore;
private final CreateConnectionCallback _callback;
private final java.util.List<EndpointI> _endpoints;
private final Ice.EndpointSelectionType _selType;
private java.util.Iterator<EndpointI> _endpointsIter;
private EndpointI _currentEndpoint;
private java.util.List<ConnectorInfo> _connectors = new java.util.ArrayList<ConnectorInfo>();
private java.util.Iterator<ConnectorInfo> _iter;
private ConnectorInfo _current;
private Ice.Instrumentation.Observer _observer;
}
private Ice.Communicator _communicator;
private final Instance _instance;
private final FactoryACMMonitor _monitor;
private boolean _destroyed;
private MultiHashMap<Connector, Ice.ConnectionI> _connections = new MultiHashMap<Connector, Ice.ConnectionI>();
private MultiHashMap<EndpointI, Ice.ConnectionI> _connectionsByEndpoint =
new MultiHashMap<EndpointI, Ice.ConnectionI>();
private java.util.Map<Connector, java.util.HashSet<ConnectCallback> > _pending =
new java.util.HashMap<Connector, java.util.HashSet<ConnectCallback> >();
private int _pendingConnectCount = 0;
}
|