Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Bug_3171_Regression / server.cpp
blobde783b96f6235d94e43db3f0624fcc163cbcd1f0
1 #include "ace/Get_Opt.h"
2 #include "ace/Task.h"
3 #include "tao/corba.h"
4 #include "tao/Utils/ORB_Manager.h"
5 #include "tao/AnyTypeCode/Any.h"
6 #include "tao/Messaging/Messaging.h"
7 #include "tao/ORBInitializer_Registry.h"
8 #include "tao/PI/PI.h"
9 #include "tao/LocalObject.h"
11 int g_nthreads = 1;
12 bool g_setTimeout = true;
13 bool g_registerORBinitializer = true;
14 bool g_initInMain = false;
15 int g_argc;
16 ACE_TCHAR ** g_argv;
18 int initORB(int threadID);
20 int
21 parse_args (int argc, ACE_TCHAR *argv[])
23 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("min:"));
24 int c;
26 while ((c = get_opts ()) != -1)
27 switch (c)
29 case 'm':
30 g_initInMain = true;
31 break;
33 case 'i':
34 g_registerORBinitializer = false;
35 break;
37 case 'n':
38 g_nthreads = ACE_OS::atoi (get_opts.opt_arg ());
39 break;
41 case '?':
42 default:
43 ACE_ERROR_RETURN ((LM_ERROR,
44 "usage: %s "
45 "-m (Initialize an ORB from the main thread first) "
46 "-i (Do not register ORB initializer) "
47 "-n <numberOfThreads>"
48 "\n",
49 argv [0]),
50 -1);
52 // Indicates successful parsing of the command line
53 return 0;
56 class Worker : public ACE_Task_Base
58 public:
59 Worker ();
61 virtual int svc (void);
63 private:
66 class MyORBinitializer
67 : public virtual PortableInterceptor::ORBInitializer
68 , public virtual CORBA::LocalObject
70 public:
71 MyORBinitializer( ACE_CString orbID )
72 : orbID_( orbID ) {};
74 virtual
75 void
76 pre_init( PortableInterceptor::ORBInitInfo_ptr )
78 ACE_DEBUG ((LM_DEBUG,
79 "MyORBinitializer::pre_init() called for ORB \"%C\"\n",
80 orbID_.c_str()));
83 virtual
84 void
85 post_init( PortableInterceptor::ORBInitInfo_ptr )
87 ACE_DEBUG ((LM_DEBUG,
88 "MyORBinitializer::post_init() called for ORB \"%C\"\n",
89 orbID_.c_str()));
92 private:
93 ACE_CString orbID_;
96 int
97 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
99 g_argc = argc;
100 g_argv = argv;
104 if (parse_args (argc, argv) != 0)
105 return 1;
107 if ( g_initInMain )
109 initORB(0);
111 // Run in both main and background thread
112 // else
114 Worker worker;
115 if (worker.activate (THR_NEW_LWP | THR_JOINABLE,
116 g_nthreads) != 0)
117 ACE_ERROR_RETURN ((LM_ERROR,
118 "Cannot activate ORB thread(s)\n"),
121 worker.thr_mgr ()->wait ();
124 ACE_DEBUG ((LM_DEBUG, "Event loop finished\n"));
126 catch (const CORBA::Exception& ex)
128 ex._tao_print_exception ("Exception caught:");
129 return 1;
132 return 0;
135 // ****************************************************************
137 Worker::Worker ()
142 Worker::svc (void)
144 static int threadID = 0;
146 initORB(++threadID);
148 return 0;
152 initORB(int threadID)
156 char ORBid[10];
157 ACE_OS::sprintf (ORBid, "ORB_%d", threadID);
159 ACE_DEBUG ((LM_DEBUG, "Initializing ORB \"%C\"\n", ORBid));
161 if ( g_registerORBinitializer )
163 ACE_DEBUG ((LM_DEBUG, "Creating ORB initializer\n"));
164 PortableInterceptor::ORBInitializer_var rCOI(
165 new MyORBinitializer( ORBid ) );
166 PortableInterceptor::register_orb_initializer( rCOI.in() );
169 ACE_DEBUG ((LM_DEBUG, "Creating TAO_ORB_Manager\n"));
170 TAO_ORB_Manager* pORBmgr = new TAO_ORB_Manager;
172 if ( -1 == pORBmgr->init( g_argc, g_argv, ORBid ) )
174 ACE_DEBUG ((LM_DEBUG, "Failed to initialize ORB \"%C\"\n", ORBid));
175 throw CORBA::INTERNAL();
178 ACE_DEBUG ((LM_DEBUG, "ORB \"%C\" initialized\n", ORBid));
180 if ( g_setTimeout )
182 ACE_DEBUG ((LM_DEBUG,
183 "Setting connection timeout policy for ORB \"%C\"\n",
184 ORBid));
186 CORBA::PolicyList policyList;
188 CORBA::ORB_var orb = pORBmgr->orb();
190 TimeBase::TimeT connectionTimeout = 100*10000;
191 CORBA::Any any;
192 any <<= connectionTimeout;
194 CORBA::ULong l( policyList.length() );
195 policyList.length( l+1 );
197 policyList[l] = orb->create_policy(
198 TAO::CONNECTION_TIMEOUT_POLICY_TYPE,
199 any );
201 ACE_DEBUG ((LM_DEBUG, "Connection timeout policy set for ORB \"%C\"\n",
202 ORBid));
205 pORBmgr->fini();
207 delete pORBmgr;
209 catch (const CORBA::Exception& ex)
211 ACE_ERROR ((LM_ERROR, "Caught exception: %C\n", ex._info().c_str()));
212 return 1;
215 return 0;