using System; using System.IO; using System.Text.RegularExpressions; using AgentService.Agent; using AgentService.Agent.Runtime; using AgentService.Agent.Behaviour; using AgentService.Agent.Knowledge; using AgentService.Platform.AMS; using AgentService.Platform.MTS; using AgentService.Platform.DF; namespace TestMessaging { /// /// Template ConvTester. Defines an agent which tests /// the conversation feature of the messagin system. /// [AgentKnowledges(typeof(ConvData), typeof(MessageData))] [AgentBehaviours(typeof(HandleConv), typeof(StartConv), typeof(SendMessage), typeof(ReceiveMessage))] public class MessagingTester : AgentTemplate { #region Override /// /// Reads the configuration file and sets up the /// dictionary of configuration parameters. /// /// path to the configuration file /// true if successful public override bool Configure(string configFile) { this.settings = new System.Collections.Generic.Dictionary(); if (File.Exists(configFile) == true) { using(StreamReader reader = new StreamReader(configFile)) { Regex numberPattern = new Regex(@"^[\+|-]?\b\d+(\.|,)?\d+\b$"); do { string line = reader.ReadLine(); if (line != null) { line = line.Trim(); // first we skip comments. Comment lines are // identified by the # character. If we have // to write multi-line comments we have to // put at the beginning of each line #. // // we also skip empty lines if ((line.StartsWith("#") == false) && (line != string.Empty)) { // ok if this is not a comment then // the form should be: // // PropertyName PropertyValue int divide = line.IndexOf(' '); if (divide < 0) { return false; } string pName = line.Substring(0, divide); // property values can be either strings // or numbers. Strings are not allowed to // contain spaces. If you need to put spaces // inside enclose the whole string within "". string pValue = line.Substring(divide + 1); pValue = pValue.Trim(); if ((pValue.StartsWith("\"") == true) && (pValue.EndsWith("\"") == true)) { pValue = pValue.Substring(1, pValue.Length - 2); this.settings[pName] = pValue; } else { string lpValue = pValue.ToLower(); // maybe a boolean value? if (lpValue == "true") { this.settings[pName] = true; } else if (lpValue == "false") { this.settings[pName] = false; } // or a number? else if (numberPattern.Match(pValue).Success == true) { int iValue = 0; float fValue = 0.0f; double dValue = 0.0d; decimal mValue = 0; // ok the property is a number // // starting with int if (int.TryParse(pValue, out iValue) == true) { this.settings[pName] = iValue; } // float? else if (float.TryParse(pValue, out fValue) == true) { this.settings[pName] = fValue; } // double? else if (double.TryParse(pValue, out dValue) == true) { this.settings[pName] = dValue; } // decimal? else if (decimal.TryParse(pValue, out mValue) == true) { this.settings[pName] = mValue; } else { // unrecognized format // saving it as a string. this.settings[pName] = pValue; } } else { // ok the property value is a string this.settings[pName] = pValue; } } } } } while (reader.EndOfStream == false); } return true; } return false; } /// /// Initializes the messaging tester agent instance /// /// factory object that allows the instantiation of knwoledge and behaviour public override void Initiate(IFactoryContext factoryContext) { string test = this.settings["TestType"] as string; if (test == "Conv Test") { this.SetupConvTest(factoryContext); } else if (test == "Ping Pong Test") { this.SetupPingPongTest(factoryContext); } } /// /// Handles the termination of the agent. /// public override void Terminate() { } /// /// Receives a conversation instance object and according to /// the specific test performed and the specific role played /// by the agent accepts or refuse the conversation. /// /// /// conversation arguments public override void HandleConversation(IAgentContext sender, ConversationArgs args) { string test = this.settings["TestType"] as string; if ((test != null) && (test == "Conv Test")) { string role = this.settings["Role"] as string; if (role == "Server") { sender.AddConversation("Handle", args.Conversation); } else { args.Conversation.Reject(); args.Conversation.Close(); } } else { args.Conversation.Reject(); args.Conversation.Close(); } } #endregion #region Helpers /// /// Reads the configuration settings and creates /// the agent instance accordingly to perform the /// "Conv Test" test. /// /// factory object that allows the instantiation of knwoledge and behaviour private void SetupConvTest(IFactoryContext factoryContext) { string role = this.settings["Role"] as string; int repetitions = (int)this.settings["Repetitions"]; int maxMessages = (int)this.settings["MaxMessages"]; if (role == "Server") { bool bRefuse = (bool) this.settings["Refuse"]; // we do not need peerName here factoryContext.CreateKnowledge(typeof(ConvData), "ConvData", PersistenceMode.Persistent, string.Empty, repetitions, maxMessages, false); factoryContext.CreateBehaviour(typeof(HandleConv), "Handle", "ConvData"); } else { // we do not need the Refuse property here string peerName = this.settings["PeerName"] as string; factoryContext.CreateKnowledge(typeof(ConvData), "ConvData", PersistenceMode.Persistent, peerName, repetitions, maxMessages, false); factoryContext.CreateBehaviour(typeof(StartConv), "Start", "ConvData"); } } /// /// Reads the configuration settings and creates /// the agent instance accordingly to perform the /// "Ping Pong Test" test. /// /// factory object that allows the instantiation of knwoledge and behaviour private void SetupPingPongTest(IFactoryContext factoryContext) { string role = this.settings["Role"] as string; int maxMessages = (int) this.settings["MaxMessages"]; if (role == "Ping") { string peerName = this.settings["PeerName"] as string; factoryContext.CreateKnowledge(typeof(MessageData), "MessageData", PersistenceMode.Persistent, maxMessages, false, peerName); factoryContext.CreateBehaviour(typeof(SendMessage), "SendMessage", "MessageData"); } else { bool bReplyAdd = (bool)this.settings["ReplyAdd"]; factoryContext.CreateKnowledge(typeof(MessageData), "MessageData", PersistenceMode.Persistent, maxMessages, bReplyAdd, string.Empty); factoryContext.CreateBehaviour(typeof(ReceiveMessage), "ReceiveMessage", "MessageData"); } } #endregion } }