#!/bin/sh
# This is a shell archive (produced by GNU sharutils 4.2).
# To extract the files from this archive, save it to some FILE, remove
# everything before the `!/bin/sh' line above, then type `sh FILE'.
#
# Made on 1999-02-14 13:38 EST by
In the client/server tutorials that we've done so far it was just a
X matter of sending a buffer of data to the peer. This was done
X with the send*() and recv*() methods of the ACE_SOCK* objects.
In a more robust system, one might want to process the data before
X sending it to a peer and "unprocess" it after reading from a
X peer. These processing steps might include encryption,
X compression, applying checksums or any number of other actions.
In this tutorial a Protocol_Stream object is created to encrypt and
X compress* data being sent between peers. Both client and server
X applications are presented as well. I present the application
level code first and then go into the details of the protocol stream
and it's helper objects. If the stream stuff in the application logic
is confusing then just read on by and come back to it after the later
discussions.
Disclaimer:
Kirthika's abstract:
ACE_Message_Blocks are used to communicate between the client and the
server across the Protocol Stream, which abstracts the protocol
conformance details. The underlying SOCK_Stream is used to set up the
connection using the ACE_SOCK_Connector class. Once the connector
completes its job, the SOCK_Stream pointer is passed on to the
Protocol Stream which now takes over. The Client has put() and get()
methods to send and receive data from the server.
The server is implemented using the ACE_Acceptor to listen at the port
for connections and a reactor for delegating events to the appropriate
event handler. The handle_input () method of the handler simply allows
the stream to receive the data and hand it over to the Handler_Task (a
derivative of the ACE_Task) which will then process it.
The implementation of this Protocol Stream model is done using the
ACE_Module class. The module for Xmit/Recv is shoved in first
into the stream, followed by the compression and encryption modules. The
optional Reader if defined is bundled with a dummy task
(ACE_Thru_Task class) into a module. The get() and put() methods do
the job of reading and writing to the Stream. Each module is made up
of a pair of Protocol Tasks. A Protocol Task is a derivative of the
ACE_Task and whose recv() and send() methods need to be filled to
perform the appropriate task.
The Xmit object derives from the Protocol task and has a send() method
which does the task of transmitting data to the underlying
SOCK_Stream. Keeping the fragmentation and reassembly issues in mind,
block-size is also sent across with the block of data.
The Recv object uses a dummy Message Block to provoke the Protocol
Task object to call the recv() on it. This is done by being
foresighted about the use of mutliple threads instead of a single
thread.
The compression/decompression is bundled in a single Protocol Task
object with the send () method doing the compression and the recv()
doing the decompression. Similarly, the encrption/decryption is done
using a single Protocol Task object.
This tutorial provides a glimpse on how to design and implement a
protocol in layers and also revises a lot of what has been learnt until
now from the previous tutorials.
(for instance, Message_Block, Task, Acceptor, Connector, Event_Handler
etc.)
* Ok, I didn't really implement encryption and
X compression objects. I'll leave that as a thought
X exercise!
SHAR_EOF
$shar_touch -am 0214124599 'page01.pre' &&
chmod 0664 'page01.pre' ||
$echo 'restore of' 'page01.pre' 'failed'
if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
&& ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
md5sum -c << SHAR_EOF >/dev/null 2>&1 \
|| $echo 'page01.pre:' 'MD5 check failed'
a09c3ff1f21a90aab991e0f38dc00458 page01.pre
SHAR_EOF
else
shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'page01.pre'`"
test 4228 -eq "$shar_count" ||
$echo 'page01.pre:' 'original size' '4228,' 'current size' "$shar_count!"
fi
fi
# ============= page02.pre ==============
if test -f 'page02.pre' && test "$first_param" != -c; then
$echo 'x -' SKIPPING 'page02.pre' '(file already exists)'
else
$echo 'x -' extracting 'page02.pre' '(text)'
sed 's/^X//' << 'SHAR_EOF' > 'page02.pre' &&
We'll take a look first at the client application. As usual, our goal
X is to keep the main() application as simple as possible and
X delegate the tricky stuff to another object.
X
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!
The class declaration is taken almost exactly from a previous
tutorial. A good design will have a simple handler object that will
collect data from the peer and pass it along to another object for
processing. Again, keep it simple and delegate authority.
The Protocol_Stream uses an ACE_Stream to move an ACE_Message_Block
through a series of tasks. Each task in the stream is responsible for
performing some operation on the data in the message block. That is
the nature of a protocol stream (or "stack" if you prefer). In this
stream, the data is compressed and encrypted* on its way between
peers. We also allow users of the stream to install a reader task to
handle data that percolates up from the peer. As you saw a page or
two ago, this is most useful for a server.
X
*Again, I just pretend to do these things. It would
take another day or two to go through any sort of reasonable
encryption or compression!
Before we get into the code, here's a picture that's shows what's
going on here.
SHAR_EOF
$shar_touch -am 1019173798 'hdr' &&
chmod 0664 'hdr' ||
$echo 'restore of' 'hdr' 'failed'
if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
&& ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
md5sum -c << SHAR_EOF >/dev/null 2>&1 \
|| $echo 'hdr:' 'MD5 check failed'
41322d388f7bb6c8eba031c4a6ab53ce hdr
SHAR_EOF
else
shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'hdr'`"
test 414 -eq "$shar_count" ||
$echo 'hdr:' 'original size' '414,' 'current size' "$shar_count!"
fi
fi
# ============= bodies ==============
if test -f 'bodies' && test "$first_param" != -c; then
$echo 'x -' SKIPPING 'bodies' '(file already exists)'
else
$echo 'x -' extracting 'bodies' '(text)'
sed 's/^X//' << 'SHAR_EOF' > 'bodies' &&
#
# The client application specific files
#
PAGE=2
client.cpp
Client_i.h
Client_i.cpp
#
# The server application specific files
#
server.cpp
Server_i.h
Server_i.cpp
Handler.h
Handler.cpp
#
# The basic protocol stream
#
Protocol_Stream.h
Protocol_Stream.cpp
Protocol_Task.h
Protocol_Task.cpp
#
# Send/Receive objects
#
XXmit.h
XXmit.cpp
Recv.h
Recv.cpp
#
# Protocol objects
#
Compressor.h
Compressor.cpp
Crypt.h
Crypt.cpp
SHAR_EOF
$shar_touch -am 1022205498 'bodies' &&
chmod 0664 'bodies' ||
$echo 'restore of' 'bodies' 'failed'
if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
&& ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
md5sum -c << SHAR_EOF >/dev/null 2>&1 \
|| $echo 'bodies:' 'MD5 check failed'
a6c99d6567b0640ad524b196dc43647e bodies
SHAR_EOF
else
shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'bodies'`"
test 419 -eq "$shar_count" ||
$echo 'bodies:' 'original size' '419,' 'current size' "$shar_count!"
fi
fi
# ============= page01.pre ==============
if test -f 'page01.pre' && test "$first_param" != -c; then
$echo 'x -' SKIPPING 'page01.pre' '(file already exists)'
else
$echo 'x -' extracting 'page01.pre' '(text)'
sed 's/^X//' << 'SHAR_EOF' > 'page01.pre' &&
X
In a typical client/server system you will be sending and receiving
X data. That's the whole point after all.
Several folks have reported problems with this tutorial on Win32.
There are a couple of ways to solve this. I've chosen to solve it by
using the ACE_Select_Reactor on all platforms instead of taking the
OS-default.
The Protocol Stream model consists of a stream layer which pushes the
data towards the underlying SOCK_Stream thru the stages of encryption
and compression. This data is received at the other end of the
SOCK_Stream and sent up to its Protocol_Stream layer via the stages of
decompression and decryption and an optional Reader task. This is very
similar to the model of the TCP/IP stack (specifically the datalink to
physical layer portion).
SHAR_EOF
$shar_touch -am 1019173798 'page02.pre' &&
chmod 0664 'page02.pre' ||
$echo 'restore of' 'page02.pre' 'failed'
if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
&& ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
md5sum -c << SHAR_EOF >/dev/null 2>&1 \
|| $echo 'page02.pre:' 'MD5 check failed'
6a2e64962c95b349625f418502c95952 page02.pre
SHAR_EOF
else
shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'page02.pre'`"
test 194 -eq "$shar_count" ||
$echo 'page02.pre:' 'original size' '194,' 'current size' "$shar_count!"
fi
fi
# ============= page03.pre ==============
if test -f 'page03.pre' && test "$first_param" != -c; then
$echo 'x -' SKIPPING 'page03.pre' '(file already exists)'
else
$echo 'x -' extracting 'page03.pre' '(text)'
sed 's/^X//' << 'SHAR_EOF' > 'page03.pre' &&
The Client object is designed to hide all of the messy connection
X logic from it's users. It also provides put/get methods for
X sending data to the server and receiving the server's response.
X Note the Protocol_Stream member that will take care of
X converting and sending/receiving the data.
SHAR_EOF
$shar_touch -am 1019173798 'page03.pre' &&
chmod 0664 'page03.pre' ||
$echo 'restore of' 'page03.pre' 'failed'
if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
&& ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
md5sum -c << SHAR_EOF >/dev/null 2>&1 \
|| $echo 'page03.pre:' 'MD5 check failed'
95326c064b10bbda428d3c967f285760 page03.pre
SHAR_EOF
else
shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'page03.pre'`"
test 318 -eq "$shar_count" ||
$echo 'page03.pre:' 'original size' '318,' 'current size' "$shar_count!"
fi
fi
# ============= page04.pre ==============
if test -f 'page04.pre' && test "$first_param" != -c; then
$echo 'x -' SKIPPING 'page04.pre' '(file already exists)'
else
$echo 'x -' extracting 'page04.pre' '(text)'
sed 's/^X//' << 'SHAR_EOF' > 'page04.pre' &&
The implementation of the Client object. Only the open() method
X really does any work. The other methods simply delegate their
X function to the Protocol_Stream.
SHAR_EOF
$shar_touch -am 1019173798 'page04.pre' &&
chmod 0664 'page04.pre' ||
$echo 'restore of' 'page04.pre' 'failed'
if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
&& ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
md5sum -c << SHAR_EOF >/dev/null 2>&1 \
|| $echo 'page04.pre:' 'MD5 check failed'
2955ca8d3b0fc6840f3d371aea528b8d page04.pre
SHAR_EOF
else
shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'page04.pre'`"
test 178 -eq "$shar_count" ||
$echo 'page04.pre:' 'original size' '178,' 'current size' "$shar_count!"
fi
fi
# ============= page05.pre ==============
if test -f 'page05.pre' && test "$first_param" != -c; then
$echo 'x -' SKIPPING 'page05.pre' '(file already exists)'
else
$echo 'x -' extracting 'page05.pre' '(text)'
sed 's/^X//' << 'SHAR_EOF' > 'page05.pre' &&
Like the client, we want to keep the main() part of our server code as
X simple as possible. This is done by putting most of the work
X into the Handler object that will deal with client connections.
XFrom the looks of the code below, I think we've been successful in our
simplification.
X
SHAR_EOF
$shar_touch -am 1019173798 'page05.pre' &&
chmod 0664 'page05.pre' ||
$echo 'restore of' 'page05.pre' 'failed'
if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
&& ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
md5sum -c << SHAR_EOF >/dev/null 2>&1 \
|| $echo 'page05.pre:' 'MD5 check failed'
8833b0d7f90a4d13f68b8cdb9147c29a page05.pre
SHAR_EOF
else
shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'page05.pre'`"
test 304 -eq "$shar_count" ||
$echo 'page05.pre:' 'original size' '304,' 'current size' "$shar_count!"
fi
fi
# ============= page06.pre ==============
if test -f 'page06.pre' && test "$first_param" != -c; then
$echo 'x -' SKIPPING 'page06.pre' '(file already exists)'
else
$echo 'x -' extracting 'page06.pre' '(text)'
sed 's/^X//' << 'SHAR_EOF' > 'page06.pre' &&
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.
SHAR_EOF
$shar_touch -am 1019173798 'page06.pre' &&
chmod 0664 'page06.pre' ||
$echo 'restore of' 'page06.pre' 'failed'
if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
&& ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
md5sum -c << SHAR_EOF >/dev/null 2>&1 \
|| $echo 'page06.pre:' 'MD5 check failed'
06a0abefdf4e704b42147f433bd27e4d page06.pre
SHAR_EOF
else
shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'page06.pre'`"
test 418 -eq "$shar_count" ||
$echo 'page06.pre:' 'original size' '418,' 'current size' "$shar_count!"
fi
fi
# ============= page07.pre ==============
if test -f 'page07.pre' && test "$first_param" != -c; then
$echo 'x -' SKIPPING 'page07.pre' '(file already exists)'
else
$echo 'x -' extracting 'page07.pre' '(text)'
sed 's/^X//' << 'SHAR_EOF' > 'page07.pre' &&
And now the implementation of Server. This is actually just the
main() code from a previous tutorial broken into appropriate method
calls. It may seem silly to do this rather than keeping the stuff in
main() but you'll find that you have less trouble enhancing an
application when you take this sort of approach.
X
SHAR_EOF
$shar_touch -am 1019173798 'page07.pre' &&
chmod 0664 'page07.pre' ||
$echo 'restore of' 'page07.pre' 'failed'
if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
&& ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
md5sum -c << SHAR_EOF >/dev/null 2>&1 \
|| $echo 'page07.pre:' 'MD5 check failed'
7dfb75884939c3c05ee1e1000956e9f4 page07.pre
SHAR_EOF
else
shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'page07.pre'`"
test 321 -eq "$shar_count" ||
$echo 'page07.pre:' 'original size' '321,' 'current size' "$shar_count!"
fi
fi
# ============= page08.pre ==============
if test -f 'page08.pre' && test "$first_param" != -c; then
$echo 'x -' SKIPPING 'page08.pre' '(file already exists)'
else
$echo 'x -' extracting 'page08.pre' '(text)'
sed 's/^X//' << 'SHAR_EOF' > 'page08.pre' &&
The Handler object is our event handler. You can use either
ACE_Event_Handler or ACE_Svc_Handler<> for the baseclass. I generally
prefer the latter since it takes care of some housekeeping that I
would otherwise be responsible for.
SHAR_EOF
$shar_touch -am 1019173798 'page08.pre' &&
chmod 0664 'page08.pre' ||
$echo 'restore of' 'page08.pre' 'failed'
if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
&& ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
md5sum -c << SHAR_EOF >/dev/null 2>&1 \
|| $echo 'page08.pre:' 'MD5 check failed'
471889eb736a025fc46719549bca160a page08.pre
SHAR_EOF
else
shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'page08.pre'`"
test 501 -eq "$shar_count" ||
$echo 'page08.pre:' 'original size' '501,' 'current size' "$shar_count!"
fi
fi
# ============= page09.pre ==============
if test -f 'page09.pre' && test "$first_param" != -c; then
$echo 'x -' SKIPPING 'page09.pre' '(file already exists)'
else
$echo 'x -' extracting 'page09.pre' '(text)'
sed 's/^X//' << 'SHAR_EOF' > 'page09.pre' &&
Like any other event handler, the handle_input() method will be
responsible for getting data from the peer() and doing something with
it. In this case, we have a Protocol_Stream to deal with. We'll use
the stream for the actual I/O but we are ultimately responsible for
processing the data from the peer. To do that, we've created a
Handler_Task that fits within the Protocol_Stream framework to process
data that has been received. Handler::handle_input() will tell the stream that
it's time to read data and that data will eventually show up at
Handler_Task::recv() where we'll process it as required by our
application logic.
SHAR_EOF
$shar_touch -am 1019173798 'page09.pre' &&
chmod 0664 'page09.pre' ||
$echo 'restore of' 'page09.pre' 'failed'
if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \
&& ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then
md5sum -c << SHAR_EOF >/dev/null 2>&1 \
|| $echo 'page09.pre:' 'MD5 check failed'
da5c4b11d356dc9caf3a8e3b6b2527a6 page09.pre
SHAR_EOF
else
shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'page09.pre'`"
test 640 -eq "$shar_count" ||
$echo 'page09.pre:' 'original size' '640,' 'current size' "$shar_count!"
fi
fi
# ============= page10.pre ==============
if test -f 'page10.pre' && test "$first_param" != -c; then
$echo 'x -' SKIPPING 'page10.pre' '(file already exists)'
else
$echo 'x -' extracting 'page10.pre' '(text)'
sed 's/^X//' << 'SHAR_EOF' > 'page10.pre' &&
And so finally we come to the Protocol_Stream. That, after all, is
the focus of the entire tutorial but it took us half of the day to get
here!
The only thing you might want to do is combine it with Recv. Why? As you'll realize in a page or two, the Xmit and Recv objects must interact if you're going to ensure a safe transit. By having a single object it's easier to coordinate and maintain the interaction.
Note that close() must decide if it's being called when the stream is shutdown or when it's svc() method exits. Since we tell the baseclass not to use any threads it's a safe bet that flags will always be non-zero. Still, it's good practice to plan for the future by checking the value.
Note also that when we send the data we prefix it with the data size. This let's our sibling Recv ensure that an entire block is received together. This can be very important for compression and encryption processes which typically work better with blocks of data instead of streams of data.
An ACE_Stream is designed to handle downstream traffic very well. You put() data into it and it flows along towards the tail. However, there doesn't seem to be a way to put data in such that it will travel upstream. To get around that, I've added a get() method to Recv that will trigger a read on the socket. Recv will then put the data to the next upstream module and we're on our way. As noted earlier, that data will eventually show up either in the reader (if installed on the stream open()) or the stream head reader task's message queue.
So, while I was lazy with Compress, I'm realistic with Crypt. I'll show you the hooks and entry points and let someone else contribute an encryptor.
A quick review of what we've done:
X It doesn't sound like much but it certainly took a bunch of files to get there. It's easy to get lost in the details when there's so much to cover so you're encouraged to go over things a couple of times. As always, enhancments of the tutorials is welcome!
Here's the complete file list:
Ok, that's it for the client. We've seen a very simple main() X followed by an equally simple Client object.
For a quick look back:
Now we'll move on and examine the server counter-part of our client. SHAR_EOF $shar_touch -am 1022205698 'page04.pst' && chmod 0664 'page04.pst' || $echo 'restore of' 'page04.pst' 'failed' if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \ && ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then md5sum -c << SHAR_EOF >/dev/null 2>&1 \ || $echo 'page04.pst:' 'MD5 check failed' 00419a8ab9a3ddae3261840b62afdc4a page04.pst SHAR_EOF else shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'page04.pst'`" test 406 -eq "$shar_count" || $echo 'page04.pst:' 'original size' '406,' 'current size' "$shar_count!" fi fi # ============= page09.pst ============== if test -f 'page09.pst' && test "$first_param" != -c; then $echo 'x -' SKIPPING 'page09.pst' '(file already exists)' else $echo 'x -' extracting 'page09.pst' '(text)' sed 's/^X//' << 'SHAR_EOF' > 'page09.pst' &&
That's it for the server-specific code. I think I've been fairly successful in keeping it simple and to the point. There are a couple of places where the as-yet-undescribed Protocol_Stream pops up and may cause confusion. We're going to discuss that mystery now but before we do here's the list of server files if you want to review: X
SHAR_EOF $shar_touch -am 1022205698 'page09.pst' && chmod 0664 'page09.pst' || $echo 'restore of' 'page09.pst' 'failed' if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \ && ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then md5sum -c << SHAR_EOF >/dev/null 2>&1 \ || $echo 'page09.pst:' 'MD5 check failed' a96009f43a6fe8e6b52ffa923993a3e1 page09.pst SHAR_EOF else shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'page09.pst'`" test 617 -eq "$shar_count" || $echo 'page09.pst:' 'original size' '617,' 'current size' "$shar_count!" fi fi rm -fr _sh31334 exit 0