Changes to attempt to silence bcc64x
[ACE_TAO.git] / TAO / examples / Persistent_Grid / Simple_util.cpp
blob90bae9293f51e1bccd1fdc84be9453a268a47d51
1 #ifndef SIMPLE_UTIL_C
2 #define SIMPLE_UTIL_C
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"
10 // Constructor.
12 template <class Servant>
13 Server<Servant>::Server ()
14 : ior_output_file_ (0),
15 mem_pool_name_ (0)
19 // Destructor.
21 template <class Servant>
22 Server<Servant>::~Server ()
26 // Parse the command-line arguments and set options.
28 template <class Servant> int
29 Server<Servant>::parse_args ()
31 ACE_Get_Opt get_opts (this->argc_, this->argv_, ACE_TEXT("do:m:"));
32 int c = 0;
34 while ((c = get_opts ()) != -1)
35 switch (c)
37 case 'd': // debug flag.
38 TAO_debug_level++;
39 break;
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);
46 break;
48 case 'm':
49 this->mem_pool_name_ = get_opts.opt_arg ();
50 break;
51 case '?': // display help for use of the server.
52 default:
53 ACE_ERROR_RETURN ((LM_ERROR,
54 "usage: %s"
55 " [-d]"
56 " [-o] <ior_output_file>"
57 "\n",
58 this->argv_ [0]),
59 -1);
62 // Indicates successful parsing of command line.
63 return 0;
67 // Initialize the server.
68 template <class Servant> int
69 Server<Servant>::init (const char *servant_name,
70 int argc,
71 ACE_TCHAR *argv[])
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,
76 argv,
77 "child_poa") == -1)
78 ACE_ERROR_RETURN ((LM_ERROR,
79 "%p\n",
80 "init_child_poa"),
81 -1);
84 this->argc_ = argc;
85 this->argv_ = argv;
87 int retval = this->parse_args ();
89 if (retval != 0)
90 return retval;
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
103 // macros?!
106 CORBA::String_var str =
107 this->orb_manager_.activate_under_child_poa (servant_name,
108 &this->servant_);
110 ACE_DEBUG ((LM_DEBUG,
111 "The IOR is: <%C>\n",
112 str.in ()));
114 if (this->ior_output_file_)
116 ACE_OS::fprintf (this->ior_output_file_,
117 "%s",
118 str.in ());
119 ACE_OS::fclose (this->ior_output_file_);
123 catch (const CORBA::Exception& ex)
125 ex._tao_print_exception ("\tException in activation of POA");
126 return -1;
129 return 0;
132 template <class Servant> int
133 Server<Servant>::run ()
135 // Run the main event loop for the ORB.
136 int ret = this->orb_manager_.run ();
138 if (ret == -1)
139 ACE_ERROR_RETURN ((LM_ERROR,
140 "Server_i::run"),
141 -1);
143 return 0;
147 /////////////////////////////////////////////////////////////////
148 // Client code Starts here
149 ////////////////////////////////////////////////////////////////
151 // Constructor.
152 template <class InterfaceObj, class Var>
153 Client<InterfaceObj, Var>::Client ()
154 : ior_ (0)
156 //no-op
159 // Reads the Server ior from a file
161 template <class InterfaceObj, class Var> int
162 Client<InterfaceObj, Var>::read_ior (ACE_TCHAR *filename)
164 // Open the file for reading.
165 ACE_HANDLE f_handle = ACE_OS::open (filename, 0);
167 if (f_handle == ACE_INVALID_HANDLE)
168 ACE_ERROR_RETURN ((LM_ERROR,
169 "Unable to open %s for writing: %p\n",
170 filename),
171 -1);
173 ACE_Read_Buffer ior_buffer (f_handle);
174 char *data = ior_buffer.read ();
176 if (data == 0)
177 ACE_ERROR_RETURN ((LM_ERROR,
178 "Unable to read ior: %p\n"),
179 -1);
181 this->ior_ = ACE_OS::strdup (data);
182 ior_buffer.alloc ()->free (data);
184 ACE_OS::close (f_handle);
186 return 0;
189 // Parses the command line arguments and returns an error status.
191 template <class InterfaceObj, class Var> int
192 Client<InterfaceObj, Var>::parse_args ()
194 ACE_Get_Opt get_opts (argc_, argv_, ACE_TEXT("df:k:x"));
195 int c = 0;
196 int result = 0;
198 while ((c = get_opts ()) != -1)
199 switch (c)
201 case 'd': // debug flag
202 TAO_debug_level++;
203 break;
204 case 'k': // ior provide on command line
205 this->ior_ = ACE_OS::strdup (ACE_TEXT_ALWAYS_CHAR(get_opts.opt_arg ()));
206 break;
207 case 'f': // read the IOR from the file.
208 result = this->read_ior (get_opts.opt_arg ());
209 if (result < 0)
210 ACE_ERROR_RETURN ((LM_ERROR,
211 "Unable to read ior from %s : %p\n",
212 get_opts.opt_arg ()),
213 -1);
214 break;
215 case 'x': // read the flag for shutting down
216 this->shutdown_ = 1;
217 break;
220 // Indicates successful parsing of command line.
221 return 0;
224 template <class InterfaceObj, class Var>
225 Client<InterfaceObj, Var>::~Client ()
227 ACE_OS::free (this->ior_);
230 template <class InterfaceObj, class Var> int
231 Client<InterfaceObj, Var>::init (const char * /*name*/,
232 int argc,
233 ACE_TCHAR **argv)
235 this->argc_ = argc;
236 this->argv_ = argv;
240 // Retrieve the ORB.
241 this->orb_ = CORBA::ORB_init (this->argc_, this->argv_);
243 // Parse command line and verify parameters.
244 if (this->parse_args () == -1)
245 return -1;
247 if(this->ior_ != 0)
249 CORBA::Object_var server_object =
250 this->orb_->string_to_object (this->ior_);
252 if (CORBA::is_nil (server_object.in ()))
253 ACE_ERROR_RETURN ((LM_ERROR,
254 "invalid ior <%C>\n",
255 this->ior_),
256 -1);
257 this->server_ = InterfaceObj::_narrow (server_object.in ());
259 else
260 ACE_ERROR_RETURN ((LM_ERROR,
261 "no ior or naming options specified\n"),
262 -1);
264 catch (const CORBA::Exception& ex)
266 ex._tao_print_exception ("Client_i::init");
267 return -1;
271 return 0;
275 template <class InterfaceObj, class Var> int
276 Client<InterfaceObj, Var>::shutdown ()
278 // Returns the shutdwon flag
279 return shutdown_;
282 template <class InterfaceObj, class Var> void
283 Client<InterfaceObj, Var>::shutdown (int flag)
285 // Fills the flag
286 shutdown_ = flag;
289 #endif