summaryrefslogtreecommitdiff
path: root/examples/Connection/misc/Connection_Handler.cpp
blob: 861c45bb088869c3c0ceb11a3162281b71a6df0f (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
// $Id$

// ============================================================================
//
// = FILENAME
//    Connection_Handler.cpp
//
// = DESCRIPTION
//      This test illustrates how to use the Acceptor pattern to
//      create multiple threads, each running its own Reactor.  You
//      can connect to this via telnet and keep typing until you enter
//      '^D'.
//
// = AUTHOR
//    Doug Schmidt
//
// ============================================================================

#include "ace/Acceptor.h"
#include "ace/SOCK_Acceptor.h"
#include "ace/Service_Config.h"
#include "ace/Thread.h"

#include "Connection_Handler.h"

ACE_RCSID(misc, Connection_Handler, "$Id$")

int
Connection_Handler::open (void *)
{
  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) in open()\n"));

  // Make ourselves an Active Object.
  return this->activate (THR_NEW_LWP | THR_DETACHED);
}

int
Connection_Handler::close (u_long)
{
  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) in close()\n"));

  // Shut ourself down.  Note that this doesn't destroy the thread,
  // just the state of the object. 
  this->destroy ();
  return 0;
}

int
Connection_Handler::svc (void)
{
  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) in svc()\n"));

  this->finished_ = 0;

  // Create our own personal Reactor just for this thread.  Note that
  // we create this on the stack of the thread since it's only used
  // for the duration of this connection!

  ACE_Reactor reactor;

  // Each <ACE_Svc_Handler> has its own <ACE_Reactor *>.  By default,
  // this points to the <Acceptor's> Reactor.  However, we can point
  // it to our local Reactor, which is what we do in this case.
  this->reactor (&reactor);

  // Register ourselves to handle input in this thread without
  // blocking.
  if (this->reactor ()->register_handler
      (this, ACE_Event_Handler::READ_MASK) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "can' (%P|%t) t register with reactor\n"),
                      -1);
  // Schedule a timer.
  else if (this->reactor ()->schedule_timer (this,
					     (const void *) this,
					     ACE_Time_Value (2),
					     ACE_Time_Value (2)) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "(%P|%t) can't register with reactor\n"),
                      -1);
  else
    ACE_DEBUG ((LM_DEBUG,
                "(%P|%t) connected with client\n"));

  // Keep looping until we receive SIGQUIT or the client shutsdown.

  while (this->finished_ == 0)
    {
      ACE_DEBUG ((LM_DEBUG,
                  "(%P|%t) handling events\n"));
      this->reactor ()->handle_events ();
    }

  // Cancel all pending timeouts.
  this->reactor ()->cancel_timer (this);

  // Remove ourselves from the Reactor.
  this->reactor ()->remove_handler
    (this, ACE_Event_Handler::READ_MASK | ACE_Event_Handler::DONT_CALL);

  // Zero-out the Reactor field so it isn't accessed later on.
  this->reactor (0);

  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) exiting svc\n"));
  return 0;
}

int
Connection_Handler::handle_close (ACE_HANDLE,
				  ACE_Reactor_Mask)
{
  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) in handle_close \n"));

  // Signal the svc() event loop to shut down.
  this->finished_ = 1;
  return 0;
}

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

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

  switch (this->peer ().recv (buf, sizeof buf))
    {
    case -1:
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%P|%t) %p bad read\n",
                         "client logger"),
                        -1);
    case 0:
      ACE_ERROR_RETURN ((LM_ERROR,
			 "(%P|%t) closing log daemon (fd = %d)\n",
                         this->get_handle ()),
                        -1);
    default:
      if (((int) buf[0]) == (int) EOF)
	ACE_ERROR_RETURN ((LM_ERROR,
			   "(%P|%t) closing log daemon (fd = %d)\n",
                           this->get_handle ()),
                          -1);
      else
	ACE_DEBUG ((LM_DEBUG,
                    "(%P|%t) from client: %s",
                    buf));
    }

  return 0;
}

int
Connection_Handler::handle_signal (int signum,
				   siginfo_t *,
				   ucontext_t *)
{
  // @@ Note that this code is not portable to all OS platforms since
  // it uses print statements within signal handler context.

  ACE_DEBUG ((LM_DEBUG,
              "received signal %S\n",
              signum));

  this->finished_ = 1;
  return 0;
}

int
Connection_Handler::handle_timeout (const ACE_Time_Value &tv,
				    const void *arg)
{
  ACE_UNUSED_ARG (tv);

  ACE_ASSERT (arg == this);
  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) handling timeout from this = %u\n",
              this));
  return 0;
}

// Define an Acceptor for the <Connection_Handler>.

typedef ACE_Acceptor <Connection_Handler, ACE_SOCK_ACCEPTOR>
	Connection_Acceptor;

int
main (int argc, char *argv[])
{
  ACE_Service_Config daemon (argv[0]);

  u_short port = argc > 1 ? ACE_OS::atoi (argv[1]) : ACE_DEFAULT_SERVER_PORT;

  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) in main\n"));

  // Acceptor factory.
  Connection_Acceptor peer_acceptor;

  // Create an adapter to end the event loop.
  ACE_Sig_Adapter sa ((ACE_Sig_Handler_Ex) ACE_Reactor::end_event_loop);

  // Register the signal handler adapter.
  if (daemon.reactor ()->register_handler (SIGINT, &sa) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "register_handler"),
                      -1);

  // Open the Acceptor.
  else if (peer_acceptor.open (ACE_INET_Addr (port),
			       daemon.reactor ()) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "open"),
                      -1);

  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) starting up connection server\n"));

  // Perform connection service until we receive SIGINT.

  while (ACE_Reactor::event_loop_done() == 0)
    ACE_Reactor::run_event_loop ();

  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) shutting down connection server\n"));
  return 0;
}

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Acceptor<Connection_Handler, ACE_SOCK_ACCEPTOR>;
template class ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_NULL_SYNCH>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Acceptor<Connection_Handler, ACE_SOCK_ACCEPTOR>
#pragma instantiate ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_NULL_SYNCH>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */