Merge pull request #2222 from jwillemsen/jwi-dllexportwarning
[ACE_TAO.git] / TAO / tests / Bug_3683_Regression / Simple_util.cpp
blob186b561ecbf757bc2292e0f1013a57373143af36
1 #ifndef SIMPLE_UTIL_C
2 #define SIMPLE_UTIL_C
4 #include "Simple_util.h"
5 #include "tao/IORTable/IORTable.h"
6 #include "tao/debug.h"
7 #include "ace/OS_NS_stdio.h"
8 #include "ace/OS_NS_unistd.h"
9 #include "ace/OS_NS_string.h"
10 #include "ace/OS_NS_fcntl.h"
12 // Constructor.
13 template <class Servant>
14 Server<Servant>::Server ()
15 : servant_ (0)
16 , ior_output_file_ (0)
17 , ins_ (0)
19 Servant *tmp = 0;
20 ACE_NEW_THROW_EX (tmp,
21 Servant (),
22 CORBA::NO_MEMORY ());
23 this->servant_ = tmp;
26 // Destructor.
27 template <class Servant>
28 Server<Servant>::~Server ()
32 // Parse the command-line arguments and set options.
33 template <class Servant> int
34 Server<Servant>::parse_args ()
36 ACE_Get_Opt get_opts (this->argc_, this->argv_,
37 ACE_TEXT ("do:i:"), 1, 0,
38 ACE_Get_Opt::REQUIRE_ORDER);
39 int c = 0;
41 while ((c = get_opts ()) != -1)
42 switch (c)
44 case 'd': // debug flag.
45 TAO_debug_level++;
46 break;
47 case 'o': // output the IOR to a file.
48 this->ior_output_file_ = get_opts.opt_arg ();
49 break;
50 case 'i': // For Testing the InterOperable Naming Service.
51 this->ins_ = get_opts.opt_arg ();
52 break;
55 // Indicates successful parsing of command line.
56 return 0;
59 // Add the ObjectID:IOR mapping to the IOR table of
60 // the ORB. Ignore this method if you are not testing for
61 // the InterOperable Naming Service.
62 template <class Servant> int
63 Server<Servant>::test_for_ins (const char *ior)
65 CORBA::ORB_var orb = this->orb_manager_.orb ();
67 if (TAO_debug_level > 0)
68 ACE_DEBUG ((LM_DEBUG,
69 ACE_TEXT ("Adding (KEY:IOR) %s:%C\n"),
70 this->ins_,
71 ior));
73 try
75 CORBA::Object_var table_object =
76 orb->resolve_initial_references ("IORTable");
78 IORTable::Table_var adapter =
79 IORTable::Table::_narrow (table_object.in ());
80 if (CORBA::is_nil (adapter.in ()))
82 ACE_ERROR_RETURN ((LM_ERROR,
83 ACE_TEXT ("Nil IORTable\n")),
84 -1);
87 adapter->bind (ACE_TEXT_ALWAYS_CHAR (this->ins_), ior);
89 catch (const CORBA::Exception& ex)
91 ex._tao_print_exception ("ERROR: test_for_ins failed\n");
92 return -1;
95 return 0;
98 // Initialize the server.
99 template <class Servant> int
100 Server<Servant>::init (const char *servant_name,
101 int argc,
102 ACE_TCHAR *argv[])
104 // Call the init of <TAO_ORB_Manager> to initialize the ORB and
105 // create a child POA under the root POA.
106 if (this->orb_manager_.init_child_poa (argc,
107 argv,
108 "child_poa") == -1)
109 ACE_ERROR_RETURN ((LM_ERROR,
110 ACE_TEXT ("%p\n"),
111 ACE_TEXT ("init_child_poa")),
112 -1);
114 this->argc_ = argc;
115 this->argv_ = argv;
117 int retval = this->parse_args ();
119 if (retval != 0)
120 return retval;
122 CORBA::ORB_var orb = this->orb_manager_.orb ();
124 // Stash our ORB pointer for later reference.
125 this->servant_->orb (orb.in ());
127 // Activate the servant in its own child POA.
129 // Make sure that you check for failures here via the try?!
132 CORBA::String_var str =
133 this->orb_manager_.activate_under_child_poa (servant_name,
134 this->servant_.in ());
136 ACE_DEBUG ((LM_DEBUG,
137 ACE_TEXT ("The IOR is: <%C>\n"),
138 str.in ()));
140 if (this->ins_ && this->test_for_ins (str.in ()) != 0)
141 ACE_ERROR_RETURN ((LM_ERROR,
142 ACE_TEXT ("test_for_ins (): failed\n")),
143 -1);
145 if (this->ior_output_file_)
147 FILE *fh = ACE_OS::fopen (this->ior_output_file_, "w");
148 if (fh == 0)
149 ACE_ERROR_RETURN ((LM_ERROR,
150 ACE_TEXT ("Unable to open %s for writing (%p)\n"),
151 this->ior_output_file_,
152 ACE_TEXT ("fopen")),
153 -1);
154 ACE_OS::fprintf (fh, "%s", str.in ());
155 ACE_OS::fclose (fh);
158 catch (const CORBA::Exception& ex)
160 ex._tao_print_exception ("\tException in activation of POA");
161 return -1;
164 return 0;
167 template <class Servant>int
168 Server<Servant>::run ()
170 // Run the main event loop for the ORB.
171 if (this->orb_manager_.run () == -1)
172 ACE_ERROR_RETURN ((LM_ERROR,
173 ACE_TEXT ("Server_i::run")),
174 -1);
176 return 0;
179 // Constructor.
180 template <class ServerInterface>
181 Client<ServerInterface>::Client ()
182 : ior_ ("")
183 , do_shutdown_ (0)
185 //no-op
188 // Reads the Server ior from a file
189 template <class ServerInterface> int
190 Client<ServerInterface>::read_ior (ACE_TCHAR *filename)
192 // Open the file for reading.
193 ACE_HANDLE f_handle = ACE_OS::open (filename, 0);
195 if (f_handle == ACE_INVALID_HANDLE)
196 ACE_ERROR_RETURN ((LM_ERROR,
197 ACE_TEXT ("Unable to open %s for writing (%p)\n"),
198 filename,
199 ACE_TEXT ("open")),
200 -1);
202 ACE_Read_Buffer ior_buffer (f_handle);
203 char *data = ior_buffer.read ();
205 if (data == 0)
206 ACE_ERROR_RETURN ((LM_ERROR,
207 ACE_TEXT ("Unable to read ior (%p)\n"),
208 ACE_TEXT ("read")),
209 -1);
211 this->ior_ = data;
212 ior_buffer.alloc ()->free (data);
214 ACE_OS::close (f_handle);
216 return 0;
219 // Parses the command line arguments and returns an error status.
220 template <class ServerInterface> int
221 Client<ServerInterface>::parse_args ()
223 ACE_Get_Opt get_opts (argc_, argv_,
224 ACE_TEXT ("df:nk:x"), 1, 0,
225 ACE_Get_Opt::REQUIRE_ORDER);
226 int c = 0;
227 int result = 0;
229 while ((c = get_opts ()) != -1)
230 switch (c)
232 case 'd': // debug flag
233 TAO_debug_level++;
234 break;
235 case 'k': // ior provide on command line
236 this->ior_ = ACE_TEXT_ALWAYS_CHAR(get_opts.opt_arg ());
237 break;
238 case 'f': // read the IOR from the file.
239 result = this->read_ior (get_opts.opt_arg ());
240 if (result < 0)
241 ACE_ERROR_RETURN ((LM_ERROR,
242 ACE_TEXT ("Unable to read ior from %s (%p)\n"),
243 get_opts.opt_arg (),
244 ACE_TEXT ("read_ior")),
245 -1);
246 break;
247 case 'x': // read the flag for shutting down
248 this->do_shutdown_ = 1;
249 break;
250 case 'h': // display help for use of the client.
251 ACE_ERROR_RETURN ((LM_ERROR,
252 ACE_TEXT ("usage: %s")
253 ACE_TEXT (" [-d (debug)]")
254 ACE_TEXT (" [-k] <ior>")
255 ACE_TEXT (" [-f] <ior_output_file>")
256 ACE_TEXT (" [-x (shutdown server)]")
257 ACE_TEXT (" [-h (help)]")
258 ACE_TEXT ("\n"),
259 this->argv_ [0]),
260 -1);
263 // Indicates successful parsing of command line.
264 return 0;
267 template <class ServerInterface>
268 Client<ServerInterface>::~Client ()
270 this->orb_->destroy ();
273 template <class ServerInterface> int
274 Client<ServerInterface>::init (const char *,
275 int argc,
276 ACE_TCHAR **argv)
278 this->argc_ = argc;
279 this->argv_ = argv;
283 // Retrieve the ORB.
284 this->orb_ = CORBA::ORB_init (this->argc_, this->argv_);
286 // Parse command line and verify parameters.
287 if (this->parse_args () == -1)
288 return -1;
290 if (this->ior_.length () != 0)
292 CORBA::Object_var server_object =
293 this->orb_->string_to_object (this->ior_.c_str ());
295 if (CORBA::is_nil (server_object.in ()))
296 ACE_ERROR_RETURN ((LM_ERROR,
297 ACE_TEXT ("invalid ior <%C>\n"),
298 this->ior_.c_str ()),
299 -1);
300 this->server_ = ServerInterface::_narrow (server_object.in ());
301 if (CORBA::is_nil (this->server_.in ()))
303 ACE_ERROR_RETURN ((LM_ERROR,
304 ACE_TEXT ("Nil Server\n")),
305 -1);
308 else
309 ACE_ERROR_RETURN ((LM_ERROR,
310 ACE_TEXT ("no ior or naming options specified\n")),
311 -1);
313 catch (const CORBA::Exception& ex)
315 ex._tao_print_exception ("Client_i::init");
316 return -1;
319 return 0;
322 template <class ServerInterface> int
323 Client<ServerInterface>::do_shutdown ()
325 // Returns the shutdwon flag
326 return this->do_shutdown_;
329 template <class ServerInterface> void
330 Client<ServerInterface>::do_shutdown (int flag)
332 // Fills the flag
333 this->do_shutdown_ = flag;
336 #endif /* SIMPLE_UTIL_C */