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.
71 enum NSGROUP_COMMAND
{
82 NSGROUP_MEMBER_REMOVE
,
87 NSGROUP (int argc
, ACE_TCHAR
**argv
);
92 /// parse command line, validate arguments and run the command
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_
; }
106 /// parse command line arguments
107 NSGROUP_COMMAND
parse_command_line (void);
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
)
131 nsgroup_cmd_(NSGROUP::NSGROUP_NONE
),
141 NSGROUP::start_orb (void)
143 const int RC_SUCCESS
= 0;
144 const int RC_ERROR
= -1;
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"),
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")),
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")),
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")),
203 //////////////////////////////////////////////////////////////////////////
205 //////////////////////////////////////////////////////////////////////////
208 catch (const CORBA::Exception
& ex
)
210 ex
._tao_print_exception (ACE_TEXT("\nException raised initialising ORB\n"));
219 NSGROUP::run_cmd(void)
221 const int RC_ERROR
= -1;
222 const int RC_BADARG
= -2;
226 switch( parse_command_line () )
230 return show_usage ();
233 case NSGROUP_GROUP_CREATE
:
234 rc
= svc_
.group_create ( group_arg(), policy_arg() );
237 case NSGROUP_GROUP_BIND
:
238 rc
= svc_
.group_bind ( group_arg(), namepath_arg() );
241 case NSGROUP_GROUP_UNBIND
:
242 rc
= svc_
.group_unbind ( namepath_arg() );
245 case NSGROUP_GROUP_MODIFY
:
246 rc
= svc_
.group_modify ( group_arg(), policy_arg() );
249 case NSGROUP_GROUP_LIST
:
250 rc
= svc_
.group_list ();
253 case NSGROUP_GROUP_REMOVE
:
254 rc
= svc_
.group_remove ( group_arg() );
257 case NSGROUP_MEMBER_LIST
:
258 rc
= svc_
.member_list ( group_arg() );
261 case NSGROUP_MEMBER_ADD
:
262 rc
= svc_
.member_add ( group_arg(), location_arg(), ior_arg() );
265 case NSGROUP_MEMBER_REMOVE
:
266 rc
= svc_
.member_remove ( group_arg(), location_arg() );
269 case NSGROUP_MEMBER_SHOW
:
270 rc
= svc_
.member_show ( group_arg(), location_arg() );
287 NSGROUP::NSGROUP_COMMAND
288 NSGROUP::parse_command_line (void)
291 ACE_DEBUG ((LM_DEBUG
,
292 ACE_TEXT ("parse_command_line::argc(%u)\n"),
294 for( int i
= 0; i
< this->argc_
; ++i
){
295 ACE_DEBUG ((LM_DEBUG
,
296 ACE_TEXT ("parse_command_line::argv(%u:%s)\n"),
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 (
318 this->group_arg_
= 0;
319 if (get_opts
.long_option (ACE_TEXT ("group"),
321 ACE_Get_Opt::ARG_REQUIRED
) != 0)
322 ACE_ERROR_RETURN ((LM_ERROR
,
323 ACE_TEXT ("Unable to add long option 'g'\n")),
326 this->policy_arg_
= 0;
327 if (get_opts
.long_option (ACE_TEXT ("policy"),
329 ACE_Get_Opt::ARG_REQUIRED
) != 0)
330 ACE_ERROR_RETURN ((LM_ERROR
,
331 ACE_TEXT ("Unable to add long option 'p'\n")),
334 this->location_arg_
= 0;
335 if (get_opts
.long_option (ACE_TEXT ("location"),
337 ACE_Get_Opt::ARG_REQUIRED
) != 0)
338 ACE_ERROR_RETURN ((LM_ERROR
,
339 ACE_TEXT ("Unable to add long option 'l'\n")),
343 if (get_opts
.long_option (ACE_TEXT ("ior"),
345 ACE_Get_Opt::ARG_REQUIRED
) != 0)
346 ACE_ERROR_RETURN ((LM_ERROR
,
347 ACE_TEXT ("Unable to add long option 'i'\n")),
350 this->namepath_arg_
= 0;
351 if (get_opts
.long_option (ACE_TEXT ("name"),
353 ACE_Get_Opt::ARG_REQUIRED
) != 0)
354 ACE_ERROR_RETURN ((LM_ERROR
,
355 ACE_TEXT ("Unable to add long option 'n'\n")),
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")),
364 while ((c
= get_opts ()) != -1)
368 this->group_arg_
= get_opts
.opt_arg ();
371 this->policy_arg_
= get_opts
.opt_arg ();
373 case 'l': // location
374 this->location_arg_
= get_opts
.opt_arg ();
377 this->ior_arg_
= get_opts
.opt_arg ();
380 this->namepath_arg_
= get_opts
.opt_arg ();
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")))
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
;
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
;
463 NSGROUP::show_usage ( void )
465 const int RC_SUCCESS
= 0;
468 ACE_TEXT ("Usage:\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")
486 //============================================================================
488 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
490 const int RC_ERROR
= -1;
491 const int RC_SUCCESS
= 0;
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"),
518 return (rc
== RC_SUCCESS
) ? 0 : 1;