using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace Tor.Controller
{
///
/// A class containing the base methods and properties for a command which will be executed across a control connection,
/// and will return a response corresponding to the response of the tor application.
///
internal abstract class Command where T : Response
{
///
/// Creates a new object instance and dispatches the command to the specified client.
///
/// The type of the command.
/// The type of the response generated from the command.
/// The client hosting the control connection port.
/// true if the command was created and dispatched successfully; otherwise, false.
public static bool DispatchAndReturn(Client client) where TCommand : Command
{
try
{
TCommand command = Activator.CreateInstance();
if (command == null)
return false;
T response = command.Dispatch(client);
return response.Success;
}
catch
{
return false;
}
}
///
/// Dispatches the command to the client control port and produces a response result.
///
/// The client hosting the control connection port.
/// A object instance containing the response data.
public T Dispatch(Client client)
{
if (client == null)
throw new ArgumentNullException("client");
if (!client.IsRunning)
throw new TorException("A command cannot be dispatched to a client which is no longer running");
try
{
using (Connection connection = new Connection(client))
{
if (!connection.Connect())
throw new TorException("A command could not be dispatched to a client because the command failed to connect to the control port");
if (!connection.Authenticate(client.GetControlPassword()))
throw new TorException("A command could not be dispatched to a client because the control could not be authenticated");
return Dispatch(connection);
}
}
catch (Exception exception)
{
throw new TorException("A command could not be dispatched to a client because an error occurred", exception);
}
}
///
/// 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 abstract T Dispatch(Connection connection);
}
}