summaryrefslogtreecommitdiff
path: root/docs/tutorials/015/page05.html
blob: 82370135201c0b7d5aa579764e8870b8138ea9ab (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
<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%">
Like the client, we want to keep the main() part of our server code as 
      simple as possible.  This is done by putting most of the work
      into the Handler object that will deal with client connections.
From the looks of the code below, I think we've been successful in our 
simplification.
<HR>

<PRE>

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

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

<font color=red>// A signal handler that will close the server object</font>
extern "<font color=green>C</font>" void handler (int)
{
    <font color=#008888>Server::close</font>();
}

int main (int, char **)
{
        <font color=red>// The server object that abstracts away all of difficult parts.</font>
    Server server;

        <font color=red>// Attempt to open the server.  Like all good ACE-based</font>
        <font color=red>// objects, we'll get -1 on failure.</font>
    if( server.open() == -1 )
    {
        ACE_ERROR_RETURN ((LM_ERROR, "<font color=green>%p\n</font>", "<font color=green>server.open()</font>"), -1);
    }

        <font color=red>// Install a signal handler for ^C so that we can exit gracefully</font>
    ACE_Sig_Action sa ((ACE_SignalHandler) handler, SIGINT);

        <font color=red>// Run the server's main loop until we're interrupted</font>
    if( server.run() == -1 )
    {
        ACE_ERROR_RETURN ((LM_ERROR, "<font color=green>%p\n</font>", "<font color=green>server.run()</font>"), -1);
    }

    return 0;
}

<font color=red>/* These explicit instantiations were taken from an earlier tutorial.
   Your compiler may require others as well.
*/</font>
<font color=blue>#if defined</font> (<font color=purple>ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION</font>)
template class ACE_Acceptor &lt;Handler, ACE_SOCK_ACCEPTOR>;
template class ACE_Svc_Handler&lt;ACE_SOCK_STREAM, ACE_NULL_SYNCH>;
<font color=blue>#elif defined</font> (<font color=purple>ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA</font>)
<font color=blue>#pragma</font> <font color=purple>instantiate</font> ACE_Acceptor &lt;Handler, ACE_SOCK_ACCEPTOR>
<font color=blue>#pragma</font> <font color=purple>instantiate</font> ACE_Svc_Handler&lt;ACE_SOCK_STREAM, ACE_NULL_SYNCH>
<font color=blue>#endif</font> <font color=red>/* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */</font>

</PRE>
<P><HR WIDTH="100%">
<CENTER>[<A HREF="..">Tutorial Index</A>] [<A HREF="page06.html">Continue This Tutorial</A>]</CENTER>