using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Tor.Controller { /// /// A class containing the command responsible for extending a circuit. /// internal class ExtendCircuitCommand : Command { private readonly Circuit circuit; private readonly List routers; /// /// Initializes a new instance of the class. /// /// The circuit which should be the target of extension. A null value indicates a new circuit. public ExtendCircuitCommand(Circuit circuit) { this.circuit = circuit; this.routers = new List(); } #region Properties /// /// Gets a collection containing the list of routers which should be extended onto this circuit. /// public List Routers { get { return routers; } } #endregion #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 Response Dispatch(Connection connection) { if (routers.Count == 0) return new Response(false); int circuitID = 0; if (circuit != null) circuitID = circuit.ID; StringBuilder builder = new StringBuilder("extendcircuit"); builder.AppendFormat(" {0}", circuitID); foreach (string router in routers) builder.AppendFormat(" {0}", router); if (connection.Write(builder.ToString())) { ConnectionResponse response = connection.Read(); return new Response(response.Success); } return new Response(false); } #endregion } }