Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / examples / Persistent_Grid / Simple_util.cpp
blobd692a78648dacd2a64ca5cbf267b8b942cd7e08b
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 (void)
14 : ior_output_file_ (0),
15 mem_pool_name_ (0)
19 // Destructor.
21 template <class Servant>
22 Server<Servant>::~Server (void)
26 // Parse the command-line arguments and set options.
28 template <class Servant> int
29 Server<Servant>::parse_args (void)
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 (void)
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;
148 /////////////////////////////////////////////////////////////////
149 // Client code Starts here
150 ////////////////////////////////////////////////////////////////
152 // Constructor.
153 template <class InterfaceObj, class Var>
154 Client<InterfaceObj, Var>::Client (void)
155 : ior_ (0)
157 //no-op
160 // Reads the Server ior from a file
162 template <class InterfaceObj, class Var> int
163 Client<InterfaceObj, Var>::read_ior (ACE_TCHAR *filename)
165 // Open the file for reading.
166 ACE_HANDLE f_handle = ACE_OS::open (filename, 0);
168 if (f_handle == ACE_INVALID_HANDLE)
169 ACE_ERROR_RETURN ((LM_ERROR,
170 "Unable to open %s for writing: %p\n",
171 filename),
172 -1);
174 ACE_Read_Buffer ior_buffer (f_handle);
175 char *data = ior_buffer.read ();
177 if (data == 0)
178 ACE_ERROR_RETURN ((LM_ERROR,
179 "Unable to read ior: %p\n"),
180 -1);
182 this->ior_ = ACE_OS::strdup (data);
183 ior_buffer.alloc ()->free (data);
185 ACE_OS::close (f_handle);
187 return 0;
190 // Parses the command line arguments and returns an error status.
192 template <class InterfaceObj, class Var> int
193 Client<InterfaceObj, Var>::parse_args (void)
195 ACE_Get_Opt get_opts (argc_, argv_, ACE_TEXT("df:k:x"));
196 int c = 0;
197 int result = 0;
199 while ((c = get_opts ()) != -1)
200 switch (c)
202 case 'd': // debug flag
203 TAO_debug_level++;
204 break;
205 case 'k': // ior provide on command line
206 this->ior_ = ACE_OS::strdup (ACE_TEXT_ALWAYS_CHAR(get_opts.opt_arg ()));
207 break;
208 case 'f': // read the IOR from the file.
209 result = this->read_ior (get_opts.opt_arg ());
210 if (result < 0)
211 ACE_ERROR_RETURN ((LM_ERROR,
212 "Unable to read ior from %s : %p\n",
213 get_opts.opt_arg ()),
214 -1);
215 break;
216 case 'x': // read the flag for shutting down
217 this->shutdown_ = 1;
218 break;
221 // Indicates successful parsing of command line.
222 return 0;
225 template <class InterfaceObj, class Var>
226 Client<InterfaceObj, Var>::~Client (void)
228 ACE_OS::free (this->ior_);
231 template <class InterfaceObj, class Var> int
232 Client<InterfaceObj, Var>::init (const char * /*name*/,
233 int argc,
234 ACE_TCHAR **argv)
236 this->argc_ = argc;
237 this->argv_ = argv;
241 // Retrieve the ORB.
242 this->orb_ = CORBA::ORB_init (this->argc_, this->argv_);
244 // Parse command line and verify parameters.
245 if (this->parse_args () == -1)
246 return -1;
248 if(this->ior_ != 0)
250 CORBA::Object_var server_object =
251 this->orb_->string_to_object (this->ior_);
253 if (CORBA::is_nil (server_object.in ()))
254 ACE_ERROR_RETURN ((LM_ERROR,
255 "invalid ior <%C>\n",
256 this->ior_),
257 -1);
258 this->server_ = InterfaceObj::_narrow (server_object.in ());
260 else
261 ACE_ERROR_RETURN ((LM_ERROR,
262 "no ior or naming options specified\n"),
263 -1);
267 catch (const CORBA::Exception& ex)
269 ex._tao_print_exception ("Client_i::init");
270 return -1;
274 return 0;
278 template <class InterfaceObj, class Var> int
279 Client<InterfaceObj, Var>::shutdown (void)
281 // Returns the shutdwon flag
282 return shutdown_;
285 template <class InterfaceObj, class Var> void
286 Client<InterfaceObj, Var>::shutdown (int flag)
288 // Fills the flag
289 shutdown_ = flag;
292 #endif