/// <summary> /// Messages that are considered the same. /// TODO: we didn't use this. /// </summary> /// <param name="t"> /// The other telegram. /// </param> /// <returns> /// True if this telegram is considered the same as the specified one. /// </returns> public bool IsSameAs(Telegram t) { return Math.Abs(DispatchTime - t.DispatchTime) < SMALLEST_DELAY && Sender == t.Sender && Receiver == t.Receiver && Msg == t.Msg; }
/// <summary> /// Given a message, a receiver, a sender and any time delay, this method routes the message /// to the correct agent (if no delay) or stores in the message queue to be dispatched at /// the correct time. /// </summary> /// <param name="delay"> /// The message delay. /// </param> /// <param name="senderId"> /// The sender ID. /// </param> /// <param name="receiverId"> /// The receiver ID. /// </param> /// <param name="msg"> /// The message. /// </param> /// <param name="extraInfo"> /// Extra message data. /// </param> public void DispatchMsg( float delay, int senderId, int receiverId, MessageTypes msg, object extraInfo) { // get a pointer to the receiver Entity receiver = EntityManager.Find<Entity>(receiverId); // make sure the receiver is valid if (receiver == null) { return; } // create the telegram var telegram = new Telegram(SEND_MSG_IMMEDIATELY, senderId, receiverId, msg, extraInfo); // if there is no delay, route telegram immediately if (delay <= SEND_MSG_IMMEDIATELY) { // send the telegram to the recipient Discharge(receiver, telegram); } // else calculate the time when the telegram should be dispatched else { float currentTime = Time.time; telegram.DispatchTime = currentTime + delay; // and put it in the queue MessageQueue.Enqueue(telegram, telegram.DispatchTime); } }
/// <summary> /// Message ordering. /// TODO: we didn't use this. /// </summary> /// <param name="t"> /// The other telegram. /// </param> /// <returns> /// True if this telegram is earlier than the specified one. /// </returns> public bool IsEarlierThan(Telegram t) { if (this == t) { return false; } return DispatchTime < t.DispatchTime; }
/// <summary> /// This method is utilized by <see cref="DispatchMsg"/> or /// <see cref="DispatchDelayedMessages"/>. This method calls the message handling member of /// the receiving entity, receiver, with the newly created telegram. /// </summary> /// <param name="receiver"> /// The intended receiver of the telegram. /// </param> /// <param name="telegram"> /// The telegram. /// </param> public void Discharge(Entity receiver, Telegram telegram) { if (!receiver.HandleMessage(telegram)) { // telegram could not be handled } }