using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections.ObjectModel; namespace Tor.Controller { /// /// A class containing the command to get network information from the tor service. /// internal sealed class GetInfoCommand : Command { private string request; /// /// Initializes a new instance of the class. /// public GetInfoCommand() { this.request = null; } /// /// Initializes a new instance of the class. /// /// The request to send with the getinfo command. public GetInfoCommand(string request) { this.request = request; } #region Tor.Controller.Command<> /// /// Dispatches the command to the client control port and produces a response result. /// /// The control connection where the command should be dispatched. /// /// A object instance containing the response data. /// protected override GetInfoResponse Dispatch(Connection connection) { if (request == null) return new GetInfoResponse(false); if (connection.Write("getinfo {0}", request)) { ConnectionResponse response = connection.Read(); if (!response.Success || !response.Responses[0].StartsWith(request, StringComparison.CurrentCultureIgnoreCase)) return new GetInfoResponse(false); List values = new List(response.Responses.Count); if (response.Responses.Count == 1) { string[] parts = response.Responses[0].Split(new[] { '=' }, 2); values.Add(parts.Length == 1 ? null : parts[1]); } else { for (int i = 1; i < response.Responses.Count; i++) { if (".".Equals(response.Responses[i])) break; values.Add(response.Responses[i]); } } return new GetInfoResponse(true, values.AsReadOnly()); } return new GetInfoResponse(false); } #endregion } /// /// A class containing the response values for a getinfo command. /// internal sealed class GetInfoResponse : Response { private readonly ReadOnlyCollection values; /// /// Initializes a new instance of the class. /// /// A value indicating whether the command was received and processed successfully. public GetInfoResponse(bool success) : base(success) { this.values = new List().AsReadOnly(); } /// /// Initializes a new instance of the class. /// /// A value indicating whether the command was received and processed successfully. /// The values returned from the control connection. public GetInfoResponse(bool success, ReadOnlyCollection values) : base(success) { this.values = values; } #region Properties /// /// Gets a read-only collection of the values returned from the control connection. /// public ReadOnlyCollection Values { get { return values; } } #endregion } }