Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / tests / OBV / Simple / Simple_util.cpp
blobc8282e111d1bf26286b9bb0e35a792c6b735f06e
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.
23 template <class Servant>
24 Server<Servant>::~Server ()
28 // Parse the command-line arguments and set options.
30 template <class Servant> int
31 Server<Servant>::parse_args ()
33 ACE_Get_Opt get_opts (this->argc_, this->argv_, ACE_TEXT("do:ni:"));
34 int c = 0;
36 while ((c = get_opts ()) != -1)
37 switch (c)
39 case 'd': // debug flag.
40 TAO_debug_level++;
41 break;
42 case 'o': // output the IOR to a file.
43 this->ior_output_file_ = ACE_OS::fopen (get_opts.opt_arg (), "w");
44 if (this->ior_output_file_ == 0)
45 ACE_ERROR_RETURN ((LM_ERROR,
46 "Unable to open %s for writing: %p\n",
47 get_opts.opt_arg ()), -1);
48 break;
50 case '?': // display help for use of the server.
51 default:
52 ACE_ERROR_RETURN ((LM_ERROR,
53 "usage: %s"
54 " [-d]"
55 " [-o] <ior_output_file>"
56 "\n",
57 argv_ [0]),
58 -1);
61 // Indicates successful parsing of command line.
62 return 0;
65 // Initialize the server.
66 template <class Servant> int
67 Server<Servant>::init (const char *servant_name,
68 int argc,
69 ACE_TCHAR *argv[])
71 // Call the init of <TAO_ORB_Manager> to initialize the ORB and
72 // create a child POA under the root POA.
73 if (this->orb_manager_.init_child_poa (argc,
74 argv,
75 "child_poa") == -1)
76 ACE_ERROR_RETURN ((LM_ERROR,
77 "%p\n",
78 "init_child_poa"),
79 -1);
82 this->argc_ = argc;
83 this->argv_ = argv;
85 int retval = this->parse_args ();
87 if (retval != 0)
88 return retval;
90 CORBA::ORB_var orb = this->orb_manager_.orb ();
92 // Stash our ORB pointer for later reference.
93 this->servant_.orb (orb.in ());
95 // Activate the servant in its own child POA.
97 // Make sure that you check for failures here via the ACE_TRY
98 // macros?!
99 try
101 CORBA::String_var str =
102 this->orb_manager_.activate_under_child_poa (servant_name,
103 &this->servant_);
105 ACE_DEBUG ((LM_DEBUG,
106 "The IOR is: <%C>\n",
107 str.in ()));
109 if (this->ior_output_file_)
111 ACE_OS::fprintf (this->ior_output_file_,
112 "%s",
113 str.in ());
114 ACE_OS::fclose (this->ior_output_file_);
118 catch (const CORBA::Exception& ex)
120 ex._tao_print_exception ("Exception in activation of POA");
121 return -1;
124 return 0;
127 template <class Servant>int
128 Server<Servant>::run ()
130 // Run the main event loop for the ORB.
131 if (this->orb_manager_.run () == -1)
132 ACE_ERROR_RETURN ((LM_ERROR,
133 "Server_i::run"),
134 -1);
136 return 0;
139 template <class Servant> int
140 Server<Servant>::register_name ()
142 return -1;
145 // Constructor.
147 template <class InterfaceObj, class Var>
148 Client<InterfaceObj, Var>::Client ()
149 : ior_ (0)
151 //no-op
154 // Reads the Server ior from a file
156 template <class InterfaceObj, class Var> int
157 Client<InterfaceObj, Var>::read_ior (ACE_TCHAR *filename)
159 // Open the file for reading.
160 ACE_HANDLE f_handle = ACE_OS::open (filename, 0);
162 if (f_handle == ACE_INVALID_HANDLE)
163 ACE_ERROR_RETURN ((LM_ERROR,
164 "Unable to open %s for writing: %p\n",
165 filename),
166 -1);
168 ACE_Read_Buffer ior_buffer (f_handle);
169 char *data = ior_buffer.read ();
171 if (data == 0)
172 ACE_ERROR_RETURN ((LM_ERROR,
173 "Unable to read ior: %p\n"),
174 -1);
176 this->ior_ = ACE_OS::strdup (ACE_TEXT_CHAR_TO_TCHAR(data));
177 ior_buffer.alloc ()->free (data);
179 ACE_OS::close (f_handle);
181 return 0;
184 // Parses the command line arguments and returns an error status.
186 template <class InterfaceObj, class Var> int
187 Client<InterfaceObj, Var>::parse_args ()
189 ACE_Get_Opt get_opts (argc_, argv_, ACE_TEXT("df:nk:x"));
190 int c = 0;
191 int result = 0;
193 while ((c = get_opts ()) != -1)
194 switch (c)
196 case 'd': // debug flag
197 TAO_debug_level++;
198 break;
199 case 'k': // ior provide on command line
200 this->ior_ = ACE_OS::strdup (get_opts.opt_arg ());
201 break;
202 case 'f': // read the IOR from the file.
203 result = this->read_ior (get_opts.opt_arg ());
204 if (result < 0)
205 ACE_ERROR_RETURN ((LM_ERROR,
206 "Unable to read ior from %s : %p\n",
207 get_opts.opt_arg ()),
208 -1);
209 break;
210 case 'x': // read the flag for shutting down
211 this->shutdown_ = 1;
212 break;
215 // Indicates successful parsing of command line.
216 return 0;
219 template <class InterfaceObj, class Var>
220 Client<InterfaceObj, Var>::~Client ()
222 ACE_OS::free (this->ior_);
225 template <class InterfaceObj, class Var> int
226 Client<InterfaceObj, Var>::init (const char *name,
227 int argc,
228 ACE_TCHAR **argv)
230 this->argc_ = argc;
231 this->argv_ = argv;
236 // Retrieve the ORB.
237 this->orb_ = CORBA::ORB_init (this->argc_,
238 this->argv_,
239 name);
241 // Parse command line and verify parameters.
242 if (this->parse_args () == -1)
243 return -1;
245 if(this->ior_ != 0)
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",
254 this->ior_),
255 -1);
256 this->server_ = InterfaceObj::_narrow (server_object.in ());
258 else
259 ACE_ERROR_RETURN ((LM_ERROR,
260 "no ior or naming options specified\n"),
261 -1);
263 catch (const CORBA::Exception& ex)
265 ex._tao_print_exception ("Client_i::init");
266 return -1;
270 return 0;
274 template <class InterfaceObj, class Var> int
275 Client<InterfaceObj, Var>::obtain_initial_references ()
277 return 0;
280 template <class InterfaceObj, class Var> int
281 Client<InterfaceObj, Var>::shutdown ()
283 // Returns the shutdwon flag
284 return shutdown_;
287 template <class InterfaceObj, class Var> void
288 Client<InterfaceObj, Var>::shutdown (int flag)
290 // Fills the flag
291 shutdown_ = flag;
294 #endif