Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

ProtocolWriter.d

Go to the documentation of this file.
00001 /*******************************************************************************
00002 
00003         @file ProtocolWriter.d
00004         
00005         Copyright (C) 2004 Kris Bell
00006         
00007         This software is provided 'as-is', without any express or implied
00008         warranty. In no event will the authors be held liable for damages
00009         of any kind arising from the use of this software.
00010         
00011         Permission is hereby granted to anyone to use this software for any 
00012         purpose, including commercial applications, and to alter it and/or 
00013         redistribute it freely, subject to the following restrictions:
00014         
00015         1. The origin of this software must not be misrepresented; you must 
00016            not claim that you wrote the original software. If you use this 
00017            software in a product, an acknowledgment within documentation of 
00018            said product would be appreciated but is not required.
00019 
00020         2. Altered source versions must be plainly marked as such, and must 
00021            not be misrepresented as being the original software.
00022 
00023         3. This notice may not be removed or altered from any distribution
00024            of the source.
00025 
00026 
00027                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00028 
00029 
00030         @version        Initial version, July 2004      
00031         @author         Kris
00032 
00033 
00034 *******************************************************************************/
00035 
00036 module mango.cluster.qos.socket.ProtocolWriter;
00037 
00038 private import mango.io.model.IPickle;
00039 
00040 private import  mango.io.PickleWriter,
00041                 mango.io.EndianWriter;
00042 
00043 private import  mango.cluster.model.IChannel;
00044 
00045 /*******************************************************************************
00046         
00047         Objects passed around a cluster are prefixed with a header, so the 
00048         receiver can pick them apart correctly. This header consists of:
00049 
00050         - the packet size, including the header (16 bits)
00051         - a command code (8 bits)
00052         - the length of the channel name (8 bits)
00053         - the channel name
00054         - the payload (an IPickle class)
00055 
00056         Everything is written in Network order (big endian).
00057 
00058 *******************************************************************************/
00059 
00060 class ProtocolWriter : PickleWriter
00061 {
00062         const ubyte Version = 0x01;
00063 
00064         enum Command : ubyte {OK, Exception, Full, Locked, 
00065                               Add, Copy, Remove, Load, AddQueue, RemoveQueue};
00066 
00067         /***********************************************************************
00068         
00069                 Construct a ProtocolWriter upon the given buffer. As
00070                 Objects are serialized, their content is written to this
00071                 buffer. The buffer content is then typically flushed to 
00072                 some external conduit, such as a file or socket.
00073 
00074                 Note that serialized data is always in Network order.
00075 
00076         ***********************************************************************/
00077         
00078         this (IBuffer buffer)
00079         in {
00080            assert (buffer);
00081            }
00082         body
00083         {
00084                 super (buffer);
00085         }
00086 
00087         /***********************************************************************
00088                 
00089                 Flush the output
00090         
00091         ***********************************************************************/
00092 
00093         override IWriter flush () 
00094         {
00095                 // remember the current buffer content
00096                 uint content = buffer.readable ();
00097 
00098                 // flush pickled object(s) to output
00099                 super.flush ();
00100 
00101                 // skip back in case we have to try this again
00102                 buffer.skip (-content);
00103 
00104                 return this;
00105         }
00106 
00107         /***********************************************************************
00108         
00109         ***********************************************************************/
00110 
00111         ProtocolWriter put (Command cmd, char[] channel, IPickle object = null, char[] element = null)
00112         {
00113                 // reset the buffer first!
00114                 buffer.clear ();
00115                 ubyte[] content = cast(ubyte[]) buffer.getContent();
00116 
00117                 super.put (cast(ushort) 0)
00118                      .put (cast(ubyte) cmd)
00119                      .put (cast(ubyte) Version)
00120                      .put (channel)
00121                      .put (element);
00122 
00123                 // is there a payload?
00124                 if (object)
00125                     super.freeze (object);
00126 
00127                 // go back and write the total number of bytes
00128                 ushort size = buffer.getLimit;
00129                 content[0] = size >> 8;
00130                 content[1] = size & 0xff;
00131                 return this;
00132         }
00133 
00134         /***********************************************************************
00135         
00136                 Write an exception message
00137 
00138         ***********************************************************************/
00139 
00140         ProtocolWriter exception (char[] message)
00141         {
00142                 put (ProtocolWriter.Command.Exception, message);
00143                 return this;
00144         }
00145 
00146         /***********************************************************************
00147                 
00148                 Write an "OK" confirmation
00149 
00150         ***********************************************************************/
00151 
00152         ProtocolWriter success (char[] message = null)
00153         {
00154                 put (ProtocolWriter.Command.OK, message);
00155                 return this;
00156         }
00157 
00158         /***********************************************************************
00159                 
00160                 Indicate something has filled up
00161 
00162         ***********************************************************************/
00163 
00164         ProtocolWriter full (char[] message)
00165         {
00166                 put (ProtocolWriter.Command.Full, message);
00167                 return this;
00168         }
00169 
00170         /***********************************************************************
00171         
00172                 Write a serialized payload as constructed by the method
00173                 ProtocolReader.getPacket()
00174 
00175         ***********************************************************************/
00176 
00177         ProtocolWriter reply (ClusterContent content)
00178         {
00179                 // reset the buffer first
00180                 buffer.clear ();
00181 
00182                 // write the length ...
00183                 super.put (cast(ushort) (content.length + ushort.sizeof + ubyte.sizeof + ubyte.sizeof));
00184 
00185                 // and the ack ...
00186                 super.put (cast(ubyte) ProtocolWriter.Command.OK);
00187 
00188                 // and the version ...
00189                 super.put (cast(ubyte) Version);
00190 
00191                 // and the payload (which includes both channel & element)
00192                 if (content.length)
00193                     buffer.append (content);
00194                 else
00195                    // or filler for an empty channel & element ...
00196                    super.put (cast(int) 0).put(cast(int) 0);
00197 
00198                 return this;
00199         }
00200 }
00201 

Generated on Sun Nov 7 19:06:52 2004 for Mango by doxygen 1.3.6