Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / tests / TransportCurrent / IIOP / server.cpp
blob1a5ea9153a60b08b1ecdf027b291c6d8cfb2c29b
1 // -*- C++ -*-
2 #include "ace/Get_Opt.h"
3 #include "ace/OS_NS_stdio.h"
4 #include "ace/Task.h"
6 #include "tao/ORBInitializer_Registry.h"
8 #include "Current_Test_Impl.h"
9 #include "Server_Request_Interceptor.h"
10 #include "Server_ORBInitializer.h"
12 const ACE_TCHAR *ior_output_file = ACE_TEXT ("server.ior");
13 int nthreads = 1;
14 int use_collocated_call = 1;
16 int
17 parse_args (int argc, ACE_TCHAR *argv[])
19 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("t:o:n:c:"));
20 int c;
22 while ((c = get_opts ()) != -1)
23 switch (c)
25 case 'c':
26 use_collocated_call = ACE_OS::atoi (get_opts.opt_arg ());
27 break;
28 case 'o':
29 ior_output_file = get_opts.opt_arg ();
30 break;
31 case 't':
32 nthreads = ACE_OS::atoi (get_opts.opt_arg ());
33 break;
34 case 'n': // Does nothing. Added just for symetry with the client.
35 break;
36 default:
37 ACE_ERROR_RETURN ((LM_ERROR,
38 "Usage: %s "
39 "-o <iorfile>"
40 "-t threads "
41 "-n n (ignored, if provided) "
42 "-c make_collocated_calls "
43 "\n",
44 argv[0]),
45 -1);
48 // Indicates successful parsing of the command line
49 return 0;
52 /// A helper class to encapsulate a task
53 class Worker : public ACE_Task_Base
55 public:
56 Worker (CORBA::ORB_ptr);
57 virtual int svc ();
59 private:
60 // The ORB
61 CORBA::ORB_var orb_;
65 /// Ctor
66 Worker::Worker (CORBA::ORB_ptr orb)
67 : orb_ (CORBA::ORB::_duplicate (orb))
71 /// Test referencing the TC data *inside* the context of a client-side
72 /// interceptor
73 int
74 Worker::svc ()
76 try
78 this->orb_->run ();
80 catch (const CORBA::Exception& ex)
82 ex._tao_print_exception ("Server: exception raised");
84 return 0;
88 /// Main driver
90 int
91 server_main (int argc,
92 ACE_TCHAR *argv[],
93 Test::Server_Request_Interceptor *cri)
95 #if TAO_HAS_TRANSPORT_CURRENT == 1
97 try
99 PortableInterceptor::ORBInitializer_ptr temp_initializer = 0;
100 ACE_NEW_RETURN (temp_initializer,
101 Test::Server_ORBInitializer (cri),
102 -1); // No exceptions yet!
104 PortableInterceptor::ORBInitializer_var orb_initializer (temp_initializer);
106 PortableInterceptor::register_orb_initializer (orb_initializer.in ());
109 CORBA::ORB_var orb = CORBA::ORB_init (argc,
110 argv,
111 "test_orb");
113 CORBA::Object_var obj =
114 orb->resolve_initial_references ("RootPOA");
116 PortableServer::POA_var root_poa =
117 PortableServer::POA::_narrow (obj.in ());
119 if (CORBA::is_nil (root_poa.in ()))
120 ACE_ERROR_RETURN ((LM_ERROR,
121 ACE_TEXT ("Server (%P|%t) Unable to obtain")
122 ACE_TEXT (" RootPOA reference.\n")),
123 -1);
125 PortableServer::POAManager_var poa_manager =
126 root_poa->the_POAManager ();
128 poa_manager->activate ();
130 if (parse_args (argc, argv) != 0)
131 return -1;
133 Current_Test_Impl server_impl (orb.in (),
134 root_poa.in (),
135 use_collocated_call);
137 obj = server_impl._this ();
139 Test::Transport::CurrentTest_var server =
140 Test::Transport::CurrentTest::_narrow (obj.in ());
142 if (CORBA::is_nil (server.in ()))
143 ACE_ERROR_RETURN ((LM_ERROR,
144 ACE_TEXT ("Server (%P|%t) Unable to obtain ")
145 ACE_TEXT ("reference to CurrentTest object.\n")),
146 -1);
148 CORBA::String_var ior =
149 orb->object_to_string (server.in ());
151 // If the ior_output_file exists, output the IOR to it.
152 if (ior_output_file != 0)
154 FILE *output_file = ACE_OS::fopen (ior_output_file, "w");
155 if (output_file == 0)
156 ACE_ERROR_RETURN ((LM_ERROR,
157 "Server (%P|%t) Cannot write file %s "
158 "IOR: %C - %m",
159 ior_output_file,
160 ior.in ()),
161 -1);
162 ACE_OS::fprintf (output_file, "%s", ior.in ());
163 ACE_OS::fclose (output_file);
166 // Spawn a number of clients doing the same thing for a
167 // predetermined number of times
168 Worker worker (orb.in ());
170 #if defined (ACE_HAS_THREADS)
171 if (worker.activate (THR_NEW_LWP | THR_JOINABLE,
172 nthreads) != 0)
173 ACE_ERROR_RETURN ((LM_ERROR,
174 ACE_TEXT ("Server (%P|%t) Cannot activate %d threads\n"),
175 nthreads),
176 -1);
177 worker.thr_mgr ()->wait ();
178 #else
179 if (nthreads > 1)
180 ACE_ERROR ((LM_WARNING,
181 ACE_TEXT ("Server (%P|%t) Cannot use threads other than ")
182 ACE_TEXT ("the only one available.\n")));
183 worker.svc ();
184 #endif
186 if (TAO_debug_level >= 1)
187 ACE_DEBUG ((LM_INFO, ACE_TEXT ("Server (%P|%t) Event loop finished.\n")));
189 if (!cri->self_test ())
190 ACE_ERROR ((LM_ERROR,
191 ACE_TEXT ("Server (%P|%t) ERROR: Interceptor self_test failed\n")));
193 server->shutdown ();
195 orb->destroy ();
197 catch (const CORBA::Exception& ex)
199 ex._tao_print_exception (ACE_TEXT ("Server (%P|%t) ERROR: "));
201 return -1;
204 ACE_DEBUG ((LM_INFO, ACE_TEXT ("Server (%P|%t) Completed successfuly.\n")));
205 return 0;
206 #else /* TAO_HAS_TRANSPORT_CURRENT == 1 */
207 ACE_DEBUG ((LM_INFO, ACE_TEXT ("Server (%P|%t) Need TAO_HAS_TRANSPORT_CURRENT enabled to run.\n")));
208 return 0;
209 #endif /* TAO_HAS_TRANSPORT_CURRENT == 1 */