=default for generated implementation copy ctor
[ACE_TAO.git] / TAO / examples / OBV / Simple_util.cpp
blobf760acfdf4c754450b359d5290346854358b3dd3
1 // This version of Simple_Util doesn't need to link against orbsvcs
2 // On the other hand it has no naming service.
4 #ifndef SIMPLE_UTIL_C
5 #define SIMPLE_UTIL_C
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"
13 // Constructor.
15 template <class Servant>
16 Server<Servant>::Server ()
17 : ior_output_file_ (0)
21 // Destructor.
22 template <class Servant>
23 Server<Servant>::~Server ()
27 // Parse the command-line arguments and set options.
29 template <class Servant> int
30 Server<Servant>::parse_args ()
32 ACE_Get_Opt get_opts (this->argc_, this->argv_, ACE_TEXT("do:ni:"));
33 int c = 0;
35 while ((c = get_opts ()) != -1)
36 switch (c)
38 case 'd': // debug flag.
39 TAO_debug_level++;
40 break;
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);
47 break;
49 case '?': // display help for use of the server.
50 default:
51 ACE_ERROR_RETURN ((LM_ERROR,
52 "usage: %s"
53 " [-d]"
54 " [-o] <ior_output_file>"
55 "\n",
56 argv_ [0]),
57 -1);
60 // Indicates successful parsing of command line.
61 return 0;
64 // Initialize the server.
65 template <class Servant> int
66 Server<Servant>::init (const char *servant_name,
67 int argc,
68 ACE_TCHAR *argv[])
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,
73 argv,
74 "child_poa") == -1)
75 ACE_ERROR_RETURN ((LM_ERROR,
76 "%p\n",
77 "init_child_poa"),
78 -1);
81 this->argc_ = argc;
82 this->argv_ = argv;
84 int retval = this->parse_args ();
86 if (retval != 0)
87 return retval;
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
97 // macros?!
98 try
100 CORBA::String_var str =
101 this->orb_manager_.activate_under_child_poa (servant_name,
102 &this->servant_);
104 ACE_DEBUG ((LM_DEBUG,
105 "The IOR is: <%s>\n",
106 str.in ()));
108 if (this->ior_output_file_)
110 ACE_OS::fprintf (this->ior_output_file_,
111 "%s",
112 str.in ());
113 ACE_OS::fclose (this->ior_output_file_);
117 catch (const CORBA::Exception& ex)
119 ex._tao_print_exception ("Exception in activation of POA");
120 return -1;
123 return 0;
126 template <class Servant>int
127 Server<Servant>::run ()
129 // Run the main event loop for the ORB.
130 if (this->orb_manager_.run () == -1)
131 ACE_ERROR_RETURN ((LM_ERROR,
132 "Server_i::run"),
133 -1);
135 return 0;
138 template <class Servant> int
139 Server<Servant>::register_name ()
141 return -1;
144 // Constructor.
146 template <class InterfaceObj, class Var>
147 Client<InterfaceObj, Var>::Client ()
148 : ior_ (0)
150 //no-op
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",
164 filename),
165 -1);
167 ACE_Read_Buffer ior_buffer (f_handle);
168 char *data = ior_buffer.read ();
170 if (data == 0)
171 ACE_ERROR_RETURN ((LM_ERROR,
172 "Unable to read ior: %p\n"),
173 -1);
175 this->ior_ = ACE_OS::strdup (data);
176 ior_buffer.alloc ()->free (data);
178 ACE_OS::close (f_handle);
180 return 0;
183 // Parses the command line arguments and returns an error status.
185 template <class InterfaceObj, class Var> int
186 Client<InterfaceObj, Var>::parse_args ()
188 ACE_Get_Opt get_opts (argc_, argv_, ACE_TEXT("df:nk:x"));
189 int c = 0;
190 int result = 0;
192 while ((c = get_opts ()) != -1)
193 switch (c)
195 case 'd': // debug flag
196 TAO_debug_level++;
197 break;
198 case 'k': // ior provide on command line
199 this->ior_ = ACE_OS::strdup (ACE_TEXT_ALWAYS_CHAR(get_opts.opt_arg ()));
200 break;
201 case 'f': // read the IOR from the file.
202 result = this->read_ior (get_opts.opt_arg ());
203 if (result < 0)
204 ACE_ERROR_RETURN ((LM_ERROR,
205 "Unable to read ior from %s : %p\n",
206 get_opts.opt_arg ()),
207 -1);
208 break;
209 case 'x': // read the flag for shutting down
210 this->shutdown_ = 1;
211 break;
214 // Indicates successful parsing of command line.
215 return 0;
218 template <class InterfaceObj, class Var>
219 Client<InterfaceObj, Var>::~Client ()
221 ACE_OS::free (this->ior_);
224 template <class InterfaceObj, class Var> int
225 Client<InterfaceObj, Var>::init (const char *name,
226 int argc,
227 ACE_TCHAR **argv)
229 this->argc_ = argc;
230 this->argv_ = argv;
235 // Retrieve the ORB.
236 this->orb_ = CORBA::ORB_init (this->argc_,
237 this->argv_,
238 name);
240 // Parse command line and verify parameters.
241 if (this->parse_args () == -1)
242 return -1;
244 if(this->ior_ != 0)
246 CORBA::Object_var server_object =
247 this->orb_->string_to_object (this->ior_);
250 if (CORBA::is_nil (server_object.in ()))
251 ACE_ERROR_RETURN ((LM_ERROR,
252 "invalid ior <%s>\n",
253 this->ior_),
254 -1);
255 this->server_ = InterfaceObj::_narrow (server_object.in ());
257 else
258 ACE_ERROR_RETURN ((LM_ERROR,
259 "no ior or naming options specified\n"),
260 -1);
262 catch (const CORBA::Exception& ex)
264 ex._tao_print_exception ("Client_i::init");
265 return -1;
269 return 0;
273 template <class InterfaceObj, class Var> int
274 Client<InterfaceObj, Var>::obtain_initial_references ()
276 return 0;
279 template <class InterfaceObj, class Var> int
280 Client<InterfaceObj, Var>::shutdown ()
282 // Returns the shutdwon flag
283 return shutdown_;
286 template <class InterfaceObj, class Var> void
287 Client<InterfaceObj, Var>::shutdown (int flag)
289 // Fills the flag
290 shutdown_ = flag;
293 #endif