1 //==========================================================================
5 * @author Phillip LaBanca <labancap@ociweb.com>
7 //==========================================================================
10 #include "orbsvcs/Naming/FaultTolerant/nsgroup_svc.h"
11 #include "ace/Get_Opt.h"
13 //============================================================================
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
29 // Did they ask for help ?
30 ACE_Get_Opt
get_opts (
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")),
48 while ((c
= get_opts ()) != -1)
64 * @brief Encapsulate the NS group command line operations in a class.
70 enum NSGROUP_COMMAND
{
81 NSGROUP_MEMBER_REMOVE
,
86 NSGROUP (int argc
, ACE_TCHAR
**argv
);
91 /// parse command line, validate arguments and run the command
94 /// Display command line interface 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_
; }
104 /// parse command line arguments
105 NSGROUP_COMMAND
parse_command_line ();
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
)
128 nsgroup_cmd_(NSGROUP::NSGROUP_NONE
),
138 NSGROUP::start_orb ()
140 const int RC_SUCCESS
= 0;
141 const int RC_ERROR
= -1;
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"),
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")),
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")),
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")),
198 //////////////////////////////////////////////////////////////////////////
200 //////////////////////////////////////////////////////////////////////////
203 catch (const CORBA::Exception
& ex
)
205 ex
._tao_print_exception (ACE_TEXT("\nException raised initialising ORB\n"));
215 const int RC_ERROR
= -1;
216 const int RC_BADARG
= -2;
220 switch( parse_command_line () )
223 return show_usage ();
226 case NSGROUP_GROUP_CREATE
:
227 rc
= svc_
.group_create ( group_arg(), policy_arg() );
230 case NSGROUP_GROUP_BIND
:
231 rc
= svc_
.group_bind ( group_arg(), namepath_arg() );
234 case NSGROUP_GROUP_UNBIND
:
235 rc
= svc_
.group_unbind ( namepath_arg() );
238 case NSGROUP_GROUP_MODIFY
:
239 rc
= svc_
.group_modify ( group_arg(), policy_arg() );
242 case NSGROUP_GROUP_LIST
:
243 rc
= svc_
.group_list ();
246 case NSGROUP_GROUP_REMOVE
:
247 rc
= svc_
.group_remove ( group_arg() );
250 case NSGROUP_MEMBER_LIST
:
251 rc
= svc_
.member_list ( group_arg() );
254 case NSGROUP_MEMBER_ADD
:
255 rc
= svc_
.member_add ( group_arg(), location_arg(), ior_arg() );
258 case NSGROUP_MEMBER_REMOVE
:
259 rc
= svc_
.member_remove ( group_arg(), location_arg() );
262 case NSGROUP_MEMBER_SHOW
:
263 rc
= svc_
.member_show ( group_arg(), location_arg() );
280 NSGROUP::NSGROUP_COMMAND
281 NSGROUP::parse_command_line ()
284 ACE_DEBUG ((LM_DEBUG
,
285 ACE_TEXT ("parse_command_line::argc(%u)\n"),
287 for( int i
= 0; i
< this->argc_
; ++i
){
288 ACE_DEBUG ((LM_DEBUG
,
289 ACE_TEXT ("parse_command_line::argv(%u:%s)\n"),
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 (
311 this->group_arg_
= 0;
312 if (get_opts
.long_option (ACE_TEXT ("group"),
314 ACE_Get_Opt::ARG_REQUIRED
) != 0)
315 ACE_ERROR_RETURN ((LM_ERROR
,
316 ACE_TEXT ("Unable to add long option 'g'\n")),
319 this->policy_arg_
= 0;
320 if (get_opts
.long_option (ACE_TEXT ("policy"),
322 ACE_Get_Opt::ARG_REQUIRED
) != 0)
323 ACE_ERROR_RETURN ((LM_ERROR
,
324 ACE_TEXT ("Unable to add long option 'p'\n")),
327 this->location_arg_
= 0;
328 if (get_opts
.long_option (ACE_TEXT ("location"),
330 ACE_Get_Opt::ARG_REQUIRED
) != 0)
331 ACE_ERROR_RETURN ((LM_ERROR
,
332 ACE_TEXT ("Unable to add long option 'l'\n")),
336 if (get_opts
.long_option (ACE_TEXT ("ior"),
338 ACE_Get_Opt::ARG_REQUIRED
) != 0)
339 ACE_ERROR_RETURN ((LM_ERROR
,
340 ACE_TEXT ("Unable to add long option 'i'\n")),
343 this->namepath_arg_
= 0;
344 if (get_opts
.long_option (ACE_TEXT ("name"),
346 ACE_Get_Opt::ARG_REQUIRED
) != 0)
347 ACE_ERROR_RETURN ((LM_ERROR
,
348 ACE_TEXT ("Unable to add long option 'n'\n")),
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")),
357 while ((c
= get_opts ()) != -1)
361 this->group_arg_
= get_opts
.opt_arg ();
364 this->policy_arg_
= get_opts
.opt_arg ();
366 case 'l': // location
367 this->location_arg_
= get_opts
.opt_arg ();
370 this->ior_arg_
= get_opts
.opt_arg ();
373 this->namepath_arg_
= get_opts
.opt_arg ();
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")))
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
;
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
;
454 NSGROUP::show_usage ()
456 const int RC_SUCCESS
= 0;
459 ACE_TEXT ("Usage:\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")
477 //============================================================================
479 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
481 const int RC_ERROR
= -1;
482 const int RC_SUCCESS
= 0;
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"),
508 return (rc
== RC_SUCCESS
) ? 0 : 1;