00001 using System;
00002 using System.Collections;
00003 using GPRSWeb.SmartDeviceClient.SmartProtocolStack.RemoteHost;
00004
00005 namespace GPRSWeb.SmartDeviceClient.SmartProtocolStack {
00009 public class StackInterface {
00010 protected IncomingMessageQueue receivedMessages;
00015 public IncomingMessageQueue ReceivedMessages {
00016 get { return receivedMessages; }
00017 set {receivedMessages = value; }
00018 }
00023 public OutgoingMessageQueue MessagesToSend {
00024 get { return messagesToSend; }
00025 set { messagesToSend = value; }
00026 }
00027 protected OutgoingMessageQueue messagesToSend;
00028
00032 internal SegmentReceiver segmentReceiver;
00033
00037 public StackSettings Settings {
00038 get { return settings; }
00039 }
00040 internal StackSettings settings;
00041
00045 protected Hashtable remoteHostList;
00046
00050 internal IPMapper ipMappings;
00051
00055 internal ConnectionResetDelegate connectionReset;
00056
00060 internal MessageLogger msgLog;
00061
00066 public StackInterface(GlobalSettings globalSettings, ConnectionResetDelegate connectionReset) {
00067 this.msgLog = msgLog;
00068 this.connectionReset = connectionReset;
00069 this.settings = globalSettings.stackSettings;
00070
00071 msgLog.Log("Initialising GPRSWeb Smart Protocol Stack (Version {0}, Build {1})", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(2), System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.Build);
00072 settings = new StackSettings();
00073 messagesToSend = new OutgoingMessageQueue(this);
00074 receivedMessages = new IncomingMessageQueue(this);
00075 remoteHostList = new Hashtable();
00076 segmentReceiver = new SegmentReceiver(this);
00077 ipMappings = new IPMapper(this);
00078 AddDefaultRemoteHostTEMP();
00079 }
00080
00081
00085 public void ResetConnection() {
00086 msgLog.Log("Reseting Protocol Stack ...");
00087
00088 Stop();
00089 receivedMessages.ResetConnection();
00090 foreach (DictionaryEntry de in remoteHostList) {
00091 ((RemoteHostComms)de.Value).ResetConnection();
00092 }
00093 Start();
00094 msgLog.Log("... Protocol Stack Reset");
00095 }
00096
00100 public void Start() {
00101 msgLog.Log("Starting Protocol Stack ...");
00102 messagesToSend.Start();
00103 foreach (DictionaryEntry de in remoteHostList) {
00104 ((RemoteHostComms)de.Value).Start();
00105 }
00106 segmentReceiver.Start();
00107 msgLog.Log("... Protocol Stack Started");
00108 }
00109
00110
00111 internal void AddRemoteHost(RemoteHostComms remoteHost) {
00112 remoteHostList.Add(remoteHost.RemoteDeviceID, remoteHost);
00113 }
00114
00115 internal RemoteHostComms GetRemoteHostByID(ushort remoteID) {
00116 RemoteHostComms result = (RemoteHostComms)remoteHostList[remoteID];
00117 if (result == null) {
00118 throw new ApplicationException("Unable to find remote host communiation object for device " + remoteID);
00119 }
00120 return result;
00121 }
00122
00123 private void AddDefaultRemoteHostTEMP() {
00124 if (settings.MyDeviceID == 0 ) {
00125 msgLog.LogTodo(" Find a better way of adding remote host clients");
00126 AddRemoteHost(new RemoteHostComms(this, 1));
00127
00128 } else {
00129 AddRemoteHost(new RemoteHostComms(this, 0));
00130 }
00131 }
00132 }
00133 }