00001 using System;
00002 using System.Threading;
00003 using Common;
00004
00005 namespace Server {
00006
00010 public class ClientStub {
00011 ushort deviceID;
00012 MessageQueue fromClientMsgQ;
00013 HTTPObjectQueue outgoingRequestQ;
00014 Thread processingThread;
00015 MCP mcp;
00016
00017 public ClientStub(ushort deviceID, MessageQueue incomingMessages, HTTPObjectQueue outgoingRequestQueue, MCP mcp) {
00018 this.mcp = mcp;
00019 this.deviceID = deviceID;
00020 this.fromClientMsgQ = incomingMessages;
00021 this.outgoingRequestQ = outgoingRequestQueue;
00022 }
00023
00024 public void Start() {
00025 if (processingThread == null || !processingThread.IsAlive) {
00026 processingThread = new Thread(new ThreadStart(this.run));
00027 processingThread.Start();
00028 }
00029 }
00030 public void Stop() {
00031 if (processingThread != null || processingThread.IsAlive) {
00032 processingThread.Abort();
00033 processingThread.Join();
00034 }
00035 }
00036
00037 void run() {
00038 Message currentMsg;
00039 try{
00040 while(true) {
00041 currentMsg = fromClientMsgQ.DequeueBlocking();
00042 currentMsg.source = deviceID;
00043 ActionMessage(currentMsg);
00044 }
00045 } catch (ThreadInterruptedException) {
00046 Console.WriteLine("[ClientStub for Device {0} shutting down queue processing]", deviceID);
00047 }
00048 }
00049
00050 public void ActionMessage(Message msg) {
00051 switch(msg.Type) {
00052 case MessageType.HTTPResponse: {
00053
00054 throw new Exception("Warning! HTTP Response received from client at server");
00055 }
00056 case MessageType.HTTPRequest: {
00057
00058 ActionHTTPRequest((HTTPRequestMessage)msg);
00059 break;
00060 }
00061 case MessageType.CacheIndex: {
00062 ActionCacheIndexMessage((CacheIndexMessage)msg);
00063 break;
00064 }
00065 case MessageType.Retransmission: {
00066
00067 throw new Exception("Warning! Retransmission message made it past the protocol level");
00068 }
00069 case MessageType.Noop: {
00070 break;
00071 }
00072 default: {
00073 throw new Exception("Unrecognised message type in Client Stub receive queue processing");
00074 }
00075 }
00076 }
00077
00078 public void ActionHTTPRequest(HTTPRequestMessage reqMsg) {
00079 HTTPRequestQueueObject reqQObj = new HTTPRequestQueueObject(reqMsg.encodedRequest.Decode());
00080 Console.WriteLine("--> HTTP Request recieved from client proxy for {0}", reqQObj.URI);
00081 reqQObj.sourceDeviceID = reqMsg.source;
00082 reqQObj.callbackReceipt = reqMsg.callbackReceipt;
00083 reqQObj.originalChk = reqMsg.CHK;
00084 outgoingRequestQ.EnqueueBlocking(reqQObj);
00085 }
00086
00087 public void ActionCacheIndexMessage(CacheIndexMessage msg) {
00088 Console.WriteLine("> Cache Index message received from client proxy");
00089 mcp.UpdateChkList(msg.source, msg.index);
00090 }
00091 }
00092
00093 }