public ManagementCenterConnection() { try { TcpClient tcpClient = new TcpClient("localhost", centerPort); stream = tcpClient.GetStream(); reader = new StreamReader(stream); writer = new StreamWriter(stream); connected = true; GUIWindow.PrintLog("Connection with Management Center has been established"); //SendRegistrationRequest(); GUIWindow.PrintLog("Sent registration request to Management Center"); SendRegistrationRequest(); RecieveRegistrationInfo(); //ReceiveMPLSTable(); //new Thread(ReceiveMessages).Start(); lock (Program.waiterCloud) { Monitor.Pulse(Program.waiterCloud); } //while(true) { // ReceiveMPLSTable(); //} } catch (SocketException se) { GUIWindow.PrintLog("Connection with Management Center has NOT been established"); connected = false; } }
public GUIWindow() { InitializeComponent(); CreateRoutingTable(); instance = this; lock (Program.waiterConfig) { Monitor.Pulse(Program.waiterConfig); } }
public static readonly LinkedList <int> ports = new LinkedList <int>(); //ID|IP|PORT public static void LoadConfig(string file, string id) { XmlDocument doc = new XmlDocument(); doc.LoadXml(file); XmlElement root = doc.DocumentElement; XmlNodeList routerNodesList = root.SelectNodes("/config/routers/router"); XmlNodeList controlCenterList = root.SelectNodes("/config/control-centers/control-center"); nodeID = Int32.Parse(id); foreach (XmlNode node in routerNodesList) { if (nodeID == Int32.Parse(node.Attributes["id"].Value)) { asID = Int32.Parse(node.Attributes["as-id"].Value); ip = node.SelectSingleNode("router-ip").InnerText; subnetworkRouter = Boolean.Parse(node.Attributes["subnetwork-router"].Value); XmlNodeList routerPortsList = node.SelectNodes("router-ports/router-port"); foreach (XmlNode portNode in routerPortsList) { ports.AddLast(Int32.Parse(portNode.InnerText)); } foreach (XmlNode n in controlCenterList) { if (Int32.Parse(n.Attributes["id"].Value) == asID) { ccPort = Int32.Parse(n.Attributes["listening-port"].Value); break; } } break; } } String msg = ""; int iter = 0; ushort[] tmpPorts = new ushort[ports.Count]; foreach (int i in ports) { msg = msg + "|" + i; tmpPorts[iter] = (ushort)ports.ToArray()[iter]; iter++; } CloudConnection.ClientIP = ip + "/24"; CloudConnection.ClientPorts = tmpPorts; CloudConnection.asID = asID; CloudConnection.subnetworkRouter = subnetworkRouter; GUIWindow.PrintLog("Config loaded: " + id + "|" + ip + msg); GUIWindow.ChangeWindowName("Router" + nodeID); GUIWindow.ChangeIP(ip); }
public static void SendMessage(Frame frame) { if (connected) { byte[] bytes = SerializeObject(frame); stream.Write(bytes, 0, bytes.Length); } else { GUIWindow.PrintLog("Connect with Cloud to send a message..."); } }
private void ReceiveRoutingTable() { while (true) { try { byte[] receivedBuffer = new byte[8192]; try { stream.Read(receivedBuffer, 0, receivedBuffer.Length); } catch { break; } Dictionary <int, int> routingTable = (Dictionary <int, int>)CloudConnection.DeserializeObject(receivedBuffer); //foreach (int key in routingTable.Keys) { // routingTable.TryGetValue(key, out int value); // GUIWindow.PrintLog("ID: " + key + " port: " + value); //} foreach (int key in routingTable.Keys) { int val; routingTable.TryGetValue(key, out val); Program.routingTable.Add(new Tuple <int, int>(key, val)); GUIWindow.PrintLog("CC: Received MatrixConnection(" + key + ", " + val + ") from network's CC"); GUIWindow.PrintLog("CC: Sent MatrixConnectionResponse() to network's CC"); } TimeSpan currentTime = DateTime.UtcNow - Process.GetCurrentProcess().StartTime.ToUniversalTime(); TimeSpan diff = currentTime - timeSpan; timeSpan = currentTime; //if (diff.TotalMilliseconds > 500) LinkedList <string[]> rows = new LinkedList <string[]>(); foreach (int key in routingTable.Keys) { routingTable.TryGetValue(key, out int val); rows.AddLast(new string[] { key.ToString(), val.ToString() }); } GUIWindow.UpdateRoutingTable(rows); } catch (Exception ex) { GUIWindow.PrintLog(ex.StackTrace); } } }
static void Main(string[] args) { try { args = Environment.GetCommandLineArgs(); new Thread(() => { //Thread.Sleep(2000); lock (waiterConfig) { Monitor.Wait(Program.waiterConfig); //String config = String.Concat(File.ReadAllLines("./../../../../sharedResources/tsst_config.xml")); //ConfigLoader.LoadConfig(config, "1"); String config = String.Concat(File.ReadAllLines(args[1])); ConfigLoader.LoadConfig(config, args[2]); lock (Program.waiterManagement) { Monitor.Pulse(Program.waiterManagement); } } }).Start(); new Thread(() => { //Thread.Sleep(2000); lock (Program.waiterManagement) { Monitor.Wait(Program.waiterManagement); } new ManagementCenterConnection(); new ControlCenterConnection(); }).Start(); new Thread(() => { lock (Program.waiterCloud) { Monitor.Wait(Program.waiterCloud); } cloudConnection = new CloudConnection(); }).Start(); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new GUIWindow()); }catch (Exception e) { GUIWindow.PrintLog(e.StackTrace); } }
public CloudConnection() { try { TcpClient tcpClient = new TcpClient("localhost", CloudPort); stream = tcpClient.GetStream(); connected = true; GUIWindow.PrintLog("Connection with Cloud has been established"); SendRegistrationRequest(); GUIWindow.PrintLog("Sent registration request to Cloud"); new Thread(ReceiveMessages).Start(); } catch (SocketException se) { GUIWindow.PrintLog("Connection with Cloud has NOT been established"); connected = false; } }
private void RecieveRegistrationInfo() { try { string msg = reader.ReadLine(); string[] parameters = msg.Split(':'); if (parameters[0].Equals("REGISTRATION") && parameters[1].Equals("OK")) { GUIWindow.PrintLog("Control Center accepted registration request"); } else { GUIWindow.PrintLog("Control Center denied registration request"); } } catch (IOException e) { GUIWindow.PrintLog(e.Message); } }
public void ReceiveMessages() { while (true) { byte[] receivedBuffer = new byte[8192]; try { stream.Read(receivedBuffer, 0, receivedBuffer.Length); } catch { break; } Frame frame = (Frame)DeserializeObject(receivedBuffer); GUIWindow.PrintLog("Received message addressed to " + frame.DestinationIP + " with Connection ID #" + frame.ConnectionID); int outPort = 0; List <int> ports = new List <int>(); foreach (Tuple <int, int> tuple in Program.routingTable) { if (tuple.Item1 == frame.ConnectionID) { ports.Add(tuple.Item2); } } if (ports[0] == frame.RouterInPort) { outPort = ports[1]; } else { outPort = ports[0]; } GUIWindow.PrintLog("Redirecting message through port " + frame.SourcePort); //GUIWindow.PrintLog(outPort.ToString()); frame.SourcePort = (ushort)outPort; frame.SourceIP = ClientIP; SendMessage(frame); } }
public ControlCenterConnection() { try { TcpClient tcpClient = new TcpClient("localhost", centerPort); stream = tcpClient.GetStream(); reader = new StreamReader(stream); writer = new StreamWriter(stream); connected = true; GUIWindow.PrintLog("Connection with Control Center has been established"); GUIWindow.PrintLog("Sent registration request to Control Center"); SendRegistrationRequest(); RecieveRegistrationInfo(); SendKeepAlive(); ReceiveRoutingTable(); } catch (SocketException se) { GUIWindow.PrintLog("Connection with Control Center has NOT been established"); GUIWindow.PrintLog(se.Message); connected = false; } }
public void RecieveConfig() { try { String msg = reader.ReadLine(); String[] parameters = msg.Split(':'); if (parameters[0].Equals("CONFIG")) { ClientIP = parameters[1]; ClientPorts = new ushort[parameters.Length - 3]; int iter = 0; String ports = ""; for (int i = 2; i < parameters.Length - 1; i++) { ClientPorts[iter] = ushort.Parse(parameters[i]); ports += "|" + ClientPorts[iter]; iter++; } CloudConnection.ClientIP = ClientIP + "/24"; CloudConnection.ClientPorts = ClientPorts; int routerID = int.Parse(parameters[parameters.Length - 1]); GUIWindow.ChangeWindowName("Router" + routerID); GUIWindow.PrintLog("Config received: " + ClientIP + ports + " (" + "Router" + routerID + ")"); } else { GUIWindow.PrintLog("Managment Center denied registration request"); } } catch (IOException) { } }