00001 using System;
00002 using System.Threading;
00003 using GPRSWeb.SmartDeviceClient.Common;
00004
00005 namespace GPRSWeb.SmartDeviceClient.SmartProtocolStack
00006 {
00010 public class OutgoingMessageQueue : MessageQueue {
00011 private DateTime lastTimestampUsed;
00012 private int lastOffsetUsed;
00013 private StackInterface thisStack;
00014 private Thread processingThread;
00015
00020 public OutgoingMessageQueue(StackInterface thisStack) : base() {
00021 this.thisStack = thisStack;
00022 }
00023
00024 protected void Initialise() {
00025 lastTimestampUsed = DateTime.MinValue;
00026 lastOffsetUsed = 0;
00027 }
00028
00029 public void Start() {
00030 processingThread = new Thread(new ThreadStart(ProcessQueue));
00031 processingThread.Start();
00032 }
00033
00037 protected void ProcessQueue() {
00038 Message workingMessage;
00039 while (true) {
00040 workingMessage = this.DequeueBlocking();
00041 try {
00042 switch (workingMessage.Priority) {
00043 case MessagePriority.Low : {
00044 ProcessLowPriorityMessage(workingMessage);
00045 break;
00046 }
00047 case MessagePriority.Normal : {
00048 ProcessNormalPriorityMessage(workingMessage);
00049 break;
00050 }
00051 case MessagePriority.High : {
00052 processHighPriorityMessage(workingMessage);
00053 break;
00054 }
00055 default: {
00056 thisStack.msgLog.LogError("Outgoing Message Queue was unable to determine prioriry of outgoing message");
00057 break;
00058 }
00059 }
00060 } catch (ApplicationException appEx) {
00061 thisStack.msgLog.LogError("Outgoing Message Queue was unable to redirect a message for transmission. Continuing...\r\n{0}", appEx);
00062 }
00063 }
00064 }
00065
00066 protected void ProcessLowPriorityMessage(Message messageToProcess) {
00067 MessageChunk[] chunks = MessageChunk.GetChunksFromMessage(workingMessage, ref lastTimestampUsed, ref lastOffsetUsed);
00068 foreach (MessageChunk chunk in chunks) {
00069 chunk.Destination = workingMessage.Destination;
00070 chunk.Source = 0;
00071 thisStack.GetRemoteHostByID(chunk.Destination).TxQueue.Enqueue(TransmissionQueueEntry.FromMessage(chunk, true), true);
00072 }
00073 }
00074
00075 protected void ProcessNormalPriorityMessage(Message messageToProcess) {
00076 thisStack.GetRemoteHostByID(messageToProcess.Destination).TxQueue.Enqueue(TransmissionQueueEntry.FromMessage(workingMessage), false);
00077 }
00078
00079 protected void ProcessHighPriorityMessage(Message messageToProcess) {
00080 throw new ApplicationException("Sending High Priority Messages is not implemented");
00081 }
00082 }
00083 }