summaryrefslogtreecommitdiff
path: root/docs/tutorials/015/Server_i.cpp
blob: 5de37d07e853b1b3afb95e25b461e2a68694b26a (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

// $Id$

#include "Server_i.h"

/* We have to allocate space for our static finished_ flag.  We also
   initialize it to 'false' so that we don't exit immediately.
*/
sig_atomic_t Server::finished_ = 0;

/* The simple constructor and destructor don't do anything but give us
   a place to expand in the future if we want.
*/
Server::Server(void)
{
    ;
}

Server::~Server(void)
{
    ;
}

/* Opening the server is as simple as opening the acceptor with the
   default ACE_Reactor instance.  If we want to allow multiple
   instances of Server objects then we should have an ACE_Reactor
   member variable that we can register with.
*/
int Server::open(void)
{
    if (acceptor_.open (ACE_INET_Addr (ACE_DEFAULT_SERVER_PORT), ACE_Reactor::instance()) == -1)
        ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "open"), -1);

    return(0);
}

/* Running the server just means that we execute the basic event
   loop for the reactor.  Again, if we had a private reactor then we
   could have multiple server's in their run() method.
*/
int Server::run(void)
{
    ACE_DEBUG ((LM_DEBUG, "(%P|%t) starting up server daemon\n"));

        // Here's the basic event loop.  I have a 2-second timeout on
        // the handle_events() so that we don't have to wait too long
        // when we set the finished_ flag.
    while (!finished_)
    {
         // Some (all?) platforms return the "remaining time" in the
         // timeout parameter.  If we don't reset it each time, we
         // will end up with a 100% CPU spin loop!
        ACE_Time_Value timeout(2);

        ACE_Reactor::instance()->handle_events (&timeout);
    }

        // Close the acceptor when we're done.  This may be handled by
        // the framework but it's good practice to be explicit about things.
    acceptor_.close();

    ACE_DEBUG ((LM_DEBUG, "(%P|%t) shutting down server daemon\n"));

    return 0;
}

/* The close() method simply sets the finished_ flag so that run()
   will leave the event loop and exit.
*/
int Server::close(void)
{
    finished_ = 1;
    ACE_Reactor::instance()->notify();
    return(0);
}