4 #include "Simple_util.h"
5 #include "tao/IORTable/IORTable.h"
7 #include "ace/OS_NS_stdio.h"
8 #include "ace/OS_NS_unistd.h"
9 #include "ace/OS_NS_string.h"
10 #include "ace/OS_NS_fcntl.h"
13 template <class Servant
>
14 Server
<Servant
>::Server ()
16 , ior_output_file_ (0)
20 ACE_NEW_THROW_EX (tmp
,
27 template <class Servant
>
28 Server
<Servant
>::~Server ()
32 // Parse the command-line arguments and set options.
33 template <class Servant
> int
34 Server
<Servant
>::parse_args ()
36 ACE_Get_Opt
get_opts (this->argc_
, this->argv_
,
37 ACE_TEXT ("do:i:"), 1, 0,
38 ACE_Get_Opt::REQUIRE_ORDER
);
41 while ((c
= get_opts ()) != -1)
44 case 'd': // debug flag.
47 case 'o': // output the IOR to a file.
48 this->ior_output_file_
= get_opts
.opt_arg ();
50 case 'i': // For Testing the InterOperable Naming Service.
51 this->ins_
= get_opts
.opt_arg ();
55 // Indicates successful parsing of command line.
59 // Add the ObjectID:IOR mapping to the IOR table of
60 // the ORB. Ignore this method if you are not testing for
61 // the InterOperable Naming Service.
62 template <class Servant
> int
63 Server
<Servant
>::test_for_ins (const char *ior
)
65 CORBA::ORB_var orb
= this->orb_manager_
.orb ();
67 if (TAO_debug_level
> 0)
69 ACE_TEXT ("Adding (KEY:IOR) %s:%C\n"),
75 CORBA::Object_var table_object
=
76 orb
->resolve_initial_references ("IORTable");
78 IORTable::Table_var adapter
=
79 IORTable::Table::_narrow (table_object
.in ());
80 if (CORBA::is_nil (adapter
.in ()))
82 ACE_ERROR_RETURN ((LM_ERROR
,
83 ACE_TEXT ("Nil IORTable\n")),
87 adapter
->bind (ACE_TEXT_ALWAYS_CHAR (this->ins_
), ior
);
89 catch (const CORBA::Exception
& ex
)
91 ex
._tao_print_exception ("ERROR: test_for_ins failed\n");
98 // Initialize the server.
99 template <class Servant
> int
100 Server
<Servant
>::init (const char *servant_name
,
104 // Call the init of <TAO_ORB_Manager> to initialize the ORB and
105 // create a child POA under the root POA.
106 if (this->orb_manager_
.init_child_poa (argc
,
109 ACE_ERROR_RETURN ((LM_ERROR
,
111 ACE_TEXT ("init_child_poa")),
117 int retval
= this->parse_args ();
122 CORBA::ORB_var orb
= this->orb_manager_
.orb ();
124 // Stash our ORB pointer for later reference.
125 this->servant_
->orb (orb
.in ());
127 // Activate the servant in its own child POA.
129 // Make sure that you check for failures here via the try?!
132 CORBA::String_var str
=
133 this->orb_manager_
.activate_under_child_poa (servant_name
,
134 this->servant_
.in ());
136 ACE_DEBUG ((LM_DEBUG
,
137 ACE_TEXT ("The IOR is: <%C>\n"),
140 if (this->ins_
&& this->test_for_ins (str
.in ()) != 0)
141 ACE_ERROR_RETURN ((LM_ERROR
,
142 ACE_TEXT ("test_for_ins (): failed\n")),
145 if (this->ior_output_file_
)
147 FILE *fh
= ACE_OS::fopen (this->ior_output_file_
, "w");
149 ACE_ERROR_RETURN ((LM_ERROR
,
150 ACE_TEXT ("Unable to open %s for writing (%p)\n"),
151 this->ior_output_file_
,
154 ACE_OS::fprintf (fh
, "%s", str
.in ());
158 catch (const CORBA::Exception
& ex
)
160 ex
._tao_print_exception ("\tException in activation of POA");
167 template <class Servant
>int
168 Server
<Servant
>::run ()
170 // Run the main event loop for the ORB.
171 if (this->orb_manager_
.run () == -1)
172 ACE_ERROR_RETURN ((LM_ERROR
,
173 ACE_TEXT ("Server_i::run")),
180 template <class ServerInterface
>
181 Client
<ServerInterface
>::Client ()
188 // Reads the Server ior from a file
189 template <class ServerInterface
> int
190 Client
<ServerInterface
>::read_ior (ACE_TCHAR
*filename
)
192 // Open the file for reading.
193 ACE_HANDLE f_handle
= ACE_OS::open (filename
, 0);
195 if (f_handle
== ACE_INVALID_HANDLE
)
196 ACE_ERROR_RETURN ((LM_ERROR
,
197 ACE_TEXT ("Unable to open %s for writing (%p)\n"),
202 ACE_Read_Buffer
ior_buffer (f_handle
);
203 char *data
= ior_buffer
.read ();
206 ACE_ERROR_RETURN ((LM_ERROR
,
207 ACE_TEXT ("Unable to read ior (%p)\n"),
212 ior_buffer
.alloc ()->free (data
);
214 ACE_OS::close (f_handle
);
219 // Parses the command line arguments and returns an error status.
220 template <class ServerInterface
> int
221 Client
<ServerInterface
>::parse_args ()
223 ACE_Get_Opt
get_opts (argc_
, argv_
,
224 ACE_TEXT ("df:nk:x"), 1, 0,
225 ACE_Get_Opt::REQUIRE_ORDER
);
229 while ((c
= get_opts ()) != -1)
232 case 'd': // debug flag
235 case 'k': // ior provide on command line
236 this->ior_
= ACE_TEXT_ALWAYS_CHAR(get_opts
.opt_arg ());
238 case 'f': // read the IOR from the file.
239 result
= this->read_ior (get_opts
.opt_arg ());
241 ACE_ERROR_RETURN ((LM_ERROR
,
242 ACE_TEXT ("Unable to read ior from %s (%p)\n"),
244 ACE_TEXT ("read_ior")),
247 case 'x': // read the flag for shutting down
248 this->do_shutdown_
= 1;
250 case 'h': // display help for use of the client.
251 ACE_ERROR_RETURN ((LM_ERROR
,
252 ACE_TEXT ("usage: %s")
253 ACE_TEXT (" [-d (debug)]")
254 ACE_TEXT (" [-k] <ior>")
255 ACE_TEXT (" [-f] <ior_output_file>")
256 ACE_TEXT (" [-x (shutdown server)]")
257 ACE_TEXT (" [-h (help)]")
263 // Indicates successful parsing of command line.
267 template <class ServerInterface
>
268 Client
<ServerInterface
>::~Client ()
270 this->orb_
->destroy ();
273 template <class ServerInterface
> int
274 Client
<ServerInterface
>::init (const char *,
284 this->orb_
= CORBA::ORB_init (this->argc_
, this->argv_
);
286 // Parse command line and verify parameters.
287 if (this->parse_args () == -1)
290 if (this->ior_
.length () != 0)
292 CORBA::Object_var server_object
=
293 this->orb_
->string_to_object (this->ior_
.c_str ());
295 if (CORBA::is_nil (server_object
.in ()))
296 ACE_ERROR_RETURN ((LM_ERROR
,
297 ACE_TEXT ("invalid ior <%C>\n"),
298 this->ior_
.c_str ()),
300 this->server_
= ServerInterface::_narrow (server_object
.in ());
301 if (CORBA::is_nil (this->server_
.in ()))
303 ACE_ERROR_RETURN ((LM_ERROR
,
304 ACE_TEXT ("Nil Server\n")),
309 ACE_ERROR_RETURN ((LM_ERROR
,
310 ACE_TEXT ("no ior or naming options specified\n")),
313 catch (const CORBA::Exception
& ex
)
315 ex
._tao_print_exception ("Client_i::init");
322 template <class ServerInterface
> int
323 Client
<ServerInterface
>::do_shutdown ()
325 // Returns the shutdwon flag
326 return this->do_shutdown_
;
329 template <class ServerInterface
> void
330 Client
<ServerInterface
>::do_shutdown (int flag
)
333 this->do_shutdown_
= flag
;
336 #endif /* SIMPLE_UTIL_C */