summaryrefslogtreecommitdiff
path: root/netsvcs/clients/Naming/Client/Client_Test.cpp
blob: 0f87432d246dd0d7310e3f32736591ef70d288da (plain)
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
// $Id$

#define ACE_BUILD_SVC_DLL

#include "ace/Service_Config.h"
#include "ace/Naming_Context.h"
#include "ace/Dynamic_Service.h"
#include "ace/Thread_Manager.h"
#include "Client_Test.h"
#include "ace/os_include/os_ctype.h"
#include "ace/OS_NS_signal.h"
#include "ace/OS_NS_stdio.h"
#include "ace/OS_NS_string.h"
#include "ace/OS_NS_unistd.h"
#include "ace/os_include/os_assert.h"

ACE_RCSID(Client, Client_Test, "$Id$")

class ACE_Svc_Export Client_Test : public ACE_Service_Object
{
public:
  Client_Test (void);

  int open (void);
  // Cache reactor and then register self with reactor

  int close (void);
  // Close things down and free up resources.

  virtual int handle_input (ACE_HANDLE handle);
  // Handle user entered commands

  virtual int init (int argc, char *argv[]);
  // Initialize name options and naming context when dynamically
  // linked.

  virtual int fini (void);
  // Close down the test when dynamically unlinked.

  void list_options (void);
  // Print name options

  int bind (const char *key,
            const char *value,
            const char *type = "");
  // Bind a key to a value

  int unbind (const char *key);
  // Unbind a name binding

  int rebind (const char *key,
              const char *value,
              const char *type = "");
  // Rebind a name binding

  int find (const char *key);
  // Find the value associated with a key

  int list_names (const char *pattern);
  // Find all names that match pattern

  int list_values (const char *pattern);
  // Find all values that match pattern

  int list_types (const char *pattern);
  // Find all types that match pattern

  int list_name_entries (const char *pattern);
  // Find all names that match pattern

  int list_value_entries (const char *pattern);
  // Find all values that match pattern

  int list_type_entries (const char *pattern);
  // Find all types that match pattern

private:
  ACE_Name_Options *name_options_;
  // Name Options associated with the Naming Context

  void display_menu (void);
  // Display user menu

  int set_proc_local (void);
  // Set options to use PROC_LOCAL naming context

  int set_node_local (void);
  // Set options to use NODE_LOCAL naming context

  int set_host (const char *hostname, int port);
  // Set options to use NET_LOCAL naming context specifying host name
  // and port number

  int quit (void);
  // Gracefully exit
};

// The following Factory is used by the ACE_Service_Config and
// svc.conf file to dynamically initialize the state of the client
// test.

ACE_SVC_FACTORY_DEFINE (Client_Test)

// Get the instance of Name_Service using Dynamic_Service

//inline Name_Service *
//NAME_SERVICE (void)

inline ACE_Naming_Context *
NAMING_CONTEXT (void)
{
  return ACE_Dynamic_Service<ACE_Naming_Context>::instance ("ACE_Naming_Context");
}

Client_Test::Client_Test (void)
{
  ACE_DEBUG ((LM_DEBUG,
              "Client_Test::Client_Test\n"));
}

int
Client_Test::init (int /* argc */,
                   char * /* argv */ [])
{
  ACE_DEBUG ((LM_DEBUG, "Client_Test::init\n"));

  // Cache the name options.
  this->name_options_ = NAMING_CONTEXT ()->name_options ();
  return this->open ();
}

int
Client_Test::open (void)
{
  this->display_menu ();

  if (ACE_Event_Handler::register_stdin_handler (this,
                                                 ACE_Reactor::instance (),
                                                 ACE_Thread_Manager::instance ()) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "register_stdin_handler"),
                      -1);
  return 0;
}


int
Client_Test::close (void)
{
  // Deregister this handler with the ACE_Reactor.
  return ACE_Reactor::instance ()->remove_handler
    (ACE_STDIN,
     ACE_Event_Handler::DONT_CALL | ACE_Event_Handler::READ_MASK);
}

int
Client_Test::fini (void)
{
  ACE_DEBUG ((LM_DEBUG,
              "Client_Test::fini\n"));
  return this->close ();
}

int
Client_Test::handle_input (ACE_HANDLE)
{
  char option[BUFSIZ];
  char buf1[BUFSIZ];
  char buf2[BUFSIZ];
  char buf3[BUFSIZ];
  char *temp_buf;
  int port;
  char input[1024];

  if (::scanf ("%s", option) <= 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p Try again!\n",
                       "Client_Test::handle_input"),
                      0);

  int result = -1;

  switch (isupper (option[0]) ? tolower (option[0]) : option[0])
    {
    case 'p' :
      result = this->set_proc_local ();
      break;
    case 'n' :
      result = this->set_node_local ();
      break;
    case 'h' :
      if (::scanf ("%s %d", buf1, &port) <= 0)
        break;
      result = this->set_host (buf1, port);
      break;
    case 'b' :
      // get the input from stdin
      ACE_OS::fgets (input, sizeof input, stdin);

      // get the key
      if ((temp_buf = ACE_OS::strtok (input, " ")))
        {
          ACE_OS::strcpy (buf1, temp_buf);

          temp_buf = ACE_OS::strtok (0, " ");

          // get the value
          if (temp_buf)
            {
              ACE_OS::strcpy (buf2, temp_buf);

              temp_buf = ACE_OS::strtok (0, " ");

              // get the type (if entered).
              if (temp_buf)
                {
                  ACE_OS::strcpy (buf3, temp_buf);
                  result = this->bind (buf1, buf2, buf3);
                }
              else
                result = this->bind (buf1, buf2);
            }
          else
            ACE_ERROR ((LM_ERROR,
                        "Bind Failed! Value not entered.\n"));
        }
      else
        ACE_ERROR ((LM_ERROR,
                    "Bind Failed! Key and Value not entered.\n"));
      break;
    case 'u' :
      if (::scanf ("%s", buf1) <= 0)
        break;
      result = this->unbind (buf1);
      break;
    case 'r' :
      // get the input from stdin
      ACE_OS::fgets (input, sizeof input, stdin);

      temp_buf = ACE_OS::strtok (input, " ");
      // get the key
      if (temp_buf)
        {
          ACE_OS::strcpy (buf1, temp_buf);

          temp_buf = ACE_OS::strtok (0, " ");

          // get the value
          if (temp_buf)
            {
              ACE_OS::strcpy (buf2, temp_buf);

              temp_buf = ACE_OS::strtok (0, " ");
              // get the type (if entered)
              if (temp_buf)
                {
                  ACE_OS::strcpy (buf3, temp_buf);
                  result = this->rebind (buf1, buf2, buf3);
                }
              else
                result = this->rebind (buf1, buf2);
            }
          else
            ACE_ERROR ((LM_ERROR,
                        "Rebind Failed! Value not entered.\n"));
        }
      else
        ACE_ERROR ((LM_ERROR,
                    "Reind Failed! Key and value not entered.\n"));
      break;
    case 'f' :
      if (::scanf ("%s", buf1) <= 0)
        break;
      result = this->find (buf1);
      break;
    case 'j' :
      if (::scanf ("%s", buf1) <= 0)
        break;
      else
        result = this->list_names (buf1);
      break;
    case 'k' :
      if (::scanf ("%s", buf1) <= 0)
        break;
      else
        result = this->list_values (buf1);
      break;
    case 'l' :
      if (::scanf ("%s", buf1) <= 0)
        break;
      else
        result = this->list_types (buf1);
      break;
    case 'c' :
      if (::scanf ("%s", buf1) <= 0)
        break;
      else
        result = this->list_name_entries (buf1);
      break;
    case 'd' :
      if (::scanf ("%s", buf1) <= 0)
        break;
      else
        result = this->list_value_entries (buf1);
      break;
    case 'e' :
      if (::scanf ("%s", buf1) <= 0)
        break;
      else
        result = this->list_type_entries (buf1);
      break;
    case 'q' :
      result = this->quit ();
      break;
    default :
      ACE_DEBUG ((LM_DEBUG,
                  "Unrecognized command.\n"));
    }

  this->display_menu ();
  return result;
}

void
Client_Test::display_menu (void)
{
  ACE_DEBUG ((LM_DEBUG, "\n"));
  this->list_options ();
  ACE_DEBUG ((LM_DEBUG, "         Name Service Main Menu\n"));
  ACE_DEBUG ((LM_DEBUG, "         ----------------------\n"));
  ACE_DEBUG ((LM_DEBUG, "<P> Use Process Local Database\n"));
  ACE_DEBUG ((LM_DEBUG, "<N> Use Node Local Database\n"));;
  ACE_DEBUG ((LM_DEBUG, "<H> Set Remote Name server <host> and <port>\n\n"));
  ACE_DEBUG ((LM_DEBUG, "<B> Bind <key> <value> [<type>]\n"));
  ACE_DEBUG ((LM_DEBUG, "<U> Unbind <key>\n"));
  ACE_DEBUG ((LM_DEBUG, "<R> Rebind <key> <value> [<type>]\n"));
  ACE_DEBUG ((LM_DEBUG, "<F> Find <key>\n"));
  ACE_DEBUG ((LM_DEBUG, "<J> Lookup keys matching <pattern>\n"));
  ACE_DEBUG ((LM_DEBUG, "<K> Lookup values matching <pattern>\n"));
  ACE_DEBUG ((LM_DEBUG, "<L> Lookup types matching <pattern>\n"));
  ACE_DEBUG ((LM_DEBUG, "<C> Complete lookup keys matching <pattern>\n"));
  ACE_DEBUG ((LM_DEBUG, "<D> Complete lookup values matching <pattern>\n"));
  ACE_DEBUG ((LM_DEBUG, "<E> Complete lookup types matching <pattern>\n"));

  ACE_DEBUG ((LM_DEBUG, "<Q> or ^C (exit)\n"));
}

void
Client_Test::list_options (void)
{
  switch (this->name_options_->context ())
    {
    case ACE_Naming_Context::PROC_LOCAL:
      ACE_DEBUG ((LM_DEBUG,
                  "  *** Using Process Local Database\n"));
      break;
    case ACE_Naming_Context::NODE_LOCAL:
      ACE_DEBUG ((LM_DEBUG,
                  "  *** Using Node Local Database\n"));
      break;
    case ACE_Naming_Context::NET_LOCAL:
      ACE_DEBUG ((LM_DEBUG,
                  "  *** Hostname: %s\n",
                  this->name_options_->nameserver_host ()));
      ACE_DEBUG ((LM_DEBUG,
                  "  *** Port Number: %d\n",
                  this->name_options_->nameserver_port ()));
      break;
    default:
      assert (!"shouldn't occur!\n");
      /* NOTREACHED */
    }
  ACE_DEBUG ((LM_DEBUG,
              "  *** Namespace directory is %s ***\n",
              this->name_options_->namespace_dir ()));
}

int
Client_Test::set_proc_local (void)
{
  // Close down original name space
  NAMING_CONTEXT ()->close ();
  this->name_options_->nameserver_host (ACE_TEXT ("localhost"));
  this->name_options_->context (ACE_Naming_Context::PROC_LOCAL);
  return NAMING_CONTEXT ()->open (ACE_Naming_Context::PROC_LOCAL);
}

int
Client_Test::set_node_local (void)
{
  // Close down original name space
  NAMING_CONTEXT ()->close ();
  this->name_options_->nameserver_host (ACE_TEXT ("localhost"));
  this->name_options_->context (ACE_Naming_Context::NODE_LOCAL);
  return NAMING_CONTEXT ()->open (ACE_Naming_Context::NODE_LOCAL);
}

int
Client_Test::set_host (const char *hostname, int port)
{
  // Close down original name space
  NAMING_CONTEXT ()->close ();

  this->name_options_->context (ACE_Naming_Context::NET_LOCAL);
  // Set Name Options
  this->name_options_->nameserver_host (ACE_TEXT_CHAR_TO_TCHAR (hostname));
  this->name_options_->nameserver_port (port);

  return NAMING_CONTEXT ()->open (ACE_Naming_Context::NET_LOCAL);
}

int
Client_Test::quit (void)
{
  // Send ourselves a SIGINT!
  return ACE_OS::kill (ACE_OS::getpid (), SIGINT);
}

int
Client_Test::bind (const char *key,
                   const char *value,
                   const char *type)
{
  if (NAMING_CONTEXT ()->bind (key, value, type) != 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p Bind failed! Key %s exists\n",
                       "Client_Test::bind",
                       key),
                      0);
  return 0;
}

int
Client_Test::unbind (const char *key)
{
  if (NAMING_CONTEXT ()->unbind (key) != 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p Unbind failed! Key %s not found\n",
                       "Client_Test::unbind",
                       key),
                      0);
  return 0;
}

int
Client_Test::rebind (const char *key,
                     const char *value,
                     const char *type)
{
  int result = NAMING_CONTEXT ()->rebind (key, value, type );
  return result == 1 ? 0 : result;
}

int
Client_Test::list_names (const char *pattern)
{
  ACE_PWSTRING_SET set;

  if (NAMING_CONTEXT ()->list_names (set, pattern) != 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p Pattern matching failed!\n",
                       "Client_Test::list_names"),
                      0);
  else
    {
      ACE_PWSTRING_ITERATOR set_iterator (set);

      for (ACE_NS_WString *name = 0;
           set_iterator.next (name) !=0;
           set_iterator.advance())
        ACE_DEBUG ((LM_DEBUG,
                    "%s\n",
                    name->char_rep ()));
    }
  return 0;
}

int
Client_Test::list_values (const char *pattern)
{
  ACE_PWSTRING_SET set;

  if (NAMING_CONTEXT ()->list_values (set, pattern) != 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p Pattern matching failed!\n",
                       "Client_Test::list_values"),
                      0);
  else
    {
      ACE_PWSTRING_ITERATOR set_iterator (set);

      for (ACE_NS_WString *value = 0;
           set_iterator.next (value) !=0;
           set_iterator.advance())
        ACE_DEBUG ((LM_DEBUG,
                    "%s\n",
                    value->char_rep ()));
    }
  return 0;
}

int
Client_Test::list_types (const char *pattern)
{
  ACE_PWSTRING_SET set;

  if (NAMING_CONTEXT ()->list_types (set, pattern) != 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p Pattern matching failed!\n",
                       "Client_Test::list_types"),
                      0);
  else
    {
      ACE_PWSTRING_ITERATOR set_iterator (set);

      for (ACE_NS_WString *type = 0;
           set_iterator.next (type) !=0;
           set_iterator.advance())
        ACE_DEBUG ((LM_DEBUG,
                    "%s\n",
                    type->char_rep ()));
    }
  return 0;
}

int
Client_Test::list_name_entries (const char *pattern)
{
  ACE_BINDING_SET set;

  if (NAMING_CONTEXT ()->list_name_entries (set, pattern) != 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p Pattern matching failed!\n",
                       "Client_Test::list_names"),
                      0);
  else
    {
      ACE_BINDING_ITERATOR set_iterator (set);

      for (ACE_Name_Binding *entry = 0;
           set_iterator.next (entry) !=0;
           set_iterator.advance())
        {
          ACE_DEBUG ((LM_DEBUG,
                      "%s\t",
                      entry->name_.char_rep ()));
          ACE_DEBUG ((LM_DEBUG,
                      "%s\t",
                      entry->value_.char_rep ()));
          if (entry->type_)
            ACE_DEBUG ((LM_DEBUG,
                        "%s\n",
                        entry->type_));
        }
    }
  return 0;
}

int
Client_Test::list_value_entries (const char *pattern)
{
  ACE_BINDING_SET set;

  if (NAMING_CONTEXT ()->list_value_entries (set, pattern) != 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p Pattern matching failed!\n",
                       "Client_Test::list_values"),
                      0);
  else
    {
      ACE_BINDING_ITERATOR set_iterator (set);
      for (ACE_Name_Binding *entry = 0;
           set_iterator.next (entry) !=0;
           set_iterator.advance())
        {
          ACE_DEBUG ((LM_DEBUG,
                      "%s\t",
                      entry->name_.char_rep ()));
          ACE_DEBUG ((LM_DEBUG,
                      "%s\t",
                      entry->value_.char_rep ()));
          if (entry->type_)
            ACE_DEBUG ((LM_DEBUG,
                        "%s\n",
                        entry->type_));
        }
    }
  return 0;
}

int
Client_Test::list_type_entries (const char *pattern)
{
  ACE_BINDING_SET set;

  if (NAMING_CONTEXT ()->list_type_entries (set, pattern) != 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p Pattern matching failed!\n",
                       "Client_Test::list_types"),
                      0);
  else
    {
      ACE_BINDING_ITERATOR set_iterator (set);

      for (ACE_Name_Binding *entry = 0;
           set_iterator.next (entry) !=0;
           set_iterator.advance())
        {
          ACE_DEBUG ((LM_DEBUG,
                      "%s\t",
                      entry->name_.char_rep ()));
          ACE_DEBUG ((LM_DEBUG,
                      "%s\t",
                      entry->value_.char_rep ()));
          ACE_DEBUG ((LM_DEBUG,
                      "%s\n",
                      entry->type_));
        }
    }
  return 0;
}

int
Client_Test::find (const char *key)
{
  char *value = 0;
  char *type = 0;

  if (NAMING_CONTEXT ()->resolve (key, value, type) != 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p Find failed! Key %s not found\n",
                       "Client_Test::list_find",
                       key),
                      0);
  else
    {
      ACE_DEBUG ((LM_DEBUG,
                  "Binding for %s : value = %s\ttype = %s\n",
                  key,
                  value,
                  type));
      if (type)
        delete [] type;
      return 0;
    }
}

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Dynamic_Service<ACE_Naming_Context>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Dynamic_Service<ACE_Naming_Context>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */