protected void CheckPingList() { var expiredPing = ReplyWaitingList.Find(m => Time.GlobalSimulationTime - m.RoutingTime > MaxPingDelay); if (expiredPing?.ReceiverAgent is Leader) { // Leader is lost var messenger = FindNearestMessenger(); if (messenger != null) { ReplyWaitingList.Remove(expiredPing); // remove to prevent duplicate request if (expiredPing.ReceiverAgentId != LeaderAgent.AgentId) { return; } var ping = new Message() { RoutingTime = Time.GlobalSimulationTime, SenderAgent = this, SenderAgentId = AgentId, CurrentSenderAgent = this, CurrentReceiverAgent = messenger, CurrentReceiverAgentId = messenger.AgentId, ReceiverAgent = messenger, ReceiverAgentId = messenger.AgentId, MessageType = BroadcastType.SingleCast, MessageContent = MessagesContent.LostLeader, DataMessageText = LeaderAgent.AgentId }; SendMessage(ping); } } }
protected void ProcessMessage(Message message) { if (message.MessageContent == MessagesContent.ReplyRulerNum) { if (message.RulerPingReply != null) { if (AdaptingWaitingList.Count > 0) { foreach (var adaptingMessage in AdaptingWaitingList) { SendMessage(this, this, adaptingMessage.SenderAgent, adaptingMessage.SenderAgentId, BroadcastType.SingleCast, MessagesContent.ReplyRulerNum, message.RulerPingReply); var replyListMessage = ReplyWaitingList.Find(tempMessage => tempMessage.SenderAgent == adaptingMessage.SenderAgent); ReplyWaitingList.Remove(replyListMessage); } AdaptingWaitingList.Clear(); } } } else if (message.MessageContent == MessagesContent.LostLeader) // reply worker to assign it to leader of team { var team = Container.TeamList.Find(t => t.ActiveLeader.AgentId == message.DataMessageText); var tempMessage = new Message() { SenderAgent = this, SenderAgentId = AgentId, CurrentSenderAgent = this, ReceiverAgent = message.SenderAgent, ReceiverAgentId = message.SenderAgent.AgentId, MessageType = BroadcastType.SingleCast, MessageContent = MessagesContent.AssignLeader, DataMessageText = "GetTeamData", Data = team }; SendMessage(tempMessage); } else if (message.MessageContent == MessagesContent.AssignLeader) { if (message.Data is Team team && message.SenderAgent is Leader leader) { foreach (var worker in team.AgentsArray) { worker.LeaderAgent = leader; } Time.OursAdaptingTime = Time.GlobalSimulationTime; } } }
public override void OnMessage(Message message) { if (Status == State.Failed) { return; } if (message.ReceiverAgentId == AgentId) { ProcessMessage(message); } else if (message.ReceiverAgentId == "-1") { message.CurrentReceiverAgentId = "-1"; message.CurrentSenderAgent = this; message.CurrentSenderAgentId = AgentId; message.RoutingList.Add(this); Container.ContainerMedia.SendMessage(message.Copy()); } else if (Position.Position.CalculateDistance(message.ReceiverAgent.Position.Position) <= RadioRange) { if (message.MessageContent == MessagesContent.Ping) { message.RoutingTime = Time.GlobalSimulationTime; message.CurrentReceiverAgentId = message.ReceiverAgentId; message.CurrentReceiverAgent = message.ReceiverAgent; message.CurrentSenderAgentId = AgentId; message.CurrentSenderAgent = this; message.RoutingList.Add(this); ReplyWaitingList.Add(message.Copy()); Container.ContainerMedia.SendMessage(message.Copy()); } else if (message.MessageContent == MessagesContent.PingReply) { message.CurrentReceiverAgentId = message.ReceiverAgentId; message.CurrentReceiverAgent = message.ReceiverAgent; message.CurrentSenderAgentId = AgentId; message.CurrentSenderAgent = this; message.RoutingList.Add(this); Container.ContainerMedia.SendMessage(message.Copy()); foreach (var pingMessage in ReplyWaitingList) { if (pingMessage.ReceiverAgent == message.SenderAgent) { ReplyWaitingList.Remove(pingMessage); break; } } } else { message.CurrentReceiverAgentId = message.ReceiverAgentId; message.CurrentReceiverAgent = message.ReceiverAgent; message.CurrentSenderAgentId = AgentId; message.CurrentSenderAgent = this; message.RoutingList.Add(this); Container.ContainerMedia.SendMessage(message.Copy()); } } else { if (message.MessageContent == MessagesContent.PingReply) { foreach (var pingMessage in ReplyWaitingList) { if (pingMessage.ReceiverAgent == message.SenderAgent) { ReplyWaitingList.Remove(pingMessage); break; } } } var mAgent = FindNearestMessenger(Position, message.ReceiverAgent.Position, message); if (mAgent == null) { RadioRange += 50; OnMessage(message); return; } message.CurrentReceiverAgentId = mAgent.AgentId; message.CurrentReceiverAgent = mAgent; message.CurrentSenderAgent = this; message.CurrentSenderAgentId = AgentId; message.RoutingList.Add(this); Container.ContainerMedia.SendMessage(message.Copy()); } }