Debian Codegraph @

netsvcs/clients/Naming/Client/Client_Test.cpp

raw symbols SCIP
1 #include "ace/Service_Config.h"
2 #include "ace/Naming_Context.h"
3 #include "ace/Dynamic_Service.h"
4 #include "ace/Thread_Manager.h"
5 #include "ace/Reactor.h"
6 #include "ace/os_include/os_ctype.h"
7 #include "ace/OS_NS_signal.h"
8 #include "ace/OS_NS_stdio.h"
9 #include "ace/OS_NS_string.h"
10 #include "ace/OS_NS_unistd.h"
11 #include "ace/OS_NS_ctype.h"
12 #include "ace/os_include/os_assert.h"
13
14 #include "Client_Test.h"
15
16 // Get the instance of Name_Service using Dynamic_Service
17
18 //inline Name_Service *
19 //NAME_SERVICE ()
20
21 inline ACE_Naming_Context *
22 NAMING_CONTEXT ()
23 {
24 return ACE_Dynamic_Service<ACE_Naming_Context>::instance ("ACE_Naming_Context");
25 }
26
27 Client_Test::Client_Test ()
28 {
29 ACE_DEBUG ((LM_DEBUG,
30 "Client_Test::Client_Test\n"));
31 }
32
33 int
34 Client_Test::open ()
35 {
36 // Cache the name options.
37 this->name_options_ = NAMING_CONTEXT ()->name_options ();
38
39 this->display_menu ();
40
41 if (ACE_Event_Handler::register_stdin_handler (this,
42 ACE_Reactor::instance (),
43 ACE_Thread_Manager::instance ()) == -1)
44 ACE_ERROR_RETURN ((LM_ERROR,
45 "%p\n",
46 "register_stdin_handler"),
47 -1);
48 return 0;
49 }
50
51
52 int
53 Client_Test::close ()
54 {
55 // Deregister this handler with the ACE_Reactor.
56 return ACE_Reactor::instance ()->remove_handler
57 (ACE_STDIN,
58 ACE_Event_Handler::DONT_CALL | ACE_Event_Handler::READ_MASK);
59 }
60
61 int
62 Client_Test::handle_input (ACE_HANDLE)
63 {
64 char option[BUFSIZ];
65 char buf1[BUFSIZ];
66 char buf2[BUFSIZ];
67 char buf3[BUFSIZ];
68 char *temp_buf;
69 int port;
70 char input[1024];
71
72 if (::scanf ("%s", option) <= 0)
73 ACE_ERROR_RETURN ((LM_ERROR,
74 "%p Try again!\n",
75 "Client_Test::handle_input"),
76 0);
77
78 int result = -1;
79
80 switch (ACE_OS::ace_isupper (option[0]) ? ACE_OS::ace_tolower (option[0]) : option[0])
81 {
82 case 'p' :
83 result = this->set_proc_local ();
84 break;
85 case 'n' :
86 result = this->set_node_local ();
87 break;
88 case 'h' :
89 if (::scanf ("%s %d", buf1, &port) <= 0)
90 break;
91 result = this->set_host (buf1, port);
92 break;
93 case 'b' :
94 // get the input from stdin
95 ACE_OS::fgets (input, sizeof input, stdin);
96
97 // get the key
98 if (0 != (temp_buf = ACE_OS::strtok (input, " ")))
99 {
100 ACE_OS::strcpy (buf1, temp_buf);
101
102 temp_buf = ACE_OS::strtok (0, " ");
103
104 // get the value
105 if (temp_buf)
106 {
107 ACE_OS::strcpy (buf2, temp_buf);
108
109 temp_buf = ACE_OS::strtok (0, " ");
110
111 // get the type (if entered).
112 if (temp_buf)
113 {
114 ACE_OS::strcpy (buf3, temp_buf);
115 result = this->bind (buf1, buf2, buf3);
116 }
117 else
118 result = this->bind (buf1, buf2);
119 }
120 else
121 ACE_ERROR ((LM_ERROR,
122 "Bind Failed! Value not entered.\n"));
123 }
124 else
125 ACE_ERROR ((LM_ERROR,
126 "Bind Failed! Key and Value not entered.\n"));
127 break;
128 case 'u' :
129 if (::scanf ("%s", buf1) <= 0)
130 break;
131 result = this->unbind (buf1);
132 break;
133 case 'r' :
134 // get the input from stdin
135 ACE_OS::fgets (input, sizeof input, stdin);
136
137 temp_buf = ACE_OS::strtok (input, " ");
138 // get the key
139 if (temp_buf)
140 {
141 ACE_OS::strcpy (buf1, temp_buf);
142
143 temp_buf = ACE_OS::strtok (0, " ");
144
145 // get the value
146 if (temp_buf)
147 {
148 ACE_OS::strcpy (buf2, temp_buf);
149
150 temp_buf = ACE_OS::strtok (0, " ");
151 // get the type (if entered)
152 if (temp_buf)
153 {
154 ACE_OS::strcpy (buf3, temp_buf);
155 result = this->rebind (buf1, buf2, buf3);
156 }
157 else
158 result = this->rebind (buf1, buf2);
159 }
160 else
161 ACE_ERROR ((LM_ERROR,
162 "Rebind Failed! Value not entered.\n"));
163 }
164 else
165 ACE_ERROR ((LM_ERROR,
166 "Reind Failed! Key and value not entered.\n"));
167 break;
168 case 'f' :
169 if (::scanf ("%s", buf1) <= 0)
170 break;
171 result = this->find (buf1);
172 break;
173 case 'j' :
174 if (::scanf ("%s", buf1) <= 0)
175 break;
176 else
177 result = this->list_names (buf1);
178 break;
179 case 'k' :
180 if (::scanf ("%s", buf1) <= 0)
181 break;
182 else
183 result = this->list_values (buf1);
184 break;
185 case 'l' :
186 if (::scanf ("%s", buf1) <= 0)
187 break;
188 else
189 result = this->list_types (buf1);
190 break;
191 case 'c' :
192 if (::scanf ("%s", buf1) <= 0)
193 break;
194 else
195 result = this->list_name_entries (buf1);
196 break;
197 case 'd' :
198 if (::scanf ("%s", buf1) <= 0)
199 break;
200 else
201 result = this->list_value_entries (buf1);
202 break;
203 case 'e' :
204 if (::scanf ("%s", buf1) <= 0)
205 break;
206 else
207 result = this->list_type_entries (buf1);
208 break;
209 case 'q' :
210 result = -1;
211 break;
212 default :
213 ACE_DEBUG ((LM_DEBUG,
214 "Unrecognized command.\n"));
215 }
216
217 if (result == -1)
218 ACE_Reactor::instance ()->end_reactor_event_loop ();
219 else
220 this->display_menu ();
221 return result;
222 }
223
224 void
225 Client_Test::display_menu ()
226 {
227 ACE_DEBUG ((LM_DEBUG, "\n"));
228 this->list_options ();
229 ACE_DEBUG ((LM_DEBUG, " Name Service Main Menu\n"));
230 ACE_DEBUG ((LM_DEBUG, " ----------------------\n"));
231 ACE_DEBUG ((LM_DEBUG, "<P> Use Process Local Database\n"));
232 ACE_DEBUG ((LM_DEBUG, "<N> Use Node Local Database\n"));
233 ACE_DEBUG ((LM_DEBUG, "<H> Set Remote Name server <host> and <port>\n\n"));
234 ACE_DEBUG ((LM_DEBUG, "<B> Bind <key> <value> [<type>]\n"));
235 ACE_DEBUG ((LM_DEBUG, "<U> Unbind <key>\n"));
236 ACE_DEBUG ((LM_DEBUG, "<R> Rebind <key> <value> [<type>]\n"));
237 ACE_DEBUG ((LM_DEBUG, "<F> Find <key>\n"));
238 ACE_DEBUG ((LM_DEBUG, "<J> Lookup keys matching <pattern>\n"));
239 ACE_DEBUG ((LM_DEBUG, "<K> Lookup values matching <pattern>\n"));
240 ACE_DEBUG ((LM_DEBUG, "<L> Lookup types matching <pattern>\n"));
241 ACE_DEBUG ((LM_DEBUG, "<C> Complete lookup keys matching <pattern>\n"));
242 ACE_DEBUG ((LM_DEBUG, "<D> Complete lookup values matching <pattern>\n"));
243 ACE_DEBUG ((LM_DEBUG, "<E> Complete lookup types matching <pattern>\n"));
244
245 ACE_DEBUG ((LM_DEBUG, "<Q> or ^C (exit)\n"));
246 }
247
248 void
249 Client_Test::list_options ()
250 {
251 switch (this->name_options_->context ())
252 {
253 case ACE_Naming_Context::PROC_LOCAL:
254 ACE_DEBUG ((LM_DEBUG,
255 " *** Using Process Local Database\n"));
256 break;
257 case ACE_Naming_Context::NODE_LOCAL:
258 ACE_DEBUG ((LM_DEBUG,
259 " *** Using Node Local Database\n"));
260 break;
261 case ACE_Naming_Context::NET_LOCAL:
262 ACE_DEBUG ((LM_DEBUG,
263 " *** Hostname: %s\n",
264 this->name_options_->nameserver_host ()));
265 ACE_DEBUG ((LM_DEBUG,
266 " *** Port Number: %d\n",
267 this->name_options_->nameserver_port ()));
268 break;
269 default:
270 ACE_ERROR ((LM_ERROR, "ERROR: shouldn't occur!\n"));
271 break;
272 }
273 ACE_DEBUG ((LM_DEBUG,
274 " *** Namespace directory is %s ***\n",
275 this->name_options_->namespace_dir ()));
276 }
277
278 int
279 Client_Test::set_proc_local ()
280 {
281 // Close down original name space
282 NAMING_CONTEXT ()->close ();
283 this->name_options_->nameserver_host (ACE_TEXT ("localhost"));
284 this->name_options_->context (ACE_Naming_Context::PROC_LOCAL);
285 return NAMING_CONTEXT ()->open (ACE_Naming_Context::PROC_LOCAL);
286 }
287
288 int
289 Client_Test::set_node_local ()
290 {
291 // Close down original name space
292 NAMING_CONTEXT ()->close ();
293 this->name_options_->nameserver_host (ACE_TEXT ("localhost"));
294 this->name_options_->context (ACE_Naming_Context::NODE_LOCAL);
295 return NAMING_CONTEXT ()->open (ACE_Naming_Context::NODE_LOCAL);
296 }
297
298 int
299 Client_Test::set_host (const char *hostname, int port)
300 {
301 // Close down original name space
302 NAMING_CONTEXT ()->close ();
303
304 this->name_options_->context (ACE_Naming_Context::NET_LOCAL);
305 // Set Name Options
306 this->name_options_->nameserver_host (ACE_TEXT_CHAR_TO_TCHAR (hostname));
307 this->name_options_->nameserver_port (port);
308
309 return NAMING_CONTEXT ()->open (ACE_Naming_Context::NET_LOCAL);
310 }
311
312 int
313 Client_Test::bind (const char *key,
314 const char *value,
315 const char *type)
316 {
317 if (NAMING_CONTEXT ()->bind (key, value, type) != 0)
318 ACE_ERROR_RETURN ((LM_ERROR,
319 "%p Bind failed! Key %s exists\n",
320 "Client_Test::bind",
321 key),
322 0);
323 return 0;
324 }
325
326 int
327 Client_Test::unbind (const char *key)
328 {
329 if (NAMING_CONTEXT ()->unbind (key) != 0)
330 ACE_ERROR_RETURN ((LM_ERROR,
331 "%p Unbind failed! Key %s not found\n",
332 "Client_Test::unbind",
333 key),
334 0);
335 return 0;
336 }
337
338 int
339 Client_Test::rebind (const char *key,
340 const char *value,
341 const char *type)
342 {
343 int result = NAMING_CONTEXT ()->rebind (key, value, type );
344 return result == 1 ? 0 : result;
345 }
346
347 int
348 Client_Test::list_names (const char *pattern)
349 {
350 ACE_PWSTRING_SET set;
351
352 if (NAMING_CONTEXT ()->list_names (set, pattern) != 0)
353 ACE_ERROR_RETURN ((LM_ERROR,
354 "%p Pattern matching failed!\n",
355 "Client_Test::list_names"),
356 0);
357 else
358 {
359 ACE_PWSTRING_ITERATOR set_iterator (set);
360
361 for (ACE_NS_WString *name = 0;
362 set_iterator.next (name) !=0;
363 set_iterator.advance())
364 ACE_DEBUG ((LM_DEBUG,
365 "%s\n",
366 name->char_rep ()));
367 }
368 return 0;
369 }
370
371 int
372 Client_Test::list_values (const char *pattern)
373 {
374 ACE_PWSTRING_SET set;
375
376 if (NAMING_CONTEXT ()->list_values (set, pattern) != 0)
377 ACE_ERROR_RETURN ((LM_ERROR,
378 "%p Pattern matching failed!\n",
379 "Client_Test::list_values"),
380 0);
381 else
382 {
383 ACE_PWSTRING_ITERATOR set_iterator (set);
384
385 for (ACE_NS_WString *value = 0;
386 set_iterator.next (value) !=0;
387 set_iterator.advance())
388 ACE_DEBUG ((LM_DEBUG,
389 "%s\n",
390 value->char_rep ()));
391 }
392 return 0;
393 }
394
395 int
396 Client_Test::list_types (const char *pattern)
397 {
398 ACE_PWSTRING_SET set;
399
400 if (NAMING_CONTEXT ()->list_types (set, pattern) != 0)
401 ACE_ERROR_RETURN ((LM_ERROR,
402 "%p Pattern matching failed!\n",
403 "Client_Test::list_types"),
404 0);
405 else
406 {
407 ACE_PWSTRING_ITERATOR set_iterator (set);
408
409 for (ACE_NS_WString *type = 0;
410 set_iterator.next (type) !=0;
411 set_iterator.advance())
412 ACE_DEBUG ((LM_DEBUG,
413 "%s\n",
414 type->char_rep ()));
415 }
416 return 0;
417 }
418
419 int
420 Client_Test::list_name_entries (const char *pattern)
421 {
422 ACE_BINDING_SET set;
423
424 if (NAMING_CONTEXT ()->list_name_entries (set, pattern) != 0)
425 ACE_ERROR_RETURN ((LM_ERROR,
426 "%p Pattern matching failed!\n",
427 "Client_Test::list_names"),
428 0);
429 else
430 {
431 ACE_BINDING_ITERATOR set_iterator (set);
432
433 for (ACE_Name_Binding *entry = 0;
434 set_iterator.next (entry) !=0;
435 set_iterator.advance())
436 {
437 ACE_DEBUG ((LM_DEBUG,
438 "%s\t",
439 entry->name_.char_rep ()));
440 ACE_DEBUG ((LM_DEBUG,
441 "%s\t",
442 entry->value_.char_rep ()));
443 if (entry->type_)
444 ACE_DEBUG ((LM_DEBUG,
445 "%s\n",
446 entry->type_));
447 }
448 }
449 return 0;
450 }
451
452 int
453 Client_Test::list_value_entries (const char *pattern)
454 {
455 ACE_BINDING_SET set;
456
457 if (NAMING_CONTEXT ()->list_value_entries (set, pattern) != 0)
458 ACE_ERROR_RETURN ((LM_ERROR,
459 "%p Pattern matching failed!\n",
460 "Client_Test::list_values"),
461 0);
462 else
463 {
464 ACE_BINDING_ITERATOR set_iterator (set);
465 for (ACE_Name_Binding *entry = 0;
466 set_iterator.next (entry) !=0;
467 set_iterator.advance())
468 {
469 ACE_DEBUG ((LM_DEBUG,
470 "%s\t",
471 entry->name_.char_rep ()));
472 ACE_DEBUG ((LM_DEBUG,
473 "%s\t",
474 entry->value_.char_rep ()));
475 if (entry->type_)
476 ACE_DEBUG ((LM_DEBUG,
477 "%s\n",
478 entry->type_));
479 }
480 }
481 return 0;
482 }
483
484 int
485 Client_Test::list_type_entries (const char *pattern)
486 {
487 ACE_BINDING_SET set;
488
489 if (NAMING_CONTEXT ()->list_type_entries (set, pattern) != 0)
490 ACE_ERROR_RETURN ((LM_ERROR,
491 "%p Pattern matching failed!\n",
492 "Client_Test::list_types"),
493 0);
494 else
495 {
496 ACE_BINDING_ITERATOR set_iterator (set);
497
498 for (ACE_Name_Binding *entry = 0;
499 set_iterator.next (entry) !=0;
500 set_iterator.advance())
501 {
502 ACE_DEBUG ((LM_DEBUG,
503 "%s\t",
504 entry->name_.char_rep ()));
505 ACE_DEBUG ((LM_DEBUG,
506 "%s\t",
507 entry->value_.char_rep ()));
508 ACE_DEBUG ((LM_DEBUG,
509 "%s\n",
510 entry->type_));
511 }
512 }
513 return 0;
514 }
515
516 int
517 Client_Test::find (const char *key)
518 {
519 char *value = 0;
520 char *type = 0;
521
522 if (NAMING_CONTEXT ()->resolve (key, value, type) != 0)
523 ACE_ERROR_RETURN ((LM_ERROR,
524 "%p Find failed! Key %s not found\n",
525 "Client_Test::list_find",
526 key),
527 0);
528 else
529 {
530 ACE_DEBUG ((LM_DEBUG,
531 "Binding for %s : value = %s\ttype = %s\n",
532 key,
533 value,
534 type));
535 if (type)
536 delete [] type;
537 return 0;
538 }
539 }
540