00001 using System;
00002 using System.Collections;
00003 using System.Net;
00004 using Microsoft.Win32;
00005
00006 namespace ProtocolStack
00007 {
00011 public class IPMapper : System.Collections.DictionaryBase
00012 {
00016 public IPMapper(StackInterface thisStack) : base() {
00017
00018 IPAddress hostIP = thisStack.settings.ServerIP;
00019 Console.Write("Server IP Address (ENTER Selects [{0}]): ", hostIP.ToString());
00020 string serverIPString = Console.ReadLine();
00021 if (serverIPString == "") { serverIPString = hostIP.ToString(); }
00022 IPAddress myServerIP = IPAddress.Parse(serverIPString);
00023 this.AddMapping(0, myServerIP);
00024
00025
00026 IPAddress defaultAddress = thisStack.settings.ClientIP;
00027 Console.Write("GPRS Phone (Client) Address (Enter Selects [{0}]): ", defaultAddress.ToString());
00028 string gprsIPstring = Console.ReadLine();
00029 if (gprsIPstring == "") { gprsIPstring = defaultAddress.ToString(); }
00030 this.AddMapping(1, IPAddress.Parse(gprsIPstring));
00031 }
00032
00033 public IPAddress Lookup(ushort deviceID) {
00034 if (Dictionary.Contains(deviceID)) {
00035 return (IPAddress)Dictionary[deviceID];
00036 } else {
00037 throw new IPMappingNotFoundException(String.Format("IP Mapping for Device {0} not found", deviceID));
00038 }
00039 }
00040
00041 public void AddMapping(ushort deviceID, IPAddress ipAddress) {
00042 try {
00043 Dictionary[deviceID] = ipAddress;
00044 } catch (Exception ex) {
00045 throw new IPMappingException("Unable to Add mappting to IP Mapper", ex);
00046 }
00047 }
00048 }
00049
00050 public class IPMappingException : ApplicationException {
00051 public IPMappingException() : base() {}
00052 public IPMappingException(string message) : base(message) {}
00053 public IPMappingException(string message, Exception innerException) : base(message, innerException) {}
00054 }
00055
00056 public class IPMappingNotFoundException : IPMappingException {
00057 public IPMappingNotFoundException() : base() {}
00058 public IPMappingNotFoundException(string message) : base(message) {}
00059 public IPMappingNotFoundException(string message, Exception innerException) : base(message, innerException) {}
00060 };
00061
00062
00063
00064 public class StackSettings {
00065 public StackSettings() {
00066 LoadFromReg();
00067
00068 }
00069
00074 private int myDeviceID = -1 ;
00075
00079 public ushort MyDeviceID {
00080 get { if (myDeviceID >= 0) {
00081 return (ushort)myDeviceID;
00082 } else {
00083 throw new ApplicationException("Unable to fetch Device ID for stack - no device ID set");
00084 }
00085 }
00086 set { myDeviceID = value;}
00087 }
00088
00092 public int ClientReceivePortNumber {
00093 get { return 30481; }
00094 }
00095 private int clientRxPortNum;
00096
00100 public int ClientSendFromPortNumber {
00101 get { return 12346; }
00102 }
00103
00107 public int ServerSendFromPortNumber {
00108 get { return 12345; }
00109 }
00110
00114 public int ServerReceivePortNumber {
00115 get { return 30481; }
00116 }
00117 private int serverRxPortNum;
00118
00119 private void LoadFromReg() {
00120
00121 RegistryKey regKey = Registry.CurrentUser.OpenSubKey("Software\\University of Cambridge\\GPRSWeb", true);
00122 myDeviceID = (int)regKey.GetValue("DeviceID", -1);
00123 defaultBDP = (int)regKey.GetValue("DefaultBDP", -1);
00124 interAckableTime = (int)regKey.GetValue("InterAckableTime", -1);
00125 firstAckableTime = (int)regKey.GetValue("FirstAckableTime", -1);
00126 ackArrivalTimeout = (int)regKey.GetValue("AckArrivalTimeout", -1);
00127 serverIP = (string)regKey.GetValue("ServerIP", "");
00128 clientIP = (string)regKey.GetValue("ClientIP", "");
00129 clientRxPortNum = (int)regKey.GetValue("ClientRxPort", -1);
00130 serverRxPortNum = (int)regKey.GetValue("ServerRxPort", -1);
00131 }
00132
00133 private int defaultBDP;
00134 public int DefaultBandwidthDelayProdut {
00135 get {
00136 if (defaultBDP >= 0) {
00137 return defaultBDP;
00138 } else {
00139 throw new ApplicationException("Unable to fetch Default Bandwidth Deplay Product - not set");
00140 }
00141 }
00142 }
00143
00144 private int interAckableTime;
00145 public int InterAckableTime {
00146 get {
00147 if (interAckableTime >= 0) {
00148 return interAckableTime;
00149 } else {
00150 throw new ApplicationException("Unable to fetch InterAckableTime - not set");
00151 }
00152 }
00153 }
00154
00155 private int firstAckableTime;
00156 public int FirstAckableTime {
00157 get {
00158 if (firstAckableTime >= 0) {
00159 return firstAckableTime;
00160 } else {
00161 throw new ApplicationException("Unable to fetch FirstAckableTime - not set");
00162 }
00163 }
00164 }
00165
00166 private int ackArrivalTimeout;
00167 public int AckArrivalTimeout {
00168 get {
00169 if (ackArrivalTimeout >= 0) {
00170 return ackArrivalTimeout;
00171 } else {
00172 throw new ApplicationException("Unable to fetch AckArrivalTimeout- not set");
00173 }
00174 }
00175 }
00176
00177 private string serverIP;
00178 public IPAddress ServerIP {
00179 get {
00180 try {
00181 return IPAddress.Parse(serverIP);
00182 } catch (Exception ex) {
00183 throw new ApplicationException("Unable to fetch Server IP", ex);
00184 }
00185 }
00186 }
00187
00188 private string clientIP;
00189 public IPAddress ClientIP {
00190 get {
00191 try {
00192 return IPAddress.Parse(clientIP);
00193 } catch (Exception ex) {
00194 throw new ApplicationException("Unable to fetch Client IP", ex);
00195 }
00196 }
00197 }
00198
00202 public ushort SERVER_DEVICE_ID {
00203 get { return 0; }
00204 }
00205
00206 }
00207 }