Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / tests / DLL_ORB / Test_Server_Module.cpp
blob5e5b1e1fb479b4570c72f49e0a09e5265e66ae35
1 // -*- C++ -*-
2 #include "Test_Server_Module.h"
3 #include "tao/TAO_Singleton_Manager.h"
4 #include "tao/StringSeqC.h"
6 #include "ace/Service_Config.h"
7 #include "ace/Argv_Type_Converter.h"
8 #include "ace/Get_Opt.h"
11 const ACE_TCHAR *ior_file = ACE_TEXT("test.ior");
13 int
14 parse_args (int argc, ACE_TCHAR *argv[])
16 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
17 int c;
19 while ((c = get_opts ()) != -1)
20 switch (c)
22 case 'o':
24 ior_file = get_opts.opt_arg ();
25 break;
27 default:
28 ACE_ERROR_RETURN ((LM_ERROR,
29 "Usage: -o IOR\n"),
30 -1);
33 return 0;
36 Test_Server_Module::~Test_Server_Module ()
40 int
41 Test_Server_Module::init (int argc, ACE_TCHAR *argv[])
43 // -----------------------------------------------------------------
44 // Pre-ORB initialization steps necessary for proper DLL ORB
45 // support.
46 // -----------------------------------------------------------------
47 // Make sure TAO's singleton manager is initialized, and set to not
48 // register itself with the ACE_Object_Manager since it is under the
49 // control of the Service Configurator. If we register with the
50 // ACE_Object_Manager, then the ACE_Object_Manager will still hold
51 // (dangling) references to instances of objects created by this
52 // module and destroyed by this object when it is dynamically
53 // unloaded.
54 int register_with_object_manager = 0;
56 if (TAO_Singleton_Manager::instance ()->init (
57 register_with_object_manager) == -1)
58 ACE_ERROR_RETURN ((LM_ERROR,
59 "Test_Server_Module::init -- ORB pre-initialization "
60 "failed."),
61 -1); // No CORBA exceptions available yet, so
62 // return an error status.
65 // -----------------------------------------------------------------
66 // Boilerplate CORBA/TAO server-side ORB initialization code.
67 // -----------------------------------------------------------------
68 try
70 // Add one to the new argc since "dummy" is being added to the
71 // argv vector.
72 int new_argc = argc + 1;
74 CORBA::StringSeq new_argv (new_argc);
75 new_argv.length (new_argc);
77 // Prevent the ORB from opening the Service Configurator file
78 // again since the Service Configurator file is already in the
79 // process of being opened.
80 new_argv[0] = CORBA::string_dup ("dummy");
82 // Copy the remaining arguments into the new argument vector.
83 for (int i = new_argc - argc, j = 0;
84 j < argc;
85 ++i, ++j)
86 new_argv[i] = CORBA::string_dup (ACE_TEXT_ALWAYS_CHAR(argv[j]));
88 // Initialize the ORB.
89 this->orb_ = CORBA::ORB_init (new_argc,
90 new_argv.get_buffer (),
91 "SERVER");
93 if (CORBA::is_nil (this->orb_.in ()))
94 return -1;
96 CORBA::Object_var poa_object =
97 this->orb_->resolve_initial_references ("RootPOA");
99 if (CORBA::is_nil (poa_object.in ()))
100 ACE_ERROR_RETURN ((LM_ERROR,
101 " (%P|%t) Unable to initialize the POA.\n"),
104 this->poa_ =
105 PortableServer::POA::_narrow (poa_object.in ());
107 this->poa_manager_ = this->poa_->the_POAManager ();
109 this->poa_manager_->activate ();
111 ACE_Argv_Type_Converter converter (new_argc, new_argv.get_buffer ());
112 if (::parse_args (new_argc, converter.get_TCHAR_argv ()) != 0)
113 return -1;
115 PortableServer::ObjectId_var id =
116 this->poa_->activate_object (&servant_);
118 CORBA::Object_var obj = this->poa_->id_to_reference (id.in ());
120 CORBA::String_var ior =
121 this->orb_->object_to_string (obj.in ());
123 ACE_DEBUG ((LM_DEBUG,
124 "Servant:\n<%C>\n",
125 ior.in ()));
127 // Write IOR to a file.
128 FILE *output_file= ACE_OS::fopen (ior_file, "w");
129 if (output_file == 0)
130 ACE_ERROR_RETURN ((LM_ERROR,
131 "Cannot open output file <%s> for writing "
132 "IOR: %s",
133 ior.in ()),
135 ACE_OS::fprintf (output_file, "%s", ior.in ());
136 ACE_OS::fclose (output_file);
138 this->servant_.orb (this->orb_.in ());
140 catch (const CORBA::Exception& ex)
142 ex._tao_print_exception (ACE_TEXT ("Test_Server_Module::init"));
143 return -1;
146 #if defined (ACE_HAS_THREADS)
148 // Become an Active Object so that the ORB will execute in a
149 // separate thread.
150 return this->activate ();
152 #else
154 return 0;
156 #endif /* ACE_HAS_THREADS */
160 Test_Server_Module::fini ()
162 ACE_DEBUG ((LM_INFO,
163 "Server is being finalized.\n"));
165 // ------------------------------------------------------------
166 // Pre-Test_Server_Module termination steps.
167 // ------------------------------------------------------------
168 // Explicitly clean up singletons created by TAO before
169 // unloading this module.
170 if (TAO_Singleton_Manager::instance ()->fini () == -1)
171 ACE_ERROR_RETURN ((LM_ERROR,
172 "Test_Server_Module::fini -- ORB pre-termination "
173 "failed."),
174 -1);
176 return 0;
180 Test_Server_Module::svc ()
184 // Run the ORB event loop in its own thread.
185 this->orb_->run ();
187 ACE_DEBUG ((LM_INFO,
188 "Server is being destroyed.\n"));
190 // Make sure the ORB is destroyed here - before the thread
191 // exits, because it may be holding global resources, owned by
192 // the Object Manager (thru its core, which is in turn owned by
193 // the ORB table; which is owned by the Object Manager).
194 // Otherwise the Object Manager will have clobbered them by the
195 // time it gets to destroy the ORB Table, which calls our
196 // fini(). Had we destroyed the ORB in our fini(), its core
197 // fininalization would have required access to those already
198 // deleted resources.
199 if (!CORBA::is_nil (this->orb_.in ()))
201 this->orb_->destroy ();
204 catch (const CORBA::Exception& ex)
206 ex._tao_print_exception (ACE_TEXT ("Test_Server_Module::svc"));
207 return -1;
210 return 0;
214 ACE_STATIC_SVC_DEFINE (Test_Server_Module,
215 ACE_TEXT ("Test_Server_Module"),
216 ACE_SVC_OBJ_T,
217 &ACE_SVC_NAME (Test_Server_Module),
218 ACE_Service_Type::DELETE_THIS
219 | ACE_Service_Type::DELETE_OBJ,
222 ACE_FACTORY_DEFINE (Test_Server_Module, Test_Server_Module)