00001 using System;
00002 using System.Data;
00003 using System.Net;
00004 using System.Net.Sockets;
00005 using System.IO;
00006 using System.Text;
00007
00008 namespace SmartDeviceTestbed
00009 {
00013 public class Class1
00014 {
00015 SegmentReceiver rx = new SegmentReceiver();
00016 public Class1(ref string s) {
00017 StringWriter strWriter = new StringWriter();
00018 StackInterface stackitnf = new StackInterface(strWriter);
00019 s = strWriter.GetStringBuilder().ToString();
00020 }
00021 }
00022
00023 class StackInterface {
00024 public MessageLogger msgLog;
00025 public StackInterface(TextWriter writer) {
00026 msgLog = new MessageLogger(writer);
00027 }
00028 }
00029
00030 public class MessageLogger {
00031 private System.IO.TextWriter writer;
00032 public MessageLogger(System.IO.TextWriter loggerOutput) {
00033 writer = loggerOutput;
00034 }
00035
00036 public void Log(string s) {
00037 writer.WriteLine(" " + s);
00038 }
00039 public void Log(string s, params object[] args) {
00040 writer.WriteLine(" " + s, args);
00041 }
00042 public void LogError(string s) {
00043 writer.WriteLine("*** " + s);
00044 }
00045 public void LogError(string s, params object[] args) {
00046 writer.WriteLine("*** " + s, args);
00047 }
00048 public void LogTodo(string s) {
00049 writer.WriteLine("### " + s);
00050 }
00051 public void LogTodo(string s, params object[] args) {
00052 writer.WriteLine("### " + s, args);
00053 }
00054 public void LogStartBegin(string deviceStarting) {
00055 writer.WriteLine("+++ Starting " + deviceStarting);
00056 }
00057 public void LogStartComplete(string deviceStarted) {
00058 writer.WriteLine("+++ " + deviceStarted + " Started");
00059 }
00060 }
00061
00065 public class SegmentReceiver {
00066 Thread processingThread;
00067
00068 UdpClient udpListener;
00069
00074 public SegmentReceiver(StackInterface thisStack) {
00075 this.thisStack = thisStack;
00076 }
00077
00078 public void Start() {
00079 thisStack.msgLog.LogStartBegin("Segment Receiver");
00080 processingThread = new Thread(new ThreadStart(Listen));
00081 processingThread.Start();
00082 thisStack.msgLog.LogStartComplete("Segment Receiver");
00083 }
00084
00086 private void Listen() {
00087 IPEndPoint currentEndPoint;
00088 Segment receivedSegment;
00089 int localPortNumber = -1;
00090 if (thisStack.settings.MyDeviceID == thisStack.Settings.SERVER_DEVICE_ID) {
00091 localPortNumber = thisStack.Settings.ServerReceivePortNumber;
00092 } else {
00093 localPortNumber = thisStack.Settings.ClientReceivePortNumber;
00094 }
00095
00096 udpListener = new UdpClient(new IPEndPoint(IPAddress.Any, localPortNumber));
00097 try {
00098 while(true) {
00099 try {
00100
00101 currentEndPoint = new IPEndPoint(IPAddress.Any, localPortNumber);
00102 receivedSegment = new Segment(udpListener.Receive(ref currentEndPoint));
00103 receivedSegment.Headers.TimeReceived = DateTime.Now.ToUniversalTime();
00104 Console.WriteLine("Got segment #{0}", receivedSegment.Headers.SequenceNumber);
00105 thisStack.GetRemoteHostByID(receivedSegment.Headers.SourceDeviceID).AcceptIncomingSegment(receivedSegment);
00106 } catch (Exception ex) {
00107 thisStack.msgLog.LogError("Error in Protocol Stack\n\rUnable to correctly receive segment.\n\rSkipping over and continuting. Details:\n\r(" + ex + ")");
00108 }
00109 }
00110 } catch (ThreadAbortException) {
00111 Console.WriteLine("Segment Receive Listen Thread Aborted");
00112 } finally {
00113 if (udpListener != null) { udpListener.Close(); }
00114 }
00115 }
00116 }
00117 }