1 // This version of Simple_Util doesn't need to link against orbsvcs
2 // On the other hand it has no naming service.
6 # include "Simple_util.h"
7 # include "tao/debug.h"
8 # include "ace/OS_NS_stdio.h"
9 # include "ace/OS_NS_fcntl.h"
10 # include "ace/OS_NS_unistd.h"
11 # include "ace/OS_NS_string.h"
15 template <class Servant
>
16 Server
<Servant
>::Server (void)
17 : ior_output_file_ (0)
22 template <class Servant
>
23 Server
<Servant
>::~Server (void)
27 // Parse the command-line arguments and set options.
29 template <class Servant
> int
30 Server
<Servant
>::parse_args (void)
32 ACE_Get_Opt
get_opts (this->argc_
, this->argv_
, ACE_TEXT("do:ni:"));
35 while ((c
= get_opts ()) != -1)
38 case 'd': // debug flag.
41 case 'o': // output the IOR to a file.
42 this->ior_output_file_
= ACE_OS::fopen (get_opts
.opt_arg (), "w");
43 if (this->ior_output_file_
== 0)
44 ACE_ERROR_RETURN ((LM_ERROR
,
45 "Unable to open %s for writing: %p\n",
46 get_opts
.opt_arg ()), -1);
49 case '?': // display help for use of the server.
51 ACE_ERROR_RETURN ((LM_ERROR
,
54 " [-o] <ior_output_file>"
60 // Indicates successful parsing of command line.
64 // Initialize the server.
65 template <class Servant
> int
66 Server
<Servant
>::init (const char *servant_name
,
70 // Call the init of <TAO_ORB_Manager> to initialize the ORB and
71 // create a child POA under the root POA.
72 if (this->orb_manager_
.init_child_poa (argc
,
75 ACE_ERROR_RETURN ((LM_ERROR
,
84 int retval
= this->parse_args ();
89 CORBA::ORB_var orb
= this->orb_manager_
.orb ();
91 // Stash our ORB pointer for later reference.
92 this->servant_
.orb (orb
.in ());
94 // Activate the servant in its own child POA.
96 // Make sure that you check for failures here via the ACE_TRY
100 CORBA::String_var str
=
101 this->orb_manager_
.activate_under_child_poa (servant_name
,
104 ACE_DEBUG ((LM_DEBUG
,
105 "The IOR is: <%s>\n",
108 if (this->ior_output_file_
)
110 ACE_OS::fprintf (this->ior_output_file_
,
113 ACE_OS::fclose (this->ior_output_file_
);
117 catch (const CORBA::Exception
& ex
)
119 ex
._tao_print_exception ("Exception in activation of POA");
126 template <class Servant
>int
127 Server
<Servant
>::run (void)
129 // Run the main event loop for the ORB.
130 if (this->orb_manager_
.run () == -1)
131 ACE_ERROR_RETURN ((LM_ERROR
,
138 template <class Servant
> int
139 Server
<Servant
>::register_name (void)
146 template <class InterfaceObj
, class Var
>
147 Client
<InterfaceObj
, Var
>::Client (void)
153 // Reads the Server ior from a file
155 template <class InterfaceObj
, class Var
> int
156 Client
<InterfaceObj
, Var
>::read_ior (ACE_TCHAR
*filename
)
158 // Open the file for reading.
159 ACE_HANDLE f_handle
= ACE_OS::open (filename
, 0);
161 if (f_handle
== ACE_INVALID_HANDLE
)
162 ACE_ERROR_RETURN ((LM_ERROR
,
163 "Unable to open %s for writing: %p\n",
167 ACE_Read_Buffer
ior_buffer (f_handle
);
168 char *data
= ior_buffer
.read ();
171 ACE_ERROR_RETURN ((LM_ERROR
,
172 "Unable to read ior: %p\n"),
175 this->ior_
= ACE_OS::strdup (data
);
176 ior_buffer
.alloc ()->free (data
);
178 ACE_OS::close (f_handle
);
183 // Parses the command line arguments and returns an error status.
185 template <class InterfaceObj
, class Var
> int
186 Client
<InterfaceObj
, Var
>::parse_args (void)
188 ACE_Get_Opt
get_opts (argc_
, argv_
, ACE_TEXT("df:nk:x"));
192 while ((c
= get_opts ()) != -1)
195 case 'd': // debug flag
198 case 'k': // ior provide on command line
199 this->ior_
= ACE_OS::strdup (ACE_TEXT_ALWAYS_CHAR(get_opts
.opt_arg ()));
201 case 'f': // read the IOR from the file.
202 result
= this->read_ior (get_opts
.opt_arg ());
204 ACE_ERROR_RETURN ((LM_ERROR
,
205 "Unable to read ior from %s : %p\n",
206 get_opts
.opt_arg ()),
209 case 'x': // read the flag for shutting down
214 // Indicates successful parsing of command line.
218 template <class InterfaceObj
, class Var
>
219 Client
<InterfaceObj
, Var
>::~Client (void)
221 ACE_OS::free (this->ior_
);
224 template <class InterfaceObj
, class Var
> int
225 Client
<InterfaceObj
, Var
>::init (const char *name
,
237 this->orb_
= CORBA::ORB_init (this->argc_
,
241 // Parse command line and verify parameters.
242 if (this->parse_args () == -1)
247 CORBA::Object_var server_object
=
248 this->orb_
->string_to_object (this->ior_
);
251 if (CORBA::is_nil (server_object
.in ()))
252 ACE_ERROR_RETURN ((LM_ERROR
,
253 "invalid ior <%s>\n",
256 this->server_
= InterfaceObj::_narrow (server_object
.in ());
259 ACE_ERROR_RETURN ((LM_ERROR
,
260 "no ior or naming options specified\n"),
265 catch (const CORBA::Exception
& ex
)
267 ex
._tao_print_exception ("Client_i::init");
276 template <class InterfaceObj
, class Var
> int
277 Client
<InterfaceObj
, Var
>::obtain_initial_references (void)
283 template <class InterfaceObj
, class Var
> int
284 Client
<InterfaceObj
, Var
>::shutdown (void )
286 // Returns the shutdwon flag
290 template <class InterfaceObj
, class Var
> void
291 Client
<InterfaceObj
, Var
>::shutdown (int flag
)