summaryrefslogtreecommitdiff
path: root/TAO/IIOP/lib/bridge/tcpoa.cpp
blob: 5e98c21e82cde6a05f23227949d9839452a7d685 (plain)
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
// @(#)tcpoa.cpp	1.16 95/11/04
// Copyright 1994-1995 by Sun Microsystems Inc.
// All Rights Reserved
//
// IIOP:	Small "TCP OA", thinly layered atop IIOP.
//
// Like all OAs, this provides an object creation facility and a simple
// DSI-based operation dispatch facility.  However, it's so simple it
// doesn't do any more than that!  In particular, it maintains no per-object
// state either on disk or in memory.
//
// NOTE:  It's undesirable in firewall and threaded environments to continue
// with the current restriction of only one OA per process.
//

#include	<assert.h>
#include	<stdio.h>
#include	<string.h>

#if	unix
#include	<unistd.h>
#include	<netdb.h>

#else
#include	<winsock.h>

#endif

#include	<corba/orb.hh>
#include	<corba/toa.hh>

#include	"runtime/thread.hh"
#include	"runtime/cdr.hh"
#include	"runtime/debug.hh"

#include	"bridge/connmgr.hh"
#include	"bridge/tcpoa.hh"
#include	"bridge/giop.hh"
#include	"bridge/svrrqst.hh"

#include	<initguid.h>


#ifdef	NO_HOSTNAMES
#include	<netinet/in.h>
#include	<arpa/inet.h>
#endif	// NO_HOSTNAMES


#if !defined (DECLARED_GETHOSTNAME)
extern "C" int		gethostname (char *, int);
#endif



static TCP_OA		*the_oa;
static short		tcp_port;
static char		namebuf [65];


#ifdef _POSIX_THREADS
static pthread_key_t	request_key;
static pthread_mutex_t	tcpoa_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t	tcpoa_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_attr_t	thread_attr;
#endif	// _POSIX_THREADS


TCP_OA::TCP_OA (
    CORBA_ORB_ptr	owning_orb,
    CORBA_UShort	_port,
    CORBA_Environment	&env
)
{
    assert (the_oa == 0); 

    port = _port;
    do_exit = CORBA_B_FALSE;
    _orb = owning_orb;
    call_count = 0;
    skeleton = 0;

    if (gethostname (namebuf, sizeof namebuf) < 0) {
	dsockerr ("gethostname");
	return;
    }

#ifdef	NO_HOSTNAMES
    //
    // In some highly static environments, or where even the most basic
    // Internet services are unavailable, it's desirable to use IP addresses
    // rather than host names in object references.  IP addresses are
    // normally bad to use since they need to change.
    //
    // In general, TCP OA initialization options are needed to control at
    // least three kinds of host identifiers to embed in objrefs:
    //
    //	(a) unqualified host names, which is what gethostname() returns
    //	    at most sites;
    //
    //	(b) qualified host names (including fully qualified ones, also
    //	    ones with just subdomain qualification) which are hard to
    //	    discover in a portable way;
    //
    //	(c) "Dot notation" IP addresses, which can get stale after a short
    //	    while ... hosts move between networks, network numbers change,
    //	    and so on.  These will also cause problems in the upcoming
    //	    evolution of the Internet to IPv6.
    //
    // At this time, no general framework is available to control the choice
    // of these kinds of identifiers.  A remotely administerable framework
    // (e.g. Win32 registry) seems like it'd be most appropriate.
    //
    // NOTE:  gethostbyname() is MT-unsafe, call only in one thread!!
    //
    hostent	*hp;
    char	*dot_notation;

    hp = gethostbyname (namebuf);
    assert (hp != 0);

    dot_notation = inet_ntoa (*(in_addr *)hp->h_addr);
    assert (dot_notation != 0);

    (void) strcpy (namebuf, dot_notation);
#endif	// NO_HOSTNAMES

    //
    // Initialize the endpoint ... or try!
    //
    // NOTE that this sets "port" if it was originally zero.  An
    // original value of zero indicates that the OS should select
    // a port on which this OA will listen.
    //
    endpoint = server_endpoint::initialize (port, env);
    tcp_port = port;

    if (env.exception () != 0)  {
	dmsg2 ("TCP OA:  '%s', port %u", namebuf, port);
	the_oa = this;
    }
}

TCP_OA::~TCP_OA ()
{
    if (endpoint) {
	endpoint->shutdown_connections (GIOP::close_connection, 0);
	endpoint = 0;
    }
}


//
// Public initialisation routine for this OA.
//
TCP_OA_ptr
TCP_OA::init (
    CORBA_ORB_ptr	parent,
    char		*oa_name,
    CORBA_Environment	&env
)
{
    env.clear ();

#ifdef	_POSIX_THREADS
    Critical		region (&tcpoa_mutex);
#endif	// _POSIX_THREADS

    if (the_oa) {
	env.exception (new CORBA_INITIALIZE (COMPLETED_NO));
	return 0;
    }

#ifdef	_POSIX_THREADS

    //
    // Initialize POSIX thread stuff:  TSD key for request data, and
    // attributes for threads that can be dynamically created.
    //
    // XXX this stuff should be guarded by "pthread_once", it's not
    // at all OA-specific.
    //
    (void) pthread_key_create (&request_key, 0);

    (void) pthread_attr_init (&thread_attr);
    (void) pthread_attr_setdetachstate (&thread_attr, PTHREAD_CREATE_DETACHED);

#ifdef	_POSIX_THREAD_PRIORITY_SCHEDULING
    (void) pthread_attr_setscope (&thread_attr, PTHREAD_SCOPE_PROCESS);
#endif	// _POSIX_THREAD_PRIORITY_SCHEDULING

#endif	// _POSIX_THREADS


    //
    // The "OA Name" is either the service name (normally listed in the
    // /etc/services file) or is the string form of the port number.
    // These are both controlled by local administration, though in some
    // cases the IETF will register port numbers.   If the OA name is
    // unspecified (null pointer) or zero, the OS assigns some port
    // which is currently unused.
    //
    // XXX getservent() is MT-unsafe; use getservent_r() where it exists
    // on the target platform
    //
    unsigned short	port;
    struct servent	*sp;

    if (oa_name && (sp = getservbyname (oa_name, "tcp")) != 0)
	port = (unsigned short) sp->s_port;
    else if (oa_name != 0 && atoi (oa_name) != -1)
	port = (unsigned short) atoi (oa_name);
    else
	port = 0;

    the_oa = new TCP_OA (parent, port, env);

    return the_oa;
}


//
// Create an objref
//
CORBA_Object_ptr
__stdcall
TCP_OA::create (
    CORBA_OctetSeq	_FAR &key,
    CORBA_String	_FAR type_id,
    CORBA_Environment	_FAR &env
)
{
    CORBA_String	id;
    IIOP_Object		*data;

    if (type_id)
	id = CORBA_string_copy (type_id);
    else
	id = 0;
    data = new IIOP_Object (id);

    if (data != 0)
	env.clear ();
    else {
	env.exception (new CORBA_NO_MEMORY (COMPLETED_NO));
	return 0;
    }

    data->profile.iiop_version.major = IIOP::MY_MAJOR;
    data->profile.iiop_version.minor = IIOP::MY_MINOR;
    data->profile.host = strdup (namebuf);
    data->profile.port = port;
    data->profile.object_key.length =  key.length;
    data->profile.object_key.maximum = key.length;
    data->profile.object_key.buffer = new CORBA_Octet [(size_t) key.length];

    //
    // Verify the memory allocations we just did ...
    //
    if (data->profile.host == 0 || data->profile.object_key.buffer == 0) {
	env.exception (new CORBA_NO_MEMORY (COMPLETED_NO));
	data->Release ();
	return 0;
    }

    memcpy (data->profile.object_key.buffer, key.buffer, (size_t) key.length);

    //
    // Return the CORBA_Object_ptr interface to this objref.
    //
    CORBA_Object_ptr		new_obj;

    if (data->QueryInterface (IID_CORBA_Object, (void **)&new_obj)
	    != NOERROR) {
	env.exception (new CORBA_INTERNAL (COMPLETED_NO));
    }
    data->Release ();
    return new_obj;
}


//
// Return the key fed into an object at creation time.
//
CORBA_OctetSeq *
__stdcall
TCP_OA::get_key (
    CORBA_Object_ptr	,
    CORBA_Environment	&env
)
{
    // XXX implement me ! ... must have been created by this OA.
    env.exception (new CORBA_IMP_LIMIT (COMPLETED_NO));
    return 0;
}


#ifdef	_POSIX_THREADS
//
// Use the TSD key set up as part of OA initialization to get the
// thread-specific data describing this invocation
//
#define	request_tsd \
	((GIOP::RequestHeader *) pthread_getspecific (request_key))

#else

//
// This emulates "thread specific data" to hold data about the request
// that the thread is processing.  It needs to be accessed for a variety
// of purposes in the course of request processing.
//
static GIOP::RequestHeader	*request_tsd;
#endif	// _POSIX_THREADS


//
// Return the objref which is target of the call.  This must be freed
// by whoever calls the routine.
//
// XXX should be consistent with other explicit/implicit request data
// and have the ORB free it when the call returns; that'll let the cost
// of the four mallocs (yeech!) be amortized over more work.
//
// XXX there's no way to report exceptions here ...
//
CORBA_Object_ptr
__stdcall
_this ()
{
    IIOP_Object			*data;
    opaque			*key = &request_tsd->object_key;

    //
    // NOTE:  The "TCP_OA" can't return the type ID of this object
    // since it has no persistent storage.  The expectation is that
    // code layered on top of this OA, providing whatever storage is
    // needed and defining their own policies for object keys, will be
    // built on top of this interface; their implementations of the
    // "_this()" routine will return a full objref (and also be able
    // to answer questions like whether it _is_a given type).
    //
    if ((data = new IIOP_Object (0)) == 0) {	// null type ID
	return 0;
    }

    data->profile.iiop_version.major = IIOP::MY_MAJOR;
    data->profile.iiop_version.minor = IIOP::MY_MINOR;
    data->profile.host = strdup (namebuf);
    data->profile.port = tcp_port;
    data->profile.object_key.length =  key->length;
    data->profile.object_key.maximum = key->length;
    data->profile.object_key.buffer = new CORBA_Octet [(size_t) key->length];
    memcpy (data->profile.object_key.buffer, key->buffer, (size_t) key->length);

    if (data->profile.host == 0 || data->profile.object_key.buffer == 0) {
	data->Release ();
	return 0;
    }

    //
    // Return the CORBA_Object_ptr interface to this objref.
    //
    CORBA_Object_ptr		new_obj;

    (void) data->QueryInterface (IID_CORBA_Object, (void **)&new_obj);
    data->Release ();
    return new_obj;
}


//
// return the target's key
//
// NOTE:  as with all "in" parameters to a call, this memory is freed
// by the ORB not by the object implementation.
//
CORBA_OctetSeq *
__stdcall
TCP_OA::get_target_key (
    CORBA_Environment		&env
)
{
    env.clear ();

    return &request_tsd->object_key;
}


//
// return the caller's principal
//
// NOTE:  as with all "in" parameters to a call, this memory is freed
// by the ORB not by the object implementation.
//
CORBA_Principal_ptr
__stdcall
TCP_OA::get_client_principal (
    CORBA_Environment		&env
)
{
    env.clear ();

    return request_tsd->requesting_principal;
}


//
// Dispatch routine that provides most of the IIOP glue ... constructs
// a dynamic ServerRequest and any reply message that's needed.
//
static void
tcp_oa_dispatcher (
    GIOP::RequestHeader		&req,
    CDR				&request_body,
    CDR				*reply,
    void			*context,
    CORBA_Environment		&env
)
{
    IIOP_ServerRequest		svr_req (
				    &request_body,
				    the_oa->orb (),
				    the_oa
				);

    //
    // ServerRequest is what does the unmarshaling, driven by typecodes
    // that the DSI user provides.  Create the ServerRequest, store away
    // information that'll be needed by some methods, and call the dispatch
    // routine that the user supplied.  Then release the reference so it
    // can be safely destroyed sometime later.
    //
    svr_req._opname = req.operation;

#ifdef	_POSIX_THREADS
    (void) pthread_setspecific (request_key, &req);
#else
    request_tsd = &req;
#endif	// _POSIX_THREADS

    TCP_OA::dispatch_context		*helper;

    helper = (TCP_OA::dispatch_context *) context;
    helper->skeleton (req.object_key, svr_req, helper->context, env);

    svr_req.release ();

    //
    // If reply is null, this was a oneway request ... return!
    //
    if (reply == 0)
	return;

    //
    // Otherwise check for correct parameter handling, and reply as
    // appropriate.
    //
    // NOTE:  if "env" is set, it takes precedence over exceptions
    // reported using the mechanism of the ServerRequest.  Only system
    // exceptions are reported that way ...
    //
    // XXX  Exception reporting is ambiguous; it can be cleaner than
    // this.  With both language-mapped and dynamic/explicit reporting
    // mechanisms, one of must be tested "first" ... so an exception
    // reported using the other mechanism could be "lost".  Perhaps only
    // the language mapped one should be used for system exceptions.
    //
    CORBA_TypeCode_ptr		tc;
    const void			*value;

    if (!svr_req._params && env.exception () == 0) {
	dmsg ("DSI user error, didn't supply params");
	env.exception (new CORBA_BAD_INV_ORDER (COMPLETED_NO));
    }

    if (env.exception () != 0) {	// standard exceptions only
	CORBA_Environment	env2;
	CORBA_Exception		*x = env.exception ();
	CORBA_TypeCode_ptr	except_tc = x->type ();

	reply->put_ulong (GIOP::SYSTEM_EXCEPTION);
	(void) CDR::encoder (except_tc, x, 0, reply, env2);
	
    } else if (svr_req._exception) {	// any exception at all
	CORBA_Exception		*x;
	CORBA_TypeCode_ptr	except_tc;

	x = (CORBA_Exception *) svr_req._exception->value ();
	except_tc = svr_req._exception->type ();

	//
	// Finish the GIOP Reply header, then marshal the exception.
	//
	// XXX x->type() someday ...
	//
	if (svr_req._ex_type == SYSTEM_EXCEPTION)
	    reply->put_ulong (GIOP::SYSTEM_EXCEPTION);
	else
	    reply->put_ulong (GIOP::USER_EXCEPTION);

	(void) CDR::encoder (except_tc, x, 0, reply, env);

    } else {				// normal reply
	//
	// First finish the GIOP header ...
	//
	reply->put_ulong (GIOP::NO_EXCEPTION);

	//
	// ... then send any return value ...
	//
	if (svr_req._retval) {
	    tc = svr_req._retval->type ();
	    value = svr_req._retval->value ();
	    (void) CDR::encoder (tc, value, 0, reply, env);
	}

	//
	// ... followed by "inout" and "out" parameters, left to right
	//
	unsigned			i;

	for (i = 0; i < svr_req._params->count (); i++) {
	    CORBA_NamedValue_ptr	nv = svr_req._params->item (i);
	    CORBA_Any_ptr		any;

	    if (!(nv->flags () & (CORBA_ARG_INOUT|CORBA_ARG_OUT)))
		continue;

	    any = nv->value ();
	    tc = any->type ();
	    value = any->value ();
	    (void) CDR::encoder (tc, value, 0, reply, env);
	}
    }
}


//
// Helper routine that provides IIOP glue for forwarding requests
// to specific objects from one process to another.
//
static GIOP::LocateStatusType
tcp_oa_forwarder (
    opaque			&target_key,
    CORBA_Object_ptr		&forward_reference,
    void			*ctx
)
{
    TCP_OA::dispatch_context	*helper;
    CORBA_Environment		env;

    helper = (TCP_OA::dispatch_context *) ctx;
    assert (helper->check_forward != 0);
    helper->check_forward (target_key, forward_reference, helper->context, env);

    if (env.exception () != 0)
	return GIOP::UNKNOWN_OBJECT;
    else if (forward_reference == 0)
	return GIOP::OBJECT_HERE;
    else
	return GIOP::OBJECT_FORWARD;
}


//
// Generic routine to handle a message.
//
void
TCP_OA::handle_message (
    dispatch_context			&ctx,
    CORBA_Environment			&env
)
{
    GIOP::incoming_message (ctx.endpoint->fd,
	    ctx.check_forward ? tcp_oa_forwarder : 0,
	    tcp_oa_dispatcher, &ctx, env);

#ifdef	_POSIX_THREADS
    Critical		region (&tcpoa_mutex);
#endif	// _POSIX_THREADS

    call_count--;
}


#ifdef	_POSIX_THREADS
//
// For cases where the OA creates new threads to handle messages (which
// are typically, but not always, GIOP::Request messages) we need to have
// a routine for that new thread to execute.  This is that routine:  its
// role is only to set up to call the common "handle_message" routine,
// knowing that it's not the thread which was initially handed the message.
//
// In "aggressive" threading mode, it continues reading from the connection
// until the connection goes away (e.g. client goes away, or server does
// its shutdown work).  This is a big win.
//
// XXX this needs to set up so it goes away with pthread_cancel().  We
// lack any kind of "timed read" primitive so the best we could otherwise
// do is to select (with timeout) before issuing the read, possibly with
// a switch to another thread in between.
//
void *
TCP_OA::worker (void *arg)
{
    CORBA_Environment		env;
    dispatch_context		*context = (dispatch_context *)arg;

    do {
	dmsg1 ("worker starting on FD %d", context->endpoint->fd);
	context->oa->handle_message (*context, env);

	if (env.exception () != 0) {
	    dexc (env, "TCP_OA, worker");
	    env.clear ();
	}
    } while (context->aggressive && context->endpoint->fd != -1);

    delete context;
    return 0;
}
#endif	// _POSIX_THREADS


//
// Block till some request comes in, and call "skeleton" when it does.
// Uses helper routines above to do most of the work.
//
// THREADING NOTE:  Only one thread may call this at a time, due to some
// underlying issues with select().  If you want a model wherein a finite
// pool of threads handles your requests, this doesn't do that.  (This is
// probably a good thing:  it's all but impossible to accurately bound the
// number of concurrent requests any given server will need to handle.)
//
void
__stdcall
TCP_OA::get_request (
    TOA::dsi_handler	handler,
    void		check_forward (
	CORBA_OctetSeq	&key,
	CORBA_Object_ptr	&fwd_ref,
	void			*ctx,
	CORBA_Environment	&env
    ),
    CORBA_Boolean	do_thr_create,
    void		*app_state,
    timeval		*timeout,
    CORBA_Environment	&env
)
{
    server_endpoint	*fd;

    env.clear ();

    //
    // Two bits of OA-private state need to be guarded by a critical
    // region:  the "do_exit" flag, and the "call_count" flag.  These
    // are used in clean shutdown, and can be modified here.
    //
    {
#ifdef	_POSIX_THREADS
    	Critical		region (&tcpoa_mutex);
#endif	// _POSIX_THREADS

	//
	// Applications sometimes make mistakes:  here, it'd be
	// polling for a new request after initiating shutdown.
	//
	if (do_exit) {
	    dmsg ("called get_request during OA shutdown");
	    env.exception (new CORBA_BAD_INV_ORDER (COMPLETED_NO));
	    return;
	}

#ifndef	_POSIX_THREADS
	//
	// Here, some unthreaded app asked for thread support.  Ooops!
	//
	if (do_thr_create) {
	    env.exception (new CORBA_IMP_LIMIT (COMPLETED_NO));
	    dexc (env, "TCP_OA, unthreaded: can't create worker threads");
	    return;
	}
#endif	// _POSIX_THREADS

	//
	// Get a request/message ... if no file descriptor is returned,
	// the application-specified timeout was reached, leading to a
	// clean shutdown.  Otherwise we flag an incoming message (so
	// shutdown will know it can't start yet), and leave the critical
	// section.  Some other thread could now get a request on some
	// other connection, if one's ready.
	//
	// THREADING NOTE:  what block_for_connection() returns will not
	// be read or written by any other thread until it's released ...
	// that is, until "fd" goes out of scope.  At least, in the
	// current implementation, which doesn't let threads share a
	// connection.
	//
	// Also, note that the underlying constraint of only allowing a
	// single thread to block_for_connection() call bubbles up here too.
	//
#ifdef	_POSIX_THREADS
	static int		blocking;	// = 0

	if (blocking) {
	    dmsg ("concurrent TCP_OA::get_request() calls");
	    env.exception (new CORBA_IMP_LIMIT (COMPLETED_NO));
	    return;
	} else
	    blocking = 1;

	region.leave ();
#endif	// _POSIX_THREADS

	//
	// Get a connection and hand it to method code.
	//
	// There are four "threading modes" currently possible with
	// this software, though only two are exposed in the OA API.
	//
	//	- Single-Threaded ... where either no threads are
	//	  used, or method code only sees one thread.
	//
	//	- Simple MT ... like single threaded, except that
	//	  (a) method code can see multiple threads, (b) a thread
	//	  context switch happens before incoming methods get
	//	  processed, and (c) there's expensive handshaking re
	//	  connections when they're released.
	//
	//	- "Eager" MT ... like simple MT, except that the context
	//	  switch (b) is gone, and incoming data is handled right
	//	  away by a thread that's always blocked in a read.  The
	//	  initial request latency is lower than "Simple MT".
	//
	//	- "Aggressive" MT ... like "eager" MT except that the
	//	  costly handshaking (c) is removed along with thread
	//	  creation costs.  Throughput is higher than with any
	//	  of the other models, since it's not just initial
	//	  requests which benefit from reduced latency and
	//	  since overall MT-related costs are minimized.
	//
	// To the application, only "single threaded" and "MT" policies
	// are exposed ... the rest is implementation details, all they
	// really care about is whether method code seems multiple threads
	// and the quality of the implementation (size, speed, etc).
	//
	// XXX Right now we equate "MT" to "Aggressive", despite problems.
	// If clients behave well, they're not an issue; in real systems,
	// we know clients don't always behave.
	//
	// One forward-looking way is to use pthread_cancel to kill idle
	// workers ... that looks forward to a later implementation which
	// always has a read outstanding on a connection, and so can also
	// handle GIOP::CancelRequest messages.
	//

	fd = endpoint->block_for_connection (do_thr_create, timeout, env);

#ifdef	_POSIX_THREADS
	region.enter ();
	blocking = 0;
#endif	// _POSIX_THREADS

	if (env.exception () != 0) {
	    dexc (env, "TCP_OA, block for connection");
	    return;
	}
	if (fd == 0) {
	    do_exit = CORBA_B_TRUE;
	    return;
	}

	//
	// THREADING NOTE:  This is the last of the need to have the OA
	// locked ... next, let other threads in to access its state.
	//
	call_count++;
    }

    //
    // OK, now we've got a connection with a "live" IIOP message ready.
    //
    // If we're "eager", there's not actually any data on that connection
    // yet ... but we still hand it to some other thread as if it were
    // all there.  This gets all the setup out of the critical path so that
    // when data does arrive, its latency is minimal:  straight into the
    // IIOP data buffer, in the best (zero copy) case, with no lingering in
    // kernel read queues.
    //
    if (do_thr_create) {
#ifdef	_POSIX_THREADS
	//
	// Want to handle it in another thread.  That means handing off a
	// bunch of information to that thread and then having it do the
	// work ... the simplest alternative is to heap-allocate the data
	// and hand it over.  That involves no handshaking with the thread
	// we're about to create.  Note that we're also handing off the
	// connection resource too -- when this dispatch context gets
	// destroyed, only then is the connection released.
	//
	dispatch_context		*ctx;

	ctx = new dispatch_context;
	ctx->skeleton = handler;
	ctx->check_forward = check_forward;
	ctx->context = app_state;
	ctx->oa = this;
	ctx->endpoint = fd;
	ctx->aggressive = do_thr_create;

	//
	// Actually create the thread.
	//
	int				errcode;
	pthread_t			tid;

	// XXX Devpro bug at "-O3"
	void *(*func)(void *)		= worker;

	errcode = pthread_create (&tid, &thread_attr, func, ctx);

	if (errcode == 0)
	    return;

	//
	// Can't create a thread as requested.  Rather than handling it
	// in another thread, we must then handle it ourselves.  It's bad
	// news to drop requests, as would happen if we were to just
	// report an exception.
	//
	// XXX this should be logged as some kind of system fault;
	// an administrator would need to deal with these failures
	// if they happen repatedly.
	//
	dmsg2 ("pthread_create error: %d (%s)", errcode,
		strerror (errcode));
	delete context;

#endif	// _POSIX_THREADS
    }

    //
    // Handle it in this thread.  We can do it without any need
    // to dynamically allocate memory.
    //
    dispatch_context		ctx;

    ctx.skeleton = handler;
    ctx.check_forward = check_forward;
    ctx.context = app_state;
    ctx.oa = this;
    ctx.endpoint = fd;

#ifdef	_POSIX_THREADS
    ctx.aggressive = CORBA_B_FALSE;
#endif

    handle_message (ctx, env);

    //
    // Don't report any errors from the application/skeleton back to the
    // top level ... the application already has a variety of means to
    // pass whatever data needs passing, and we don't need to confuse
    // things by mixing ORB and application errors here.
    //
    if (env.exception () != 0) {
	dexc (env, "TCP_OA, handle incoming message");
	env.clear ();
    }
}


//
// Used by method code to ask the OA to shut down.
//
void
__stdcall
TCP_OA::please_shutdown (
    CORBA_Environment	&env
)
{
#ifdef	_POSIX_THREADS
    Critical		region (&tcpoa_mutex);
#endif	// _POSIX_THREADS

    env.clear ();
    do_exit = CORBA_B_TRUE;
}

//
// Used by non-method code to tell the OA to shut down.
//
void
__stdcall
TCP_OA::clean_shutdown (
    CORBA_Environment	&env
)
{
#ifdef	_POSIX_THREADS
    Critical		region (&tcpoa_mutex);
#endif	// _POSIX_THREADS

    env.clear ();

    if (call_count != 0) {
	dmsg ("called clean_shutdown with requests outstanding");
	env.exception (new CORBA_BAD_INV_ORDER (COMPLETED_NO));
	return;
    }

    endpoint->shutdown_connections (GIOP::close_connection, 0);
    endpoint = 0;
}


//
// For TOA -- TOA operations for which we provide the vtable entry
//

void
__stdcall
TCP_OA::register_dir (
    TOA::dsi_handler	handler,
    void		*ctx,
    CORBA_Environment	&env
)
{
    if (handler == 0) {
	env.exception (new CORBA_BAD_PARAM (COMPLETED_NO));
	return;
    }

    skeleton = handler;
    context = ctx;

    env.clear ();
}

void
__stdcall
TCP_OA::get_request  (
    CORBA_Boolean	use_threads,
    struct timeval	*tvp,
    CORBA_Environment	&env
)
{
    //
    // API spec calls for the DIR to be registered and for this to
    // report an invocation order problem if this is called after
    // shutdown was started (app can always avoid the latter).
    //
    if (skeleton == 0) {
	env.exception (new CORBA_INITIALIZE (COMPLETED_NO));
	return;
    }

    //
    // Just call the IIOP level dispatch code without allowing it to
    // ever forward requests to another TCP_OA.
    //
    get_request (skeleton, 0, use_threads, context, tvp, env);
}


//
// For COM -- IUnknown operations, we provide the vtable entry
//

// {A201E4C4-F258-11ce-9598-0000C07CA898}
DEFINE_GUID (IID_TCP_OA,
0xa201e4c4, 0xf258, 0x11ce, 0x95, 0x98, 0x0, 0x0, 0xc0, 0x7c, 0xa8, 0x98);


ULONG
__stdcall
TCP_OA::AddRef ()
{
#ifdef	_POSIX_THREADS
    Critical		region (&tcpoa_lock);
#endif

    return ++refcount;
}

ULONG
__stdcall
TCP_OA::Release ()
{
#ifdef	_POSIX_THREADS
    Critical		region (&tcpoa_lock);
#endif

    if (--refcount != 0)
	return refcount;

#ifdef	_POSIX_THREADS
    region.leave ();
#endif

    delete this;
    return 0;
}

HRESULT
__stdcall
TCP_OA::QueryInterface (
    REFIID	riid,
    void	**ppv
)
{
    *ppv = 0;

    if (IID_TCP_OA == riid
	    || IID_TOA == riid
	    || IID_IUnknown == riid)
	*ppv = this;

    if (*ppv == 0)
	return ResultFromScode (E_NOINTERFACE);

    (void) AddRef ();
    return NOERROR;
}