00001 using System;
00002 using System.IO;
00003 using GPRSWeb.SmartDeviceClient.Common;
00004
00005 namespace GPRSWeb.SmartDeviceClient.SmartProtocolStack {
00006
00007 public class EmptyMessage : Message {
00008 public override MemoryStream SerialiseData() {
00009 return new MemoryStream(0);
00010 }
00011
00012 public override void DeserialiseData(Stream serialisedDataStream) {
00013 if (serialisedDataStream.Length != 0) {
00014 throw new ApplicationException("Attempting to deserialise empty message, and messgae isn't empty!");
00015 } else {
00016
00017 }
00018 }
00019
00023 public EmptyMessage() : base(MessageType.Noop) {}
00028 public EmptyMessage(ushort destinationDeviceID) : base(MessageType.Noop, destinationDeviceID) {}
00029 }
00030
00031
00032 public class RetransmissionMessage : Message {
00034 private Segment data;
00035
00037 public Segment RawSegment {
00038 get { return data; }
00039 }
00040
00041 public override MemoryStream SerialiseData() {
00042 return data.Serialise();
00043 }
00044
00045 public override void DeserialiseData(Stream serialisedDataStream) {
00046 data = new Segment(serialisedDataStream);
00047 }
00048
00050 public RetransmissionMessage(ushort destination) : base(MessageType.Retransmission, destination) {}
00051 public RetransmissionMessage(ushort destination, Segment src) : this(destination) {
00052 data = src;
00053 }
00054 }
00055
00056 public class MessageChunk : Message {
00057 private static readonly int MAX_DATA_SIZE = SegmentData.MAX_DATA_SIZE - 4 - 4 - 4 - 8 -2 -1;
00058
00059
00060
00061
00062 public int firstInMessage;
00063 public int lastInMessage;
00064 public int offset;
00065 public DateTime timestamp;
00066 public SegmentData segmentData;
00067
00068 public static MessageChunk[] GetChunksFromMessage(Message msg, ref DateTime lastTimestampUsed, ref int lastOffsetUsed) {
00069 SegmentData[] segmentDataArr = SegmentData.FromMessage(msg, MAX_DATA_SIZE);
00070 MessageChunk[] resultArr = new MessageChunk[segmentDataArr.Length];
00071 DateTime now = DateTime.Now;
00072 int currentOffset;
00073 if (now == lastTimestampUsed) {
00074 currentOffset = lastOffsetUsed + 1;
00075 } else {
00076 currentOffset = 0;
00077 }
00078 int initialOffset = currentOffset;
00079
00080 for(int i = 0; i < segmentDataArr.Length; i++) {
00081 resultArr[i] = new MessageChunk(segmentDataArr[i], now, currentOffset);
00082 resultArr[i].firstInMessage = initialOffset;
00083 resultArr[i].lastInMessage = initialOffset + segmentDataArr.Length - 1;
00084 currentOffset++;
00085 }
00086
00087 lastOffsetUsed = currentOffset;
00088 lastTimestampUsed = now;
00089 return resultArr;
00090 }
00091
00097 public static SegmentData[] GetMessageFromChunks(MessageChunk[] chunks) {
00098 SegmentData[] dataChunks = new SegmentData[chunks.Length];
00099 for(int i = 0; i < chunks.Length; i++) {
00100 dataChunks[i] = chunks[i].segmentData;
00101 }
00102 return dataChunks;
00103 }
00104
00105
00106 public MessageChunk(SegmentData data, DateTime timestamp, int offset) : base(MessageType.Chunk) {
00107 this.segmentData = data;
00108 this.timestamp = timestamp;
00109 this.offset = offset;
00110 }
00111
00112 public MessageChunk(byte[] src) : base(MessageType.Chunk) {
00113 Deserialise(src);
00114 }
00115
00116 public override MemoryStream SerialiseData() {
00117 return new MemoryStream(Serialise2());
00118 }
00119
00120
00121 private byte[] Serialise2() {
00122 byte[] firstInMessageByte = BitConverter.GetBytes(this.firstInMessage);
00123 byte[] lastInMessageByte = BitConverter.GetBytes(this.lastInMessage);
00124 byte[] offsetBytes = BitConverter.GetBytes(this.offset);
00125 byte[] timestampBytes = BitConverter.GetBytes(this.timestamp.Ticks);
00126 byte[] result = new byte[firstInMessageByte.Length + lastInMessageByte.Length + offsetBytes.Length + timestampBytes.Length + segmentData.ToByteArray().Length];
00127 MemoryStream ms = new MemoryStream(result);
00128 ms.Write(firstInMessageByte, 0, firstInMessageByte.Length);
00129 ms.Write(lastInMessageByte, 0, lastInMessageByte.Length);
00130 ms.Write(offsetBytes, 0, offsetBytes.Length);
00131 ms.Write(timestampBytes, 0, timestampBytes.Length);
00132 ms.Write(segmentData.ToByteArray(), 0, segmentData.ToByteArray().Length);
00133 ms.Close();
00134 return result;
00135 }
00136
00137 public override void DeserialiseData(Stream serialisedDataStream) {
00138 byte[] buffer = new byte[serialisedDataStream.Length];
00139 Deserialise2(buffer);
00140 }
00141
00142 private void Deserialise2(byte[] src) {
00143 int currentPos = 0;
00144 firstInMessage = BitConverter.ToInt32(src, currentPos);
00145 currentPos+=4;
00146 lastInMessage = BitConverter.ToInt32(src, currentPos);
00147 currentPos+=4;
00148 offset = BitConverter.ToInt32(src, currentPos);
00149 currentPos += 4;
00150 timestamp = new DateTime(BitConverter.ToInt64(src, currentPos));
00151 currentPos += 8;
00152
00153
00154 segmentData = new SegmentData();
00155 segmentData.FromByteArray(ref src, ref currentPos);
00156 }
00157
00158 }
00159 }