00001 /******************************************************************************* 00002 00003 @file ProtocolReader.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 4. Derivative works are permitted, but they must carry this notice 00027 in full and credit the original source. 00028 00029 00030 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00031 00032 00033 @version Initial version, July 2004 00034 @author Kris 00035 00036 00037 *******************************************************************************/ 00038 00039 module mango.cluster.qos.socket.ProtocolReader; 00040 00041 private import mango.io.Exception, 00042 mango.io.PickleReader, 00043 mango.io.EndianReader; 00044 00045 private import mango.cluster.model.ICluster; 00046 00047 /******************************************************************************* 00048 00049 Objects passed around a cluster are prefixed with a header, so the 00050 receiver can pick them apart correctly. This header consists of: 00051 00052 - the packet size, including the header (16 bits) 00053 - a command code (8 bits) 00054 - the length of the channel name (8 bits) 00055 - the channel name 00056 - the payload (an IPickle class) 00057 00058 Everything is written in Network order (big endian). 00059 00060 *******************************************************************************/ 00061 00062 class ProtocolReader : PickleReader 00063 { 00064 /*********************************************************************** 00065 00066 Construct a ProtocolReader upon the given buffer. As 00067 Objects are serialized, their content is written to this 00068 buffer. The buffer content is then typically flushed to 00069 some external conduit, such as a file or socket. 00070 00071 Note that serialized data is always in Network order. 00072 00073 ***********************************************************************/ 00074 00075 this (IBuffer buffer) 00076 in { 00077 assert (buffer); 00078 } 00079 body 00080 { 00081 super (buffer); 00082 } 00083 00084 /*********************************************************************** 00085 00086 Return the payload if there was one, null otherwise. 00087 00088 ***********************************************************************/ 00089 00090 IPayload getPayload (inout char[] channel, inout char[] element, inout ubyte cmd) 00091 { 00092 int position = buffer.getPosition; 00093 00094 ushort size; 00095 ubyte versn; 00096 00097 get (size); 00098 get (cmd); 00099 get (versn); 00100 00101 // avoid allocation for these two strings 00102 get (channel); 00103 get (element); 00104 00105 // is there a payload attached? 00106 if (size > (buffer.getPosition - position)) 00107 return cast(IPayload) thaw (); 00108 00109 return null; 00110 } 00111 00112 /*********************************************************************** 00113 00114 Return a duplicated slice of the buffer representing the 00115 recieved payload. This is a bit of a hack, but eliminates 00116 a reasonable amount of overhead. Note that the channel/key 00117 text is retained right at the start of the buffer, allowing 00118 the writer to toss the whole thing back without any further 00119 munging. 00120 00121 ***********************************************************************/ 00122 00123 ClusterContent getPacket (inout char[] channel, inout char[] element, inout ubyte cmd) 00124 { 00125 ushort size; 00126 ubyte versn; 00127 00128 // load up the length first. 00129 get (size); 00130 //printf ("size: %d\n", cast(int) size); 00131 00132 // read the header 00133 get (cmd); 00134 00135 // read the version stamp 00136 get (versn); 00137 00138 // subtract header size 00139 size -= buffer.getPosition; 00140 00141 // may throw an exception if the payload is too large to fit 00142 // completely inside the buffer! 00143 buffer.get (size, false); 00144 00145 // duplicate the remaining packet (with channel/key text) 00146 ClusterContent content = cast(ClusterContent) buffer.toString().dup; 00147 00148 // get a slice upon the channel name. Note this is 00149 // dup'd as part of the payload 00150 get (channel); 00151 00152 // get a slice upon the element name. Note this is 00153 // dup'd as part of the payload 00154 get (element); 00155 00156 // return the dup'd payload (including channel/key text) 00157 return content; 00158 } 00159 } 00160