File: remote_service_directory_test.cpp

package info (click to toggle)
ace 6.0.3%2Bdfsg-0.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 49,368 kB
  • sloc: cpp: 341,826; perl: 30,850; ansic: 20,952; makefile: 10,144; sh: 4,744; python: 828; exp: 787; yacc: 511; xml: 330; lex: 158; lisp: 116; csh: 48; tcl: 5
file content (109 lines) | stat: -rw-r--r-- 2,620 bytes parent folder | download | duplicates (2)
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
// $Id: remote_service_directory_test.cpp 91670 2010-09-08 18:02:26Z johnnyw $

// Test program for the INET IPC-SAPs...

#include "ace/OS_main.h"
#include "ace/OS_NS_stdlib.h"
#include "ace/OS_NS_unistd.h"
#include "ace/OS_NS_string.h"
#include "ace/SOCK_Connector.h"
#include "ace/INET_Addr.h"
#include "ace/Get_Opt.h"
#include "ace/Log_Msg.h"

// Port number to use.
static unsigned short port_number = ACE_DEFAULT_SERVICE_PORT;

// Name of remote host.
static const ACE_TCHAR *host_name = ACE_DEFAULT_SERVER_HOST;

// Trigger a remote reconfiguration.
static int remote_reconfigure = 0;

static void
print_usage_and_die (void)
{
  ACE_ERROR ((LM_ERROR,
              "usage: %n [-p portnum] [-h host_name] [-r]\n"));
  ACE_OS::exit (1);
}

void
parse_args (int argc, ACE_TCHAR *argv[])
{
  ACE_Get_Opt get_opt (argc, argv, ACE_TEXT("p:h:r"));

  for (int c; (c = get_opt ()) != -1; )
    switch (c)
    {
    case 'h':
      host_name = get_opt.opt_arg ();
      break;
    case 'p':
      port_number = ACE_OS::atoi (get_opt.opt_arg ());
      break;
    case 'r':
      remote_reconfigure = 1;
      break;
    default:
      print_usage_and_die ();
      break;
  }
}

int
ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
  ACE_LOG_MSG->open (argv[0]);

  parse_args (argc, argv);
  // Default is to ask the server for ``help.''
  static char buf[BUFSIZ] = "help\n";
  int n;
  ACE_SOCK_Stream   sc;
  ACE_SOCK_Connector con;

  if (con.connect (sc,
                   ACE_INET_Addr (port_number,
                                  host_name)) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n%a",
                       "connect",
                       1),
                      -1);

  if (remote_reconfigure)
    // Remotely instruct the server to reconfigure itself.
    ACE_OS::strcpy (buf, "reconfigure\n");

  // Send the command.

  if (sc.send_n (buf,
                 ACE_OS::strlen (buf) + 1) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n%a",
                       "send",
                       1), -1);

  // Next, read the response.

  while ((n = sc.recv (buf,
                       sizeof buf)) > 0)
    if (ACE_OS::write (ACE_STDOUT,
                       buf,
                       n) != n)
      ACE_ERROR_RETURN ((LM_ERROR,
                         "%p\n%a",
                         "write",
                         1),
                        -1);

  if (sc.close () == -1)
      ACE_ERROR_RETURN ((LM_ERROR,
                         "%p\n%a",
                         "close",
                         1),
                        -1);

  return 0;
}