private void Send(ClientInfo client, ContainerMessage message) { try { string mc = message.Content; if (mc.Length > 20) { mc = mc.Substring(0, 20) + "..."; } RaiseNewTextEvent(string.Format("Sending message [{0} -> {1}: {2} # {3}]", message.Sender, message.Receiver, message.Info, mc)); string s = ContainerMessage.Serialize(message); byte[] buffer = ASCIIEncoding.Unicode.GetBytes(s); BasicMessage m = new BasicMessage(_serverGuid, buffer); client.TcpSocketClientHandler.SendAsync(m); } catch (SocketException se) { // "An existing connection was forcibly closed by the remote host" RaiseNewTextEvent("Container " + _containerNames[client] + " is disconnected."); _containerNames.Remove(client); client.TcpSocketClientHandler.Close(); RaiseNewTextEvent("Exception in Server.Send: " + se.Message + "."); } catch (Exception ex) { RaiseNewTextEvent("Exception in Server.Send: " + ex.Message + "."); } }
internal void MoveAgent(AgentState state, string destination) { string serializedAgentState = ContainerMessage.Serialize(state); var cm = new ContainerMessage(_name, destination, "Request Move Agent", serializedAgentState); Send(cm); }
private void ProcessMessage(ContainerMessage message) { string mc = message.Content; if (mc.Length > 20) { mc = mc.Substring(0, 20) + "..."; } RaiseNewTextEvent(string.Format("Message received [{0} -> {1}: {2} # {3}]", message.Sender, message.Receiver, message.Info, mc)); if (message.Sender == "Server" && message.Info == "Inform Invalid Name") { RaiseNewTextEvent("Container name refused by server.\r\n"); } else if (message.Info == "Request Move Agent") { AgentHasArrived(message.Content); } else if (message.Sender == "Server" && message.Info == "Inform Containers") { string[] toks = message.Content.Split(); _allContainers.Clear(); foreach (string t in toks) { if (t != "" && t != " ") { _allContainers.Add(t); } } } }
private void Send(ClientInfo client, ContainerMessage message) { try { RaiseNewTextEvent($"Sending message [{message.Format()}]"); string s = ContainerMessage.Serialize(message); byte[] buffer = ASCIIEncoding.Unicode.GetBytes(s); var m = new BasicMessage(_serverGuid, buffer); client.TcpSocketClientHandler.SendAsync(m); } catch (SocketException se) { // "An existing connection was forcibly closed by the remote host" RaiseNewTextEvent($"Container {_containerNames[client]} is disconnected."); _containerNames.Remove(client); client.TcpSocketClientHandler.Close(); RaiseNewTextEvent($"Exception in Server.Send: {se.Message}."); } catch (Exception ex) { RaiseNewTextEvent($"Exception in Server.Send: {ex.Message}."); } }
private void ProcessMessage(ContainerMessage message) { RaiseNewTextEvent($"Message received [{message.Format()}]"); if (message.Sender == "Server" && message.Info == "Inform Invalid Name") { RaiseNewTextEvent("Container name refused by server.\r\n"); } else if (message.Sender == "Server" && message.Info == "Inform Containers") { string[] toks = message.Content.Split(); _allContainers.Clear(); foreach (string t in toks) { if (t != "" && t != " ") { _allContainers.Add(t); } } } else if (message.Info == "Request Move Agent") { AgentHasArrived(message.Content); } else if (message.Info == "Send LD Message") { LDMessageReceived(message.Content); } }
internal void SendLDAgentMessage(string receiverContainer, Message message) { message.Sender = $"{message.Sender}@{this.Name}"; string serializedMessage = ContainerMessage.Serialize(message); var cm = new ContainerMessage(_name, receiverContainer, "Send LD Message", serializedMessage); Send(cm); }
private void Send(ContainerMessage message) { try { RaiseNewTextEvent($"Sending message [{message.Format()}]"); string s = ContainerMessage.Serialize(message); byte[] buffer = ASCIIEncoding.Unicode.GetBytes(s); var serializedMessage = new BasicMessage(_clientGuid, buffer); _client.SendAsync(serializedMessage); } catch (Exception ex) { RaiseNewTextEvent(ex.Message); } }
private void client_ReceiveMessageEvent(AbstractTcpSocketClientHandler handler, AbstractMessage message) { try { var receivedMessage = (BasicMessage)message; byte[] buffer = receivedMessage.GetBuffer(); string s = ASCIIEncoding.Unicode.GetString(buffer); if (ContainerMessage.Deserialize(s) is ContainerMessage deserializedMessage) { ProcessMessage(deserializedMessage); } } catch (Exception ex) { RaiseNewTextEvent(ex.Message); } }
private void _timer_Elapsed(object sender, ElapsedEventArgs e) { try { if (_server == null) { return; } ClientInfo[] clientList = _server.GetClientList(); if (clientList.Length != 0) { string containerListAsString = ""; foreach (string cn in _containerNames.Values.ToList()) { if (cn.StartsWith("Unknown container")) // ignore container until it registers { continue; } containerListAsString += cn + " "; } foreach (ClientInfo ci in clientList) { if (_containerNames.ContainsKey(ci)) { var m = new ContainerMessage("Server", _containerNames[ci], "Inform Containers", containerListAsString); Send(ci, m); } } DisplayContainerList(); } } catch (Exception ex) { RaiseNewTextEvent("Exception in Server.TimerElapsed: " + ex.Message + "."); } }
private void Send(ContainerMessage message) { try { string mc = message.Content; if (mc.Length > 20) { mc = mc.Substring(0, 20) + "..."; } RaiseNewTextEvent(string.Format("Sending message [{0} -> {1}: {2} # {3}]", message.Sender, message.Receiver, message.Info, mc)); string s = ContainerMessage.Serialize(message); byte[] buffer = ASCIIEncoding.Unicode.GetBytes(s); BasicMessage serializedMessage = new BasicMessage(_clientGuid, buffer); _client.SendAsync(serializedMessage); } catch (Exception ex) { RaiseNewTextEvent(ex.Message); } }
private void server_ReceiveMessageEvent(AbstractTcpSocketClientHandler handler, AbstractMessage message) { try { BasicMessage receivedMessage = (BasicMessage)message; byte[] buffer = receivedMessage.GetBuffer(); string s = System.Text.ASCIIEncoding.Unicode.GetString(buffer); ClientInfo[] clientList = _server.GetClientList(); if (clientList.Length == 0) { return; } ClientInfo sender = null; foreach (ClientInfo ci in clientList) { if (ci.TcpSocketClientHandler == handler) { sender = ci; break; } } var deserializedMessage = ContainerMessage.Deserialize(s) as ContainerMessage; if (deserializedMessage != null) { ProcessMessage(deserializedMessage, sender); } } catch (Exception ex) { RaiseNewTextEvent("Exception in Server.ReceiveMessageEvent: " + ex.Message + "."); } }
private void ProcessMessage(ContainerMessage message, ClientInfo sender) { if (message.Sender == "Server") { return; } string mc = message.Content; if (mc.Length > 20) { mc = mc.Substring(0, 20) + "..."; } RaiseNewTextEvent(string.Format("Message received [{0} -> {1}: {2} # {3}]", message.Sender, message.Receiver, message.Info, mc)); if (message.Receiver != "Server" && message.Info == "Request Move Agent") { RaiseNewTextEvent(string.Format("Moving agent from {0} to {1}", message.Sender, message.Receiver)); } if (message.Receiver == "Server" && message.Info == "Request Register") { bool exists = false; foreach (string s in _containerNames.Values) { if (s == message.Sender) { exists = true; break; } } if (exists) { var m = new ContainerMessage("Server", message.Sender, "Inform Invalid Name", ""); Send(sender, m); sender.TcpSocketClientHandler.Close(); } else { _containerNames[sender] = message.Sender; DisplayContainerList(); } } else if (message.Info == "Request Move Agent") { foreach (string s in _containerNames.Values) { if (s == message.Sender) { ClientInfo[] clientList = _server.GetClientList(); if (clientList.Length == 0) { return; } foreach (ClientInfo ci in clientList) { if (_containerNames.ContainsKey(ci) && _containerNames[ci] == message.Receiver) { Send(ci, message); } } } } } }
private void Register() { var message = new ContainerMessage(_name, "Server", "Request Register", ""); Send(message); }
internal void AgentHasArrived(string agentState) { AgentState state = ContainerMessage.Deserialize(agentState) as AgentState; _environment.AgentHasArrived(state); }
internal void LDMessageReceived(string content) { var message = ContainerMessage.Deserialize(content) as Message; _environment.LDMessageReceived(message); }