summaryrefslogtreecommitdiff
path: root/examples/IPC_SAP/SOCK_SAP/CPP-inclient.cpp
blob: 54ed35d8518b56efe24e39b9fe2629757a019b71 (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
// $Id$

// This tests the non-blocking features of the ACE_SOCK_Connector
// class.

#include "ace/SOCK_Connector.h"
#include "ace/INET_Addr.h"
#include "ace/Thread_Manager.h"
#include "ace/Singleton.h"
#include "ace/Get_Opt.h"
#include "ace/Synch.h"

// ACE SOCK_SAP client.

class Options
  // = TITLE
  //   Define the options for this test.
{
public:
  Options (void);
  // Constructor.

  int parse_args (int argc, char *argv[]);
  // Parse the command-line arguments.

  ACE_Time_Value *timeout (void) const;
  // Timeout value.

  u_short port (void) const;
  // Port of the server.

  const char *host (void) const;
  // Host of the server.

  size_t threads (void) const;
  // Number of threads.

  const char *quit_string (void) const;
  // String that shuts down the client/server.

  ssize_t read (char *buf, size_t len, size_t &iterations);
  // Read from the appropriate location.

private:
  const char *host_;
  // Host of the server.

  u_short port_;
  // Port of the server.

  ACE_Time_Value timeout_;
  // Timeout value.

  size_t threads_;
  // Number of threads.

  const char *quit_string_;
  // String that shuts down the client/server.

  ACE_HANDLE io_source_;
  // Are we reading I/O from ACE_STDIN or from our generator?

  size_t iterations_;
  // Number of iterations.
};

Options::Options (void)
  : host_ (ACE_DEFAULT_SERVER_HOST),
    port_ (ACE_DEFAULT_SERVER_PORT),
    timeout_ (ACE_DEFAULT_TIMEOUT),
    threads_ (10),
    quit_string_ ("quit"),
    io_source_ (ACE_INVALID_HANDLE), // Defaults to using the generator.
    iterations_ (10000)
{
}

ssize_t
Options::read (char *buf, size_t len, size_t &iteration)
{
  if (io_source_ == ACE_STDIN)
    return ACE_OS::read (ACE_STDIN, buf, sizeof buf);
  else if (iteration >= this->iterations_)
    return 0;
  else
    {
      ACE_OS::strncpy (buf, "TAO", len);
      iteration++;
      return ACE_OS::strlen ("TAO") + 1;
    }
}

int
Options::parse_args (int argc, char *argv[])
{
  ACE_Get_Opt getopt (argc, argv, "h:i:p:q:st:T:", 1);

  for (int c; (c = getopt ()) != -1; )
    switch (c)
      {
      case 'h':
        this->host_ = getopt.optarg;
        break;
      case 'i':
        this->iterations_ = ACE_OS::atoi (getopt.optarg);
        break;
      case 'p':
        this->port_ = ACE_OS::atoi (getopt.optarg);
        break;
      case 'q':
        this->quit_string_ = getopt.optarg;
        break;
      case 's':
        this->io_source_ = ACE_STDIN;
        break;
      case 't':
        this->threads_ = (size_t) ACE_OS::atoi (getopt.optarg);
        break;
      case 'T':
        this->timeout_ = ACE_OS::atoi (getopt.optarg);
        break;
      default:
        ACE_ERROR_RETURN ((LM_ERROR,
                           "(%P|%t) usage: %s [-h <host>] [-p <port>] [-q <quit string>] [-s] [-t <threads>] [-T <timeout>]"),
                          1);
      }

  return 0;
}

u_short
Options::port (void) const
{
  return this->port_;
}

const char *
Options::host (void) const
{
  return this->host_;
}

const char *
Options::quit_string (void) const
{
  return this->quit_string_;
}

size_t
Options::threads (void) const
{
  return this->threads_;
}

ACE_Time_Value *
Options::timeout (void) const
{
  return (ACE_Time_Value *) &this->timeout_;
}

// Options Singleton.
typedef ACE_Singleton<Options, ACE_SYNCH_RECURSIVE_MUTEX> OPTIONS;

// Entry point to the client service.

void *
client (void *)
{
  char buf[BUFSIZ];

  Options *options = OPTIONS::instance ();

  ACE_SOCK_Stream cli_stream;
  ACE_INET_Addr remote_addr (options->port (),
                             options->host ());

  ACE_SOCK_Connector con;

  // Initiate blocking connection with server.
  ACE_DEBUG ((LM_DEBUG, "(%P|%t) starting connect\n"));

  if (con.connect (cli_stream, remote_addr) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "(%P|%t) %p\n",
                       "connection failed"),
                      0);
  else
    ACE_DEBUG ((LM_DEBUG,
                "(%P|%t) connected to %s\n",
                remote_addr.get_host_name ()));

  // This variable is allocated off the stack to obviate the need for
  // locking.
  size_t iteration = 0;

  // Send data to server (correctly handles "incomplete writes").

  for (ssize_t r_bytes;
       (r_bytes = options->read (buf, sizeof buf, iteration)) > 0;
       )
    if (ACE_OS::strncmp (buf,
                         options->quit_string (),
                         ACE_OS::strlen (options->quit_string ())) == 0)
      break;
    else if (cli_stream.send (buf, r_bytes, 0) == -1)
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%P|%t) %p\n",
                         "send_n"),
                        0);
    else if (cli_stream.recv (buf, sizeof buf) <= 0)
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%P|%t) %p\n",
                         "recv"),
                        0);

  // Close the connection completely.
  if (cli_stream.close () == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "(%P|%t) %p\n",
                       "close"),
                      0);
  return 0;
}

int
main (int argc, char *argv[])
{
  OPTIONS::instance ()->parse_args (argc, argv);

#if defined (ACE_HAS_THREADS)
  if (ACE_Thread_Manager::instance ()->spawn_n (OPTIONS::instance ()->threads (),
                                                (ACE_THR_FUNC) client) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) %p\n", "spawn_n"), 1);
  else
    ACE_Thread_Manager::instance ()->wait ();
#else
  client (0);
#endif /* ACE_HAS_THREADS */

  return 0;
}

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Singleton<Options, ACE_SYNCH_RECURSIVE_MUTEX>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Singleton<Options, ACE_SYNCH_RECURSIVE_MUTEX>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */