/*********************************************************************/ /* */ /* 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.Runtime.Serialization; using AgentService.Platform.AMS; namespace AgentService.Platform.MTS { /// /// Enum MessageType. Defines the message types used by the AgentMessage class. /// ServiceMessage (-1) : Service messages used to set up and close conversations. /// SimpleMessage (0) : Simple messages that do not need a conversation. /// Greater than SimpleMessage (>0) : the message is bound to a conversation. /// [DataContract] public enum MessageType { /// /// Service messages, not intended for the users (ie: conversation set up, closing etc..) /// [EnumMember] ServiceMessage = -1,//string "ServiceMessage" /// /// Simple messages that do not need a conversation /// [EnumMember] SimpleMessage = 0 //string "SimpleMessage" } /// /// Class AgentMessage. Defines the structure of a message exchanges among agents. /// [Serializable] [DataContract] public class AgentMessage/*: ISerializable*/ { /// /// The message envelope description /// protected MessageEnvelope envelope; /// /// Gets the envelope of the message /// [DataMember] public MessageEnvelope Envelope { get { return this.envelope; } set { envelope = value; } } /// /// The body part of the message /// protected MessageBody body; /// /// Gets the body part of the message /// [DataMember] public MessageBody Body { get { return this.body; } set { if(value!=null) { this.body = value; } //TODO: throw InvalidOperatorException? // message: Cannot set body to null! } } /// /// The identifier of the conversation message belonging to /// internal string conversationId; /// /// Gets the number identifying the conversation message belongs to /// [DataMember] public string ConversationId { get { return this.conversationId; } set { this.conversationId = value; } } /// /// Constructs an AgentMessage object. /// public AgentMessage() { this.body = new MessageBody(); this.envelope = new MessageEnvelope(); this.conversationId = "SimpleMessage"; } /// /// Constructs an AgentMessage object with the given receivers,acl, date time, body part, and conversation number. /// /// The name of the agent who actually sent the message /// The name of the syntax representation of the message body /// The body part of the message /// The names of the primary recipients of the message public AgentMessage(string acl,MessageBody body,params AID[] receivers) { if(receivers.Length == 0) { //todo: thinks about exception } this.envelope = new MessageEnvelope(acl,receivers); this.body = body; this.conversationId = "SimpleMessage"; } /// /// Constructs an AgentMessage object with the given envelope, body part, and conversation number /// /// envelop of the message /// body part of the message /// conversation identifier public AgentMessage(MessageEnvelope envelope, MessageBody body, string conversationId) { this.envelope = envelope; this.body = body; this.conversationId = conversationId; } /// /// Creates a reply message to the sender /// /// if true copies the body into the reply message, /// false body's reply message empty /// reply message public AgentMessage Reply(bool copyBody) { MessageEnvelope envelope = new MessageEnvelope(this.envelope.AclRepresentation,this.envelope.From); AgentMessage msg = new AgentMessage(envelope,new MessageBody(),this.conversationId); if(copyBody == true) { foreach(MessageBodyItem mbi in this.body) { msg.body.Add(mbi.Identifier,mbi.Instance); } } return msg; } /// /// Creates a reply message to the sender and all the receivers /// /// if true copies the body into the reply message, /// false body's reply message empty /// reply message public AgentMessage Reply(bool copyBody, AID aid) { int count = this.envelope.To.Count; AID[] receivers = new AID[count]; receivers[0] = this.Envelope.From; int ndx = 1; for (int i = 0; i < count; i++) { AID rAid = this.Envelope.To[i]; if (rAid.Name.Equals(aid.Name) == false) { receivers[ndx] = rAid; ndx++; } } MessageEnvelope envelope = new MessageEnvelope(this.envelope.AclRepresentation, receivers); AgentMessage msg = new AgentMessage(envelope, new MessageBody(), this.conversationId); if (copyBody == true) { foreach (MessageBodyItem mbi in this.body) { msg.body.Add(mbi.Identifier, mbi.Instance); } } return msg; } /// /// Creates a reply message to the sender and sets the given message /// body for the response. /// /// message response body /// reply message public AgentMessage Reply(MessageBody body) { MessageEnvelope envelope = new MessageEnvelope(this.envelope.AclRepresentation, this.envelope.From); AgentMessage msg = new AgentMessage(envelope, body, this.conversationId); return msg; } /// /// Creates a reply message to the sender and all the receivers. /// /// message response body /// sender aid /// reply message public AgentMessage Reply(AID aid, MessageBody body) { int count = this.envelope.To.Count; AID[] receivers = new AID[count]; receivers[0] = this.Envelope.From; int ndx = 1; for (int i = 0; i < count; i++) { AID rAid = this.Envelope.To[i]; if (rAid.Name.Equals(aid.Name) == false) { receivers[ndx] = rAid; ndx++; } } MessageEnvelope envelope = new MessageEnvelope(this.envelope.AclRepresentation, receivers); AgentMessage msg = new AgentMessage(envelope, body, this.conversationId); return msg; } /// /// Deserialization Constructor. /// /// SerializeInfo /// The context in which deserializing public AgentMessage(SerializationInfo info, StreamingContext context) { this.envelope = (MessageEnvelope)info.GetValue("envelope", typeof(MessageEnvelope)); this.body = (MessageBody)info.GetValue("body", typeof(MessageBody)); this.conversationId = (string)info.GetValue("conversationId",typeof(string)); } /// /// Serialization function. /// /// SerializeInfo /// The context in which deserializing public virtual void GetObjectData(SerializationInfo info, StreamingContext context) { Console.WriteLine("GETOBJECTDATA CHIAMATA"); info.AddValue("envelope", envelope); info.AddValue("body", body); info.AddValue("conversationId",conversationId); } /// /// Checks if the message is coming from /// the specified agent. /// /// supposed sender /// true if the message comes from the specified sender public bool IsFrom(AID sender) { return this.envelope.From.Equals(sender); } /// /// Checks if the message's envelope Contains /// the given comment. /// /// comment /// true, if the comments section of the /// envelop Contains the given comment public bool HasComment(string comment) { return this.envelope.Comments.Contains(comment); } /// /// Checks for object comparison. /// /// other object /// true if the two messages have the same envelope. /// Comparison if performed by looking at the envelope hashcodes. /// public override bool Equals(object obj) { AgentMessage msg = obj as AgentMessage; //MOD: 06/04/2006: Marco Garr changed from date.GetHashcode() to date== // because with AppDomain we need to check the value // and no more the reference. Added the conversationId // for a strong check. return (((this.envelope.date) == (msg.envelope.date)) && (this.conversationId==msg.conversationId)); } /// /// Gets the hash code for this AgentMessage instance. /// /// message date hash code public override int GetHashCode() { return this.envelope.date.GetHashCode(); } } }