/*********************************************************************/ /* */ /* 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 System.Threading; using System.Collections; using AgentService.Agent; using AgentService.Platform.AMS; using AgentService.Platform.MTS; using AgentService.Platform.Modules; namespace AgentService.Agent.Runtime { /// /// Class MsgBatchService. Default implementation of the IMsgService interface. /// Messaging module client that can be used by non agent based software components. /// Actually the client does not support converasation management, and rejects all the /// conversation requests. /// public class MsgBatchService : IMsgService { /// /// Agent identifier of the owning agent /// private AID aid; /// /// Reference to the mts agent /// private AgentService.Platform.MTS.MTS mts; /// /// Raises when a proposal for a new conversation is received. /// public event ConversationDelegate OnIncomingConversation; /// /// Constructs a DefaultMsgClient object for the given agent. /// // TODO: think about that there was the aid not the agent object /// agent identifier of the owning agent /// reference to the mts agent public MsgBatchService(AID clientAid,AgentService.Platform.MTS.MTS mts) { this.aid = clientAid; this.mts = mts; this.mts.Register(this.aid); } #region IMsgClient Members /// /// Gets the AgentDelegate that will be invoked when the agent is stopped. /// /// public AgentDelegate GetStopAgentDelegate() { throw new System.InvalidOperationException("The message client does not have an owning agent"); } /// /// Open a bidirectional conversationa with an agents. /// /// agent identifier of the agent to talk to /// identifier of the new conversation public IConversation OpenConversation(AID to) { throw new NotSupportedException("Could not handle conversations"); } public IOntologicalConversation OpenConversation(AID to, Ontology.Ontology onto) { throw new NotSupportedException("Could not handle conversations"); } /// /// Checks whether the agent's queue has messages. /// /// true if the queue has messages public bool HasMessages() { // MOD 2007-09-05 - Christian Vecchiola // API Change // OLD CODE: // AgentMessageCollection amc = this.mts.GetAllMessage(this.aid); // for (int msgndx = 0; msgndx < amc.Count; msgndx++) // { // AgentMessage msg = amc[msgndx]; // if (this.FilterConversation(msg) == true) // { // amc.Remove(msg); // msgndx--; // } // } // return amc.Count > 0; // NEW CODE: AgentMessage msg = this.mts.GetMessage(this.aid, NoConvMessageFilter.Instance, false); return msg != null; } /// /// Filters all messages that are connected with /// conversations and the conversation protocol. /// If it finds some conversation requests automatically /// rejects them. /// /// agent message /// true if the message is related to a conversation /// /// Sends a message to an agent. /// /// message /// true if the message has been successfully sent public bool SendMessage(AgentService.Platform.MTS.AgentMessage message) { message.Envelope.Date = System.DateTime.Now; message.Envelope.From = this.aid; return this.mts.SendMessage(message); } /// /// Receives a message and pops it from the queue. /// /// the first message available in the queue public AgentService.Platform.MTS.AgentMessage ReceiveMessage() { Platform.MTS.AgentMessage message = this.mts.GetMessage(this.aid,NoConvMessageFilter.Instance,true); return message; } /// /// Receives all the messages that satisfy the condition of the given filter. /// /// Specifies the selection criteria /// If true the call is blocking /// A collection of filtered messages retrieved from the agent's queue public AgentService.Platform.MTS.AgentMessageCollection ReceiveMessage(AgentService.Platform.MTS.MessageFilter filter, bool blocking) { lock (this) { AgentMessageCollection copy = new AgentMessageCollection(); START: AgentService.Platform.MTS.AgentMessageCollection amc = this.mts.GetMessages(this.aid, filter, 0, true); if ((amc.Count == 0) && (blocking == true)) { System.Threading.Thread.Sleep(100); goto START; } return amc; } } /// /// Waits until a new message arrives in the agent's queue. /// /// The new message arrived public AgentService.Platform.MTS.AgentMessage WaitForMessage() { try { while(true) { lock (this) { AgentMessage msg = this.mts.GetMessage(this.aid, NoConvMessageFilter.Instance, false); if (msg != null) { return msg; } System.Threading.Thread.Sleep(1000); } } } catch(System.Threading.ThreadAbortException) { // the thread has waken up because is going to be // terminated return null; } } /// /// Looks in the agent's queue for a message. /// /// the first message available in the queue public AgentService.Platform.MTS.AgentMessage PeekMessage() { lock (this) { AgentMessage msg = this.mts.GetMessage(this.aid, NoConvMessageFilter.Instance, false); return msg; } } /// /// Retrieves all the standard messages from the queue. /// /// if true messages are removed from the queue /// the collection of messages retrieved from the queue public AgentService.Platform.MTS.AgentMessageCollection GetAllMessages(bool bRemove) { lock(this) { AgentService.Platform.MTS.AgentMessageCollection amc = this.mts.GetMessages(this.aid, NoConvMessageFilter.Instance, 0, bRemove); return amc; } } /// /// Removes the given message from the queue. /// /// message to be destroyed /// true if message is successfully removed public bool RemoveMessage(AgentService.Platform.MTS.AgentMessage message) { return this.mts.RemoveMessage(this.aid, message); } #endregion } }