Main Page | Class Hierarchy | Class List | File List | Class Members

Messages.cs

00001 using System;
00002 using System.Text;
00003 using System.IO;
00004 using System.Runtime.Serialization;
00005 using System.Runtime.Serialization.Formatters.Binary;
00006 
00007 
00008 namespace Common {
00009 
00010         /****************************************************************************/
00011         public class HTTPRequestMessage : Message, IContainsCallback {
00012                 /* SPECIFICATION
00013                  * 
00014                  * Binary form:
00015                  * 
00016                  * int: Callback Receipt
00017                  * Bool: Has CHK
00018                  * { Byte 1..20: CHK (if null, ignore)}
00019                  * Byte 21..n: Encoded HTTP Request
00020                  */
00021 
00023                 public override byte[] RawData {
00024                         get { return Serialise(); }
00025                 }
00026 
00027                 bool hasCHK;
00028                 byte[] chk = null;
00029                 public byte[] CHK {
00030                         set {
00031                                 if (value == null) {
00032                                         hasCHK = false;
00033                                 } else {
00034                                         hasCHK = true;
00035                                         chk = value;
00036                                 }       
00037                         }
00038                         get { return chk; }
00039                 }
00040 
00041 
00042                 public int CallbackReceipt {
00043                         get { return callbackReceipt; }
00044                 }
00045                 public int callbackReceipt;
00049                 public EncodedHTTPRequest encodedRequest;
00050 
00052 
00053 
00054 
00055                 public HTTPRequestMessage() : base(MessageType.HTTPRequest) {
00056                         this.hasCHK = false;
00057                 }
00062                 public HTTPRequestMessage(byte[] src) : this(){
00063                         Deserialse(src);
00064                 }
00065 
00066                 public virtual byte[] Serialise() {
00067                         byte[] callbackBuffer = BitConverter.GetBytes(callbackReceipt);
00068                         byte[] hasChkBuffer = BitConverter.GetBytes(hasCHK);
00069                         byte[] encReqBuffer = encodedRequest.ToByteArray();
00070                         int bufferLength = callbackBuffer.Length + encReqBuffer.Length + hasChkBuffer.Length + (hasCHK ? chk.Length : 0);
00071                         byte[] buffer = new byte[bufferLength];
00072                         MemoryStream ms = new MemoryStream(buffer);
00073                         ms.Write(callbackBuffer, 0, callbackBuffer.Length);
00074                         ms.Write(hasChkBuffer, 0, hasChkBuffer.Length);
00075                         if (hasCHK) {
00076                                 ms.Write(chk, 0, chk.Length);
00077                         }
00078                         ms.Write(encReqBuffer, 0, encReqBuffer.Length);
00079                         ms.Close();
00080                         if (buffer.Length != bufferLength) {
00081                                 throw new ArithmeticException("Somebody can't count");
00082                         }
00083                         return buffer;
00084                 }
00085 
00086                 public virtual void Deserialse(byte[] src) {
00087                         int arrayPosition = 0;
00088                         callbackReceipt = BitConverter.ToInt32(src, arrayPosition);
00089                         arrayPosition += 4;
00090                         hasCHK = BitConverter.ToBoolean(src, arrayPosition);
00091                         arrayPosition += 1;
00092                         if (hasCHK) {
00093                                 chk = new byte[20];
00094                                 Array.Copy(src, arrayPosition, chk, 0, 20);
00095                                 arrayPosition += 20;
00096                         } 
00097                         encodedRequest = new EncodedHTTPRequest(src, arrayPosition, src.Length - arrayPosition);
00098                 }
00099 
00103                 public HTTPRequestMessage(EncodedHTTPRequest encReq, int callbackRcpt, byte[] originalChk) : this() {
00104                         this.CHK = originalChk;
00105                         this.encodedRequest = encReq;
00106                         this.callbackReceipt = callbackRcpt;
00107                 }
00108         
00109         }
00110         
00111         /****************************************************************************/
00112         public class HTTPResponseMessage : Message, IContainsCallback {
00113                 public EncodedHTTPResponse encodedResponse;
00114 
00115                 public override byte[] RawData {
00116                         
00117                         get {// assumes ints are 4 bytes
00118                                 return Serialise();
00119                         }
00120                 }
00121 
00122                 int callbackReceipt; 
00123 
00124                 public int CallbackReceipt {
00125                         get { return callbackReceipt; }
00126                 }
00127 
00128                 public HTTPResponseMessage() : base(MessageType.HTTPResponse) {}
00129 
00130                 public HTTPResponseMessage(EncodedHTTPResponse encResponse, int callbackReceipt) : this() {
00131                         this.callbackReceipt = callbackReceipt;
00132                         this.encodedResponse = encResponse;
00133                 }
00138                 public HTTPResponseMessage(byte[] src) : this() {
00139                         Deserialise(src);
00140                 }
00141 
00142                 public virtual void Deserialise(byte[] src) {
00143                         callbackReceipt = BitConverter.ToInt32(src, 0);
00144                         encodedResponse = new EncodedHTTPResponse(src, 4, src.Length - 4);
00145                 }
00146 
00147                 public virtual byte[] Serialise() {
00148                         byte[] callbackBuffer = BitConverter.GetBytes(callbackReceipt);
00149                         byte[] encRespBuffer = encodedResponse.Serialise();
00150                         byte[] buffer = new byte[callbackBuffer.Length + encRespBuffer.Length];
00151                         Array.Copy(callbackBuffer, buffer, callbackBuffer.Length);
00152                         Array.Copy(encRespBuffer, 0, buffer, callbackBuffer.Length, encRespBuffer.Length);
00153                         return buffer;
00154                 }
00155         }
00156         /****************************************************************************/
00157 
00158         public class NoChangeMessage : Message, IContainsCallback {
00159                 public byte[] chk;
00160                 public int callbackReceipt;
00161                 public HTTPResponseHeader responseHeaders;
00162                 private bool HasChk {
00163                         get { return chk != null; }
00164                 }
00165 
00166                 public int CallbackReceipt {
00167                         get { return callbackReceipt; }
00168                 }
00169                 public override byte[] RawData {
00176                         get {
00177                                 return Serialise();
00178                         }
00179                 }
00180 
00181                 public NoChangeMessage(byte[] chk, int callbackReceipt, string respHeader) : base(MessageType.NoChange) {
00182                         this.chk = chk;
00183                         this.callbackReceipt = callbackReceipt;
00184                         this.responseHeaders = new HTTPResponseHeader(respHeader);
00185                 }
00186 
00187                 public NoChangeMessage() : base(MessageType.NoChange) {}
00188 
00193                 public NoChangeMessage(byte[] src) : base(MessageType.NoChange) {
00194                         Deserialise(src);
00195                 }
00196 
00197                 public virtual void Deserialise(byte[] src) {
00198                         int currentPos = 0;
00199                         chk = new byte[20];
00200                         callbackReceipt = BitConverter.ToInt32(src, currentPos);
00201                         currentPos += 4;
00202                         int headerLen = BitConverter.ToInt32(src, currentPos);
00203                         currentPos += 4;
00204                         responseHeaders = new HTTPResponseHeader(Encoding.ASCII.GetString(src, currentPos, headerLen));
00205                         currentPos += headerLen;
00206                         bool hasChkTemp = BitConverter.ToBoolean(src, currentPos);
00207                         currentPos += 1; // bool is one byte
00208                         if (hasChkTemp) {
00209                                 Array.Copy(src, currentPos, chk, 0, 20);
00210                         }
00211                 }
00212 
00213                 public virtual byte[] Serialise() {
00214                         int currentPos = 0;
00215                         byte[] callbackBuffer = BitConverter.GetBytes(callbackReceipt);
00216                         byte[] respBuffer = Encoding.ASCII.GetBytes(responseHeaders.rawString);
00217                         byte[] buffer = new byte[callbackBuffer.Length + BitConverter.GetBytes(respBuffer.Length).Length + respBuffer.Length + 20 + BitConverter.GetBytes(HasChk).Length];
00218                         Array.Copy(callbackBuffer, buffer, callbackBuffer.Length); // callback int
00219                         currentPos += callbackBuffer.Length;
00220                         Array.Copy(BitConverter.GetBytes(respBuffer.Length), 0, buffer, currentPos, BitConverter.GetBytes(respBuffer.Length).Length); // response header length
00221                         currentPos += BitConverter.GetBytes(respBuffer.Length).Length;
00222                         Array.Copy(respBuffer, 0, buffer, currentPos, respBuffer.Length); // response headers
00223                         currentPos += respBuffer.Length;
00224                         Array.Copy(BitConverter.GetBytes(HasChk), 0, buffer, currentPos, BitConverter.GetBytes(HasChk).Length); // has chk?
00225                         currentPos += BitConverter.GetBytes(HasChk).Length;
00226                         if (HasChk) {
00227                                 Array.Copy(chk, 0, buffer, currentPos, 20); // chk
00228                         }
00229                         return buffer;
00230                 }
00231         }       
00232         /****************************************************************************/
00233         public class CacheIndexMessage : Message{
00234                 public CacheIndex index;
00235 
00236                 public override byte[] RawData {
00237                         get { return index.Serialise(); }
00238 
00239                 }
00240                 public CacheIndexMessage(CacheIndex index) : base(MessageType.CacheIndex){
00241                         this.index = index;
00242                 }
00243 
00244                 public CacheIndexMessage(byte[] src) : base(MessageType.CacheIndex) {
00245                         this.index = CacheIndex.Deserialise(src);
00246                 }
00247         }
00248 
00249         /****************************************************************************/
00250         public interface ICacheUpdateMessage {
00251                 Uri RequestUri {
00252                         get;
00253                         set;
00254                 }
00255         }
00256         /****************************************************************************/
00257         public class CacheUpdateHTTPResponseMsg : HTTPResponseMessage, ICacheUpdateMessage {
00258                 private Uri requestUri;
00259                 public Uri RequestUri {
00260                         get { return requestUri; }
00261                         set { requestUri = value; }
00262                 }
00263 
00264                 public CacheUpdateHTTPResponseMsg(Uri requestUri, EncodedHTTPResponse encodedResponse) : base(encodedResponse, 0) {
00265                         this.Type = MessageType.CacheUpdateHTTPResponse;
00266                         this.RequestUri = requestUri;
00267                 }
00268 
00269                 public CacheUpdateHTTPResponseMsg(byte[] src) {
00270                         this.Type = MessageType.CacheUpdateHTTPResponse;
00271                         this.Deserialise(src);
00272                 }
00273 
00274                 public override void Deserialise(byte[] src) {
00275                         long uriLength = BitConverter.ToInt64(src, 0); // 8 bytes long
00276                         byte[] uriBuffer = new byte[uriLength];
00277                         byte[] baseBuffer = new byte[src.Length - uriLength - 8];
00278                         IFormatter formatter = new BinaryFormatter();
00279                         Array.Copy(src, 8, uriBuffer, 0, (int)uriLength);
00280                         MemoryStream uriStream = new MemoryStream(uriBuffer);
00281                         requestUri = (Uri)formatter.Deserialize(uriStream);
00282                         uriStream.Close();
00283                         Array.Copy(src, 8 + (int)uriLength, baseBuffer, 0, baseBuffer.Length);
00284                         base.Deserialise(baseBuffer);
00285                 }
00286 
00287                 public override byte[] Serialise() {
00288                         MemoryStream uriStream = new MemoryStream();
00289                         MemoryStream resultStream = new MemoryStream();
00290                         IFormatter formatter = new BinaryFormatter();
00291 
00292                         formatter.Serialize(uriStream, RequestUri);
00293                         resultStream.Write(BitConverter.GetBytes(uriStream.Length), 0, BitConverter.GetBytes(uriStream.Length).Length);
00294                         uriStream.Seek(0, SeekOrigin.Begin);
00295                         uriStream.WriteTo(resultStream);
00296                         MemoryStream baseStream = new MemoryStream(base.Serialise());
00297                         baseStream.Seek(0, SeekOrigin.Begin);
00298                         baseStream.WriteTo(resultStream);
00299                         byte[] resultBig = resultStream.GetBuffer();
00300                         byte[] result = new byte[resultStream.Length];
00301                         Array.Copy(resultBig, result, (int)resultStream.Length);
00302                         resultStream.Close();
00303                         baseStream.Close();
00304                         uriStream.Close();
00305                         return result;
00306                 }
00307         }
00308         /****************************************************************************/
00309         public class CacheUpdateNoChangeMsg: NoChangeMessage, ICacheUpdateMessage {
00310                 private Uri requestUri;
00311                 public Uri RequestUri {
00312                         get { return requestUri; }
00313                         set { requestUri = value; }
00314                 }
00315 
00316                 public CacheUpdateNoChangeMsg(Uri requestUri, byte[] chk, string responseHeader) : base(chk, 0, responseHeader) {
00317                         this.Type = MessageType.CacheUpdateNoChange;
00318                         this.RequestUri = requestUri;
00319                 }
00320 
00321                 public CacheUpdateNoChangeMsg(byte[] src) {
00322                         this.Type = MessageType.CacheUpdateNoChange;
00323                         this.Deserialise(src);
00324                 }
00325 
00326                 public override void Deserialise(byte[] src) {
00327                         long uriLength = BitConverter.ToInt64(src, 0); // 8 bytes long
00328                         byte[] uriBuffer = new byte[uriLength];
00329                         byte[] baseBuffer = new byte[src.Length - uriLength - 8];
00330                         IFormatter formatter = new BinaryFormatter();
00331                         Array.Copy(src, 8, uriBuffer, 0, (int)uriLength);
00332                         MemoryStream uriStream = new MemoryStream(uriBuffer);
00333                         requestUri = (Uri)formatter.Deserialize(uriStream);
00334                         uriStream.Close();
00335                         Array.Copy(src, 8 + (int)uriLength, baseBuffer, 0, baseBuffer.Length);
00336                         base.Deserialise(baseBuffer);
00337                 }
00338 
00339                 public override byte[] Serialise() {
00340                         MemoryStream uriStream = new MemoryStream();
00341                         MemoryStream resultStream = new MemoryStream();
00342                         IFormatter formatter = new BinaryFormatter();
00343 
00344                         formatter.Serialize(uriStream, RequestUri);
00345                         resultStream.Write(BitConverter.GetBytes(uriStream.Length), 0, BitConverter.GetBytes(uriStream.Length).Length);
00346                         uriStream.Seek(0, SeekOrigin.Begin);
00347                         uriStream.WriteTo(resultStream);
00348                         MemoryStream baseStream = new MemoryStream(base.Serialise());
00349                         baseStream.Seek(0, SeekOrigin.Begin);
00350                         baseStream.WriteTo(resultStream);
00351                         byte[] resultBig = resultStream.GetBuffer();
00352                         byte[] result = new byte[resultStream.Length];
00353                         Array.Copy(resultBig, result, (int)resultStream.Length);
00354                         resultStream.Close();
00355                         baseStream.Close();
00356                         uriStream.Close();
00357                         return result;
00358                 }
00359         }
00360         /****************************************************************************/
00361         public class CacheIndexRequestMessage : Message {
00362                 public override byte[] RawData {
00363                         get { return new byte[0]; }
00364                 }
00365                 public CacheIndexRequestMessage(ushort requestIndexFrom) : base(MessageType.CacheIndexRequest) {
00366                         Destination = requestIndexFrom;
00367                 }
00368 
00369                 public CacheIndexRequestMessage(byte[] src) : base(MessageType.CacheIndexRequest) {}
00370 
00371         }
00372 
00373 }

Generated on Mon May 8 22:07:27 2006 by  doxygen 1.3.9.1