public void ActivateRouter() { Console.WriteLine("My name is: " + this.Name); SocketToForward = new Socket(cloudIp.AddressFamily, SocketType.Stream, ProtocolType.Tcp); SocketToManager = new Socket(ManagerIP.AddressFamily, SocketType.Stream, ProtocolType.Tcp); SocketToForward.Connect(new IPEndPoint(cloudIp, cloudPort)); Console.WriteLine("[" + DateTime.UtcNow.ToString("HH:mm:ss.fff", System.Globalization.CultureInfo.InvariantCulture) + "] " + "Connected with cloud :)"); SocketToForward.Send(Encoding.ASCII.GetBytes("First Message " + this.IpAddress.ToString())); SocketToManager.Connect(new IPEndPoint(ManagerIP, ManagerPort)); byte[] buffer = new byte[4096]; Console.WriteLine("[" + DateTime.UtcNow.ToString("HH:mm:ss.fff", CultureInfo.InvariantCulture) + "] " + "Connected with manager :)"); byte[] msg = Encoding.ASCII.GetBytes(Name); int bytesSent = SocketToManager.Send(msg); String data = null; while (true) { int bytesRec = SocketToManager.Receive(buffer); data += Encoding.ASCII.GetString(buffer, 0, bytesRec); if (data.IndexOf("</R_config>") > -1) { break; } } XmlSerializer serializer = new XmlSerializer(typeof(R_config)); R_config result; using (TextReader reader = new StringReader(data)) { result = (R_config)serializer.Deserialize(reader); } packageHandler.ILM_Table = result.ILM; packageHandler.FTN_Table = result.FTN; packageHandler.FEC_Table = result.FEC; packageHandler.FIB_Table = result.FIB; packageHandler.NHLFE_Table = result.NHLFE; Console.WriteLine("[" + DateTime.UtcNow.ToString("HH:mm:ss.fff", CultureInfo.InvariantCulture) + "] " + "I got MPLS Tables :) "); packageHandler.displayTables(); // SocketToForward.Connect(new IPEndPoint(cloudIp,cloudPort)); Thread forwardingThread = new Thread(WaitForPackage); Thread managementThread = new Thread(WaitForCommands); managementThread.Start(); forwardingThread.Start(); }
public void WaitForCommands() { while (true) { String data = null; // Domain będzie aktualizował te optical entry SocketToDomain.Receive(bufferForManagement);// from CC in management we have to check whether it is possible to take requested capacity Console.WriteLine("Added123"); Optical_Entry opticalEntry = null; if (Encoding.ASCII.GetString(bufferForManagement.ToList().GetRange(0, 3).ToArray()).Equals("ACK")) { Console.WriteLine("Added123456"); opticalEntry = packageHandler.FromBytesToEntry(bufferForManagement.ToList().GetRange(3, 12).ToArray()); packageHandler.Optical_Table.Add(opticalEntry); Console.WriteLine("Added"); } //Optical_Entry opticalEntry = packageHandler.FromBytesToEntry(bufferForManagement); // int k = 0; foreach (var link in linkResources) { if (link.port == opticalEntry.outPort) { for (int j = opticalEntry.startSlot; j <= opticalEntry.lastSlot; j++) { link.slots[j] = false; } break; } } //SocketToDomain.Send(Encoding.ASCII.GetBytes("G8 CONF MY M8")); // ack for Domain- odsyłamy do Domaina że j // SocketToDomain.Send(Encoding.ASCII.GetBytes("G8 CONF MY M8")); Console.WriteLine(this.Name + ": [" + DateTime.UtcNow.ToString("HH:mm:ss.fff", CultureInfo.InvariantCulture) + "] " + "I got new configuration!!!"); packageHandler.displayTables(); Console.WriteLine(this.Name + ": [" + DateTime.UtcNow.ToString("HH:mm:ss.fff", CultureInfo.InvariantCulture) + "] " + "I updated my MPLS tables! :) "); } }