summaryrefslogtreecommitdiff
path: root/examples/IPC_SAP/SOCK_SAP/CPP-inserver.cpp
blob: 0d8c588adbbf73ddb5839d448817e59b214f95ea (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
// $Id$

// This example tests the features of the <ACE_SOCK_Acceptor> and
// <ACE_SOCK_Stream> classes.  If the platform supports threads it
// uses a thread-per-connection concurrency model.

#include "ace/SOCK_Acceptor.h"
#include "ace/Thread_Manager.h"
#include "ace/Handle_Set.h"
#include "ace/Profile_Timer.h"

// Are we running verbosely?
static int verbose = 0;

// Function entry point into the twoway server task.

static void *
twoway_server (void *arg)
{
  ACE_INET_Addr cli_addr;
  ACE_SOCK_Stream new_stream;
  ACE_HANDLE handle = (ACE_HANDLE) (long) arg;

  new_stream.set_handle (handle);

  // Make sure we're not in non-blocking mode.
  if (new_stream.disable (ACE_NONBLOCK) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "disable"),
                       0);
                       
  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) client %s connected from %d\n",
              cli_addr.get_host_name (),
              cli_addr.get_port_number ()));

  // Timer business
  ACE_Profile_Timer timer;
  timer.start ();

  size_t total_bytes = 0;
  size_t message_count = 0;

  void *buf;
  ACE_INT32 len;
  
  if (new_stream.recv_n ((void *) &len,
                         sizeof (ACE_INT32)) != sizeof (ACE_INT32))
    ACE_ERROR_RETURN ((LM_ERROR,
                       "(%P|%t) %p\n",
                       "recv_n failed"),
                      0);
  else
    {
      len = ntohl (len);
      ACE_DEBUG ((LM_DEBUG,
                  "(%P|%t) reading messages of size %d\n",
                  len));
      buf = ACE_OS::malloc (len);
    }
  // Read data from client (terminate on error).

  for (;;)
    {
      ssize_t r_bytes = new_stream.recv (buf, len);
      
      if (r_bytes == -1)
        {
          ACE_ERROR ((LM_ERROR, "%p\n", "recv"));
          break;
        }
      else if (r_bytes == 0)
        {
          ACE_DEBUG ((LM_DEBUG,
                      "(%P|%t) reached end of input, connection closed by client\n"));
          break;
        }
      else if (verbose && ACE::write_n (ACE_STDOUT, buf, r_bytes) != r_bytes)
        ACE_ERROR ((LM_ERROR, "%p\n", "ACE::write_n"));
      else if (new_stream.send_n (buf, r_bytes) != r_bytes)
        ACE_ERROR ((LM_ERROR, "%p\n", "send_n"));

      total_bytes += size_t (r_bytes);
      message_count++;
    }

  timer.stop ();

  ACE_Profile_Timer::ACE_Elapsed_Time et;
  timer.elapsed_time (et);

  ACE_DEBUG ((LM_DEBUG,
	      ASYS_TEXT ("\t\treal time = %f secs \n\t\tuser time = %f secs \n\t\tsystem time = %f secs\n"),
	      et.real_time,
	      et.user_time,
	      et.system_time));

  ACE_DEBUG ((LM_DEBUG,
	      ASYS_TEXT ("\t\t messages = %d\n\t\t total bytes = %d\n\t\tmbits/sec = %f\n\t\tusec-per-message = %f\n"),
	      message_count,
              total_bytes,
	      (((double) total_bytes * 8) / et.real_time) / (double) (1024 * 1024),
	      ((et.user_time + et.system_time) / (double) message_count) * 1000000));

  // Close new endpoint (listening endpoint stays open).
  new_stream.close ();

  ACE_OS::free (buf);
  return 0;
}

// Function entry point into the oneway server task.

static void *
oneway_server (void *arg)
{
  ACE_INET_Addr cli_addr;
  ACE_SOCK_Stream new_stream;
  ACE_HANDLE handle = (ACE_HANDLE) (long) arg;

  new_stream.set_handle (handle);

  // Make sure we're not in non-blocking mode.
  if (new_stream.disable (ACE_NONBLOCK) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "disable"),
                       0);
                       
  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) client %s connected from %d\n",
              cli_addr.get_host_name (),
              cli_addr.get_port_number ()));

  // Timer business
  ACE_Profile_Timer timer;
  timer.start ();

  size_t total_bytes = 0;
  size_t message_count = 0;

  void *buf;
  ACE_INT32 len;
  
  if (new_stream.recv_n ((void *) &len,
                         sizeof (ACE_INT32)) != sizeof (ACE_INT32))
    ACE_ERROR_RETURN ((LM_ERROR,
                       "(%P|%t) %p\n",
                       "recv_n failed"),
                      0);
  else
    {
      len = ntohl (len);
      ACE_DEBUG ((LM_DEBUG,
                  "(%P|%t) reading messages of size %d\n",
                  len));
      buf = ACE_OS::malloc (len);
    }
  // Read data from client (terminate on error).

  for (;;)
    {
      ssize_t r_bytes = new_stream.recv (buf, len);
      
      if (r_bytes == -1)
        {
          ACE_ERROR ((LM_ERROR, "%p\n", "recv"));
          break;
        }
      else if (r_bytes == 0)
        {
          ACE_DEBUG ((LM_DEBUG,
                      "(%P|%t) reached end of input, connection closed by client\n"));
          break;
        }
      else if (verbose && ACE::write_n (ACE_STDOUT, buf, r_bytes) != r_bytes)
        ACE_ERROR ((LM_ERROR, "%p\n", "ACE::write_n"));

      total_bytes += size_t (r_bytes);
      message_count++;
    }

  timer.stop ();

  ACE_Profile_Timer::ACE_Elapsed_Time et;
  timer.elapsed_time (et);

  ACE_DEBUG ((LM_DEBUG,
	      ASYS_TEXT ("\t\treal time = %f secs \n\t\tuser time = %f secs \n\t\tsystem time = %f secs\n"),
	      et.real_time,
	      et.user_time,
	      et.system_time));

  ACE_DEBUG ((LM_DEBUG,
	      ASYS_TEXT ("\t\t messages = %d\n\t\t total bytes = %d\n\t\tmbits/sec = %f\n\t\tusec-per-message = %f\n"),
	      message_count,
              total_bytes,
	      (((double) total_bytes * 8) / et.real_time) / (double) (1024 * 1024),
	      ((et.user_time + et.system_time) / (double) message_count) * 1000000));

  // Close new endpoint (listening endpoint stays open).
  new_stream.close ();

  ACE_OS::free (buf);
  return 0;
}

static int
run_event_loop (u_short port)
{
  // Create the oneway and twoway acceptors.
  ACE_SOCK_Acceptor twoway_acceptor;
  ACE_SOCK_Acceptor oneway_acceptor;

  // Create the oneway and twoway server addresses.
  ACE_INET_Addr twoway_server_addr (port);
  ACE_INET_Addr oneway_server_addr (port + 1);

  // Create acceptors, reuse the address.
  if (twoway_acceptor.open (twoway_server_addr, 1) == -1
      || oneway_acceptor.open (oneway_server_addr, 1) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "open"),
                      1);
  else if (twoway_acceptor.get_local_addr (twoway_server_addr) == -1
           || oneway_acceptor.get_local_addr (oneway_server_addr) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "get_local_addr"),
                      1);

  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) starting twoway server at port %d and oneway server at port %d\n",
              twoway_server_addr.get_port_number (),
              oneway_server_addr.get_port_number ()));

  // Keep these objects out here to prevent excessive constructor
  // calls within the loop.
  ACE_SOCK_Stream new_stream;

  fd_set handles;
  
  FD_ZERO (&handles);
  FD_SET (twoway_acceptor.get_handle (), &handles);
  FD_SET (oneway_acceptor.get_handle (), &handles);

  // Performs the iterative server activities.

  for (;;)
    {
      ACE_Time_Value timeout (ACE_DEFAULT_TIMEOUT);
      fd_set temp = handles;

      int result = ACE_OS::select (int (oneway_acceptor.get_handle ()) + 1,
                                   (fd_set *) &temp,
                                   0,
                                   0,
                                   timeout);
      if (result == -1)
        ACE_ERROR ((LM_ERROR,
                    "(%P|%t) %p\n", "select"));
      else if (result == 0)
        ACE_DEBUG ((LM_DEBUG,
                    "(%P|%t) select timed out\n"));
      else
        {
          if (FD_ISSET (twoway_acceptor.get_handle (), &temp))
            {
              if (twoway_acceptor.accept (new_stream) == -1)
                {
                  ACE_ERROR ((LM_ERROR, "%p\n", "accept"));
                  continue;
                }          
              else
                ACE_DEBUG ((LM_DEBUG,
                            "(%P|%t) spawning twoway server\n"));

#if defined (ACE_HAS_THREADS)
              // Spawn a new thread and run the new connection in that thread of
              // control using the <server> function as the entry point.
              if (ACE_Thread_Manager::instance ()->spawn ((ACE_THR_FUNC) twoway_server,
                                                          (void *) new_stream.get_handle (),
                                                          THR_DETACHED) == -1)
                ACE_ERROR_RETURN ((LM_ERROR,
                                   "(%P|%t) %p\n",
                                   "spawn"),
                          1);
#else
              twoway_server ((void *) new_stream.get_handle ());
#endif /* ACE_HAS_THREADS */
            }
          if (FD_ISSET (oneway_acceptor.get_handle (), &temp))
            {
              if (oneway_acceptor.accept (new_stream) == -1)
                {
                  ACE_ERROR ((LM_ERROR, "%p\n", "accept"));
                  continue;
                }          
              else
                ACE_DEBUG ((LM_DEBUG,
                            "(%P|%t) spawning oneway server\n"));

#if defined (ACE_HAS_THREADS)
              // Spawn a new thread and run the new connection in that thread of
              // control using the <server> function as the entry point.
              if (ACE_Thread_Manager::instance ()->spawn ((ACE_THR_FUNC) oneway_server,
                                                          (void *) new_stream.get_handle (),
                                                          THR_DETACHED) == -1)
                ACE_ERROR_RETURN ((LM_ERROR,
                                   "(%P|%t) %p\n",
                                   "spawn"),
                          1);
#else
              oneway_server ((void *) new_stream.get_handle ());
#endif /* ACE_HAS_THREADS */
            }
        }
    }

  /* NOTREACHED */
}

int
main (int argc, char *argv[])
{
  return run_event_loop (argc > 1 ? ACE_OS::atoi (argv[1]) : ACE_DEFAULT_SERVER_PORT);
}