Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / utils / nsgroup / nsgroup.cpp
blobc50fed5c1b61ffa8426eb8bdd4f9be66e92385fb
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:
70 enum NSGROUP_COMMAND {
71 NSGROUP_NONE,
72 NSGROUP_HELP,
73 NSGROUP_GROUP_CREATE,
74 NSGROUP_GROUP_BIND,
75 NSGROUP_GROUP_UNBIND,
76 NSGROUP_GROUP_MODIFY,
77 NSGROUP_GROUP_LIST,
78 NSGROUP_GROUP_REMOVE,
79 NSGROUP_MEMBER_LIST,
80 NSGROUP_MEMBER_ADD,
81 NSGROUP_MEMBER_REMOVE,
82 NSGROUP_MEMBER_SHOW
85 /// Constructor
86 NSGROUP (int argc, ACE_TCHAR **argv);
88 /// start the ORB.
89 int start_orb ();
91 /// parse command line, validate arguments and run the command
92 int run_cmd ();
94 /// Display command line interface usage
95 int show_usage();
97 const ACE_TCHAR * group_arg() const { return group_arg_; }
98 const ACE_TCHAR * policy_arg() const { return policy_arg_; }
99 const ACE_TCHAR * location_arg() const { return location_arg_; }
100 const ACE_TCHAR * ior_arg() const { return ior_arg_; }
101 const ACE_TCHAR * namepath_arg() const { return namepath_arg_; }
103 private:
104 /// parse command line arguments
105 NSGROUP_COMMAND parse_command_line ();
107 private:
108 NS_group_svc svc_;
110 int argc_;
111 ACE_TCHAR **argv_;
113 /// parsed command result
114 NSGROUP_COMMAND nsgroup_cmd_;
116 /// parsed command line arguments
117 const ACE_TCHAR *group_arg_;
118 const ACE_TCHAR *policy_arg_;
119 const ACE_TCHAR *location_arg_;
120 const ACE_TCHAR *ior_arg_;
121 const ACE_TCHAR *namepath_arg_;
125 NSGROUP::NSGROUP (int argc, ACE_TCHAR **argv)
126 : argc_ (argc),
127 argv_ (argv),
128 nsgroup_cmd_(NSGROUP::NSGROUP_NONE),
129 group_arg_ (0),
130 policy_arg_ (0),
131 location_arg_ (0),
132 ior_arg_ (0),
133 namepath_arg_ (0)
138 NSGROUP::start_orb ()
140 const int RC_SUCCESS = 0;
141 const int RC_ERROR = -1;
145 #if 0
146 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("start_orb::argc(%u)\n"), this->argc_));
147 for( int i = 0; i < this->argc_; ++i){
148 ACE_DEBUG ((LM_DEBUG,
149 ACE_TEXT ("start_orb::argv(%u:%s)\n"),
151 this->argv_[i]));
153 #endif //
155 // Initialise the ORB.
157 CORBA::ORB_var orb_ = CORBA::ORB_init (this->argc_, this->argv_);
159 if (RC_SUCCESS != svc_.set_orb (orb_.in ()))
161 ACE_ERROR_RETURN (( LM_ERROR,
162 ACE_TEXT (" (%P|%t) Unable to initialize the ORB.\n")),
163 RC_ERROR);
166 //////////////////////////////////////////////////////////////////////////
168 //////////////////////////////////////////////////////////////////////////
169 CORBA::Object_var naming_manager_object =
170 orb_->resolve_initial_references ("NamingManager");
172 FT_Naming::NamingManager_var naming_manager_ =
173 FT_Naming::NamingManager::_narrow (naming_manager_object.in ());
175 if (RC_SUCCESS != svc_.set_naming_manager (naming_manager_.in ()))
177 ACE_ERROR_RETURN (( LM_ERROR,
178 ACE_TEXT (" (%P|%t) Unable to get Naming ")
179 ACE_TEXT ("Manager Reference\n")),
180 RC_ERROR);
182 //////////////////////////////////////////////////////////////////////////
184 //////////////////////////////////////////////////////////////////////////
185 CORBA::Object_var naming_object =
186 orb_->resolve_initial_references("NameService");
188 CosNaming::NamingContextExt_var name_service_ =
189 CosNaming::NamingContextExt::_narrow (naming_object.in ());
191 if (RC_SUCCESS != svc_.set_name_context (name_service_.in ()))
193 ACE_ERROR_RETURN (( LM_ERROR,
194 ACE_TEXT (" (%P|%t) Unable to get Name Service ")
195 ACE_TEXT ("Reference\n")),
196 RC_ERROR);
198 //////////////////////////////////////////////////////////////////////////
200 //////////////////////////////////////////////////////////////////////////
203 catch (const CORBA::Exception& ex)
205 ex._tao_print_exception (ACE_TEXT("\nException raised initialising ORB\n"));
206 return RC_ERROR;
209 return RC_SUCCESS;
213 NSGROUP::run_cmd()
215 const int RC_ERROR = -1;
216 const int RC_BADARG = -2;
218 int rc = RC_ERROR;
220 switch( parse_command_line () )
222 case NSGROUP_HELP:
223 return show_usage ();
224 break;
226 case NSGROUP_GROUP_CREATE:
227 rc = svc_.group_create ( group_arg(), policy_arg() );
228 break;
230 case NSGROUP_GROUP_BIND:
231 rc = svc_.group_bind ( group_arg(), namepath_arg() );
232 break;
234 case NSGROUP_GROUP_UNBIND:
235 rc = svc_.group_unbind ( namepath_arg() );
236 break;
238 case NSGROUP_GROUP_MODIFY:
239 rc = svc_.group_modify ( group_arg(), policy_arg() );
240 break;
242 case NSGROUP_GROUP_LIST:
243 rc = svc_.group_list ();
244 break;
246 case NSGROUP_GROUP_REMOVE:
247 rc = svc_.group_remove ( group_arg() );
248 break;
250 case NSGROUP_MEMBER_LIST:
251 rc = svc_.member_list ( group_arg() );
252 break;
254 case NSGROUP_MEMBER_ADD:
255 rc = svc_.member_add ( group_arg(), location_arg(), ior_arg() );
256 break;
258 case NSGROUP_MEMBER_REMOVE:
259 rc = svc_.member_remove ( group_arg(), location_arg() );
260 break;
262 case NSGROUP_MEMBER_SHOW:
263 rc = svc_.member_show ( group_arg(), location_arg() );
264 break;
266 default:
267 show_usage ();
268 return rc;
269 break;
272 if (rc == RC_BADARG)
274 show_usage ();
277 return rc;
280 NSGROUP::NSGROUP_COMMAND
281 NSGROUP::parse_command_line ()
283 #if 0
284 ACE_DEBUG ((LM_DEBUG,
285 ACE_TEXT ("parse_command_line::argc(%u)\n"),
286 this->argc_));
287 for( int i = 0; i < this->argc_; ++i){
288 ACE_DEBUG ((LM_DEBUG,
289 ACE_TEXT ("parse_command_line::argv(%u:%s)\n"),
291 this->argv_[i]));
293 #endif
295 static const ACE_TCHAR options[] = ACE_TEXT("g:p:t:l:i:n:h");
296 static const int skip_args = 1;
297 static const int report_errors = 0;
298 static const int ordering = ACE_Get_Opt::PERMUTE_ARGS;
299 static const int long_only = 1;
301 ACE_Get_Opt get_opts (
302 this->argc_,
303 this->argv_,
304 options,
305 skip_args,
306 report_errors,
307 ordering,
308 long_only
311 this->group_arg_ = 0;
312 if (get_opts.long_option (ACE_TEXT ("group"),
313 'g',
314 ACE_Get_Opt::ARG_REQUIRED) != 0)
315 ACE_ERROR_RETURN ((LM_ERROR,
316 ACE_TEXT ("Unable to add long option 'g'\n")),
317 NSGROUP_NONE);
319 this->policy_arg_ = 0;
320 if (get_opts.long_option (ACE_TEXT ("policy"),
321 'p',
322 ACE_Get_Opt::ARG_REQUIRED) != 0)
323 ACE_ERROR_RETURN ((LM_ERROR,
324 ACE_TEXT ("Unable to add long option 'p'\n")),
325 NSGROUP_NONE);
327 this->location_arg_ = 0;
328 if (get_opts.long_option (ACE_TEXT ("location"),
329 'l',
330 ACE_Get_Opt::ARG_REQUIRED) != 0)
331 ACE_ERROR_RETURN ((LM_ERROR,
332 ACE_TEXT ("Unable to add long option 'l'\n")),
333 NSGROUP_NONE);
335 this->ior_arg_ = 0;
336 if (get_opts.long_option (ACE_TEXT ("ior"),
337 'i',
338 ACE_Get_Opt::ARG_REQUIRED) != 0)
339 ACE_ERROR_RETURN ((LM_ERROR,
340 ACE_TEXT ("Unable to add long option 'i'\n")),
341 NSGROUP_NONE);
343 this->namepath_arg_ = 0;
344 if (get_opts.long_option (ACE_TEXT ("name"),
345 'n',
346 ACE_Get_Opt::ARG_REQUIRED) != 0)
347 ACE_ERROR_RETURN ((LM_ERROR,
348 ACE_TEXT ("Unable to add long option 'n'\n")),
349 NSGROUP_NONE);
351 if (get_opts.long_option (ACE_TEXT ("help"), 'h') != 0)
352 ACE_ERROR_RETURN ((LM_ERROR,
353 ACE_TEXT ("Unable to add long option 'h'\n")),
354 NSGROUP_NONE);
356 int c;
357 while ((c = get_opts ()) != -1)
358 switch (c)
360 case 'g': // group
361 this->group_arg_ = get_opts.opt_arg ();
362 break;
363 case 'p': // policy
364 this->policy_arg_ = get_opts.opt_arg ();
365 break;
366 case 'l': // location
367 this->location_arg_ = get_opts.opt_arg ();
368 break;
369 case 'i': // ior
370 this->ior_arg_ = get_opts.opt_arg ();
371 break;
372 case 'n': // name
373 this->namepath_arg_ = get_opts.opt_arg ();
374 break;
375 case 'h':
376 return NSGROUP_HELP;
379 // handle non-option arguments
380 int non_option_arg_count = 0;
381 for( int i = get_opts.opt_ind (); i < this->argc_; ++i)
383 non_option_arg_count++;
386 if( ACE_OS::strncmp (this->argv_[i],ACE_TEXT("group_create"),
387 ACE_OS::strlen (ACE_TEXT("group_create"))) == 0 )
389 nsgroup_cmd_ = NSGROUP_GROUP_CREATE;
391 else if(ACE_OS::strncmp (this->argv_[i], ACE_TEXT("group_bind"),
392 ACE_OS::strlen (ACE_TEXT("group_bind"))) == 0 )
394 nsgroup_cmd_ = NSGROUP_GROUP_BIND;
396 else if(ACE_OS::strncmp (this->argv_[i], ACE_TEXT("group_unbind"),
397 ACE_OS::strlen (ACE_TEXT("group_unbind")))
398 == 0 )
400 nsgroup_cmd_ = NSGROUP_GROUP_UNBIND;
402 else if(ACE_OS::strncmp (this->argv_[i], ACE_TEXT("group_modify"),
403 ACE_OS::strlen (ACE_TEXT("group_modify"))) == 0 )
405 /// reserve cmd NSGROUP_GROUP_MODIFY for future use
406 nsgroup_cmd_ = NSGROUP_NONE;//
408 else if(ACE_OS::strncmp (this->argv_[i], ACE_TEXT("group_list"),
409 ACE_OS::strlen (ACE_TEXT("group_list"))) == 0 )
411 nsgroup_cmd_ = NSGROUP_GROUP_LIST;
413 else if(ACE_OS::strncmp (this->argv_[i], ACE_TEXT("group_remove"),
414 ACE_OS::strlen (ACE_TEXT("group_remove"))) == 0 )
416 nsgroup_cmd_ = NSGROUP_GROUP_REMOVE;
418 else if(ACE_OS::strncmp (this->argv_[i], ACE_TEXT("member_list"),
419 ACE_OS::strlen (ACE_TEXT("member_list"))) == 0 )
421 nsgroup_cmd_ = NSGROUP_MEMBER_LIST;
423 else if(ACE_OS::strncmp (this->argv_[i], ACE_TEXT("member_add"),
424 ACE_OS::strlen (ACE_TEXT("member_add"))) == 0 )
426 nsgroup_cmd_ = NSGROUP_MEMBER_ADD;
428 else if(ACE_OS::strncmp (this->argv_[i], ACE_TEXT("member_remove"),
429 ACE_OS::strlen (ACE_TEXT("member_remove"))) == 0 )
431 nsgroup_cmd_ = NSGROUP_MEMBER_REMOVE;
433 else if(ACE_OS::strncmp (this->argv_[i], ACE_TEXT("member_show"),
434 ACE_OS::strlen (ACE_TEXT("member_show"))) == 0 )
436 nsgroup_cmd_ = NSGROUP_MEMBER_SHOW;
438 else
440 nsgroup_cmd_ = NSGROUP_NONE;
444 // The command should be the only non option argument
445 if ( non_option_arg_count > 1 ) {
446 nsgroup_cmd_ = NSGROUP_NONE;
449 return nsgroup_cmd_;
454 NSGROUP::show_usage ()
456 const int RC_SUCCESS = 0;
458 ACE_DEBUG ((LM_INFO,
459 ACE_TEXT ("Usage:\n")
460 ACE_TEXT (" %s\n")
461 ACE_TEXT (" group_create -group <group> -policy <round|random> \n")
462 ACE_TEXT (" group_bind -group <group> -name <name>\n")
463 ACE_TEXT (" group_unbind -name <name>\n")
464 ACE_TEXT (" group_list\n")
465 ACE_TEXT (" group_remove -group <group>\n")
466 ACE_TEXT (" member_list -group <group>\n")
467 ACE_TEXT (" member_add -group <group> -location <location> -ior <IOR>\n")
468 ACE_TEXT (" member_remove -group <group> -location <location>\n")
469 ACE_TEXT (" member_show -group <group> -location <location>\n")
470 ACE_TEXT (" -help\n")
471 ACE_TEXT ("\n"),
472 this->argv_[0]));
473 return RC_SUCCESS;
477 //============================================================================
479 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
481 const int RC_ERROR = -1;
482 const int RC_SUCCESS = 0;
484 int rc = RC_ERROR;
488 NSGROUP ns_group (argc, argv);
490 if ( show_help (argc, argv) )
492 rc = ns_group.show_usage ();
494 else if ( RC_SUCCESS == ns_group.start_orb ())
496 rc = ns_group.run_cmd ();
500 catch (const CORBA::Exception&)
502 ACE_ERROR_RETURN ((LM_ERROR,
503 ACE_TEXT ("Unable to run %s\n"),
504 argv[0]),
508 return (rc == RC_SUCCESS) ? 0 : 1;