// Method to send management messages over UDP to a specified port. // The messages must be derivatives of the Message object in the // Communications library. void SendManagementMsg(ushort port, Communications.Message msg) { IPEndPoint remoteEP = new IPEndPoint(localhost, port); byte[] data = Communications.Serialization.Serialize(msg); mgmtTxSocket.SendTo(data, remoteEP); }
public void listen() { IPEndPoint managingEndPoint = new IPEndPoint(IPAddress.Any, 0); EndPoint manager = (EndPoint)managingEndPoint; Socket orderingSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), CPCCport); orderingSocket.Bind(endPoint); while (true) { byte[] buffer = new byte[orderingSocket.SendBufferSize]; int readBytesNumber = orderingSocket.ReceiveFrom(buffer, ref manager); byte[] receivedData = new byte[readBytesNumber]; Array.Copy(buffer, receivedData, readBytesNumber); string stringToLog = ""; Communications.Message message = Communications.Serialization.Deserialize(receivedData); if (message.messageType.Equals("ConnectionDown")) { CPCCcommunications.ConnectionDown connectionDown = (CPCCcommunications.ConnectionDown)message; stringToLog = "Connection at label " + connectionDown.label + " is down"; } else if (message.messageType.Equals("ConnectionReady")) { CPCCcommunications.ConnectionReady connectionReady = (CPCCcommunications.ConnectionReady)message; if (connectionReady.ready) { stringToLog = "Connection at label " + connectionReady.label + " established"; } else { stringToLog = "Connection at label " + connectionReady.label + " failed"; } } else if (message.messageType.Equals("CallNotification")) { CPCCcommunications.CallNotification callNotification = (CPCCcommunications.CallNotification)message; if (callNotification.ready) { stringToLog = "Incoming call from " + callNotification.senderName; } else { stringToLog = "Connection with " + callNotification.senderName + " expired"; } } string currentTime = " <" + DateTime.Now.ToString("hh:mm:ss:fff") + ">\n"; mainWindow.Invoke(new Action(delegate() { mainWindow.logReceivedOrder(stringToLog, currentTime); })); } }
// Method invoked in a separate thread, sets up a UDP listener // that listens for anything on a management port, then detemines // what kind of message it is (with what information) and handles // the response. void ReceiveManagementMsg() { UdpClient listener = new UdpClient(mgmtLocalPort); IPEndPoint groupEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), mgmtLocalPort); try { while (true) { byte[] bytes = listener.Receive(ref groupEP); Communications.Message msg = Communications.Serialization.Deserialize(bytes); switch (msg.messageType) { case "NHLF.AddUpdateRequest": Log.WriteLine("[MGMT CCI] {1}, port {2}: {0})", msg.messageType, msg.senderID, msg.senderPort); AddUpdateRequest addUpdateReq = (AddUpdateRequest)msg; transportFunction.UpdateRoutingTable(addUpdateReq.entry, true); // !!! IT CAN GO TERRIBLY WRONG HERE !!! SendManagementMsg( (ushort)addUpdateReq.senderPort, new AddUpdateResponse(id, mgmtLocalPort, addUpdateReq.seq, true) ); Log.WriteLine("[MGMT CCI] Add/update forward: conn {2}, iface {0}, label {1}", addUpdateReq.entry.interface_in, addUpdateReq.entry.label_in, addUpdateReq.entry.connectionID); break; case "NHLF.RemoveRequest": Log.WriteLine("[MGMT CCI] {1}, port {2}: {0})", msg.messageType, msg.senderID, msg.senderPort); RemoveRequest removeReq = (RemoveRequest)msg; bool status = transportFunction.RemoveFromRoutingTable(removeReq.connectionID); SendManagementMsg( (ushort)removeReq.senderPort, new RemoveResponse(id, mgmtLocalPort, removeReq.seq, status) ); Log.WriteLine("[MGMT CCI] {0} entries for connection {1}", status ? "Removed" : "Could not remove", removeReq.connectionID); break; case "AllocateRequest": Log.WriteLine("[MGMT LRM] {1}, port {2}: {0})", msg.messageType, msg.senderID, msg.senderPort); AllocateRequest allocateReq = (AllocateRequest)msg; uint label = LRM.AssignBandwidthOnInterface(allocateReq.interfaceID, (uint)allocateReq.bitrate, allocateReq.connectionID); SendManagementMsg( (ushort)allocateReq.senderPort, new AllocateResponse( id, mgmtLocalPort, (int)label, allocateReq.seq ) ); if (label != 0) { Log.WriteLine("[MGMT LRM] Allocate {0} Mb/s on iface {1} for connection {2})", allocateReq.bitrate, allocateReq.interfaceID, allocateReq.connectionID); } else { Log.WriteLine("[MGMT LRM] Could not allocate {0} Mb/s on iface {1} for connection {2})", allocateReq.bitrate, allocateReq.interfaceID, allocateReq.connectionID); } break; case "DeallocateRequest": Log.WriteLine("[MGMT LRM] {1}, port {2}: {0})", msg.messageType, msg.senderID, msg.senderPort); DeallocateRequest deallocateReq = (DeallocateRequest)msg; LRM.ReleaseAsignedBandwidth(deallocateReq.connectionID); SendManagementMsg( (ushort)deallocateReq.senderPort, new DeallocateResponse( id, mgmtLocalPort, deallocateReq.seq ) ); Log.WriteLine("[MGMT LRM] Deallocate resources for connection {0})", deallocateReq.connectionID); break; #if DEBUG case "NHLF.AddUpdateResponse": AddUpdateResponse addUpdateResponse = (AddUpdateResponse)msg; Log.WriteLine("[CCI] AddUpdateRequest for {0} {1}.", addUpdateResponse.senderID, addUpdateResponse.status ? "successful" : "failed"); break; case "NHLF.RemoveResponse": RemoveResponse removeResp = (RemoveResponse)msg; Log.WriteLine("[CCI] Remove {0}", removeResp.status ? "some" : "none"); break; case "LRMRC.LinkStateUpdate": LinkStateUpdate linkStateUpdate = (LinkStateUpdate)msg; Log.WriteLine("[RC] {0} --{2}-> {1}", linkStateUpdate.beginNode.id, linkStateUpdate.endNode.id, linkStateUpdate.capacity); break; #endif default: break; } // NHLFMgmtMessage mgmtMessage = NHLFSerialization.Deserialize(bytes); //NHLFEntry newEntry = mgmtMessage.entry; //string result = transportFunction.UpdateRoutingTable(newEntry, mgmtMessage.addOrSwap); } } catch (Exception e) { Log.WriteLine(e.ToString()); } finally { listener.Close(); } }