Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / orbsvcs / Time_Service / Server_i.cpp
blobef6c3b7aa7ee65110b3e053423fba31c1cd8c82a
1 #include "orbsvcs/Log_Macros.h"
2 #include "Server_i.h"
3 #include "tao/debug.h"
4 #include "ace/Get_Opt.h"
5 #include "ace/Argv_Type_Converter.h"
6 #include "ace/OS_NS_stdio.h"
7 #include "ace/OS_NS_unistd.h"
8 #include "ace/OS_NS_string.h"
9 #include "ace/os_include/os_netdb.h"
11 // Constructor.
12 Server_i::Server_i (void)
13 : ior_output_file_ (0)
17 // Destructor.
18 Server_i::~Server_i (void)
22 // Parse the command-line arguments and set options.
23 int
24 Server_i::parse_args (int argc,
25 ACE_TCHAR* argv[])
27 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("do:"));
28 int c;
30 while ((c = get_opts ()) != -1)
31 switch (c)
33 case 'd': // debug flag.
34 TAO_debug_level++;
35 break;
36 case 'o': // output the IOR to a file.
37 this->ior_output_file_ =
38 ACE_OS::fopen (get_opts.opt_arg (), ACE_TEXT("a"));
40 if (this->ior_output_file_ == 0)
41 ORBSVCS_ERROR_RETURN ((LM_ERROR,
42 ACE_TEXT("[SERVER] Process/Thread Id : (%P/%t)Unable to open %s for writing: %p\n"),
43 get_opts.opt_arg ()), -1);
44 break;
45 case '?': // display help for use of the server.
46 default:
47 ORBSVCS_ERROR_RETURN ((LM_ERROR,
48 ACE_TEXT("[SERVER] Process/Thread Id : (%P/%t)")
49 ACE_TEXT("usage: %s")
50 ACE_TEXT(" [-d]")
51 ACE_TEXT(" [-o] <ior_output_file>")
52 ACE_TEXT("\n"),
53 argv[0]),
54 1);
57 // Indicates successful parsing of command line.
58 return 0;
61 // Initialise the Naming Service and register the TimeService Object
62 // with it.
64 int
65 Server_i::init_naming_service ()
67 // Initialize the Naming Client.
68 return (this->naming_client_.init (this->orb_.in ()));
71 // Create a new time server object and register it with the child POA.
72 // Print the IOR of the registered server on the console and in a file.
74 int
75 Server_i::create_server (void)
77 try
80 // Create a new server object.
81 ACE_NEW_RETURN (this->time_service_server_impl_,
82 TAO_Time_Service_Server,
83 0);
85 // Register a servant with the child poa.
86 CORBA::String_var server_str =
87 this->orb_manager_.activate_under_child_poa ("server",
88 this->time_service_server_impl_);
90 PortableServer::ObjectId_var id =
91 PortableServer::string_to_ObjectId ("server");
93 CORBA::Object_var server_ref =
94 this->orb_manager_.child_poa ()->id_to_reference (id.in ());
96 this->time_service_server_ = CosTime::TimeService::_narrow (server_ref.in ());
98 // All this !! just to register a servant with the child poa.
99 // Instead of using _this ().
101 //Convert the server reference to a string.
103 CORBA::String_var objref_server =
104 this->orb_->object_to_string (server_ref.in ());
106 // Print the server IOR on the console.
107 ORBSVCS_DEBUG ((LM_DEBUG,
108 ACE_TEXT("[SERVER] Process/Thread Id : (%P/%t) The Time Service ")
109 ACE_TEXT("SERVER IOR: <%C>\n"),
110 objref_server.in ()));
112 // Print the IOR to a file.
114 if (this->ior_output_file_)
116 // Write the IOR to the file.
117 ACE_OS::fprintf (this->ior_output_file_,
118 "%s\n",
119 objref_server.in ());
120 ACE_OS::fclose (this->ior_output_file_);
123 catch (const CORBA::Exception& ex)
125 ex._tao_print_exception (
126 ACE_TEXT ("Exception in Server_i::create_server ()"));
127 return -1;
129 return 0;
133 // Bind the Server in the context 'ServerContext' with the name
134 // 'Server:<hostname>'.
137 Server_i::register_server (void)
141 CosNaming::Name server_context_name;
142 server_context_name.length (1);
143 server_context_name[0].id = CORBA::string_dup ("ServerContext");
147 CosNaming::NamingContext_var server_context =
148 this->naming_client_->bind_new_context(server_context_name);
150 catch (const CosNaming::NamingContext::AlreadyBound& )
152 // OK, naming context already exists.
155 char host_name[MAXHOSTNAMELEN];
156 char server_mc_name[MAXHOSTNAMELEN];
157 ACE_OS::hostname (host_name,MAXHOSTNAMELEN);
159 CosNaming::Name server_name (server_context_name);
161 server_name.length (2);
162 ACE_OS::strcpy (server_mc_name, "Server:");
163 ACE_OS::strcat (server_mc_name, host_name);
164 server_name[1].id = CORBA::string_dup (server_mc_name);
166 // Bind the compound name (ServerContext(Server:<hostname>))
167 // to the Naming Server.
169 this->naming_client_->rebind (server_name,
170 this->time_service_server_.in ());
172 ORBSVCS_DEBUG ((LM_DEBUG,
173 ACE_TEXT("Binding ServerContext -> %C\n"),
174 server_name[1].id.in ()));
176 catch (const CORBA::Exception& ex)
178 ex._tao_print_exception (
179 ACE_TEXT ("(%P|%t) Exception from Register Server ()\n"));
180 return -1;
183 return 0;
186 // Initialize the server. If a filename is specified with the -f
187 // commandline option, the server writes its IOR to the file besides
188 // binding itself with the Naming Service.
191 Server_i::init (int argc,
192 ACE_TCHAR *argv[])
196 // Make a copy of command line parameter.
197 ACE_Argv_Type_Converter command(argc, argv);
199 // Call the init of <TAO_ORB_Manager> to initialize the ORB and
200 // create a child POA under the root POA.
202 int retval = this->orb_manager_.init_child_poa (
203 command.get_argc(),
204 command.get_TCHAR_argv(),
205 "time_server");
207 if (retval == -1)
208 ORBSVCS_ERROR_RETURN ((LM_ERROR,
209 ACE_TEXT("%p\n"),
210 ACE_TEXT("init_child_poa")),
211 -1);
213 // Activate the POA Manager.
214 this->orb_manager_.activate_poa_manager ();
216 int result = this->parse_args (command.get_argc(), command.get_TCHAR_argv());
218 if (result != 0)
219 return result;
221 // Get the orb.
222 this->orb_ = this->orb_manager_.orb ();
224 // Initialize Naming Service.
225 this->init_naming_service ();
227 // Create the server object.
228 this->create_server ();
230 // Register the server object with the Naming Service.
231 this->register_server ();
234 catch (const CORBA::Exception& ex)
236 ex._tao_print_exception (ACE_TEXT("Exception:"));
237 return -1;
240 return 0;
244 // Run the event loop for ORB.
247 Server_i::run (void)
249 int retval = this->orb_manager_.run ();
251 if (retval == -1)
252 ORBSVCS_ERROR_RETURN ((LM_ERROR,
253 ACE_TEXT("[SERVER] Process/Thread Id : (%P/%t) Server_i::run")),
254 -1);
255 return 0;