~ubuntu-branches/ubuntu/breezy/ace/breezy

« back to all changes in this revision

Viewing changes to docs/tutorials/007/server.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad, Benjamin Montgomery, Adam Conrad
  • Date: 2005-09-18 22:51:38 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 sarge) (0.1.2 woody)
  • Revision ID: james.westby@ubuntu.com-20050918225138-seav22q6fyylb536
Tags: 5.4.7-3ubuntu1
[ Benjamin Montgomery ]
* Added a patch for amd64 and powerpc that disables the compiler
  option -fvisibility-inlines-hidden

[ Adam Conrad ]
* Added DPATCH_OPTION_CPP=1 to debian/patches/00options to make
  Benjamin's above changes work correctly with dpatch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// server.cpp,v 1.6 2003/11/09 20:44:18 dhinton Exp
2
 
 
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".  */
7
 
 
8
 
#include "client_acceptor.h"
9
 
#include "ace/Signal.h"
10
 
 
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.  */
15
 
 
16
 
static sig_atomic_t finished = 0;
17
 
extern "C" void handler (int)
18
 
{
19
 
  finished = 1;
20
 
}
21
 
 
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.  */
29
 
 
30
 
static const u_short PORT = ACE_DEFAULT_SERVER_PORT;
31
 
 
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
35
 
   specify them.  */
36
 
 
37
 
int
38
 
main (int argc, char *argv[])
39
 
{
40
 
  ACE_UNUSED_ARG(argc);
41
 
  ACE_UNUSED_ARG(argv);
42
 
 
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.  */
47
 
  ACE_Reactor reactor;
48
 
 
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;
56
 
 
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
62
 
    server's address.
63
 
 
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,
68
 
                       "%p\n",
69
 
                       "open"),
70
 
                      -1);
71
 
 
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);
78
 
 
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
81
 
    program.  */
82
 
  ACE_DEBUG ((LM_DEBUG,
83
 
              "(%P|%t) starting up server daemon\n"));
84
 
 
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.)  */
95
 
  while (!finished)
96
 
    reactor.handle_events ();
97
 
 
98
 
  ACE_DEBUG ((LM_DEBUG,
99
 
              "(%P|%t) shutting down server daemon\n"));
100
 
 
101
 
  return 0;
102
 
}
103
 
 
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 */