//****************************************************************************************************** // ClientRequest.cs - Gbtc // // Copyright © 2012, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may // not use this file except in compliance with the License. You may obtain a copy of the License at: // // http://www.opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 08/29/2006 - Pinal C. Patel // Original version of source code generated. // 04/27/2007 - Pinal C. Patel // Added Attachments property for clients to send serializable objects as part of the request. // 09/30/2008 - J. Ritchie Carroll // Converted to C#. // 03/09/2009 - Pinal C. Patel // Edited code comments. // 09/14/2009 - Stephen C. Wills // Added new header and license agreement. // 10/14/2010 - Pinal C. Patel // Overrode ToString() method to provide a text representation of ClientRequest. // Recoded static Parse() method to make it more robust. // 01/24/201 - Pinal C. Patel // Modified ToString() remove leading and trailing white spaces. // 12/20/2012 - Starlynn Danyelle Gilliam // Modified Header. // //****************************************************************************************************** using System; using System.Collections.Generic; using System.Runtime.Serialization; using System.Security; using GSF.Console; namespace GSF.ServiceProcess { /// /// Represents a request sent by to . /// /// /// [Serializable] public class ClientRequest : ISerializable { #region [ Members ] // Fields private string m_command; private Arguments m_arguments; private readonly List m_attachments; #endregion #region [ Constructors ] /// /// Initializes a new instance of the class. /// public ClientRequest() : this("UNDEFINED") { } /// /// Initializes a new instance of the class. /// /// Command text for the . public ClientRequest(string command) : this(command, new Arguments("")) { } /// /// Initializes a new instance of the class. /// /// Command text for the . /// for the . public ClientRequest(string command, Arguments arguments) { m_command = command.ToUpper(); m_arguments = arguments; m_attachments = new List(); } /// /// Creates a new from serialization parameters. /// /// The with populated with data. /// The source for this deserialization. protected ClientRequest(SerializationInfo info, StreamingContext context) { // Deserialize client request fields m_command = info.GetOrDefault("command", ""); m_arguments = info.GetOrDefault("arguments", new Arguments("")); m_attachments = new List(); int attachmentCount = info.GetOrDefault("attachmentCount", 0); for (int i = 0; i < attachmentCount; i++) { m_attachments.Add(info.GetOrDefault("attachment" + i, null as object)); } } #endregion #region [ Properties ] /// /// Gets or sets the command text for the . /// /// The value being assigned is either a null or empty string. public string Command { get { return m_command; } set { if (string.IsNullOrEmpty(value)) throw new ArgumentNullException("value"); m_command = value.ToUpper(); } } /// /// Gets or sets the for the . /// public Arguments Arguments { get { return m_arguments; } set { m_arguments = value; } } /// /// Gets a list of attachments for the . /// public List Attachments { get { return m_attachments; } } #endregion #region [ Methods ] /// /// Returns the that represents the . /// /// A that represents the . public override string ToString() { return string.Format("{0} {1}", m_command, m_arguments).Trim(); } /// /// Populates a with the data needed to serialize the target object. /// /// The to populate with data. /// The destination (see ) for this serialization. /// The caller does not have the required permission. public void GetObjectData(SerializationInfo info, StreamingContext context) { // Serialize client request fields info.AddValue("command", m_command); info.AddValue("arguments", m_arguments, typeof(Arguments)); info.AddValue("attachmentCount", m_attachments.Count); for (int i = 0; i < m_attachments.Count; i++) { info.AddValue("attachment" + i, m_attachments[i]); } } #endregion #region [ Static ] /// /// Converts to a . /// /// Text to be converted to a . /// object if parsing is successful; otherwise null. public static ClientRequest Parse(string text) { // Input text can't be null. if ((object)text == null) return null; // Input text can't be empty. text = text.Trim(); if (text == "") return null; string[] textSegments = text.Split(' '); ClientRequest request = new ClientRequest(); request.Command = textSegments[0].ToUpper(); if (textSegments.Length == 1) request.Arguments = new Arguments(""); else request.Arguments = new Arguments(text.Remove(0, text.IndexOf(' ') + 1).Trim()); return request; } #endregion } }