1 #include "orbsvcs/Log_Macros.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"
13 : ior_output_file_ (0)
18 Server_i::~Server_i ()
22 // Parse the command-line arguments and set options.
24 Server_i::parse_args (int argc
,
27 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("do:"));
30 while ((c
= get_opts ()) != -1)
33 case 'd': // debug flag.
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);
45 case '?': // display help for use of the server.
47 ORBSVCS_ERROR_RETURN ((LM_ERROR
,
48 ACE_TEXT("[SERVER] Process/Thread Id : (%P/%t)")
51 ACE_TEXT(" [-o] <ior_output_file>")
57 // Indicates successful parsing of command line.
61 // Initialise the Naming Service and register the TimeService Object
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.
75 Server_i::create_server ()
79 // Create a new server object.
80 ACE_NEW_RETURN (this->time_service_server_impl_
,
81 TAO_Time_Service_Server
,
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_
,
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 ()"));
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"));
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
,
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 (
203 command
.get_TCHAR_argv(),
207 ORBSVCS_ERROR_RETURN ((LM_ERROR
,
209 ACE_TEXT("init_child_poa")),
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());
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:"));
242 // Run the event loop for ORB.
247 int retval
= this->orb_manager_
.run ();
250 ORBSVCS_ERROR_RETURN ((LM_ERROR
,
251 ACE_TEXT("[SERVER] Process/Thread Id : (%P/%t) Server_i::run")),