<commit_before>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 <NAME>
 */
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();
	}

}
<commit_msg>make use of ConfigChangedEvent for the Controlserver<commit_after>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.events.Event;
import org.torproject.jtor.events.EventHandler;
import org.torproject.jtor.logging.ConsoleLogger;

/**
 *
 * @author <NAME>
 */
public class ControlServerTCP extends ControlServer implements EventHandler {

	private Vector<ControlConnectionHandler> connections = new Vector<ControlConnectionHandler>();
	private ServerSocket ss;
	
	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() {
		try {
			if (host != null) {
				ss = new ServerSocket(tc.getControlPort(), 0, host);
			} else {
				ss = new ServerSocket(tc.getControlPort());
			}
		} catch (IOException ex) {
			running = false;
		}
		
		tc.registerConfigChangedHandler(this);
		
		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();
		}
		tc.unregisterConfigChangedHandler(this);
	}

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

	@Override
	public String getProtocol() {
		return "TCP";
	}

	/*
	 * Catches configuration updates
	 * (non-Javadoc)
	 * @see org.torproject.jtor.events.EventHandler#handleEvent(org.torproject.jtor.events.Event)
	 */
	public void handleEvent(Event event) {
		if (tc.getControlPort() != ss.getLocalPort()) {
			stopServer();
			startServer();
		}
	}
	
	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();
	}

}

{'fraction_non_alphanumeric': 0.12868686868686868, 'fraction_numerical': 0.0024242424242424242, 'mean_word_length': 4.2558386411889595, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4937554', 'n_tokens_mistral': 1736, 'n_tokens_neox': 1560, 'n_words': 347}