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 retrieve configuration values from a client.
///
internal sealed class GetConfCommand : Command
{
private readonly ReadOnlyCollection configurations;
///
/// Initializes a new instance of the class.
///
/// The configurations which should be retrieved from the tor application.
public GetConfCommand(List configurations)
{
this.configurations = configurations.AsReadOnly();
}
#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 GetConfResponse Dispatch(Connection connection)
{
StringBuilder builder = new StringBuilder("getconf");
foreach (string name in configurations)
{
builder.Append(' ');
builder.Append(name);
}
if (connection.Write(builder.ToString()))
{
ConnectionResponse response = connection.Read();
if (!response.Success)
return new GetConfResponse(false, null);
ResponsePairs values = new ResponsePairs(response.Responses.Count);
foreach (string value in response.Responses)
{
string[] parts = value.Split(new[] { '=' }, 2);
string name = parts[0].Trim();
if (parts.Length != 2)
values[name] = null;
else
values[name] = parts[1].Trim();
}
return new GetConfResponse(true, values);
}
return new GetConfResponse(false, null);
}
#endregion
}
///
/// A class containing a collection of configuration value responses.
///
internal sealed class GetConfResponse : Response
{
private readonly ResponsePairs values;
///
/// Initializes a new instance of the class.
///
/// A value indicating whether the command was received and processed successfully.
/// The values received from the tor control connection.
public GetConfResponse(bool success, ResponsePairs values) : base(success)
{
this.values = values;
}
#region Properties
///
/// Gets the values received from the control connection.
///
public ResponsePairs Values
{
get { return values; }
}
#endregion
}
}