summaryrefslogtreecommitdiff
path: root/tests/Process_Strategy_Test.cpp
blob: c4662fae90805aca10f75632db0e5b8f54bb2e39 (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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// $Id$

// ============================================================================
//
// = LIBRARY
//    tests
// 
// = FILENAME
//    Process_Strategy_Test.cpp
//
// = DESCRIPTION
//     This is a test of the <ACE_Strategy_Acceptor> and
//     <ACE_File_Lock> classes.  The <ACE_Strategy_Acceptor> uses the
//     <ACE_Process_Strategy>, which forks a process-per-connection
//     and runs as a concurrent server.  This concurrent server
//     queries and increments a "counting value" in a file.
//
//     To execute this test program you can simply start the server
//     process and connect to it via telnet.  If you type "inc" and
//     "read" in the telnet window you'll exercise the functionality
//     of the server.
//
// = AUTHOR
//    Doug Schmidt and Kevin Boyle <kboyle@sanwafp.com>
// 
// ============================================================================

#include "ace/Acceptor.h"
#include "ace/SOCK_Acceptor.h"
#include "ace/Strategies_T.h"
#include "ace/Service_Config.h"
#include "test_config.h"

#if !defined (ACE_LACKS_EXEC)

typedef ACE_Svc_Handler <ACE_SOCK_STREAM, ACE_NULL_SYNCH> SVC_HANDLER;

class Counting_Service : public SVC_HANDLER
  // = TITLE
  //     Reads and increments the count in a shared file.
  //
  // = DESCRIPTION
  //     Objects of this class execute in a separate process as a
  //     result of the <ACE_Strategy_Acceptor> and
  //     <ACE_Process_Strategy>.
{
public:
  Counting_Service (ACE_Thread_Manager * = 0, ACE_File_Lock * = 0);
  // Constructor.

  virtual int open (void *v);
  // Hook that is used to initialize the service (called by the
  // <ACE_Strategy_Acceptor::handle_input>).

protected:
  // = Operations corresponding to requests from the client.
  virtual int read (void);
  // Execute the read operation on the file.

  virtual int inc (void);
  // Execute the increment operation on the file.

  // = Hooks called by <Reactor> and <Strategy_Acceptor>.

  virtual int handle_input (ACE_HANDLE p);
  // Hook called by the <Reactor> when data arrives from the client.

  virtual int handle_close (ACE_HANDLE, ACE_Reactor_Mask);
  // Close down the server and exit the process.

private:
  ACE_File_Lock *file_lock_;
  // Lock for the counting file.
};

typedef ACE_Strategy_Acceptor <Counting_Service, ACE_SOCK_ACCEPTOR> ACCEPTOR;

class Counting_Acceptor : public ACCEPTOR
  // = TITLE
  //     Factory that accepts the connection and creates the
  //     <Counting_Service> handler.  This illustrates both the
  //     Strategy pattern as a means to configure the
  //     <ACE_Strategy_Acceptor> (i.e., via the
  //     <ACE_Process_Strategy>), as well as the Template Method
  //     pattern (via the <make_svc_handler> method).
{
  friend class Counting_Service;
public:
  Counting_Acceptor (LPCTSTR filename);
  // Constructor.

  virtual int make_svc_handler (Counting_Service *&);
  // Factory Method that makes a new <Counting_Service> in response to
  // a connection from a client.

  virtual int open (const ACE_INET_Addr &,
		    ACE_Reactor * = ACE_Service_Config::reactor ());
  // Initialize the Acceptor activation strategy.

  virtual int handle_signal (int, siginfo_t *, ucontext_t *);
  // Catch the SIGCHLD signal and reap the exiting child processes.

private:
  ACE_File_Lock file_lock_;
  // Lock for the counting file.

  ACE_Process_Strategy<Counting_Service> proc_strategy_;
  // Activation strategy that forks a new process for each client
  // connection.
};

// Factory Method that creates a new <Counting_Service>.

int
Counting_Acceptor::make_svc_handler (Counting_Service *&svc)
{
  ACE_NEW_RETURN (svc, Counting_Service (0, &this->file_lock_), -1);
  return 0;
}

// Reap child processes.

int
Counting_Acceptor::handle_signal (int signum, siginfo_t *, ucontext_t *)
{
  ACE_DEBUG ((LM_DEBUG, "(%P|%t) signal %S\n", signum));
  
  pid_t pid;

  while ((pid = ACE_OS::waitpid (-1, 0, WNOHANG)) > 0)
    ACE_DEBUG ((LM_DEBUG, "(%P|%t) reaping child %d\n", pid));

  return 0;
}

Counting_Acceptor::Counting_Acceptor (LPCTSTR filename)
  : file_lock_ (filename, 
		O_RDWR | O_CREAT, 
		ACE_DEFAULT_FILE_PERMS)
{
  // Note that this object lives beyond the lifetime of the Acceptor.

  ACE_DEBUG ((LM_DEBUG, 
	      "(%P|%t) opening %s on handle %d.\n",
	      filename, this->file_lock_.get_handle ()));

  int count = 0;

  // Store the initial value of the count in the file.
  if (ACE_OS::write (this->file_lock_.get_handle (),
		     (const void *) &count,
		     sizeof count) != sizeof count)
    ACE_ERROR ((LM_ERROR, "(%P|%t) %p\n", "write"));
}

int
Counting_Acceptor::open (const ACE_INET_Addr &addr,
			 ACE_Reactor *reactor)
{
  // Initialize the Concurrency strategy.
  if (this->proc_strategy_.open (1, this, reactor) == -1)
    return -1;

  // Open the <Strategy_Acceptor>.
  else if (ACCEPTOR::open (addr,
		      reactor,
		      0,
		      0,
		      &this->proc_strategy_) == -1)
    return -1;

  // Register to handle <SIGCHLD> when a child exits.
  else if (reactor->register_handler (SIGCHLD, this) == -1)
    return -1;
  else
    {
      ACE_INET_Addr my_addr;

      // Find out what port we're really connected to.
      if (this->acceptor ().get_local_addr (my_addr) == -1)
	return -1;
      else
	ACE_DEBUG ((LM_DEBUG, "(%P|%t) port = %d, handle = %d\n", 
		    my_addr.get_port_number (),
		    this->get_handle ()));
      return 0;
    }
}

Counting_Service::Counting_Service (ACE_Thread_Manager *, 
				    ACE_File_Lock *file_lock)
  : file_lock_ (file_lock)
{
  ACE_DEBUG ((LM_DEBUG, "(%P|%t) creating the Counting_Service\n"));
}

// Read the current value from the shared file and return it to the
// client.

int
Counting_Service::read (void)
{
  ACE_READ_GUARD_RETURN (ACE_File_Lock, ace_mon, *this->file_lock_, -1);

  ACE_DEBUG ((LM_DEBUG, "(%P|%t) reading on handle %d.\n",
	      this->file_lock_->get_handle ()));

  int count;
  if (ACE_OS::pread (this->file_lock_->get_handle (), 
		     (void *) &count,
		     sizeof count,
		     0) != sizeof count)
    ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) %p\n", "read"), -1);

  char buf[BUFSIZ];

  int n = ACE_OS::sprintf (buf, "count = %d\n", count);

  ACE_DEBUG ((LM_DEBUG,
	      "(%P|%t) count = %d\n",
	      count));

  if (this->peer ().send_n (buf, n) != n)
    ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) %p\n", "send_n"), -1);
  return 0;
}

// Increment the current value in the shared file by 1.

int
Counting_Service::inc (void)
{
  ACE_WRITE_GUARD_RETURN (ACE_File_Lock, ace_mon, *this->file_lock_, -1);

  ACE_DEBUG ((LM_DEBUG, 
	      "(%P|%t) incrementing on handle %d.\n",
	      this->file_lock_->get_handle ()));

  int count;
  if (ACE_OS::pread (this->file_lock_->get_handle (),
		     (void *) &count,
		     sizeof count,
		     0) != sizeof count)
    ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) %p\n", "read"), -1);

  ACE_DEBUG ((LM_DEBUG,
	      "(%P|%t) incrementing count from %d to %d\n",
	      count, count + 1));
  count++;

  if (ACE_OS::pwrite (this->file_lock_->get_handle (), 
		      (const void *) &count,
		      sizeof count,
		      0) != sizeof count)
    ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) %p\n", "write"), -1);
  return 0;
}

// Receive the request from the client and call the appropriate
// operation.

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

  ACE_DEBUG ((LM_DEBUG, 
	      "(%P|%t) reading from peer on %d\n",
	      this->peer ().get_handle ()));

  ssize_t bytes = this->peer ().recv (buf, sizeof buf);

  if (bytes <= 0 || buf[0] == EOF)
    return -1;
  else
    {
      ACE_DEBUG ((LM_DEBUG, 
		  "(%P|%t) input on %d is %*s\n",
		  this->peer ().get_handle (), bytes, buf));

      // Read and return the current value in the file.
      if (ACE_OS::strncmp (buf, "read", 4) == 0)
	return this->read ();
      // Increment the current value in the file.
      else if (ACE_OS::strncmp (buf, "inc", 3) == 0)
	return this->inc ();
      else
	ACE_DEBUG ((LM_DEBUG, "(%P|%t) no match...\n"));

      return 0;
    }
}

int 
Counting_Service::open (void *)
{
  ACE_DEBUG ((LM_DEBUG, 
	      "(%P|%t) opening service\n"));

  // Register the new handle with the reactor and look for new input
  // in the handle_input routine.

  if (ACE_Service_Config::reactor ()->register_handler 
      (this, ACE_Event_Handler::READ_MASK) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "(%P|%t) register_handler"), -1);

  // We need to rerun the event loop here since we ended up here due
  // to being fork'd and we can't just return to our context since
  // it's in the wrong location in the process.
  ACE_Service_Config::run_reactor_event_loop ();

  /* NOTREACHED */
  return 0;
}

// Hook called when the child is going to exit.

int 
Counting_Service::handle_close (ACE_HANDLE, ACE_Reactor_Mask)
{
  ACE_DEBUG ((LM_DEBUG, 
	      "(%P|%t) About to exit from the child\n"));

  // Exit the child.
  ACE_OS::exit (0);
  return 0;
}
#endif /* !ACE_LACKS_EXEC */

int
main (int argc, char *argv[])
{
  ACE_START_TEST ("Process_Stratey_Test");

#if !defined (ACE_LACKS_EXEC)
  ACE_Service_Config svc_conf;

  // Initialize the counting file.
  Counting_Acceptor counting_acceptor ("counting_file");
  
  u_short port = ACE_DEFAULT_SERVER_PORT;

  if (argc > 1)
    port = ACE_OS::atoi (argv[1]);

  ACE_INET_Addr addr (port);

  // Open the Acceptor.
  if (counting_acceptor.open (addr) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "open"), 1);

  // Run the event loop.

  ACE_Service_Config::run_reactor_event_loop ();

#else
  ACE_ERROR ((LM_ERROR, "fork() not supported on this platform\n"));
#endif /* !ACE_LACKS_EXEC */
  ACE_END_TEST;
  return 0;
}