1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
|
//==========================================================================
/**
* @file nsgroup.cpp
*
* @author Phillip LaBanca <labancap@ociweb.com>
*/
//==========================================================================
#include "orbsvcs/Naming/FaultTolerant/nsgroup_svc.h"
#include "ace/Get_Opt.h"
//============================================================================
bool
show_help(int argc, ACE_TCHAR *argv[])
{
static const ACE_TCHAR options[] = ACE_TEXT("h");
static const int skip_args = 1;
static const int report_errors = 0;
static const int ordering = ACE_Get_Opt::RETURN_IN_ORDER;
static const int long_only = 0;
// Not enough arguments indicates help is required
if( argc < 2 )
{
return true;
}
// Did they ask for help ?
ACE_Get_Opt get_opts (
argc,
argv,
options,
skip_args,
report_errors,
ordering,
long_only
);
if (get_opts.long_option (ACE_TEXT ("help"),'h') != 0)
{
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT (" Unable to add long option 'H'\n")),
true);
}
int c;
while ((c = get_opts ()) != -1)
{
switch (c)
{
case 'h': // help
return true;
break;
}
}
return false;
}
/**
* @class nsgroup
*
* @brief Encapsulate the NS group command line operations in a class.
*/
class NSGROUP
{
public:
enum NSGROUP_COMMAND {
NSGROUP_NONE,
NSGROUP_HELP,
NSGROUP_GROUP_CREATE,
NSGROUP_GROUP_BIND,
NSGROUP_GROUP_UNBIND,
NSGROUP_GROUP_MODIFY,
NSGROUP_GROUP_LIST,
NSGROUP_GROUP_REMOVE,
NSGROUP_MEMBER_LIST,
NSGROUP_MEMBER_ADD,
NSGROUP_MEMBER_REMOVE,
NSGROUP_MEMBER_SHOW
};
/// Constructor
NSGROUP (int argc, ACE_TCHAR **argv);
/// start the ORB.
int start_orb (void);
/// parse command line, validate arguments and run the command
int run_cmd (void);
/// Display command line interface usage
int show_usage( void );
const ACE_TCHAR * group_arg() const { return group_arg_; }
const ACE_TCHAR * policy_arg() const { return policy_arg_; }
const ACE_TCHAR * location_arg() const { return location_arg_; }
const ACE_TCHAR * ior_arg() const { return ior_arg_; }
const ACE_TCHAR * namepath_arg() const { return namepath_arg_; }
private:
/// parse command line arguments
NSGROUP_COMMAND parse_command_line (void);
private:
NS_group_svc svc_;
int argc_;
ACE_TCHAR **argv_;
/// parsed command result
NSGROUP_COMMAND nsgroup_cmd_;
/// parsed command line arguments
const ACE_TCHAR *group_arg_;
const ACE_TCHAR *policy_arg_;
const ACE_TCHAR *location_arg_;
const ACE_TCHAR *ior_arg_;
const ACE_TCHAR *namepath_arg_;
};
NSGROUP::NSGROUP (int argc, ACE_TCHAR **argv)
: argc_ (argc),
argv_ (argv),
nsgroup_cmd_(NSGROUP::NSGROUP_NONE),
group_arg_ (0),
policy_arg_ (0),
location_arg_ (0),
ior_arg_ (0),
namepath_arg_ (0)
{
}
int
NSGROUP::start_orb (void)
{
const int RC_SUCCESS = 0;
const int RC_ERROR = -1;
try
{
#if 0
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("start_orb::argc(%u)\n"), this->argc_));
for( int i = 0; i < this->argc_; ++i){
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("start_orb::argv(%u:%s)\n"),
i,
this->argv_[i]));
}
#endif //
// Initialise the ORB.
CORBA::ORB_var orb_ = CORBA::ORB_init (this->argc_, this->argv_);
if (RC_SUCCESS != svc_.set_orb (orb_.in ()))
{
ACE_ERROR_RETURN (( LM_ERROR,
ACE_TEXT (" (%P|%t) Unable to initialize the ORB.\n")),
RC_ERROR);
}
//////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////
CORBA::Object_var naming_manager_object =
orb_->resolve_initial_references ("NamingManager");
FT_Naming::NamingManager_var naming_manager_ =
FT_Naming::NamingManager::_narrow (naming_manager_object.in ());
if (RC_SUCCESS != svc_.set_naming_manager (naming_manager_.in ()))
{
ACE_ERROR_RETURN (( LM_ERROR,
ACE_TEXT (" (%P|%t) Unable to get Naming ")
ACE_TEXT ("Manager Reference\n")),
RC_ERROR);
}
//////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////
CORBA::Object_var naming_object =
orb_->resolve_initial_references("NameService");
CosNaming::NamingContextExt_var name_service_ =
CosNaming::NamingContextExt::_narrow (naming_object.in ());
if (RC_SUCCESS != svc_.set_name_context (name_service_.in ()))
{
ACE_ERROR_RETURN (( LM_ERROR,
ACE_TEXT (" (%P|%t) Unable to get Name Service ")
ACE_TEXT ("Reference\n")),
RC_ERROR);
}
//////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////
}
catch (const CORBA::Exception& ex)
{
ex._tao_print_exception (ACE_TEXT("\nException raised initialising ORB\n"));
return RC_ERROR;
}
return RC_SUCCESS;
}
int
NSGROUP::run_cmd(void)
{
const int RC_ERROR = -1;
const int RC_BADARG = -2;
int rc = RC_ERROR;
switch( parse_command_line () )
{
case NSGROUP_HELP:
return show_usage ();
break;
case NSGROUP_GROUP_CREATE:
rc = svc_.group_create ( group_arg(), policy_arg() );
break;
case NSGROUP_GROUP_BIND:
rc = svc_.group_bind ( group_arg(), namepath_arg() );
break;
case NSGROUP_GROUP_UNBIND:
rc = svc_.group_unbind ( namepath_arg() );
break;
case NSGROUP_GROUP_MODIFY:
rc = svc_.group_modify ( group_arg(), policy_arg() );
break;
case NSGROUP_GROUP_LIST:
rc = svc_.group_list ();
break;
case NSGROUP_GROUP_REMOVE:
rc = svc_.group_remove ( group_arg() );
break;
case NSGROUP_MEMBER_LIST:
rc = svc_.member_list ( group_arg() );
break;
case NSGROUP_MEMBER_ADD:
rc = svc_.member_add ( group_arg(), location_arg(), ior_arg() );
break;
case NSGROUP_MEMBER_REMOVE:
rc = svc_.member_remove ( group_arg(), location_arg() );
break;
case NSGROUP_MEMBER_SHOW:
rc = svc_.member_show ( group_arg(), location_arg() );
break;
default:
show_usage ();
return rc;
break;
}
if (rc == RC_BADARG)
{
show_usage ();
}
return rc;
}
NSGROUP::NSGROUP_COMMAND
NSGROUP::parse_command_line (void)
{
#if 0
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("parse_command_line::argc(%u)\n"),
this->argc_));
for( int i = 0; i < this->argc_; ++i){
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("parse_command_line::argv(%u:%s)\n"),
i,
this->argv_[i]));
}
#endif
static const ACE_TCHAR options[] = ACE_TEXT("g:p:t:l:i:n:h");
static const int skip_args = 1;
static const int report_errors = 0;
static const int ordering = ACE_Get_Opt::PERMUTE_ARGS;
static const int long_only = 1;
ACE_Get_Opt get_opts (
this->argc_,
this->argv_,
options,
skip_args,
report_errors,
ordering,
long_only
);
this->group_arg_ = 0;
if (get_opts.long_option (ACE_TEXT ("group"),
'g',
ACE_Get_Opt::ARG_REQUIRED) != 0)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Unable to add long option 'g'\n")),
NSGROUP_NONE);
this->policy_arg_ = 0;
if (get_opts.long_option (ACE_TEXT ("policy"),
'p',
ACE_Get_Opt::ARG_REQUIRED) != 0)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Unable to add long option 'p'\n")),
NSGROUP_NONE);
this->location_arg_ = 0;
if (get_opts.long_option (ACE_TEXT ("location"),
'l',
ACE_Get_Opt::ARG_REQUIRED) != 0)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Unable to add long option 'l'\n")),
NSGROUP_NONE);
this->ior_arg_ = 0;
if (get_opts.long_option (ACE_TEXT ("ior"),
'i',
ACE_Get_Opt::ARG_REQUIRED) != 0)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Unable to add long option 'i'\n")),
NSGROUP_NONE);
this->namepath_arg_ = 0;
if (get_opts.long_option (ACE_TEXT ("name"),
'n',
ACE_Get_Opt::ARG_REQUIRED) != 0)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Unable to add long option 'n'\n")),
NSGROUP_NONE);
if (get_opts.long_option (ACE_TEXT ("help"), 'h') != 0)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Unable to add long option 'h'\n")),
NSGROUP_NONE);
int c;
while ((c = get_opts ()) != -1)
switch (c)
{
case 'g': // group
this->group_arg_ = get_opts.opt_arg ();
break;
case 'p': // policy
this->policy_arg_ = get_opts.opt_arg ();
break;
case 'l': // location
this->location_arg_ = get_opts.opt_arg ();
break;
case 'i': // ior
this->ior_arg_ = get_opts.opt_arg ();
break;
case 'n': // name
this->namepath_arg_ = get_opts.opt_arg ();
break;
case 'h':
return NSGROUP_HELP;
}
// handle non-option arguments
int non_option_arg_count = 0;
for( int i = get_opts.opt_ind (); i < this->argc_; ++i)
{
non_option_arg_count++;
if( ACE_OS::strncmp (this->argv_[i],ACE_TEXT("group_create"),
ACE_OS::strlen (ACE_TEXT("group_create"))) == 0 )
{
nsgroup_cmd_ = NSGROUP_GROUP_CREATE;
}
else if(ACE_OS::strncmp (this->argv_[i], ACE_TEXT("group_bind"),
ACE_OS::strlen (ACE_TEXT("group_bind"))) == 0 )
{
nsgroup_cmd_ = NSGROUP_GROUP_BIND;
}
else if(ACE_OS::strncmp (this->argv_[i], ACE_TEXT("group_unbind"),
ACE_OS::strlen (ACE_TEXT("group_unbind")))
== 0 )
{
nsgroup_cmd_ = NSGROUP_GROUP_UNBIND;
}
else if(ACE_OS::strncmp (this->argv_[i], ACE_TEXT("group_modify"),
ACE_OS::strlen (ACE_TEXT("group_modify"))) == 0 )
{
/// reserve cmd NSGROUP_GROUP_MODIFY for future use
nsgroup_cmd_ = NSGROUP_NONE;//
}
else if(ACE_OS::strncmp (this->argv_[i], ACE_TEXT("group_list"),
ACE_OS::strlen (ACE_TEXT("group_list"))) == 0 )
{
nsgroup_cmd_ = NSGROUP_GROUP_LIST;
}
else if(ACE_OS::strncmp (this->argv_[i], ACE_TEXT("group_remove"),
ACE_OS::strlen (ACE_TEXT("group_remove"))) == 0 )
{
nsgroup_cmd_ = NSGROUP_GROUP_REMOVE;
}
else if(ACE_OS::strncmp (this->argv_[i], ACE_TEXT("member_list"),
ACE_OS::strlen (ACE_TEXT("member_list"))) == 0 )
{
nsgroup_cmd_ = NSGROUP_MEMBER_LIST;
}
else if(ACE_OS::strncmp (this->argv_[i], ACE_TEXT("member_add"),
ACE_OS::strlen (ACE_TEXT("member_add"))) == 0 )
{
nsgroup_cmd_ = NSGROUP_MEMBER_ADD;
}
else if(ACE_OS::strncmp (this->argv_[i], ACE_TEXT("member_remove"),
ACE_OS::strlen (ACE_TEXT("member_remove"))) == 0 )
{
nsgroup_cmd_ = NSGROUP_MEMBER_REMOVE;
}
else if(ACE_OS::strncmp (this->argv_[i], ACE_TEXT("member_show"),
ACE_OS::strlen (ACE_TEXT("member_show"))) == 0 )
{
nsgroup_cmd_ = NSGROUP_MEMBER_SHOW;
}
else
{
nsgroup_cmd_ = NSGROUP_NONE;
}
}
// The command should be the only non option argument
if ( non_option_arg_count > 1 ) {
nsgroup_cmd_ = NSGROUP_NONE;
}
return nsgroup_cmd_;
}
int
NSGROUP::show_usage ( void )
{
const int RC_SUCCESS = 0;
ACE_DEBUG ((LM_INFO,
ACE_TEXT ("Usage:\n")
ACE_TEXT (" %s\n")
ACE_TEXT (" group_create -group <group> -policy <round|random> \n")
ACE_TEXT (" group_bind -group <group> -name <name>\n")
ACE_TEXT (" group_unbind -name <name>\n")
ACE_TEXT (" group_list\n")
ACE_TEXT (" group_remove -group <group>\n")
ACE_TEXT (" member_list -group <group>\n")
ACE_TEXT (" member_add -group <group> -location <location> -ior <IOR>\n")
ACE_TEXT (" member_remove -group <group> -location <location>\n")
ACE_TEXT (" member_show -group <group> -location <location>\n")
ACE_TEXT (" -help\n")
ACE_TEXT ("\n"),
this->argv_[0]));
return RC_SUCCESS;
}
//============================================================================
int
ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
const int RC_ERROR = -1;
const int RC_SUCCESS = 0;
int rc = RC_ERROR;
try
{
NSGROUP ns_group (argc, argv);
if ( show_help (argc, argv) )
{
rc = ns_group.show_usage ();
}
else if ( RC_SUCCESS == ns_group.start_orb ())
{
rc = ns_group.run_cmd ();
}
}
catch (const CORBA::Exception&)
{
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Unable to run %s\n"),
argv[0]),
1);
}
return (rc == RC_SUCCESS) ? 0 : 1;
}
|