Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / utils / nsgroup / nsgroup.cpp
blob3186f2d8db1b4b2bcf763f24e2db60a379de94c0
1 //==========================================================================
2 /**
3 * @file nsgroup.cpp
5 * @author Phillip LaBanca <labancap@ociweb.com>
6 */
7 //==========================================================================
10 #include "orbsvcs/Naming/FaultTolerant/nsgroup_svc.h"
11 #include "ace/Get_Opt.h"
13 //============================================================================
14 bool
15 show_help(int argc, ACE_TCHAR *argv[])
17 static const ACE_TCHAR options[] = ACE_TEXT("h");
18 static const int skip_args = 1;
19 static const int report_errors = 0;
20 static const int ordering = ACE_Get_Opt::RETURN_IN_ORDER;
21 static const int long_only = 0;
23 // Not enough arguments indicates help is required
24 if( argc < 2 )
26 return true;
29 // Did they ask for help ?
30 ACE_Get_Opt get_opts (
31 argc,
32 argv,
33 options,
34 skip_args,
35 report_errors,
36 ordering,
37 long_only
40 if (get_opts.long_option (ACE_TEXT ("help"),'h') != 0)
42 ACE_ERROR_RETURN ((LM_ERROR,
43 ACE_TEXT (" Unable to add long option 'H'\n")),
44 true);
47 int c;
48 while ((c = get_opts ()) != -1)
50 switch (c)
52 case 'h': // help
54 return true;
55 break;
58 return false;
61 /**
62 * @class nsgroup
64 * @brief Encapsulate the NS group command line operations in a class.
67 class NSGROUP
69 public:
71 enum NSGROUP_COMMAND {
72 NSGROUP_NONE,
73 NSGROUP_HELP,
74 NSGROUP_GROUP_CREATE,
75 NSGROUP_GROUP_BIND,
76 NSGROUP_GROUP_UNBIND,
77 NSGROUP_GROUP_MODIFY,
78 NSGROUP_GROUP_LIST,
79 NSGROUP_GROUP_REMOVE,
80 NSGROUP_MEMBER_LIST,
81 NSGROUP_MEMBER_ADD,
82 NSGROUP_MEMBER_REMOVE,
83 NSGROUP_MEMBER_SHOW
86 /// Constructor
87 NSGROUP (int argc, ACE_TCHAR **argv);
89 /// start the ORB.
90 int start_orb (void);
92 /// parse command line, validate arguments and run the command
93 int run_cmd (void);
95 /// Display command line interface usage
96 int show_usage( void );
98 const ACE_TCHAR * group_arg(void) const { return group_arg_; }
99 const ACE_TCHAR * policy_arg(void) const { return policy_arg_; }
100 const ACE_TCHAR * location_arg(void) const { return location_arg_; }
101 const ACE_TCHAR * ior_arg(void) const { return ior_arg_; }
102 const ACE_TCHAR * namepath_arg(void) const { return namepath_arg_; }
104 private:
106 /// parse command line arguments
107 NSGROUP_COMMAND parse_command_line (void);
109 private:
111 NS_group_svc svc_;
113 int argc_;
114 ACE_TCHAR **argv_;
116 /// parsed command result
117 NSGROUP_COMMAND nsgroup_cmd_;
119 /// parsed command line arguments
120 const ACE_TCHAR *group_arg_;
121 const ACE_TCHAR *policy_arg_;
122 const ACE_TCHAR *location_arg_;
123 const ACE_TCHAR *ior_arg_;
124 const ACE_TCHAR *namepath_arg_;
128 NSGROUP::NSGROUP (int argc, ACE_TCHAR **argv)
129 : argc_ (argc),
130 argv_ (argv),
131 nsgroup_cmd_(NSGROUP::NSGROUP_NONE),
132 group_arg_ (0),
133 policy_arg_ (0),
134 location_arg_ (0),
135 ior_arg_ (0),
136 namepath_arg_ (0)
141 NSGROUP::start_orb (void)
143 const int RC_SUCCESS = 0;
144 const int RC_ERROR = -1;
149 #if 0
150 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("start_orb::argc(%u)\n"), this->argc_));
151 for( int i = 0; i < this->argc_; ++i){
152 ACE_DEBUG ((LM_DEBUG,
153 ACE_TEXT ("start_orb::argv(%u:%s)\n"),
155 this->argv_[i]));
157 #endif //
159 // Initialise the ORB.
161 CORBA::ORB_var orb_ = CORBA::ORB_init (this->argc_, this->argv_);
163 if (RC_SUCCESS != svc_.set_orb (orb_.in ()))
166 ACE_ERROR_RETURN (( LM_ERROR,
167 ACE_TEXT (" (%P|%t) Unable to initialize the ORB.\n")),
168 RC_ERROR);
171 //////////////////////////////////////////////////////////////////////////
173 //////////////////////////////////////////////////////////////////////////
174 CORBA::Object_var naming_manager_object =
175 orb_->resolve_initial_references ("NamingManager");
177 FT_Naming::NamingManager_var naming_manager_ =
178 FT_Naming::NamingManager::_narrow (naming_manager_object.in ());
180 if (RC_SUCCESS != svc_.set_naming_manager (naming_manager_.in ()))
182 ACE_ERROR_RETURN (( LM_ERROR,
183 ACE_TEXT (" (%P|%t) Unable to get Naming ")
184 ACE_TEXT ("Manager Reference\n")),
185 RC_ERROR);
187 //////////////////////////////////////////////////////////////////////////
189 //////////////////////////////////////////////////////////////////////////
190 CORBA::Object_var naming_object =
191 orb_->resolve_initial_references("NameService");
193 CosNaming::NamingContextExt_var name_service_ =
194 CosNaming::NamingContextExt::_narrow (naming_object.in ());
196 if (RC_SUCCESS != svc_.set_name_context (name_service_.in ()))
198 ACE_ERROR_RETURN (( LM_ERROR,
199 ACE_TEXT (" (%P|%t) Unable to get Name Service ")
200 ACE_TEXT ("Reference\n")),
201 RC_ERROR);
203 //////////////////////////////////////////////////////////////////////////
205 //////////////////////////////////////////////////////////////////////////
208 catch (const CORBA::Exception& ex)
210 ex._tao_print_exception (ACE_TEXT("\nException raised initialising ORB\n"));
211 return RC_ERROR;
214 return RC_SUCCESS;
219 NSGROUP::run_cmd(void)
221 const int RC_ERROR = -1;
222 const int RC_BADARG = -2;
224 int rc = RC_ERROR;
226 switch( parse_command_line () )
229 case NSGROUP_HELP:
230 return show_usage ();
231 break;
233 case NSGROUP_GROUP_CREATE:
234 rc = svc_.group_create ( group_arg(), policy_arg() );
235 break;
237 case NSGROUP_GROUP_BIND:
238 rc = svc_.group_bind ( group_arg(), namepath_arg() );
239 break;
241 case NSGROUP_GROUP_UNBIND:
242 rc = svc_.group_unbind ( namepath_arg() );
243 break;
245 case NSGROUP_GROUP_MODIFY:
246 rc = svc_.group_modify ( group_arg(), policy_arg() );
247 break;
249 case NSGROUP_GROUP_LIST:
250 rc = svc_.group_list ();
251 break;
253 case NSGROUP_GROUP_REMOVE:
254 rc = svc_.group_remove ( group_arg() );
255 break;
257 case NSGROUP_MEMBER_LIST:
258 rc = svc_.member_list ( group_arg() );
259 break;
261 case NSGROUP_MEMBER_ADD:
262 rc = svc_.member_add ( group_arg(), location_arg(), ior_arg() );
263 break;
265 case NSGROUP_MEMBER_REMOVE:
266 rc = svc_.member_remove ( group_arg(), location_arg() );
267 break;
269 case NSGROUP_MEMBER_SHOW:
270 rc = svc_.member_show ( group_arg(), location_arg() );
271 break;
273 default:
274 show_usage ();
275 return rc;
276 break;
279 if (rc == RC_BADARG)
281 show_usage ();
284 return rc;
287 NSGROUP::NSGROUP_COMMAND
288 NSGROUP::parse_command_line (void)
290 #if 0
291 ACE_DEBUG ((LM_DEBUG,
292 ACE_TEXT ("parse_command_line::argc(%u)\n"),
293 this->argc_));
294 for( int i = 0; i < this->argc_; ++i){
295 ACE_DEBUG ((LM_DEBUG,
296 ACE_TEXT ("parse_command_line::argv(%u:%s)\n"),
298 this->argv_[i]));
300 #endif
302 static const ACE_TCHAR options[] = ACE_TEXT("g:p:t:l:i:n:h");
303 static const int skip_args = 1;
304 static const int report_errors = 0;
305 static const int ordering = ACE_Get_Opt::PERMUTE_ARGS;
306 static const int long_only = 1;
308 ACE_Get_Opt get_opts (
309 this->argc_,
310 this->argv_,
311 options,
312 skip_args,
313 report_errors,
314 ordering,
315 long_only
318 this->group_arg_ = 0;
319 if (get_opts.long_option (ACE_TEXT ("group"),
320 'g',
321 ACE_Get_Opt::ARG_REQUIRED) != 0)
322 ACE_ERROR_RETURN ((LM_ERROR,
323 ACE_TEXT ("Unable to add long option 'g'\n")),
324 NSGROUP_NONE);
326 this->policy_arg_ = 0;
327 if (get_opts.long_option (ACE_TEXT ("policy"),
328 'p',
329 ACE_Get_Opt::ARG_REQUIRED) != 0)
330 ACE_ERROR_RETURN ((LM_ERROR,
331 ACE_TEXT ("Unable to add long option 'p'\n")),
332 NSGROUP_NONE);
334 this->location_arg_ = 0;
335 if (get_opts.long_option (ACE_TEXT ("location"),
336 'l',
337 ACE_Get_Opt::ARG_REQUIRED) != 0)
338 ACE_ERROR_RETURN ((LM_ERROR,
339 ACE_TEXT ("Unable to add long option 'l'\n")),
340 NSGROUP_NONE);
342 this->ior_arg_ = 0;
343 if (get_opts.long_option (ACE_TEXT ("ior"),
344 'i',
345 ACE_Get_Opt::ARG_REQUIRED) != 0)
346 ACE_ERROR_RETURN ((LM_ERROR,
347 ACE_TEXT ("Unable to add long option 'i'\n")),
348 NSGROUP_NONE);
350 this->namepath_arg_ = 0;
351 if (get_opts.long_option (ACE_TEXT ("name"),
352 'n',
353 ACE_Get_Opt::ARG_REQUIRED) != 0)
354 ACE_ERROR_RETURN ((LM_ERROR,
355 ACE_TEXT ("Unable to add long option 'n'\n")),
356 NSGROUP_NONE);
358 if (get_opts.long_option (ACE_TEXT ("help"), 'h') != 0)
359 ACE_ERROR_RETURN ((LM_ERROR,
360 ACE_TEXT ("Unable to add long option 'h'\n")),
361 NSGROUP_NONE);
363 int c;
364 while ((c = get_opts ()) != -1)
365 switch (c)
367 case 'g': // group
368 this->group_arg_ = get_opts.opt_arg ();
369 break;
370 case 'p': // policy
371 this->policy_arg_ = get_opts.opt_arg ();
372 break;
373 case 'l': // location
374 this->location_arg_ = get_opts.opt_arg ();
375 break;
376 case 'i': // ior
377 this->ior_arg_ = get_opts.opt_arg ();
378 break;
379 case 'n': // name
380 this->namepath_arg_ = get_opts.opt_arg ();
381 break;
382 case 'h':
383 return NSGROUP_HELP;
386 // handle non-option arguments
387 int non_option_arg_count = 0;
388 for( int i = get_opts.opt_ind (); i < this->argc_; ++i)
391 non_option_arg_count++;
394 if( ACE_OS::strncmp (this->argv_[i],ACE_TEXT("group_create"),
395 ACE_OS::strlen (ACE_TEXT("group_create"))) == 0 )
397 nsgroup_cmd_ = NSGROUP_GROUP_CREATE;
399 else if(ACE_OS::strncmp (this->argv_[i], ACE_TEXT("group_bind"),
400 ACE_OS::strlen (ACE_TEXT("group_bind"))) == 0 )
402 nsgroup_cmd_ = NSGROUP_GROUP_BIND;
404 else if(ACE_OS::strncmp (this->argv_[i], ACE_TEXT("group_unbind"),
405 ACE_OS::strlen (ACE_TEXT("group_unbind")))
406 == 0 )
408 nsgroup_cmd_ = NSGROUP_GROUP_UNBIND;
410 else if(ACE_OS::strncmp (this->argv_[i], ACE_TEXT("group_modify"),
411 ACE_OS::strlen (ACE_TEXT("group_modify"))) == 0 )
413 /// reserve cmd NSGROUP_GROUP_MODIFY for future use
414 nsgroup_cmd_ = NSGROUP_NONE;//
416 else if(ACE_OS::strncmp (this->argv_[i], ACE_TEXT("group_list"),
417 ACE_OS::strlen (ACE_TEXT("group_list"))) == 0 )
419 nsgroup_cmd_ = NSGROUP_GROUP_LIST;
421 else if(ACE_OS::strncmp (this->argv_[i], ACE_TEXT("group_remove"),
422 ACE_OS::strlen (ACE_TEXT("group_remove"))) == 0 )
424 nsgroup_cmd_ = NSGROUP_GROUP_REMOVE;
426 else if(ACE_OS::strncmp (this->argv_[i], ACE_TEXT("member_list"),
427 ACE_OS::strlen (ACE_TEXT("member_list"))) == 0 )
429 nsgroup_cmd_ = NSGROUP_MEMBER_LIST;
431 else if(ACE_OS::strncmp (this->argv_[i], ACE_TEXT("member_add"),
432 ACE_OS::strlen (ACE_TEXT("member_add"))) == 0 )
434 nsgroup_cmd_ = NSGROUP_MEMBER_ADD;
436 else if(ACE_OS::strncmp (this->argv_[i], ACE_TEXT("member_remove"),
437 ACE_OS::strlen (ACE_TEXT("member_remove"))) == 0 )
439 nsgroup_cmd_ = NSGROUP_MEMBER_REMOVE;
442 else if(ACE_OS::strncmp (this->argv_[i], ACE_TEXT("member_show"),
443 ACE_OS::strlen (ACE_TEXT("member_show"))) == 0 )
445 nsgroup_cmd_ = NSGROUP_MEMBER_SHOW;
447 else
449 nsgroup_cmd_ = NSGROUP_NONE;
453 // The command should be the only non option argument
454 if ( non_option_arg_count > 1 ) {
455 nsgroup_cmd_ = NSGROUP_NONE;
458 return nsgroup_cmd_;
463 NSGROUP::show_usage ( void )
465 const int RC_SUCCESS = 0;
467 ACE_DEBUG ((LM_INFO,
468 ACE_TEXT ("Usage:\n")
469 ACE_TEXT (" %s\n")
470 ACE_TEXT (" group_create -group <group> -policy <round|random> \n")
471 ACE_TEXT (" group_bind -group <group> -name <name>\n")
472 ACE_TEXT (" group_unbind -name <name>\n")
473 ACE_TEXT (" group_list\n")
474 ACE_TEXT (" group_remove -group <group>\n")
475 ACE_TEXT (" member_list -group <group>\n")
476 ACE_TEXT (" member_add -group <group> -location <location> -ior <IOR>\n")
477 ACE_TEXT (" member_remove -group <group> -location <location>\n")
478 ACE_TEXT (" member_show -group <group> -location <location>\n")
479 ACE_TEXT (" -help\n")
480 ACE_TEXT ("\n"),
481 this->argv_[0]));
482 return RC_SUCCESS;
486 //============================================================================
488 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
490 const int RC_ERROR = -1;
491 const int RC_SUCCESS = 0;
493 int rc = RC_ERROR;
498 NSGROUP ns_group (argc, argv);
500 if ( show_help (argc, argv) )
502 rc = ns_group.show_usage ();
504 else if ( RC_SUCCESS == ns_group.start_orb ())
506 rc = ns_group.run_cmd ();
510 catch (const CORBA::Exception&)
512 ACE_ERROR_RETURN ((LM_ERROR,
513 ACE_TEXT ("Unable to run %s\n"),
514 argv[0]),
518 return (rc == RC_SUCCESS) ? 0 : 1;