package org.torproject.jtor.control.impl;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Iterator;
import java.util.Vector;

import org.torproject.jtor.Logger;
import org.torproject.jtor.Tor;
import org.torproject.jtor.TorConfig;
import org.torproject.jtor.config.impl.TorConfigImpl;
import org.torproject.jtor.control.ControlConnectionHandler;
import org.torproject.jtor.control.ControlServer;
import org.torproject.jtor.logging.ConsoleLogger;

/**
 *
 * @author Merlijn Hofstra
 */
public class ControlServerTCP extends ControlServer {

	private Vector<ControlConnectionHandler> connections = new Vector<ControlConnectionHandler>();

	public ControlServerTCP(Tor tor, TorConfig tc, Logger logger) {
		super(tor, tc, logger);
	}

	@Override
	public void startServer() {
		if (tc.getControlPort() > 0 && !running) {
			running = true;
			this.start();
		}
	}

	@Override
	public void run() {
		ServerSocket ss = null;
		try {
			if (host != null) {
				ss = new ServerSocket(tc.getControlPort(), 0, host);
			} else {
				ss = new ServerSocket(tc.getControlPort());
			}
		} catch (IOException ex) {
			running = false;
		}
		while (running) {
			try {
				Socket s = ss.accept();
				ControlConnectionHandler cch = new ControlConnectionHandlerTCP(this, s);
				connections.add(cch);
				
				logger.debug("Opening new TCP Control Connection on port " + s.getLocalPort());
			} catch (Throwable t) {}
		}

	}

	@SuppressWarnings("unchecked")
	@Override
	public void stopServer() {
		running = false;
		this.interrupt();
		Iterator i = connections.iterator();
		while (i.hasNext()) {
			((ControlConnectionHandler)i.next()).disconnect();
		}
	}

	public void disconnectHandler(ControlConnectionHandler cch) {
		if (connections.remove(cch)) {
			cch.disconnect();
		}
	}

	@Override
	public String getProtocol() {
		return "TCP";
	}
	
	public static void main(String[] arg) {
		Logger logger = new ConsoleLogger();
		TorConfig tc = new TorConfigImpl(logger);
		tc.loadDefaults();
		tc.loadConf();
		tc.setControlPort((short)9051);
		tc.saveConf();
		ControlServer cs = new ControlServerTCP(new Tor(), tc, logger);
		cs.startServer();
	}

}

@@ -437,24 +437,113 @@
 trolServer;%0A
+import org.torproject.jtor.events.Event;%0Aimport org.torproject.jtor.events.EventHandler;%0A
 import org.t
@@ -671,16 +671,40 @@
 lServer 
+implements EventHandler 
 %7B%0A%0A%09priv
@@ -794,16 +794,43 @@
 ler%3E();%0A
+%09private ServerSocket ss;%0A%09
 %0A%09public
@@ -1082,34 +1082,8 @@
 ) %7B%0A
-%09%09ServerSocket ss = null;%0A
 %09%09tr
@@ -1284,16 +1284,63 @@
 se;%0A%09%09%7D%0A
+%09%09%0A%09%09tc.registerConfigChangedHandler(this);%0A%09%09%0A
 %09%09while 
@@ -1844,24 +1844,67 @@
 nect();%0A%09%09%7D%0A
+%09%09tc.unregisterConfigChangedHandler(this);%0A
 %09%7D%0A%0A%09public 
@@ -2080,16 +2080,305 @@
 TCP%22;%0A%09%7D
+%0A%0A%09/*%0A%09 * Catches configuration updates%0A%09 * (non-Javadoc)%0A%09 * @see org.torproject.jtor.events.EventHandler#handleEvent(org.torproject.jtor.events.Event)%0A%09 */%0A%09public void handleEvent(Event event) %7B%0A%09%09if (tc.getControlPort() != ss.getLocalPort()) %7B%0A%09%09%09stopServer();%0A%09%09%09startServer();%0A%09%09%7D%0A%09%7D
 %0A%09%0A%09publ

make use of ConfigChangedEvent for the Controlserver