Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / tests / TransportCurrent / Framework / server.cpp
blobbff3fe20ae0829ce400f42c337bb4251fbf0ed06
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
54 class Worker : public ACE_Task_Base
56 public:
57 Worker (CORBA::ORB_ptr);
58 virtual int svc ();
60 private:
61 // The ORB
62 CORBA::ORB_var orb_;
65 /// Ctor
67 Worker::Worker (CORBA::ORB_ptr orb)
68 : orb_ (CORBA::ORB::_duplicate (orb))
72 /// Test referencing the TC data *inside* the context of a client-side
73 /// interceptor
75 int
76 Worker::svc ()
78 try
80 this->orb_->run ();
82 catch (const CORBA::Exception& ex)
84 ex._tao_print_exception ("Server: exception raised");
86 return 0;
89 /// Main driver
91 int
92 server_main (int argc,
93 ACE_TCHAR *argv[],
94 Test::Server_Request_Interceptor *cri)
96 #if TAO_HAS_TRANSPORT_CURRENT == 1
98 try
100 PortableInterceptor::ORBInitializer_ptr temp_initializer = 0;
101 ACE_NEW_RETURN (temp_initializer,
102 Test::Server_ORBInitializer (cri),
103 -1); // No exceptions yet!
105 PortableInterceptor::ORBInitializer_var orb_initializer (temp_initializer);
107 PortableInterceptor::register_orb_initializer (orb_initializer.in ());
110 CORBA::ORB_var orb = CORBA::ORB_init (argc,
111 argv,
112 "test_orb");
114 CORBA::Object_var obj =
115 orb->resolve_initial_references ("RootPOA");
117 PortableServer::POA_var root_poa =
118 PortableServer::POA::_narrow (obj.in ());
120 if (CORBA::is_nil (root_poa.in ()))
121 ACE_ERROR_RETURN ((LM_ERROR,
122 ACE_TEXT ("Server (%P|%t) Unable to obtain")
123 ACE_TEXT (" RootPOA reference.\n")),
124 -1);
126 PortableServer::POAManager_var poa_manager =
127 root_poa->the_POAManager ();
129 poa_manager->activate ();
131 if (parse_args (argc, argv) != 0)
132 return -1;
134 Current_Test_Impl server_impl (orb.in (),
135 root_poa.in (),
136 use_collocated_call);
138 obj = server_impl._this ();
140 Test::Transport::CurrentTest_var server =
141 Test::Transport::CurrentTest::_narrow (obj.in ());
143 if (CORBA::is_nil (server.in ()))
144 ACE_ERROR_RETURN ((LM_ERROR,
145 ACE_TEXT ("Server (%P|%t) Unable to obtain ")
146 ACE_TEXT ("reference to CurrentTest object.\n")),
147 -1);
149 CORBA::String_var ior =
150 orb->object_to_string (server.in ());
152 // If the ior_output_file exists, output the IOR to it.
153 if (ior_output_file != 0)
155 FILE *output_file = ACE_OS::fopen (ior_output_file, "w");
156 if (output_file == 0)
157 ACE_ERROR_RETURN ((LM_ERROR,
158 "Server (%P|%t) Cannot write %s "
159 "IOR: %C - %m",
160 ior_output_file,
161 ior.in ()),
162 -1);
163 ACE_OS::fprintf (output_file, "%s", ior.in ());
164 ACE_OS::fclose (output_file);
167 // Spawn a number of clients doing the same thing for a
168 // predetermined number of times
169 Worker worker (orb.in ());
171 #if defined (ACE_HAS_THREADS)
172 if (worker.activate (THR_NEW_LWP | THR_JOINABLE,
173 nthreads) != 0)
174 ACE_ERROR_RETURN ((LM_ERROR,
175 ACE_TEXT ("Server (%P|%t) Cannot activate %d threads\n"),
176 nthreads),
177 -1);
178 //FUZZ: disable check_for_lack_ACE_OS
179 if(worker.thr_mgr ()->wait () != 0)
180 ACE_ERROR_RETURN ((LM_ERROR,
181 ACE_TEXT ("Server (%P|%t) wait() Cannot wait for all %d threads\n"),
182 nthreads),
183 -1);
184 //FUZZ: enable check_for_lack_ACE_OS
185 #else
186 if (nthreads > 1)
187 ACE_ERROR ((LM_WARNING,
188 ACE_TEXT ("Server (%P|%t) Cannot use threads other than ")
189 ACE_TEXT ("the only one available.\n")));
190 worker.svc ();
191 #endif
193 if (TAO_debug_level >= 1)
194 ACE_DEBUG ((LM_INFO, ACE_TEXT ("Server (%P|%t) Event loop finished.\n")));
196 if (!cri->self_test ())
197 ACE_ERROR ((LM_ERROR,
198 ACE_TEXT ("Server (%P|%t) ERROR: Interceptor self_test failed\n")));
200 root_poa->destroy (true, true);
202 server->shutdown ();
204 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Server (%P|%t) Invoking orb->destroy ()\n")));
206 orb->destroy ();
208 catch (const CORBA::Exception& ex)
210 ex._tao_print_exception (ACE_TEXT ("Server (%P|%t) ERROR: "));
212 return -1;
215 ACE_DEBUG ((LM_INFO, ACE_TEXT ("Server (%P|%t) Completed successfuly.\n")));
216 return 0;
217 #else /* TAO_HAS_TRANSPORT_CURRENT == 1 */
218 ACE_DEBUG ((LM_INFO, ACE_TEXT ("Server (%P|%t) Need TAO_HAS_TRANSPORT_CURRENT enabled to run.\n")));
219 return 0;
220 #endif /* TAO_HAS_TRANSPORT_CURRENT == 1 */