/*********************************************************************/ /* */ /* AgentService Framework */ /* www.agentservice.it */ /* */ /* Copyright (C) 2007 l.i.d.o. - DIST, University of Genoa, Italy */ /* GNU Lesser General Public License */ /* */ /* This library is free software; you can redistribute it and/or */ /* modify it under the terms of the GNU Lesser General Public */ /* License as published by the Free Software Foundation, */ /* version 2.1 of the License. */ /* */ /* This library is distributed in the hope that it will be useful, */ /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */ /* Lesser General Public License for more details. */ /* */ /* You should have received a copy of the GNU Lesser General Public */ /* License along with this library; if not, write to the */ /* Free Software Foundation, Inc., 59 Temple Place - Suite 330, */ /* Boston, MA 02111-1307, USA. */ /* */ /*********************************************************************/ using System; using AgentService.Agent.Knowledge; using AgentService.Agent.Behaviour; using AgentService.Platform.AMS; namespace AgentService.Platform.MTS { /// /// Interface IMTSGateway. Defines the common operation that are /// directly accessible from the messaging clients on each agent. /// public interface IMTSGateway { /// /// Checks whether a queue has messages. /// /// agent identifier of the owner of the queue /// true if the queue has messages bool HasMessages(AID aid); /// /// Sends a message to an agent. /// /// message /// true if the message has been successfully sent bool SendMessage(AgentMessage message); /* /// /// Receives a message pops it from the queue. /// /// agent identifier of the agent that owns the queue /// the first message available in the queue AgentMessage ReceiveMessage(AID aid); /// /// Looks in the queue for a message. /// /// agent identifier of the agent that owns the queue /// the first message available in the queue AgentMessage PeekMessage(AID aid); */ /// /// Retrieves the selected messages from the agent's message queue /// /// agent identifier of the agent that owns the queue /// message filter, set this parameter to null if you do not want to apply a filter /// maximum number of messages to retrieve (0 or negative number for all messages) /// if true removes the messages from the queue /// the collection of messages retrieved from the queue AgentMessageCollection GetMessages(AID aid, MessageFilter filter, int max, bool bRemove); /// /// Retrieves the first message satisfying the given conditions from the agent's queue. /// /// agent identifier of the agent that owns the queue /// message filter, set this parameter to null if you do not want to apply a filter /// if true removes the messages from the queue /// the collection of messages retrieved from the queue AgentMessage GetMessage(AID aid, MessageFilter filter, bool bRemove); /// /// Removes the given message from the queue. /// /// agent identifier of the agent that owns the queue /// message to be destroyed /// true if message is successfully removed bool RemoveMessage(AID aid, AgentMessage message); } /// /// Class MTS. Specific MTS Agent class. /// public class MTS : AgentService.Agent.Agent, IMTSGateway { /// /// Reference to the owning platform. /// private AgentService.Platform.IPlatform platform; /// /// Constructs an MTS Agent for the given platform. /// /// Reference to the owning platform public MTS(AgentService.Platform.IPlatform platform) { this.platform = platform; this.aid = new AID(this.platform.mtsName, this.platform.Description.Name); } /// /// Registers an agent with the Messaging system /// /// agent identifier /// false, if the agents is already registered or the queue cannot be created public bool Register(AID aid) { return this.platform.Messaging.CreateAgentQueue(aid); } /// /// Deregister an agent with the Messaging system /// /// agent identifier /// false, if the agent queue is not found or cannot be removed public bool Deregister(AID aid) { return this.platform.Messaging.RemoveAgentQueue(aid); } /// /// Checks whether a queue has messages. /// /// agent identifier of the owner of the queue /// true if the queue has messages public bool HasMessages(AID aid) { bool bReturn = false; lock(this) { bReturn = this.platform.Messaging.HasMessages(aid); } return bReturn; } /// /// Sends a message to an agent. /// /// message /// true if the message has been successfully sent public bool SendMessage(AgentMessage message) { bool bReturn = false; lock(this) { bReturn = this.platform.Messaging.SendMessage(message); if (bReturn == false) { AgentService.Platform.Modules.IModuleError error = this.platform.Messaging.GetLastError(); } } return bReturn; } /* /// /// Receives a message pops it from the queue. /// /// agent identifier of the agent that owns the queue /// the first message available in the queue public AgentMessage ReceiveMessage(AID aid) { // TODO: Check the new code version AgentMessage message = null; lock (this) { message = this.platform.Messaging.ReceiveMessage(aid); } //return null; return message; } /// /// Looks in the queue for a message. /// /// agent identifier of the agent that owns the queue /// the first message available in the queue public AgentMessage PeekMessage(AID aid) { AgentMessage message = null; lock(this) { // MOD 2007-09-05 - Christian Vecchiola // // Changed the implementation of PeekMessage to the new // API GetMessage which provides a more consistent way // to retrieve messages. // // OLD CODE: // // message = this.platform.Messaging.GetMessage(aid,null,false); message = this.platform.Messaging.GetMessage(aid,null,false); // END MOD } return message; } */ /// /// Retrieves all the messages and removes them from the queue. /// /// agent identifier of the agent that owns the queue /// message filter, set this parameter to null if you do not want to apply any filter /// maximum number of messages to retrieve (0 or negative number for unlimited messages) /// if true removes the messages from the queue /// the collection of messages retrieved from the queue public AgentMessageCollection GetMessages(AID aid, MessageFilter filter, int max, bool bRemove) { AgentMessageCollection messages = null; lock(this) { messages = this.platform.Messaging.GetMessages(aid, filter, max, bRemove); return messages; } } /// /// Retrieves the first message satisfying the given conditions from the agent's queue. /// /// agent identifier of the agent that owns the queue /// message filter, set this parameter to null if you do not want to apply a filter /// if true removes the messages from the queue /// the collection of messages retrieved from the queue public AgentMessage GetMessage(AID aid, MessageFilter filter, bool bRemove) { AgentMessage message = null; lock (this) { message = this.platform.Messaging.GetMessage(aid, filter, bRemove); return message; } } /// /// Removes the given message from the queue. /// /// agent identifier of the agent that owns the queue /// message to be destroyed /// true if message is successfully removed public bool RemoveMessage(AID aid,AgentMessage message) { lock(this) { return this.platform.Messaging.RemoveMessage(aid, message); } } #region CallBacks /// /// Handler of messages directed to the MTS /// /// public void DispatchMessage(AgentMessage msg) { // ... } #endregion } }