Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / examples / Simple / time-date / Time_Date.cpp
blobd1f3f80610750e9bbf713ff7dae3b0962ff9ccb0
1 #ifndef ACE_BUILD_SVC_DLL
2 # define ACE_BUILD_SVC_DLL
3 #endif
5 #ifndef Alt_Resource_Factory_BUILD_DLL
6 # define Alt_Resource_Factory_BUILD_DLL
7 #endif
9 #include "Time_Date_i.h"
11 #include "Time_Date.h"
12 #include "tao/TAO_Singleton_Manager.h"
13 #include "tao/debug.h"
14 #include "ace/Reactor.h"
15 #include "ace/Get_Opt.h"
16 #include "ace/Dynamic_Service.h"
19 ACE_Reactor *
20 My_Resource_Factory::get_reactor ()
22 #if defined (ACE_HAS_THREADS)
23 // Use whatever the default is if we've got threads.
24 return TAO_Default_Resource_Factory::get_reactor ();
25 #else
26 // Just use the Singleton Reactor.
27 return ACE_Reactor::instance ();
28 #endif /* ACE_HAS_THREADS */
31 ACE_FACTORY_DEFINE (Alt_Resource_Factory, My_Resource_Factory)
33 int
34 DLL_ORB::svc ()
36 ACE_DEBUG ((LM_DEBUG,
37 ACE_TEXT ("\n\tRunning ORB event loop (%t)\n\n")));
39 try
41 // Run the ORB event loop in its own thread.
42 this->orb_->run ();
44 catch (const CORBA::Exception& ex)
46 ex._tao_print_exception ("Exception in DLL_ORB::svc");
47 return -1;
50 return 0;
53 int
54 DLL_ORB::init (int argc, ACE_TCHAR *argv[])
56 // Prevent TAO from registering with the ACE_Object_Manager so
57 // that it can be dynamically unloaded successfully.
59 int register_with_object_manager = 0;
60 if (TAO_Singleton_Manager::instance ()->init (register_with_object_manager) == -1)
62 // ACE_ERROR_RETURN ((LM_ERROR,
63 // ACE_TEXT ("%p\n"),
64 // ACE_TEXT ("Unable to pre-initialize ORB")),
65 // -1);
68 try
70 ACE_DEBUG ((LM_DEBUG,
71 ACE_TEXT ("\n\tInitialize ORB (%t)\n\n")));
73 // Initialize the ORB.
74 this->orb_ = CORBA::ORB_init (argc,
75 argv,
76 "An ORB");
78 if (CORBA::is_nil (this->orb_.in ()))
79 return -1;
81 CORBA::Object_var poa_object =
82 this->orb_->resolve_initial_references ("RootPOA");
84 if (CORBA::is_nil (poa_object.in ()))
85 ACE_ERROR_RETURN ((LM_ERROR,
86 ACE_TEXT (" (%P|%t) Unable to initialize the POA.\n")),
87 1);
89 this->poa_ =
90 PortableServer::POA::_narrow (poa_object.in ());
92 this->poa_manager_ =
93 this->poa_->the_POAManager ();
95 this->poa_manager_->activate ();
97 #if defined (ACE_HAS_THREADS)
98 // Become an Active Object so that the ORB
99 // will execute in a separate thread.
100 return this->activate ();
101 #else
102 return 0;
103 #endif /* ACE_HAS_THREADS */
105 catch (const CORBA::Exception& ex)
107 ex._tao_print_exception ("DLL_ORB::init");
110 return -1;
114 DLL_ORB::fini ()
116 ACE_DEBUG ((LM_DEBUG,
117 ACE_TEXT ("\n\tFinalizing the service (%t)\n\n")));
118 return TAO_Singleton_Manager::instance ()->fini ();
119 // return 0;
122 Time_Date_Servant::Time_Date_Servant ()
123 : servant_ (0)
124 , ior_output_file_ (0)
125 , orb_ ("")
130 Time_Date_Servant::parse_args (int argc, ACE_TCHAR *argv[])
132 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT ("dn:o:"));
133 int c = 0;
135 this->orb_ = "ORB";
137 while ((c = get_opts ()) != -1)
138 switch (c)
140 case 'd': // debug flag.
141 TAO_debug_level++;
142 break;
143 case 'o': // output the IOR to a file.
144 this->ior_output_file_ = get_opts.opt_arg ();
145 break;
146 // Find the ORB in the Service Repository.
147 case 'n':
148 this->orb_ = ACE_TEXT_ALWAYS_CHAR(get_opts.opt_arg ());
149 break;
150 case '?': // display help for use of the server.
151 default:
152 ACE_ERROR_RETURN ((LM_ERROR,
153 ACE_TEXT ("usage: %s")
154 ACE_TEXT (" [-d]")
155 ACE_TEXT (" [-o] <ior_output_file>")
156 ACE_TEXT ("\n"),
157 argv [0]),
158 -1);
161 // Indicates successful parsing of command line.
162 return 0;
166 Time_Date_Servant::init (int argc, ACE_TCHAR *argv[])
170 ACE_DEBUG ((LM_DEBUG,
171 ACE_TEXT ("\n\tTime_Date servant\n\n")));
173 this->parse_args (argc, argv);
175 DLL_ORB *orb =
176 ACE_Dynamic_Service<DLL_ORB>::instance (this->orb_.c_str ());
178 if (orb == 0)
179 ACE_ERROR_RETURN ((LM_ERROR,
180 ACE_TEXT ("can't find %C in the Service Repository\n"),
181 this->orb_.c_str ()),
182 -1);
184 Time_Date_i * servant = 0;
185 ACE_NEW_THROW_EX (servant,
186 Time_Date_i,
187 CORBA::NO_MEMORY ());
188 PortableServer::ServantBase_var safe_servant (servant);
189 servant->orb (orb->orb_.in ());
191 CORBA::Object_var poa_object =
192 orb->orb_->resolve_initial_references("RootPOA");
194 PortableServer::POA_var root_poa =
195 PortableServer::POA::_narrow (poa_object.in ());
197 PortableServer::ObjectId_var id =
198 root_poa->activate_object (servant);
200 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
202 CORBA::Object_var obj =
203 CORBA::Object::_narrow (object.in ());
205 CORBA::String_var str =
206 orb->orb_->object_to_string (obj.in ());
208 if (this->ior_output_file_)
210 FILE *output_file = ACE_OS::fopen (this->ior_output_file_, "w");
211 if (output_file == 0)
212 ACE_ERROR_RETURN ((LM_ERROR,
213 ACE_TEXT ("Unable to open %s for writing (%p)\n"),
214 this->ior_output_file_,
215 ACE_TEXT ("fopen")),
216 -1);
217 ACE_OS::fprintf (output_file,
218 "%s",
219 str.in ());
220 ACE_OS::fclose (output_file);
223 catch (const CORBA::Exception& ex)
225 ex._tao_print_exception ("DLL_ORB::init");
226 return -1;
228 return 0;
231 // The following Factory is used by the <ACE_Service_Config> and
232 // dll_orb.conf file to dynamically initialize the state of the
233 // Time_Date service.
234 ACE_SVC_FACTORY_DEFINE (DLL_ORB)
235 ACE_SVC_FACTORY_DEFINE (Time_Date_Servant)