/*********************************************************************/ /* */ /* 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.IO; namespace AgentService.Platform { /// /// Class PlatformConsole. Handles all the consoles used by the AgentService Platform: /// - Main Console /// - Error Console /// - Warning Console /// - Agents Console [Serializable] public class PlatformConsole : IPlatformConsole { /// /// if true indicates that the console is in use. /// private bool bUseConsoles; /// /// Output console. /// private TextWriter mainConsole; /// /// Gets, sets the output console. /// public TextWriter Main { get { return this.mainConsole; } set { if (value == null) throw new InvalidOperationException("Main Console cannot be null!"); if (this.bUseConsoles == false) { this.mainConsole = value; } else throw new InvalidOperationException("Cannot assign the console while is in use."); } } /// /// Warning console. /// private TextWriter warningConsole; /// /// Gets, sets the warning console. /// public TextWriter Warning { get { return this.mainConsole; } set { if (value == null) { throw new InvalidOperationException("Warning Console cannot be null!"); } if (this.bUseConsoles == false) { this.warningConsole = value; } else { throw new InvalidOperationException("Cannot assign the console while is in use."); } } } /// /// Platform error console. /// private TextWriter errorConsole; /// /// Gets, sets the error console. /// public TextWriter Error { get { return this.errorConsole; } set { if (value == null) { throw new InvalidOperationException("Error Console cannot be null!"); } if (this.bUseConsoles == false) { this.errorConsole = value; } else { throw new InvalidOperationException("Cannot assign the console while is in use."); } } } /// /// agents console /// private IAgentConsole blackBoard; /// /// Gets, Sets the agents console. /// public IAgentConsole BlackBoard { get { return this.blackBoard; } set { if (value == null) { throw new InvalidOperationException("Error Console cannot be null!"); } if (this.bUseConsoles == false) { this.blackBoard = value; } else { throw new InvalidOperationException("Cannot assign the console while is in use."); } } } /// /// Inpue console. /// private TextReader inputConsole; /// /// Gets, sets the input console. /// public TextReader Input { get { return this.inputConsole; } set { if (value == null) { throw new InvalidOperationException("Error Console cannot be null!"); } if (this.bUseConsoles == false) { this.inputConsole = value; } else { throw new InvalidOperationException("Cannot assign the console while is in use."); } } } /// /// Warning generation flag. /// private bool bSuppressWarnings; /// /// Gets, sets the warning generation flag. /// public bool SuppressWarnings { get { return this.bSuppressWarnings; } set { this.bSuppressWarnings = value; } } /// /// Constructs a PlatformConsole object with default consoles (System.Console.Out, /// System.Console.Error) /// public PlatformConsole() : this(System.Console.Out, System.Console.Error, System.Console.Out, new DefaultPlatformConsole(), System.Console.In, false) { } /// /// Constructs a PlatformConsole object by specifying the main console, the error console, and uses the main console for the platform console. /// /// TextWriter object used for the main console, if null throws an exception /// TextWriter object used for the error console, if null throws an exception /// TextReader object used for the input console public PlatformConsole(System.IO.TextWriter mainConsole, System.IO.TextWriter errorConsole, TextReader inputConsole) : this(mainConsole, errorConsole, mainConsole, new DefaultPlatformConsole(mainConsole), inputConsole, false) { } /// /// Constructs a PlatformConsole object by specifying the main console, the error console, and the platform console. /// /// TextWriter object used for the main console, if null throws an exception. /// TextWriter object used for the error console, if null throws an exception. /// TextWriter object used for the platform console, if null throws an exception /// TextReader object used for the input console public PlatformConsole(System.IO.TextWriter mainConsole, System.IO.TextWriter errorConsole, System.IO.TextWriter platformConsole, TextReader inputConsole) : this(mainConsole, errorConsole, mainConsole, new DefaultPlatformConsole(platformConsole), inputConsole, false) { } /// /// Constructs a PlatformConsole object by specifying the main console and the error console. Uses the /// System.Console.Out for the platform console /// /// TextWriter object used for the main console, if null throws an exception. /// TextWriter object used for the error console, if null throws an exception. /// IPlatformConsole object used for the platform console, if null throws an exception /// TextReader object used for the input console public PlatformConsole(System.IO.TextWriter mainConsole, System.IO.TextWriter errorConsole, IAgentConsole platformConsole, TextReader inputConsole) : this(mainConsole, errorConsole, mainConsole, platformConsole, inputConsole, false) { } /// /// Constructs a PlatformConsole object by specifying the main console, the error console, and the platform console. /// /// TextWriter object used for the main console, if null throws an exception. /// TextWriter object used for the error console, if null throws an exception. /// TextWriter object used for the warning console, if null throws an exception /// TextWriter object used for the platform console, if null throws an exception /// TextReader object used for the input console public PlatformConsole(System.IO.TextWriter mainConsole, System.IO.TextWriter errorConsole, System.IO.TextWriter warningConsole, System.IO.TextWriter platformConsole, TextReader inputConsole) : this(mainConsole, errorConsole, warningConsole, new DefaultPlatformConsole(platformConsole), inputConsole, false) { } /// /// Constructs a PlatformConsole object by specifying the main console and the error console. Uses the /// System.Console.Out for the platform console /// /// TextWriter object used for the main console, if null throws an exception. /// TextWriter object used for the error console, if null throws an exception. /// TextWriter object used for the warning console, if null throws an exception. /// IPlatformConsole object used for the platform console, if null throws an exception /// TextReader object used for the input console public PlatformConsole(System.IO.TextWriter mainConsole, System.IO.TextWriter errorConsole, System.IO.TextWriter warningConsole, IAgentConsole platformConsole, TextReader inputConsole) : this(mainConsole, errorConsole, warningConsole, platformConsole, inputConsole, false) { } /// /// Constructs a PlatformConsole object by specifying the main console and the error console. Uses the /// System.Console.Out for the platform console /// /// TextWriter object used for the main console, if null throws an exception. /// TextWriter object used for the error console, if null throws an exception. /// TextWriter object used for the warning console, if null throws an exception. /// IPlatformConsole object used for the platform console, if null throws an exception /// TextReader object used as input console /// warning suppression flag public PlatformConsole(System.IO.TextWriter mainConsole, System.IO.TextWriter errorConsole, System.IO.TextWriter warningConsole, IAgentConsole platformConsole, TextReader inputConsole, bool bSuppressWarnings) { this.bUseConsoles = false; this.bSuppressWarnings = bSuppressWarnings; if (mainConsole == null) { throw new ArgumentNullException("mainConsole", "mainConsole cannot be null, use default constructor or specify System.Console.Out"); } this.mainConsole = mainConsole; if (warningConsole == null) { throw new ArgumentNullException("warningConsole", "warningConsole cannot be null, use default constructor or specify System.Console.Out"); } this.warningConsole = this.mainConsole; if (errorConsole == null) { throw new ArgumentNullException("errorConsole", "errorConsole cannot be null, use default constructor or specify System.Console.Error"); } this.errorConsole = errorConsole; if (platformConsole == null) { throw new ArgumentNullException("platformConsole", "platformConsole cannot be null, use default constructor"); } this.blackBoard = platformConsole; if (inputConsole == null) { throw new ArgumentNullException("inputConsole", "inputConsole cannot be null, use default constructor or specify System.Console.In"); } this.inputConsole = inputConsole; } /// /// Writes a line to the platform's output console and formats it properly. /// /// message content public void WriteLineMessage(string message) { this.mainConsole.WriteLine("[AgentService] {0} {1}", System.DateTime.Now, message); } /// /// Writes a message to the platform's output console and formats it properly. /// /// message content public void WriteMessage(string message) { this.mainConsole.Write("[AgentService] {0} {1}", System.DateTime.Now, message); } /// /// Appends a message to the current line of the platform's output console and /// moves to new line. /// /// message content public void AppendMessage(string message) { this.mainConsole.WriteLine(message); } /// /// Writes an error to the ErrorConsole /// /// error description public void WriteError(string message) { this.errorConsole.WriteLine("[ERROR] {0} {1}", System.DateTime.Now, message); } /// /// Writes an error to the Error console. /// /// error description /// arguments public void WriteError(string message, params object[] args) { message = string.Format(message, args); this.errorConsole.WriteLine("[ERROR] {0} {1}", System.DateTime.Now, message); } /// /// Writes a warning message to the Warning console. /// /// warning message public void WriteWarning(string message) { if (this.bSuppressWarnings == false) { this.warningConsole.WriteLine("[WARNING] {0} {1}", System.DateTime.Now, message); } } /// /// Writes a warning message to the Warning console. /// /// warning message /// arguments public void WriteWarning(string message, params object[] args) { if (this.bSuppressWarnings == false) { message = string.Format(message, args); this.warningConsole.WriteLine("[WARNING] {0} {1}", System.DateTime.Now, message); } } } /// /// Class DefaultPlatformConsole. Defualt implementation of the IPlatformConsole //// Interface. Uses a System.IO.TextWriter as console for the platform /// [Serializable] public class DefaultPlatformConsole : IAgentConsole { /// /// platform console /// private System.IO.TextWriter console; /// /// Constructs a DefaultPlatformConsole object by using /// System.Console.Out as a backing console /// public DefaultPlatformConsole() { this.console = System.Console.Out; } /// /// Constructs a DefaultPlatformConsole object by using /// a System.IO.TextWriter as a backing console /// /// console for the platform, cannot be null public DefaultPlatformConsole(System.IO.TextWriter console) { if (console == null) throw new ArgumentNullException("console", "System.IO.TextWriter console, cannot be null, use default constructor instead"); this.console = console; } /// /// Appends a formatted message to the console /// /// string identifier of the entity who appends the message /// formatted message /// arguments used in the formatted message public void AppendMessage(string source, string message, params object[] objects) { lock(this.console) { string msg = string.Format("[{0}] {1}",source, message); this.console.WriteLine(msg, objects); this.console.Flush(); } } /// /// Appends a message to the console /// /// string identifier of the entity who appends the message /// message appended to the console public void AppendMessage(string source, string message) { lock (this.console) { this.console.WriteLine("[{0}] {1}", source, message); } } } }