/*********************************************************************/ /* */ /* 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.Collections; using System.Xml.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.IO; // this namespace contains classes for the interaction with the platform. using AgentService.Platform; //this namespace contains classes for the configuration management. using AgentService.Platform.Configuration; // this namespace contains classes for the interaction with the modules of the plaform. using AgentService.Platform.Modules; namespace AuctionDemo { /// /// It wraps fundamental data for the configuration and setup of an agent. /// It ease the serialization and deserialization in XML. /// [XmlRoot(ElementName="agent")] [Serializable] public class AgentConfiguration { /// /// True is the agent is the auctioneer. /// [XmlAttribute(AttributeName="is-auctioneer")] public bool IsAuctioneer; /// /// Agent name. /// [XmlAttribute(AttributeName="name")] public string Name; /// /// The path of the configuration file /// [XmlAttribute(AttributeName="conf-file")] public string ConfFile; /// /// Deserialization constructor. /// public AgentConfiguration() {} } /// /// It wraps the configuration data of the batch. /// It eases the serializazion and deserialization in XML. /// [XmlRoot("configuration")] [Serializable] public class BatchConfiguration { /// /// Array of the agent which the batch must activate. /// [XmlArrayAttribute(ElementName="agents")] [XmlArrayItemAttribute(ElementName="agent")] public AgentConfiguration[] Agents; /// /// The path of the auction demo dll. /// [XmlElement(ElementName="dll-path")] public string DLLPath; /// /// Deserialization constructor. /// public BatchConfiguration() { } } /// /// Example driver for the implementation of an auction among agents. /// It must implements the IPlatformBatch interface. /// public class AuctionBatch: AgentService.Platform.IPlatformBatch { /// /// Entry points for the batch /// /// the platform reference public void Run(AgentService.Platform.PlatformController controller) { throw new InvalidOperationException("Configuration File needed!"); } /// /// Allows the execution of the batch from a configuration file. /// /// the platform reference /// configuration file for the batch public void Run(AgentService.Platform.PlatformController controller, string configFile) { BatchConfiguration conf = this.ReadConfigFile(configFile); if (conf != null) { controller.Console.Main.WriteLine("[Batch] - Press [Return] to start..."); controller.Console.Input.ReadLine(); controller.StartPlatform(); if (controller.PlatformState == PlatformState.Ready) { IModuleError error = controller.Modules.UpLoadAssembly(conf.DLLPath, true); if (error == null) { controller.RunPlatform(); if (controller.PlatformState == PlatformState.Running) { foreach (AgentConfiguration agent in conf.Agents) { if (agent.IsAuctioneer == true) { controller.Services.CreateAgent("AuctionDemo.Auctioneer", agent.Name, agent.ConfFile, false, "Auctioneer"); // controller.Services.CreateAgent("AuctionDemo.Auctioneer", agent.Name, agent.ConfFile, false); } else { controller.Services.CreateAgent("AuctionDemo.Bidder", agent.Name, agent.ConfFile, false, "Bidders"); // controller.Services.CreateAgent("AuctionDemo.Bidder", agent.Name, agent.ConfFile, false); } } controller.Services.ActivateAllAgents(); controller.Services.WaitForAllAgents(); } else { controller.Console.Error.WriteLine("Some error occurred during platform activation."); } } else { controller.Console.Error.WriteLine("Some error occurred when uploading the assembly: {0} - Details: {1}", error.Description, (error.Exception != null ? error.Exception.Message : "no details")); } } // System.Threading.Thread.Sleep(2000); controller.Console.Main.WriteLine("[Batch] - Press [Return] to shut down.."); controller.Console.Input.ReadLine(); controller.ShutdownPlatform(); } } /// /// Reads the batch configuration, from the file /// passed as a parameter. /// /// configuration file path /// batch configuration private BatchConfiguration ReadConfigFile(string configFile) { XmlSerializer serializer = new XmlSerializer(typeof(BatchConfiguration)); System.IO.TextReader reader = null; BatchConfiguration batch = null; try { reader = new System.IO.StreamReader(configFile); batch = serializer.Deserialize(reader) as BatchConfiguration; } catch(Exception ioex) { Console.WriteLine("Error while reading {0}: {1}",configFile, ioex.Message); } finally { if (reader != null) { reader.Close(); } } return batch; } } }