summaryrefslogtreecommitdiff
path: root/docs/tutorials/015/page06.html
blob: 5fc8f8eca24eb3e351e15d6d0c12dbfbe24551ce (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
<HTML>
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="Author" CONTENT="James CE Johnson">
   <TITLE>ACE Tutorial 015</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#000FFF" VLINK="#FF0F0F">

<CENTER><B><FONT SIZE=+2>ACE Tutorial 015</FONT></B></CENTER>

<CENTER><B><FONT SIZE=+2>Building a protocol stream</FONT></B></CENTER>

<P>
<HR WIDTH="100%">
The Server object exists in order simplify the 
main() application level.  To that end, it hides the details of
creating an acceptor and managing the reactor.
<P>
The static close() method available for a signal handler as you saw on 
the previous page.  Of course the assumption here is that there would
only be one Server instance but since you can't provide a TCP/IP port, 
that's probably a valid assumption!
<HR>
<PRE>

<font color=red>// $Id$</font>

<font color=blue>#ifndef</font> <font color=purple>SERVER_H</font>
<font color=blue>#define</font> <font color=purple>SERVER_H</font>

<font color=blue>#include</font> "<font color=green>ace/Acceptor.h</font>"

<font color=blue>#if !defined</font> (<font color=purple>ACE_LACKS_PRAGMA_ONCE</font>)
# pragma once
<font color=blue>#endif</font> <font color=red>/* ACE_LACKS_PRAGMA_ONCE */</font>

<font color=blue>#include</font> "<font color=green>ace/SOCK_Acceptor.h</font>"
<font color=blue>#include</font> "<font color=green>Handler.h</font>"

<font color=red>/* Anytime I have templates I try to remember to create a typedef for
   the parameterized object.  It makes for much less typing later!
*/</font>
typedef ACE_Acceptor &lt; Handler, ACE_SOCK_ACCEPTOR > Acceptor;

class Server
{
public:
        <font color=red>// Our simple constructor takes no parameters.  To make the</font>
        <font color=red>// server a bit more useful, you may want to pass in the</font>
        <font color=red>// TCP/IP port to be used by the acceptor.</font>
    Server(void);
    ~Server(void);

        <font color=red>// Open the server for business</font>
    int open(void);

        <font color=red>// Close all server instances by setting the finished_ flag.</font>
        <font color=red>// Actually, the way this class is written, you can only have</font>
        <font color=red>// one instance.</font>
    static int close(void);

        <font color=red>// Run the server's main loop.  The use of the gloabl</font>
        <font color=red>// ACE_Reactor by this method is what limits us to one Server</font>
        <font color=red>// instance.</font>
    int run(void);

private:
        <font color=red>// This will accept client connection requests and instantiate</font>
        <font color=red>// a Handler object for each new connection.</font>
    Acceptor acceptor_;

        <font color=red>// Our shutdown flag</font>
    static sig_atomic_t finished_;
};

<font color=blue>#endif</font> <font color=red>//  SERVER_H</font>
</PRE>
<P><HR WIDTH="100%">
<CENTER>[<A HREF="../online-tutorials.html">Tutorial Index</A>] [<A HREF="page07.html">Continue This Tutorial</A>]</CENTER>