3 # include "Simple_util.h"
4 # include "tao/debug.h"
5 # include "ace/OS_NS_fcntl.h"
6 # include "ace/OS_NS_unistd.h"
7 # include "ace/OS_NS_stdio.h"
8 # include "ace/OS_NS_string.h"
12 template <class Servant
>
13 Server
<Servant
>::Server ()
14 : ior_output_file_ (0),
21 template <class Servant
>
22 Server
<Servant
>::~Server ()
26 // Parse the command-line arguments and set options.
28 template <class Servant
> int
29 Server
<Servant
>::parse_args ()
31 ACE_Get_Opt
get_opts (this->argc_
, this->argv_
, ACE_TEXT("do:m:"));
34 while ((c
= get_opts ()) != -1)
37 case 'd': // debug flag.
40 case 'o': // output the IOR to a file.
41 this->ior_output_file_
= ACE_OS::fopen (get_opts
.opt_arg (), "w");
42 if (this->ior_output_file_
== 0)
43 ACE_ERROR_RETURN ((LM_ERROR
,
44 "Unable to open %s for writing: %p\n",
45 get_opts
.opt_arg ()), -1);
49 this->mem_pool_name_
= get_opts
.opt_arg ();
51 case '?': // display help for use of the server.
53 ACE_ERROR_RETURN ((LM_ERROR
,
56 " [-o] <ior_output_file>"
62 // Indicates successful parsing of command line.
67 // Initialize the server.
68 template <class Servant
> int
69 Server
<Servant
>::init (const char *servant_name
,
73 // Call the init of <TAO_ORB_Manager> to initialize the ORB and
74 // create a child POA under the root POA.
75 if (this->orb_manager_
.init_child_poa (argc
,
78 ACE_ERROR_RETURN ((LM_ERROR
,
87 int retval
= this->parse_args ();
92 CORBA::ORB_var orb
= this->orb_manager_
.orb ();
94 // Stash our ORB pointer for later reference.
95 this->servant_
.orb (orb
.in ());
97 // Stash the memory pool name for reference
98 this->servant_
.pool_name (mem_pool_name_
);
100 // Activate the servant in its own child POA.
102 // Make sure that you check for failures here via the ACE_TRY
106 CORBA::String_var str
=
107 this->orb_manager_
.activate_under_child_poa (servant_name
,
110 ACE_DEBUG ((LM_DEBUG
,
111 "The IOR is: <%C>\n",
114 if (this->ior_output_file_
)
116 ACE_OS::fprintf (this->ior_output_file_
,
119 ACE_OS::fclose (this->ior_output_file_
);
123 catch (const CORBA::Exception
& ex
)
125 ex
._tao_print_exception ("\tException in activation of POA");
132 template <class Servant
> int
133 Server
<Servant
>::run ()
135 // Run the main event loop for the ORB.
136 int ret
= this->orb_manager_
.run ();
139 ACE_ERROR_RETURN ((LM_ERROR
,
147 /////////////////////////////////////////////////////////////////
148 // Client code Starts here
149 ////////////////////////////////////////////////////////////////
152 template <class InterfaceObj
, class Var
>
153 Client
<InterfaceObj
, Var
>::Client ()
159 // Reads the Server ior from a file
161 template <class InterfaceObj
, class Var
> int
162 Client
<InterfaceObj
, Var
>::read_ior (ACE_TCHAR
*filename
)
164 // Open the file for reading.
165 ACE_HANDLE f_handle
= ACE_OS::open (filename
, 0);
167 if (f_handle
== ACE_INVALID_HANDLE
)
168 ACE_ERROR_RETURN ((LM_ERROR
,
169 "Unable to open %s for writing: %p\n",
173 ACE_Read_Buffer
ior_buffer (f_handle
);
174 char *data
= ior_buffer
.read ();
177 ACE_ERROR_RETURN ((LM_ERROR
,
178 "Unable to read ior: %p\n"),
181 this->ior_
= ACE_OS::strdup (data
);
182 ior_buffer
.alloc ()->free (data
);
184 ACE_OS::close (f_handle
);
189 // Parses the command line arguments and returns an error status.
191 template <class InterfaceObj
, class Var
> int
192 Client
<InterfaceObj
, Var
>::parse_args ()
194 ACE_Get_Opt
get_opts (argc_
, argv_
, ACE_TEXT("df:k:x"));
198 while ((c
= get_opts ()) != -1)
201 case 'd': // debug flag
204 case 'k': // ior provide on command line
205 this->ior_
= ACE_OS::strdup (ACE_TEXT_ALWAYS_CHAR(get_opts
.opt_arg ()));
207 case 'f': // read the IOR from the file.
208 result
= this->read_ior (get_opts
.opt_arg ());
210 ACE_ERROR_RETURN ((LM_ERROR
,
211 "Unable to read ior from %s : %p\n",
212 get_opts
.opt_arg ()),
215 case 'x': // read the flag for shutting down
220 // Indicates successful parsing of command line.
224 template <class InterfaceObj
, class Var
>
225 Client
<InterfaceObj
, Var
>::~Client ()
227 ACE_OS::free (this->ior_
);
230 template <class InterfaceObj
, class Var
> int
231 Client
<InterfaceObj
, Var
>::init (const char * /*name*/,
241 this->orb_
= CORBA::ORB_init (this->argc_
, this->argv_
);
243 // Parse command line and verify parameters.
244 if (this->parse_args () == -1)
249 CORBA::Object_var server_object
=
250 this->orb_
->string_to_object (this->ior_
);
252 if (CORBA::is_nil (server_object
.in ()))
253 ACE_ERROR_RETURN ((LM_ERROR
,
254 "invalid ior <%C>\n",
257 this->server_
= InterfaceObj::_narrow (server_object
.in ());
260 ACE_ERROR_RETURN ((LM_ERROR
,
261 "no ior or naming options specified\n"),
264 catch (const CORBA::Exception
& ex
)
266 ex
._tao_print_exception ("Client_i::init");
275 template <class InterfaceObj
, class Var
> int
276 Client
<InterfaceObj
, Var
>::shutdown ()
278 // Returns the shutdwon flag
282 template <class InterfaceObj
, class Var
> void
283 Client
<InterfaceObj
, Var
>::shutdown (int flag
)