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 (void)
14 : ior_output_file_ (0),
21 template <class Servant
>
22 Server
<Servant
>::~Server (void)
26 // Parse the command-line arguments and set options.
28 template <class Servant
> int
29 Server
<Servant
>::parse_args (void)
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 (void)
135 // Run the main event loop for the ORB.
136 int ret
= this->orb_manager_
.run ();
139 ACE_ERROR_RETURN ((LM_ERROR
,
148 /////////////////////////////////////////////////////////////////
149 // Client code Starts here
150 ////////////////////////////////////////////////////////////////
153 template <class InterfaceObj
, class Var
>
154 Client
<InterfaceObj
, Var
>::Client (void)
160 // Reads the Server ior from a file
162 template <class InterfaceObj
, class Var
> int
163 Client
<InterfaceObj
, Var
>::read_ior (ACE_TCHAR
*filename
)
165 // Open the file for reading.
166 ACE_HANDLE f_handle
= ACE_OS::open (filename
, 0);
168 if (f_handle
== ACE_INVALID_HANDLE
)
169 ACE_ERROR_RETURN ((LM_ERROR
,
170 "Unable to open %s for writing: %p\n",
174 ACE_Read_Buffer
ior_buffer (f_handle
);
175 char *data
= ior_buffer
.read ();
178 ACE_ERROR_RETURN ((LM_ERROR
,
179 "Unable to read ior: %p\n"),
182 this->ior_
= ACE_OS::strdup (data
);
183 ior_buffer
.alloc ()->free (data
);
185 ACE_OS::close (f_handle
);
190 // Parses the command line arguments and returns an error status.
192 template <class InterfaceObj
, class Var
> int
193 Client
<InterfaceObj
, Var
>::parse_args (void)
195 ACE_Get_Opt
get_opts (argc_
, argv_
, ACE_TEXT("df:k:x"));
199 while ((c
= get_opts ()) != -1)
202 case 'd': // debug flag
205 case 'k': // ior provide on command line
206 this->ior_
= ACE_OS::strdup (ACE_TEXT_ALWAYS_CHAR(get_opts
.opt_arg ()));
208 case 'f': // read the IOR from the file.
209 result
= this->read_ior (get_opts
.opt_arg ());
211 ACE_ERROR_RETURN ((LM_ERROR
,
212 "Unable to read ior from %s : %p\n",
213 get_opts
.opt_arg ()),
216 case 'x': // read the flag for shutting down
221 // Indicates successful parsing of command line.
225 template <class InterfaceObj
, class Var
>
226 Client
<InterfaceObj
, Var
>::~Client (void)
228 ACE_OS::free (this->ior_
);
231 template <class InterfaceObj
, class Var
> int
232 Client
<InterfaceObj
, Var
>::init (const char * /*name*/,
242 this->orb_
= CORBA::ORB_init (this->argc_
, this->argv_
);
244 // Parse command line and verify parameters.
245 if (this->parse_args () == -1)
250 CORBA::Object_var server_object
=
251 this->orb_
->string_to_object (this->ior_
);
253 if (CORBA::is_nil (server_object
.in ()))
254 ACE_ERROR_RETURN ((LM_ERROR
,
255 "invalid ior <%C>\n",
258 this->server_
= InterfaceObj::_narrow (server_object
.in ());
261 ACE_ERROR_RETURN ((LM_ERROR
,
262 "no ior or naming options specified\n"),
267 catch (const CORBA::Exception
& ex
)
269 ex
._tao_print_exception ("Client_i::init");
278 template <class InterfaceObj
, class Var
> int
279 Client
<InterfaceObj
, Var
>::shutdown (void)
281 // Returns the shutdwon flag
285 template <class InterfaceObj
, class Var
> void
286 Client
<InterfaceObj
, Var
>::shutdown (int flag
)