00001 using System;
00002 using System.Drawing;
00003 using System.Collections;
00004 using System.Windows.Forms;
00005 using System.Data;
00006 using System.Net;
00007 using System.Net.Sockets;
00008
00009 namespace SmartDeviceGUIClient {
00013 public class Form1 : System.Windows.Forms.Form {
00014 private System.Windows.Forms.Label label1;
00015 private System.Windows.Forms.Button button1;
00016 private System.Windows.Forms.TextBox textBox1;
00017 private System.Windows.Forms.Button button2;
00018 private System.Windows.Forms.Button button3;
00019 private System.Windows.Forms.TextBox textBox2;
00020 private System.Windows.Forms.Label label2;
00021 private System.Windows.Forms.MainMenu mainMenu1;
00022
00023 public Form1() {
00024
00025
00026
00027 InitializeComponent();
00028
00029
00030
00031
00032 }
00036 protected override void Dispose( bool disposing ) {
00037 base.Dispose( disposing );
00038 }
00039 #region Windows Form Designer generated code
00044 private void InitializeComponent() {
00045 this.mainMenu1 = new System.Windows.Forms.MainMenu();
00046 this.label1 = new System.Windows.Forms.Label();
00047 this.button1 = new System.Windows.Forms.Button();
00048 this.textBox1 = new System.Windows.Forms.TextBox();
00049 this.button2 = new System.Windows.Forms.Button();
00050 this.button3 = new System.Windows.Forms.Button();
00051 this.textBox2 = new System.Windows.Forms.TextBox();
00052 this.label2 = new System.Windows.Forms.Label();
00053
00054
00055
00056 this.label1.Size = new System.Drawing.Size(240, 20);
00057 this.label1.Text = "Welcome to GPRSWeb Mobile Client";
00058
00059
00060
00061 this.button1.Location = new System.Drawing.Point(8, 32);
00062 this.button1.Size = new System.Drawing.Size(64, 24);
00063 this.button1.Text = "button1";
00064 this.button1.Click += new System.EventHandler(this.button1_Click);
00065
00066
00067
00068 this.textBox1.AcceptsReturn = true;
00069 this.textBox1.Location = new System.Drawing.Point(8, 96);
00070 this.textBox1.Multiline = true;
00071 this.textBox1.ReadOnly = true;
00072 this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
00073 this.textBox1.Size = new System.Drawing.Size(224, 168);
00074 this.textBox1.Text = "textBox1";
00075 this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
00076
00077
00078
00079 this.button2.Location = new System.Drawing.Point(80, 32);
00080 this.button2.Size = new System.Drawing.Size(80, 24);
00081 this.button2.Text = "Show Text";
00082 this.button2.Click += new System.EventHandler(this.button2_Click);
00083
00084
00085
00086 this.button3.Location = new System.Drawing.Point(168, 32);
00087 this.button3.Size = new System.Drawing.Size(64, 24);
00088 this.button3.Text = "button3";
00089 this.button3.Click += new System.EventHandler(this.button3_Click);
00090
00091
00092
00093 this.textBox2.Location = new System.Drawing.Point(64, 64);
00094 this.textBox2.Size = new System.Drawing.Size(168, 20);
00095 this.textBox2.Text = "textBox2";
00096
00097
00098
00099 this.label2.Location = new System.Drawing.Point(8, 64);
00100 this.label2.Size = new System.Drawing.Size(48, 20);
00101 this.label2.Text = "This IP:";
00102 this.label2.ParentChanged += new System.EventHandler(this.label2_ParentChanged);
00103
00104
00105
00106 this.ClientSize = new System.Drawing.Size(240, 270);
00107 this.Controls.Add(this.label2);
00108 this.Controls.Add(this.textBox2);
00109 this.Controls.Add(this.button3);
00110 this.Controls.Add(this.button2);
00111 this.Controls.Add(this.textBox1);
00112 this.Controls.Add(this.button1);
00113 this.Controls.Add(this.label1);
00114 this.Menu = this.mainMenu1;
00115 this.Text = "Form1";
00116 this.Load += new System.EventHandler(this.Form1_Load);
00117
00118 }
00119 #endregion
00120
00124
00125 static void Main() {
00126 Application.Run(new Form1());
00127 }
00128
00129 private void Form1_Load(object sender, System.EventArgs e) {
00130 label1.TextAlign = ContentAlignment.TopCenter;
00131 }
00132
00133 StackInterface stackInf;
00134
00135 private void button1_Click(object sender, System.EventArgs e) {
00136
00137
00138 stackInf = new StackInterface();
00139 SegmentReceiver rx = new SegmentReceiver(stackInf);
00140 SegmentTransmitter tx = new SegmentTransmitter();
00141 rx.Start();
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158 }
00159
00160 private void textBox1_TextChanged(object sender, System.EventArgs e) {
00161
00162 }
00163
00164 private void button2_Click(object sender, System.EventArgs e) {
00165 textBox1.Text = stackInf.strWriter.ToString();
00166 }
00167
00168 private void button3_Click(object sender, System.EventArgs e) {
00169 textBox1.Text += "\r\nSend button Pressed";
00170 SendTQE();
00171 }
00172
00173 UdpClient myUDP = null;
00174 private void SendTQE() {
00176 myUDP = new UdpClient(new IPEndPoint(IPAddress.Parse(textBox2.Text), 12346));
00177 Segment workingSegment = new Segment();
00178 uint sequenceOffset = 0;
00179
00180
00181 bool isAckable = false;
00182 AckInfo ackInfo = null;
00183 workingSegment.Headers = new SegmentHeaders();
00184 NackInfo nackInfo = null;
00185 uint seqNum = 1;
00186 bool isResynch = false;
00187
00188
00189 IPAddress currentDestination = IPAddress.Parse("131.111.251.46");
00190
00191
00192 workingSegment.Data = null;
00193
00194
00195
00196 workingSegment.Headers.IsAckable = isAckable;
00197 workingSegment.Headers.IsResynch = isResynch;
00198 workingSegment.Headers.IsFirstInMessage = true;
00199 workingSegment.Headers.IsLastInMessage = true;
00200
00201
00202 workingSegment.Headers.SourceDeviceID = 1;
00203 workingSegment.Headers.SequenceNumber = seqNum;
00204
00205
00206 if (ackInfo == null) {
00207 workingSegment.Headers.ClearAck();
00208 } else {
00209 workingSegment.Headers.SetAck(ackInfo);
00210 }
00211 if (nackInfo == null) {
00212 workingSegment.Headers.ClearNACKStatus();
00213 } else {
00214 workingSegment.Headers.SetNACKStatus(nackInfo);
00215
00216 }
00217
00218
00219
00220
00221 workingSegment.Headers.TimeSent = DateTime.Now.ToUniversalTime();
00222 tempSend(workingSegment, currentDestination);
00223
00224
00225 }
00226
00227 public void tempSend(Segment targetSegment, IPAddress destination) {
00228 byte[] datagram = targetSegment.ToByteArray();
00229 myUDP.Send(datagram, datagram.Length, new IPEndPoint(destination, 30481));
00230 MessageBox.Show("Sent segment");
00231 }
00232
00233 private void label2_ParentChanged(object sender, System.EventArgs e) {
00234
00235 }
00236 }
00237
00238 internal class Console {
00239 TextBox textOut;
00240 public Console(TextBox textOut) {
00241 this.textOut = textOut;
00242 }
00243 public void WriteLine(string s) {
00244 WriteLine(s, null);
00245 }
00246 public void WriteLine(string s, params object[] objs) {
00247 textOut.Text += String.Format(s, objs) + "\r\n";
00248 }
00249 public void ReadLine() {}
00250 public void Write(string s) { Write(s, null); }
00251 public void Write(string s, params object[] objs) {
00252 textOut.Text += String.Format(s, objs);
00253 }
00254 }
00255
00256 public class StackInterface {
00257 public MessageLogger msgLog;
00258 public System.IO.StringWriter strWriter;
00259 public StackSettings settings;
00260 public StackInterface() {
00261 strWriter = new System.IO.StringWriter();
00262 msgLog = new MessageLogger(strWriter);
00263 settings = new StackSettings();
00264 }
00265 }
00266
00267 public class StackSettings {
00268 public StackSettings() {
00269 LoadFromReg();
00270
00271 }
00272
00277 private int myDeviceID = -1 ;
00278
00282 public ushort MyDeviceID {
00283 get { if (myDeviceID >= 0) {
00284 return (ushort)myDeviceID;
00285 } else {
00286 throw new ApplicationException("Unable to fetch Device ID for stack - no device ID set");
00287 }
00288 }
00289 set { myDeviceID = value;}
00290 }
00291
00295 public int ClientReceivePortNumber {
00296 get { return 30481; }
00297 }
00298 private int clientRxPortNum;
00299
00303 public int ClientSendFromPortNumber {
00304 get { return 12346; }
00305 }
00306
00310 public int ServerSendFromPortNumber {
00311 get { return 12345; }
00312 }
00313
00317 public int ServerReceivePortNumber {
00318 get { return 30481; }
00319 }
00320 private int serverRxPortNum;
00321
00322 private void LoadFromReg() {
00323
00324
00325 myDeviceID = 1;
00326 defaultBDP = 8 * 1024;
00327 interAckableTime = 1;
00328 firstAckableTime = 2;
00329 ackArrivalTimeout = 60;
00330 serverIP = "131.111.251.169";
00331 clientIP = "128.232.98.28";
00332 clientRxPortNum = 30481;
00333 serverRxPortNum = 30481;
00334 }
00335
00336 private int defaultBDP;
00337 public int DefaultBandwidthDelayProdut {
00338 get {
00339 if (defaultBDP >= 0) {
00340 return defaultBDP;
00341 } else {
00342 throw new ApplicationException("Unable to fetch Default Bandwidth Deplay Product - not set");
00343 }
00344 }
00345 }
00346
00347 private int interAckableTime;
00348 public int InterAckableTime {
00349 get {
00350 if (interAckableTime >= 0) {
00351 return interAckableTime;
00352 } else {
00353 throw new ApplicationException("Unable to fetch InterAckableTime - not set");
00354 }
00355 }
00356 }
00357
00358 private int firstAckableTime;
00359 public int FirstAckableTime {
00360 get {
00361 if (firstAckableTime >= 0) {
00362 return firstAckableTime;
00363 } else {
00364 throw new ApplicationException("Unable to fetch FirstAckableTime - not set");
00365 }
00366 }
00367 }
00368
00369 private int ackArrivalTimeout;
00370 public int AckArrivalTimeout {
00371 get {
00372 if (ackArrivalTimeout >= 0) {
00373 return ackArrivalTimeout;
00374 } else {
00375 throw new ApplicationException("Unable to fetch AckArrivalTimeout- not set");
00376 }
00377 }
00378 }
00379
00380 private string serverIP;
00381 public IPAddress ServerIP {
00382 get {
00383 try {
00384 return IPAddress.Parse(serverIP);
00385 } catch (Exception ex) {
00386 throw new ApplicationException("Unable to fetch Server IP\r\n" + ex.ToString());
00387 }
00388 }
00389 set {
00390 serverIP = value.ToString();
00391 }
00392 }
00393
00394 private string clientIP;
00395 public IPAddress ClientIP {
00396 get {
00397 try {
00398 return IPAddress.Parse(clientIP);
00399 } catch (Exception ex) {
00400 throw new ApplicationException("Unable to fetch Client IP\r\n" + ex.ToString());
00401 }
00402 }
00403 set { clientIP = value.ToString(); }
00404 }
00405
00409 public ushort SERVER_DEVICE_ID {
00410 get { return 0; }
00411 }
00412 }
00413 }