Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / tests / Bug_1551_Regression / server.cpp
blobc5ba38858a754a4f31968811f6ae3b363d414fd6
1 #include "Hello.h"
2 #include "Server_Task.h"
3 #include "ace/Get_Opt.h"
4 #include "ace/High_Res_Timer.h"
6 const ACE_TCHAR *ior_output_file = ACE_TEXT("test.ior");
8 bool simulate_crashes = true;
10 int nthreads = 1;
12 int
13 parse_args (int argc, ACE_TCHAR *argv[])
15 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("xn:o:"));
16 int c;
18 while ((c = get_opts ()) != -1)
19 switch (c)
21 case 'o':
22 ior_output_file = get_opts.opt_arg ();
23 break;
25 case 'x':
26 simulate_crashes = false;
27 break;
29 case 'n':
30 nthreads = ACE_OS::atoi(get_opts.opt_arg());
31 break;
33 case '?':
34 default:
35 ACE_ERROR_RETURN ((LM_ERROR,
36 "usage: %s"
37 " -o <iorfile>"
38 " -x (do not simulate crashes)"
39 " -n <nthreads>"
40 "\n",
41 argv [0]),
42 -1);
44 // Indicates successful parsing of the command line
45 return 0;
48 int
49 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
51 // Fetching the high res timer's global scale factor ensures that it
52 // is calibrated (if necessary on this platform) at the beginning of
53 // the test. While the timer would otherwise be calibrated on first
54 // use, this introduces delay in the middle of the test's execution.
55 // This leads to failures due to timing assumptions (timeouts, etc.)
56 // within the test itself.
57 (void) ACE_High_Res_Timer::global_scale_factor();
59 try
61 CORBA::ORB_var orb =
62 CORBA::ORB_init (argc, argv);
64 CORBA::Object_var poa_object =
65 orb->resolve_initial_references("RootPOA");
67 PortableServer::POA_var root_poa =
68 PortableServer::POA::_narrow (poa_object.in ());
70 if (CORBA::is_nil (root_poa.in ()))
71 ACE_ERROR_RETURN ((LM_ERROR,
72 " (%P|%t) Panic: nil RootPOA\n"),
73 -1);
75 PortableServer::POAManager_var poa_manager =
76 root_poa->the_POAManager ();
78 if (parse_args (argc, argv) != 0)
79 return -1;
81 PortableServer::ServantBase_var hello_impl(
82 new Hello(orb.in(), simulate_crashes));
84 PortableServer::ObjectId_var id =
85 root_poa->activate_object (hello_impl.in ());
87 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
89 Test::Hello_var hello =
90 Test::Hello::_narrow (object.in ());
92 CORBA::String_var ior =
93 orb->object_to_string (hello.in ());
95 // Output the IOR to the <ior_output_file>
96 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
97 if (output_file == 0)
98 ACE_ERROR_RETURN ((LM_ERROR,
99 "Cannot open output file for writing IOR: %s",
100 ior_output_file),
101 -1);
102 ACE_OS::fprintf (output_file, "%s", ior.in ());
103 ACE_OS::fclose (output_file);
105 poa_manager->activate ();
108 Server_Task st (orb.in (),
109 ACE_Thread_Manager::instance ());
111 if (st.activate (THR_NEW_LWP | THR_JOINABLE, nthreads, 1) == -1)
113 ACE_ERROR ((LM_ERROR,
114 "Error activating server task\n"));
116 return -1;
119 ACE_Thread_Manager::instance()->wait();
121 ACE_DEBUG ((LM_DEBUG, "(%P|%t) server - event loop finished\n"));
123 root_poa->destroy (true, true);
125 orb->destroy ();
127 catch (const CORBA::Exception& ex)
129 ex._tao_print_exception ("Exception caught:");
130 return -1;
133 return 0;