summaryrefslogtreecommitdiff
path: root/docs/tutorials/001/page02.html
diff options
context:
space:
mode:
Diffstat (limited to 'docs/tutorials/001/page02.html')
-rw-r--r--docs/tutorials/001/page02.html208
1 files changed, 112 insertions, 96 deletions
diff --git a/docs/tutorials/001/page02.html b/docs/tutorials/001/page02.html
index 5f4ca811e83..f5979936078 100644
--- a/docs/tutorials/001/page02.html
+++ b/docs/tutorials/001/page02.html
@@ -1,121 +1,137 @@
-<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
- <TITLE>ACE Tutorial 001</TITLE>
- <META NAME="GENERATOR" CONTENT="Mozilla/3.01Gold (Win95; I) [Netscape]">
+ <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
+ <META NAME="GENERATOR" CONTENT="Mozilla/4.04 [en] (X11; I; Linux 2.0.32 i486) [Netscape]">
<META NAME="Author" CONTENT="James CE Johnson">
<META NAME="Description" CONTENT="A first step towards using ACE productively">
+ <TITLE>ACE Tutorial 001</TITLE>
</HEAD>
-<BODY text = "#000000" link="#000fff" vlink="#ff0f0f" bgcolor="#ffffff">
+<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#000FFF" VLINK="#FF0F0F">
+
+<CENTER><B><FONT SIZE=+2>ACE Tutorial 001</FONT></B></CENTER>
+<CENTER><B><FONT SIZE=+2>A Beginners Guide to Using the ACE Toolkit</FONT></B></CENTER>
-<CENTER><P><B><FONT SIZE=+2>ACE&nbsp;Tutorial 001<BR>
-A Beginners Guide to Using the ACE&nbsp;Toolkit</FONT></B></P></CENTER>
<P>
-<HR WIDTH="100%"></P>
+<HR WIDTH="100%">
<P>From here, I want to move on to the main program loop. In a way, we're
starting at the final product when we do this, but it is a very simple
-piece of code and a good place to start.</P>
+piece of code and a good place to start.
-<P>The main program is really quite simple. The real work is done in the
-ACE derived classes.</P>
-
-<UL>
-<PRE>1. #include &quot;acceptor.h&quot;
+<P>The <A HREF="server.cpp">main</A>
+program is really quite simple. The real work is done in the ACE derived
+classes.
-2. ACE_Reactor g_reactor;
-
-3. const unsigned int PORT = 10000;
-
-4. main()
+<P>
+<HR WIDTH="100%">
+<PRE>/*
+&nbsp; Include the header file where our client acceptor is defined.
+&nbsp;*/
+#include "ace/Reactor.h"
+
+/*&nbsp;&nbsp;
+&nbsp; For simplicity, we create our reactor in the global address space.
+&nbsp; In later tutorials we will do something more clever and appropriate. However,
+&nbsp; the purpose of this tutorial is to introduce a connection acceptance and
+&nbsp; handling, not the full capabilities of a reactor.
+*/
+ACE_Reactor * g_reactor;
+
+/*
+&nbsp; Include the header where we define our acceptor object.&nbsp; An acceptor is
+&nbsp; an abstraction that allows a server to "accept" connections from clients.
+*/
+#include "acceptor.h"
+
+/*
+&nbsp; A TCP/IP server can listen to only one port for connection requests.
+&nbsp; Well-known services can always be found at the same address. Lesser-known
+&nbsp; services are generally told where to listen by a configuration file or
+&nbsp; command-line parameter. For this example, we're satisfied with simply hard-coding
+&nbsp; a random but known value.
+*/
+static const u_short PORT = ACE_DEFAULT_SERVER_PORT;
+
+int main (int, char **)
{
- 5. ACE_INET_Addr addr(PORT);
-
- 6. Client_Acceptor * ca = new Client_Acceptor(addr);
-
- 7. g_reactor.register_handler(ca,ACE_Event_Handler::READ_MASK);
-
- 8. for(;;) g_reactor.handle_events();
-
- 9. return(0);
-}
-</PRE>
-</UL>
-
-<P>Here's a blow-by-blow account of what's being done:</P>
-
-<OL>
-<LI>Include the header file where our client acceptor is defined.</LI>
-
-<LI>For simplicity, we create our reactor in the global address space.
-In later tutorials we will do something more clever and appropriate. However,
-the purpose of this tutorial is to introduce a connection acceptance and
-handling, not the full capabilities of a reactor.</LI>
-
-<LI>A TCP/IP server can listen to only one <I>port </I>for connection requests.
-Well-known services can always be found at the same address. Lesser-known
-services are generally told where to listen by a configuration file or
-command-line parameter. For this example, we're satisfied with simply hard-coding
-a random but known value.</LI>
-
-<LI>Define the program entry point. Obviously, we are ignoring any command
-line parameters at this point. We may explore other options later.</LI>
-
-<LI>Like the Reactor, I'm skimming over the details of the <I>ADDR</I>
-object. What it provides is an abstraction for addressing services in the
-network. All we need to know at this point is that we are creating an address
-object which specifies the TCP/IP&nbsp;<I>port</I> on which the server
-will listen for new connection requests.</LI>
-
-<LI>Using the address object created above, we now create an acceptor object.
-This is all it takes to create the TCP/IP&nbsp;server and get it ready
-to listen on the specified port. As I understand it, no connections will
-yet be established because the object isn't &quot;open for business&quot;
-at this time. Which brings us to the next line...</LI>
-
-<LI>where the acceptor object is registered with the reactor. Upon registration,
-the reactor <I>open</I>s the object. For an acceptor, this means that client
-requests for connection will now be allowed. In the registration, we also
-tell the reactor that we are interested in <I>read</I> events. A read event
-for an acceptor is a request for connection. When the reactor sees one
-of these events, it will invoke the <I>handle_input</I> member function
-of the acceptor object. The <I>handle_input</I> function will then do whatever
-is necessary to deal with the new connection request. This is discussed
-in detail on the next page of this tutorial.</LI>
-
-<LI>The reactor's <I>handle_events</I> member function is responsible for
-looking at all registered objects and invoking their member function when
-anything of interest occurs. When an event is processed, the <I>handle_events
-</I>function returns. In order to get all events, we embed this in an infinite
-loop.</LI>
-
-<LI>This is redundant since the <I>handle_events</I> infinite loop will
-never exit. Some compilers will complain if it isn't there though, so in
-it goes.</LI>
-</OL>
-
-<P>As I said, the main program is really quite simple:</P>
-
+&nbsp; /*
+&nbsp;&nbsp;&nbsp; Create a Reactor instance.&nbsp; Again, a global pointer isn't exactly the
+&nbsp;&nbsp;&nbsp; best way to handle this but for the simple example here, it will be OK.
+&nbsp;&nbsp;&nbsp; We'll get cute with it later.
+&nbsp; */
+&nbsp; g_reactor = new ACE_Reactor;
+
+&nbsp; /*
+&nbsp;&nbsp;&nbsp; Like the Reactor, I'm skimming over the details of the ADDR
+&nbsp;&nbsp;&nbsp; object. What it provides is an abstraction for addressing services in the
+&nbsp;&nbsp;&nbsp; network. All we need to know at this point is that we are creating an address
+&nbsp;&nbsp;&nbsp; object which specifies the TCP/IP port on which the server
+&nbsp;&nbsp;&nbsp; will listen for new connection requests.
+&nbsp; */
+&nbsp; ACE_INET_Addr addr (PORT);
+
+&nbsp; /*
+&nbsp;&nbsp;&nbsp; We now create an acceptor object.&nbsp; No connections will
+&nbsp;&nbsp;&nbsp; yet be established because the object isn't "open for business"
+&nbsp;&nbsp;&nbsp; at this time. Which brings us to the next line...
+&nbsp; */
+&nbsp; Logging_Acceptor * peer_acceptor = new Logging_Acceptor();
+
+&nbsp; /*
+&nbsp;&nbsp;&nbsp; where the acceptor object is opened.&nbsp; You'll find that most ACE
+&nbsp;&nbsp;&nbsp; objects have to be open()ed before they're of any use to you.
+&nbsp;&nbsp;&nbsp; On this open() call, we're telling the acceptor where to listen
+&nbsp;&nbsp;&nbsp; for connections via the 'addr' object.&nbsp; We're also telling it
+&nbsp;&nbsp;&nbsp; that we want it to be registered with our 'g_reactor' instance.
+&nbsp;&nbsp; */
+&nbsp; if (peer_acceptor->open(addr,g_reactor) == -1 )
+&nbsp;&nbsp;&nbsp; ACE_ERROR_RETURN ((LM_ERROR, "Opening Acceptor\n"), -1);
+
+&nbsp; ACE_DEBUG ((LM_DEBUG, "(%P|%t) starting up server logging daemon\n"));
+
+&nbsp; /*
+&nbsp;&nbsp;&nbsp; The reactor's handle_events member function is responsible for looking at
+&nbsp;&nbsp;&nbsp; all registered objects and invoking an appropriate member function when
+&nbsp;&nbsp;&nbsp; anything of interest occurs. When an event is processed, the handle_events
+&nbsp;&nbsp;&nbsp; function returns. In order to get all events, we embed this in an infinite
+&nbsp;&nbsp;&nbsp; loop.
+
+&nbsp;&nbsp;&nbsp; Since we put ourselves into an infinite loop, you'll need to CTRL-C
+&nbsp;&nbsp;&nbsp; to exit the program.
+&nbsp; */
+&nbsp; while (1)
+&nbsp;&nbsp;&nbsp;&nbsp; g_reactor-> handle_events ();
+
+&nbsp; return 0;
+}</PRE>
+
+<HR WIDTH="100%">
+
+<P>As I said, the main program is really quite simple:
<UL>
-<LI>Create an address for the <I>port</I> we want to listen to</LI>
+<LI>
+Create an address for the <I>port</I> we want to listen to</LI>
-<LI>Create an acceptor which listens on that address</LI>
+<LI>
+Create an acceptor which listens on that address</LI>
-<LI>Register the acceptor with a reactor to respond to the connection requests</LI>
+<LI>
+Register the acceptor with a reactor to respond to the connection requests</LI>
-<LI>Enter an infinite loop to let the reactor handle the events</LI>
+<LI>
+Enter an infinite loop to let the reactor handle the events</LI>
</UL>
-
-<P>On the next page, we will take a look at the acceptor and how it responds
-to new connection requests.</P>
+On the next page, we will take a look at the acceptor and how it responds
+to new connection requests.
<P>
-<HR WIDTH="100%"></P>
-
-<CENTER><P>[<A HREF="..">Tutorial Index</A>] [<A HREF="page01.html">Previous
-Page</A>] [<A HREF="page03.html">Continue This Tutorial</A>] </P></CENTER>
+<HR WIDTH="100%">
+<CENTER>[<A HREF="..">Tutorial
+Index</A>] [<A HREF="page01.html">Previous
+Page</A>] [<A HREF="page03.html">Continue
+This Tutorial</A>]</CENTER>
</BODY>
</HTML>