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"
12 Server_i::Server_i (void)
13 : ior_output_file_ (0)
18 Server_i::~Server_i (void)
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 (void)
80 // Create a new server object.
81 ACE_NEW_RETURN (this->time_service_server_impl_
,
82 TAO_Time_Service_Server
,
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_
,
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 ()"));
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"));
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
,
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 (
204 command
.get_TCHAR_argv(),
208 ORBSVCS_ERROR_RETURN ((LM_ERROR
,
210 ACE_TEXT("init_child_poa")),
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());
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:"));
244 // Run the event loop for ORB.
249 int retval
= this->orb_manager_
.run ();
252 ORBSVCS_ERROR_RETURN ((LM_ERROR
,
253 ACE_TEXT("[SERVER] Process/Thread Id : (%P/%t) Server_i::run")),