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
|
// **********************************************************************
//
// Copyright (c) 2003-2005 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.
//
// **********************************************************************
package IceInternal;
public class IncomingConnectionFactory extends EventHandler
{
public synchronized void
activate()
{
setState(StateActive);
}
public synchronized void
hold()
{
setState(StateHolding);
}
public synchronized void
destroy()
{
setState(StateClosed);
}
public void
waitUntilHolding()
{
java.util.LinkedList connections;
synchronized(this)
{
//
// First we wait until the connection factory itself is in holding
// state.
//
while(_state < StateHolding)
{
try
{
wait();
}
catch(InterruptedException ex)
{
}
}
//
// We want to wait until all connections are in holding state
// outside the thread synchronization.
//
connections = (java.util.LinkedList)_connections.clone();
}
//
// Now we wait until each connection is in holding state.
//
java.util.ListIterator p = connections.listIterator();
while(p.hasNext())
{
Ice.ConnectionI connection = (Ice.ConnectionI)p.next();
connection.waitUntilHolding();
}
}
public void
waitUntilFinished()
{
Thread threadPerIncomingConnectionFactory = null;
java.util.LinkedList connections;
synchronized(this)
{
//
// First we wait until the factory is destroyed. If we are using
// an acceptor, we also wait for it to be closed.
//
while(_state != StateClosed || _acceptor != null)
{
try
{
wait();
}
catch(InterruptedException ex)
{
}
}
threadPerIncomingConnectionFactory = _threadPerIncomingConnectionFactory;
_threadPerIncomingConnectionFactory = null;
//
// We want to wait until all connections are finished outside the
// thread synchronization.
//
connections = _connections;
_connections = new java.util.LinkedList();
}
if(threadPerIncomingConnectionFactory != null &&
threadPerIncomingConnectionFactory != Thread.currentThread())
{
try
{
threadPerIncomingConnectionFactory.join();
}
catch(InterruptedException ex)
{
}
}
java.util.ListIterator p = connections.listIterator();
while(p.hasNext())
{
Ice.ConnectionI connection = (Ice.ConnectionI)p.next();
connection.waitUntilFinished();
}
}
public Endpoint
endpoint()
{
// No mutex protection necessary, _endpoint is immutable.
return _endpoint;
}
public boolean
equivalent(Endpoint endp)
{
if(_transceiver != null)
{
return endp.equivalent(_transceiver);
}
assert(_acceptor != null);
return endp.equivalent(_acceptor);
}
public synchronized Ice.ConnectionI[]
connections()
{
java.util.LinkedList connections = new java.util.LinkedList();
//
// Only copy connections which have not been destroyed.
//
java.util.ListIterator p = _connections.listIterator();
while(p.hasNext())
{
Ice.ConnectionI connection = (Ice.ConnectionI)p.next();
if(!connection.isDestroyed())
{
connections.add(connection);
}
}
Ice.ConnectionI[] arr = new Ice.ConnectionI[connections.size()];
connections.toArray(arr);
return arr;
}
public void
flushBatchRequests()
{
Ice.ConnectionI[] c = connections(); // connections() is synchronized, so no need to synchronize here.
for(int i = 0; i < c.length; i++)
{
try
{
c[i].flushBatchRequests();
}
catch(Ice.LocalException ex)
{
// Ignore.
}
}
}
//
// Operations from EventHandler.
//
public boolean
datagram()
{
assert(!_instance.threadPerConnection()); // Only for use with a thread pool.
return _endpoint.datagram();
}
public boolean
readable()
{
assert(!_instance.threadPerConnection()); // Only for use with a thread pool.
return false;
}
public void
read(BasicStream unused)
{
assert(!_instance.threadPerConnection()); // Only for use with a thread pool.
assert(false); // Must not be called.
}
public void
message(BasicStream unused, ThreadPool threadPool)
{
assert(!_instance.threadPerConnection()); // Only for use with a thread pool.
Ice.ConnectionI connection = null;
synchronized(this)
{
try
{
if(_state != StateActive)
{
Thread.yield();
return;
}
//
// Reap connections for which destruction has completed.
//
java.util.ListIterator p = _connections.listIterator();
while(p.hasNext())
{
Ice.ConnectionI con = (Ice.ConnectionI)p.next();
if(con.isFinished())
{
p.remove();
}
}
//
// Now accept a new connection.
//
Transceiver transceiver;
try
{
transceiver = _acceptor.accept(0);
}
catch(Ice.TimeoutException ex)
{
// Ignore timeouts.
return;
}
catch(Ice.LocalException ex)
{
// Warn about other Ice local exceptions.
if(_warn)
{
warning(ex);
}
return;
}
assert(transceiver != null);
//
// Create a connection object for the connection.
//
connection = new Ice.ConnectionI(_instance, transceiver, _endpoint, _adapter);
_connections.add(connection);
}
finally
{
//
// This makes sure that we promote a follower before we leave
// the scope of the mutex above, but after we call accept()
// (if we call it).
//
threadPool.promoteFollower();
}
}
assert(connection != null);
//
// We validate and activate outside the thread synchronization, to not block
// the factory.
//
try
{
connection.validate();
}
catch(Ice.LocalException ex)
{
//
// Ignore all exceptions while validating the
// connection. Warning or error messages for such
// exceptions are printed directly by the validation code.
//
}
connection.activate();
}
public synchronized void
finished(ThreadPool threadPool)
{
assert(!_instance.threadPerConnection()); // Only for use with a thread pool.
threadPool.promoteFollower();
if(_state == StateActive)
{
registerWithPool();
}
else if(_state == StateClosed)
{
_acceptor.close();
_acceptor = null;
notifyAll();
}
}
public void
exception(Ice.LocalException ex)
{
assert(false); // Must not be called.
}
public synchronized String
toString()
{
if(_transceiver != null)
{
return _transceiver.toString();
}
assert(_acceptor != null);
return _acceptor.toString();
}
public
IncomingConnectionFactory(Instance instance, Endpoint endpoint, Ice.ObjectAdapter adapter)
{
super(instance);
_endpoint = endpoint;
_adapter = adapter;
_registeredWithPool = false;
_warn = _instance.properties().getPropertyAsInt("Ice.Warn.Connections") > 0 ? true : false;
_state = StateHolding;
DefaultsAndOverrides defaultsAndOverrides = _instance.defaultsAndOverrides();
if(defaultsAndOverrides.overrideTimeout)
{
_endpoint = _endpoint.timeout(defaultsAndOverrides.overrideTimeoutValue);
}
if(defaultsAndOverrides.overrideCompress)
{
_endpoint = _endpoint.compress(defaultsAndOverrides.overrideCompressValue);
}
try
{
EndpointHolder h = new EndpointHolder();
h.value = _endpoint;
_transceiver = _endpoint.serverTransceiver(h);
if(_transceiver != null)
{
_endpoint = h.value;
Ice.ConnectionI connection = new Ice.ConnectionI(_instance, _transceiver, _endpoint, _adapter);
//
// In thread per connection mode, the connection's thread will
// take care of connection validation, and we don't want to
// block here waiting until validation is complete. Therefore
// we don't call validate() in thread per connection mode.
//
if(!_instance.threadPerConnection())
{
connection.validate();
}
_connections.add(connection);
}
else
{
h.value = _endpoint;
_acceptor = _endpoint.acceptor(h);
_endpoint = h.value;
assert(_acceptor != null);
_acceptor.listen();
if(_instance.threadPerConnection())
{
try
{
//
// If we are in thread per connection mode, we also use
// one thread per incoming connection factory, that
// accepts new connections on this endpoint.
//
_threadPerIncomingConnectionFactory = new ThreadPerIncomingConnectionFactory();
_threadPerIncomingConnectionFactory.start();
}
catch(RuntimeException ex)
{
error("cannot create thread for incoming connection factory", ex);
_state = StateClosed;
try
{
_acceptor.close();
}
catch(Ice.LocalException e)
{
// Here we ignore any exceptions in close().
}
_acceptor = null;
_threadPerIncomingConnectionFactory = null;
throw ex;
}
}
}
}
catch(RuntimeException ex)
{
_state = StateClosed;
_acceptor = null;
throw ex;
}
}
protected void
finalize()
throws Throwable
{
assert(_state == StateClosed);
assert(_acceptor == null);
assert(_connections.size() == 0);
assert(_threadPerIncomingConnectionFactory == null);
//
// Destroy the EventHandler's stream, so that its buffer
// can be reclaimed.
//
super._stream.destroy();
super.finalize();
}
private static final int StateActive = 0;
private static final int StateHolding = 1;
private static final int StateClosed = 2;
private void
setState(int state)
{
if(_state == state) // Don't switch twice.
{
return;
}
switch(state)
{
case StateActive:
{
if(_state != StateHolding) // Can only switch from holding to active.
{
return;
}
if(!_instance.threadPerConnection())
{
registerWithPool();
}
java.util.ListIterator p = _connections.listIterator();
while(p.hasNext())
{
Ice.ConnectionI connection = (Ice.ConnectionI)p.next();
connection.activate();
}
break;
}
case StateHolding:
{
if(_state != StateActive) // Can only switch from active to holding.
{
return;
}
if(!_instance.threadPerConnection())
{
unregisterWithPool();
}
java.util.ListIterator p = _connections.listIterator();
while(p.hasNext())
{
Ice.ConnectionI connection = (Ice.ConnectionI)p.next();
connection.hold();
}
break;
}
case StateClosed:
{
if(_instance.threadPerConnection())
{
if(_acceptor != null)
{
//
// Connect to our own acceptor, which unblocks our
// thread per incoming connection factory stuck in accept().
//
_acceptor.connectToSelf();
}
}
else
{
//
// If we come from holding state, we first need to
// register again before we unregister.
//
if(_state == StateHolding)
{
registerWithPool();
}
unregisterWithPool();
}
java.util.ListIterator p = _connections.listIterator();
while(p.hasNext())
{
Ice.ConnectionI connection = (Ice.ConnectionI)p.next();
connection.destroy(Ice.ConnectionI.ObjectAdapterDeactivated);
}
break;
}
}
_state = state;
notifyAll();
}
private void
registerWithPool()
{
assert(!_instance.threadPerConnection()); // Only for use with a thread pool.
if(_acceptor != null && !_registeredWithPool)
{
((Ice.ObjectAdapterI)_adapter).getThreadPool()._register(_acceptor.fd(), this);
_registeredWithPool = true;
}
}
private void
unregisterWithPool()
{
assert(!_instance.threadPerConnection()); // Only for use with a thread pool.
if(_acceptor != null && _registeredWithPool)
{
((Ice.ObjectAdapterI)_adapter).getThreadPool().unregister(_acceptor.fd());
_registeredWithPool = false;
}
}
private void
warning(Ice.LocalException ex)
{
java.io.StringWriter sw = new java.io.StringWriter();
java.io.PrintWriter pw = new java.io.PrintWriter(sw);
ex.printStackTrace(pw);
pw.flush();
String s = "connection exception:\n" + sw.toString() + '\n' + _acceptor.toString();
_instance.logger().warning(s);
}
private void
error(String msg, Exception ex)
{
java.io.StringWriter sw = new java.io.StringWriter();
java.io.PrintWriter pw = new java.io.PrintWriter(sw);
ex.printStackTrace(pw);
pw.flush();
String s = msg + ":\n" + toString() + sw.toString();
_instance.logger().error(s);
}
private void
run()
{
assert(_acceptor != null);
while(true)
{
//
// We must accept new connections outside the thread
// synchronization, because we use blocking accept.
//
Transceiver transceiver = null;
try
{
transceiver = _acceptor.accept(-1);
}
catch(Ice.SocketException ex)
{
// Do not ignore SocketException in Java.
throw ex;
}
catch(Ice.TimeoutException ex)
{
// Ignore timeouts.
}
catch(Ice.LocalException ex)
{
// Warn about other Ice local exceptions.
if(_warn)
{
warning(ex);
}
}
Ice.ConnectionI connection = null;
synchronized(this)
{
while(_state == StateHolding)
{
try
{
wait();
}
catch(InterruptedException ex)
{
}
}
if(_state == StateClosed)
{
if(transceiver != null)
{
try
{
transceiver.close();
}
catch(Ice.LocalException ex)
{
// Here we ignore any exceptions in close().
}
}
try
{
_acceptor.close();
}
catch(Ice.LocalException ex)
{
_acceptor = null;
notifyAll();
throw ex;
}
_acceptor = null;
notifyAll();
return;
}
assert(_state == StateActive);
//
// Reap connections for which destruction has completed.
//
java.util.ListIterator p = _connections.listIterator();
while(p.hasNext())
{
Ice.ConnectionI con = (Ice.ConnectionI)p.next();
if(con.isFinished())
{
p.remove();
}
}
//
// Create a connection object for the connection.
//
if(transceiver != null)
{
try
{
connection = new Ice.ConnectionI(_instance, transceiver, _endpoint, _adapter);
_connections.add(connection);
}
catch(RuntimeException ex)
{
//
// We don't print any errors here, the
// connection constructor is responsible for
// printing the error.
//
}
}
}
//
// In thread per connection mode, the connection's thread will
// take care of connection validation. We don't want to block
// this thread waiting until validation is complete, because
// in contrast to thread pool mode, it is the only thread that
// can accept connections with this factory's
// acceptor. Therefore we don't call validate() in thread per
// connection mode.
//
}
}
private class ThreadPerIncomingConnectionFactory extends Thread
{
public void
run()
{
try
{
IncomingConnectionFactory.this.run();
}
catch(Exception ex)
{
IncomingConnectionFactory.this.error("exception in thread per incoming connection factory", ex);
}
}
}
private Thread _threadPerIncomingConnectionFactory;
private Acceptor _acceptor;
private final Transceiver _transceiver;
private Endpoint _endpoint;
private final Ice.ObjectAdapter _adapter;
private boolean _registeredWithPool;
private final boolean _warn;
private java.util.LinkedList _connections = new java.util.LinkedList();
private int _state;
}
|