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; } } }