1
// server.cpp,v 1.6 2003/11/09 20:44:18 dhinton Exp
3
/* We try to keep main() very simple. One of the ways we do that is
4
to push much of the complicated stuff into worker objects. In this
5
case, we only need to include the acceptor header in our main
6
source file. We let it worry about the "real work". */
8
#include "client_acceptor.h"
9
#include "ace/Signal.h"
11
/* As before, we create a simple signal handler that will set our
12
finished flag. There are, of course, more elegant ways to handle
13
program shutdown requests but that isn't really our focus right
14
now, so we'll just do the easiest thing. */
16
static sig_atomic_t finished = 0;
17
extern "C" void handler (int)
22
/* A server has to listen for clients at a known TCP/IP port. The
23
default ACE port is 10002 (at least on my system) and that's good
24
enough for what we want to do here. Obviously, a more robust
25
application would take a command line parameter or read from a
26
configuration file or do some other clever thing. Just like the
27
signal handler above, though, that's what we want to focus on, so
28
we're taking the easy way out. */
30
static const u_short PORT = ACE_DEFAULT_SERVER_PORT;
32
/* Finally, we get to main. Some C++ compilers will complain loudly
33
if your function signature doesn't match the prototype. Even
34
though we're not going to use the parameters, we still have to
38
main (int argc, char *argv[])
43
/* In our earlier servers, we used a global pointer to get to the
44
reactor. I've never really liked that idea, so I've moved it into
45
main() this time. When we get to the Client_Handler object you'll
46
see how we manage to get a pointer back to this reactor. */
49
/* The acceptor will take care of letting clients connect to us. It
50
will also arrange for a Client_Handler to be created for each new
51
client. Since we're only going to listen at one TCP/IP port, we
52
only need one acceptor. If we wanted, though, we could create
53
several of these and listen at several ports. (That's what we
54
would do if we wanted to rewrite inetd for instance.) */
55
Client_Acceptor peer_acceptor;
57
/* Create an ACE_INET_Addr that represents our endpoint of a
58
connection. We then open our acceptor object with that Addr.
59
Doing so tells the acceptor where to listen for connections.
60
Servers generally listen at "well known" addresses. If not, there
61
must be some mechanism by which the client is informed of the
64
Note how ACE_ERROR_RETURN is used if we fail to open the acceptor.
65
This technique is used over and over again in our tutorials. */
66
if (peer_acceptor.open (ACE_INET_Addr (PORT), &reactor) == -1)
67
ACE_ERROR_RETURN ((LM_ERROR,
72
/* Install our signal handler. You can actually register signal
73
handlers with the reactor. You might do that when the signal
74
handler is responsible for performing "real" work. Our simple
75
flag-setter doesn't justify deriving from ACE_Event_Handler and
76
providing a callback function though. */
77
ACE_Sig_Action sa ((ACE_SignalHandler) handler, SIGINT);
79
/* Like ACE_ERROR_RETURN, the ACE_DEBUG macro gets used quite a bit.
80
It's a handy way to generate uniform debug output from your
83
"(%P|%t) starting up server daemon\n"));
85
/* This will loop "forever" invoking the handle_events() method of
86
our reactor. handle_events() watches for activity on any
87
registered handlers and invokes their appropriate callbacks when
88
necessary. Callback-driven programming is a big thing in ACE, you
89
should get used to it. If the signal handler catches something,
90
the finished flag will be set and we'll exit. Conveniently
91
enough, handle_events() is also interrupted by signals and will
92
exit back to the while() loop. (If you want your event loop to
93
not be interrupted by signals, checkout the <i>restart</i> flag on
94
the open() method of ACE_Reactor if you're interested.) */
96
reactor.handle_events ();
99
"(%P|%t) shutting down server daemon\n"));
104
#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
105
template class ACE_Acceptor <Client_Handler, ACE_SOCK_ACCEPTOR>;
106
template class ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_NULL_SYNCH>;
107
template class ACE_Guard<ACE_Mutex>;
108
template class ACE_Atomic_Op<ACE_Mutex, int>;
109
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
110
#pragma instantiate ACE_Acceptor <Client_Handler, ACE_SOCK_ACCEPTOR>
111
#pragma instantiate ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_NULL_SYNCH>
112
#pragma instantiate ACE_Guard<ACE_Mutex>
113
#pragma instantiate ACE_Atomic_Op<ACE_Mutex, int>
114
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */