summaryrefslogtreecommitdiff
path: root/examples/Reactor/Dgram/CODgram.cpp
blob: 5505bcb7ab0d8bb07def9d4be3fd1a8e77be9153 (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
// $Id$

// Exercise the <ACE_SOCK_CODgram> wrapper along with the
// <ACE_Reactor>.  This test simply ping-pongs datagrams back and
// forth between the peer1 and peer2 processes.  This test can 
// be run in two ways:
//
// 1. Stand-alone -- e.g.,  
//
//    % ./CODgram
//
//    which will spawn a child process and run peer1 and peer2
//    in different processes on the same machine.
//
// 2. Distributed -- e.g.,
//
//    # Peer1
//    % ./CODgram 10002 tango.cs.wustl.edu 10003 peer1
//
//    # Peer1
//    % ./CODgram 10003 tango.cs.wustl.edu 10002 peer2
//  
//    which will run peer1 and peer2 in different processes
//    on the same or different machines.  Note that you MUST
//    give the name "peer1" as the final argument to one and
//    only one of the programs so that the test will work properly.

#include "ace/Reactor.h"
#include "ace/SOCK_CODgram.h"
#include "ace/INET_Addr.h"
#include "ace/Process.h"

ACE_RCSID(Dgram, CODgram, "$Id$")

// Port used to receive for dgrams.
static u_short port1;

class Dgram_Endpoint : public ACE_Event_Handler
{
public:
  Dgram_Endpoint (const ACE_INET_Addr &remote_addr,
                  const ACE_INET_Addr &local_addr);

  // = Hook methods inherited from the <ACE_Event_Handler>.
  virtual ACE_HANDLE get_handle (void) const;
  virtual int handle_input (ACE_HANDLE handle);
  virtual int handle_timeout (const ACE_Time_Value & tv,
                              const void *arg = 0);

  virtual int handle_close (ACE_HANDLE handle,
                            ACE_Reactor_Mask close_mask);

  int send (const char *buf, size_t len);
  // Send the <buf> to the peer.

private:
  ACE_SOCK_CODgram endpoint_;
  // Wrapper for sending/receiving dgrams.
};

int
Dgram_Endpoint::send (const char *buf, size_t len)
{
  return this->endpoint_.send (buf, len);
}

int
Dgram_Endpoint::handle_close (ACE_HANDLE handle,
                              ACE_Reactor_Mask)
{
  ACE_UNUSED_ARG (handle);

  this->endpoint_.close ();
  return 0;
}

Dgram_Endpoint::Dgram_Endpoint (const ACE_INET_Addr &remote_addr,
                                const ACE_INET_Addr &local_addr)
  : endpoint_ (remote_addr, local_addr)
{
}

ACE_HANDLE
Dgram_Endpoint::get_handle (void) const
{
  return this->endpoint_.get_handle ();
}

int
Dgram_Endpoint::handle_input (ACE_HANDLE)
{
  char buf[BUFSIZ];

  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) activity occurred on handle %d!\n",
              this->endpoint_.get_handle ()));

  ssize_t n = this->endpoint_.recv (buf, sizeof buf);

  if (n == -1)
    ACE_ERROR ((LM_ERROR,
                "%p\n",
                "handle_input"));
  else
    ACE_DEBUG ((LM_DEBUG,
                "(%P|%t) buf of size %d = %*s\n",
                n, n, buf));
  return 0;
}

int
Dgram_Endpoint::handle_timeout (const ACE_Time_Value &,
                                const void *)
{
  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) timed out for endpoint\n"));
  return 0;
}

static int
run_test (u_short localport,
          const char *remotehost,
          u_short remoteport,
          const char *peer)
{
  ACE_INET_Addr remote_addr (remoteport,
                             remotehost);
  ACE_INET_Addr local_addr (localport);

  Dgram_Endpoint endpoint (remote_addr, local_addr);

  // Read data from other side.
  if (ACE_Reactor::instance ()->register_handler
      (&endpoint,
       ACE_Event_Handler::READ_MASK) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "ACE_Reactor::register_handler"),
                      -1);
  char buf[BUFSIZ];
  ACE_OS::strcpy (buf,
                  "Data to transmit");
  size_t len = ACE_OS::strlen (buf);

  // "peer1" is the "initiator."
  if (ACE_OS::strncmp (peer, "peer1", 5) == 0)
    {
      ACE_DEBUG ((LM_DEBUG,
                  "(%P|%t) sending data\n"));
      for (size_t i = 0; i < 20; i++)
        {
          endpoint.send (buf, len);
          ACE_DEBUG ((LM_DEBUG,
                      "(%P|%t) .\n"));
          ACE_OS::sleep (1);
        }
    }

  for (int i = 0; i < 40; i++)
    {
      // Wait up to 10 seconds for data.
      ACE_Time_Value tv (10, 0);

      if (ACE_Reactor::instance ()->handle_events (tv) <= 0)
        ACE_ERROR_RETURN ((LM_DEBUG,
                           "(%P|%t) %p\n",
                           "handle_events"),
                          -1);
      ACE_DEBUG ((LM_DEBUG,
                  "(%P|%t) return from handle events\n"));

      endpoint.send (buf, len);

      ACE_DEBUG ((LM_DEBUG,
                  "(%P|%t) .\n"));
    }

  if (ACE_Reactor::instance ()->remove_handler
      (&endpoint,
       ACE_Event_Handler::READ_MASK) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "ACE_Reactor::remove_handler"),
                      -1);
  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) exiting\n"));
  return 0;
}

int
main (int argc, char *argv[])
{
  // Estabish call backs and socket names.

  port1 = argc > 1 ? ACE_OS::atoi (argv[1]) : ACE_DEFAULT_SERVER_PORT;
  const char *remotehost = argc > 2 ? argv[2] : ACE_DEFAULT_SERVER_HOST;
  const u_short port2 = argc > 3 ? ACE_OS::atoi (argv[3]) : port1 + 1;

  // Providing the fourth command line argument indicates we don't
  // want to spawn a new process.  On Win32, we use this to exec the
  // new program.
  if (argc > 4)                 
    run_test (port1,
              remotehost,
              port2,
              argv[4]);
  else
    {
      ACE_DEBUG ((LM_DEBUG,
                  "(%P|%t) local port = %d, remote host = %s, remote port = %d\n",
                  port1,
                  remotehost,
                  port2));

      ACE_Process_Options options;
      options.command_line ("%s %d %s %d %c",
                            argv[0],
                            port1,
                            remotehost,
                            port2,
                            'c');

      // This has no effect on NT and will spawn a process that exec
      // the above run_test function.
      options.creation_flags (ACE_Process_Options::NO_EXEC); 

      ACE_Process new_process;

      switch (new_process.spawn (options))
        {
        case -1:
          return -1;

        case 0:
          run_test (port1,
                    remotehost,
                    port2,
                    "peer1");
          break;

        default:
          run_test (port2,
                    remotehost,
                    port1,
                    "peer2");
          new_process.wait ();
          break;
        }
    }

  return 0;
}