Cleanup ACE_HAS_PTHREAD_SIGMASK_PROTOTYPE, all platforms support it so far as I can...
[ACE_TAO.git] / TAO / orbsvcs / Time_Service / Server_i.cpp
blobc93028b2db6df5114f87091f4dd706e41d8ba445
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 ()
13 : ior_output_file_ (0)
17 // Destructor.
18 Server_i::~Server_i ()
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 ()
77 try
79 // Create a new server object.
80 ACE_NEW_RETURN (this->time_service_server_impl_,
81 TAO_Time_Service_Server,
82 0);
84 // Register a servant with the child poa.
85 CORBA::String_var server_str =
86 this->orb_manager_.activate_under_child_poa ("server",
87 this->time_service_server_impl_);
89 PortableServer::ObjectId_var id =
90 PortableServer::string_to_ObjectId ("server");
92 CORBA::Object_var server_ref =
93 this->orb_manager_.child_poa ()->id_to_reference (id.in ());
95 this->time_service_server_ = CosTime::TimeService::_narrow (server_ref.in ());
97 // All this !! just to register a servant with the child poa.
98 // Instead of using _this ().
100 //Convert the server reference to a string.
102 CORBA::String_var objref_server =
103 this->orb_->object_to_string (server_ref.in ());
105 // Print the server IOR on the console.
106 ORBSVCS_DEBUG ((LM_DEBUG,
107 ACE_TEXT("[SERVER] Process/Thread Id : (%P/%t) The Time Service ")
108 ACE_TEXT("SERVER IOR: <%C>\n"),
109 objref_server.in ()));
111 // Print the IOR to a file.
113 if (this->ior_output_file_)
115 // Write the IOR to the file.
116 ACE_OS::fprintf (this->ior_output_file_,
117 "%s\n",
118 objref_server.in ());
119 ACE_OS::fclose (this->ior_output_file_);
122 catch (const CORBA::Exception& ex)
124 ex._tao_print_exception (
125 ACE_TEXT ("Exception in Server_i::create_server ()"));
126 return -1;
128 return 0;
132 // Bind the Server in the context 'ServerContext' with the name
133 // 'Server:<hostname>'.
136 Server_i::register_server ()
140 CosNaming::Name server_context_name;
141 server_context_name.length (1);
142 server_context_name[0].id = CORBA::string_dup ("ServerContext");
146 CosNaming::NamingContext_var server_context =
147 this->naming_client_->bind_new_context(server_context_name);
149 catch (const CosNaming::NamingContext::AlreadyBound& )
151 // OK, naming context already exists.
154 char host_name[MAXHOSTNAMELEN];
155 char server_mc_name[MAXHOSTNAMELEN];
156 ACE_OS::hostname (host_name,MAXHOSTNAMELEN);
158 CosNaming::Name server_name (server_context_name);
160 server_name.length (2);
161 ACE_OS::strcpy (server_mc_name, "Server:");
162 ACE_OS::strcat (server_mc_name, host_name);
163 server_name[1].id = CORBA::string_dup (server_mc_name);
165 // Bind the compound name (ServerContext(Server:<hostname>))
166 // to the Naming Server.
168 this->naming_client_->rebind (server_name,
169 this->time_service_server_.in ());
171 ORBSVCS_DEBUG ((LM_DEBUG,
172 ACE_TEXT("Binding ServerContext -> %C\n"),
173 server_name[1].id.in ()));
175 catch (const CORBA::Exception& ex)
177 ex._tao_print_exception (
178 ACE_TEXT ("(%P|%t) Exception from Register Server ()\n"));
179 return -1;
182 return 0;
185 // Initialize the server. If a filename is specified with the -f
186 // commandline option, the server writes its IOR to the file besides
187 // binding itself with the Naming Service.
190 Server_i::init (int argc,
191 ACE_TCHAR *argv[])
195 // Make a copy of command line parameter.
196 ACE_Argv_Type_Converter command(argc, argv);
198 // Call the init of <TAO_ORB_Manager> to initialize the ORB and
199 // create a child POA under the root POA.
201 int retval = this->orb_manager_.init_child_poa (
202 command.get_argc(),
203 command.get_TCHAR_argv(),
204 "time_server");
206 if (retval == -1)
207 ORBSVCS_ERROR_RETURN ((LM_ERROR,
208 ACE_TEXT("%p\n"),
209 ACE_TEXT("init_child_poa")),
210 -1);
212 // Activate the POA Manager.
213 this->orb_manager_.activate_poa_manager ();
215 int result = this->parse_args (command.get_argc(), command.get_TCHAR_argv());
217 if (result != 0)
218 return result;
220 // Get the orb.
221 this->orb_ = this->orb_manager_.orb ();
223 // Initialize Naming Service.
224 this->init_naming_service ();
226 // Create the server object.
227 this->create_server ();
229 // Register the server object with the Naming Service.
230 this->register_server ();
232 catch (const CORBA::Exception& ex)
234 ex._tao_print_exception (ACE_TEXT("Exception:"));
235 return -1;
238 return 0;
242 // Run the event loop for ORB.
245 Server_i::run ()
247 int retval = this->orb_manager_.run ();
249 if (retval == -1)
250 ORBSVCS_ERROR_RETURN ((LM_ERROR,
251 ACE_TEXT("[SERVER] Process/Thread Id : (%P/%t) Server_i::run")),
252 -1);
253 return 0;