Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / tests / Bug_1495_Regression / Server_Task.cpp
blob65421ea9e3a851e890cdebc2a92b1bf513875d89
1 /**
2 * @file Server_Task.cpp
3 * @author Will Otte <wotte@dre.vanderbilt.edu>
5 * Implements the Server_Task class which acts as the process colocated
6 * corba server for Bug_1495_Regression test.
7 */
9 #include "Server_Task.h"
10 #include "ace/OS_NS_unistd.h"
11 #include "test_i.h"
13 #include "ace/Manual_Event.h"
16 Server_Task::Server_Task (const ACE_TCHAR *output,
17 CORBA::ORB_ptr sorb,
18 ACE_Manual_Event &me,
19 ACE_Thread_Manager *thr_mgr)
20 : ACE_Task_Base (thr_mgr),
21 output_ (output),
22 me_ (me),
23 sorb_ (CORBA::ORB::_duplicate (sorb))
28 int
29 Server_Task::svc ()
31 try
33 CORBA::Object_var poa_object =
34 sorb_->resolve_initial_references ("RootPOA");
36 if (CORBA::is_nil (poa_object.in ()))
38 ACE_ERROR ((LM_ERROR,
39 " (%P|%t) Unable to initialize the POA\n"));
40 return 1;
43 PortableServer::POA_var root_poa =
44 PortableServer::POA::_narrow (poa_object.in ());
46 PortableServer::POAManager_var poa_manager =
47 root_poa->the_POAManager ();
49 poa_manager->activate ();
51 Bug1495_i *server_impl = 0;
52 ACE_NEW_RETURN (server_impl,
53 Bug1495_i (sorb_.in ()),
54 0);
55 PortableServer::ServantBase_var owner_transfer (server_impl);
57 PortableServer::ObjectId_var id =
58 root_poa->activate_object (server_impl);
60 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
62 Bug1495_Regression::Bug1495_var bug1495 =
63 Bug1495_Regression::Bug1495::_narrow (object.in ());
65 CORBA::String_var ior = sorb_->object_to_string (bug1495.in ());
67 if (output_ != 0)
69 FILE *output_file = ACE_OS::fopen (output_, "w");
71 if (output_file == 0)
73 ACE_ERROR ((LM_ERROR,
74 "Cannot open output file for writing the "
75 "thread server IOR: %s", output_));
76 return 1;
79 ACE_OS::fprintf (output_file, "%s", ior.in ());
80 ACE_OS::fclose (output_file);
83 // sleep for a few seconds and hope the remote server picks up the
84 // ior.
85 ACE_OS::sleep (5);
87 // Signal the manual event to wake the main thread up.
88 me_.signal ();
90 // The ORB will run for 15 seconds and shut down.
91 ACE_Time_Value tv (15, 0);
92 sorb_->run (tv);
94 ACE_DEBUG ((LM_DEBUG,
95 "Event loop finished for the thread server.\n"));
97 root_poa->destroy (true, true);
99 sorb_->destroy ();
101 catch (const CORBA::Exception& ex)
103 ex._tao_print_exception ("Caught an exception in server task: ");
104 return 1;
107 return 0;