[or-cvs] r11423: made nodes in network accessible by their name; automaticall (in puppetor/trunk/src/de/uniba/wiai/lspi/puppetor: . impl)

kloesing at seul.org kloesing at seul.org
Tue Sep 11 13:02:21 UTC 2007


Author: kloesing
Date: 2007-09-11 09:02:21 -0400 (Tue, 11 Sep 2007)
New Revision: 11423

Modified:
   puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/Network.java
   puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/NetworkFactory.java
   puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/ProxyNode.java
   puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/RouterNode.java
   puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/impl/NetworkImpl.java
   puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/impl/ProxyNodeImpl.java
   puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/impl/RouterNodeImpl.java
Log:
made nodes in network accessible by their name; automatically assign consecutive port numbers to newly configured nodes

Modified: puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/Network.java
===================================================================
--- puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/Network.java	2007-09-11 11:30:27 UTC (rev 11422)
+++ puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/Network.java	2007-09-11 13:02:21 UTC (rev 11423)
@@ -100,6 +100,26 @@
 			int socksPort);
 
 	/**
+	 * Creates a new directory node with automatically assigned ports and adds
+	 * it to the network, but does not yet write its configuration to disk or
+	 * start the corresponding Tor process. This operation can only be invoked,
+	 * if network status is <code>NetworkState.CONFIGURING_NODES</code>.
+	 * 
+	 * @param nodeName
+	 *            The name for this node, which is used as name for the working
+	 *            directory, for logging purposes, and as node nickname. May
+	 *            neither be <code>null</code> or have zero or more than 19
+	 *            alpha-numeric characters.
+	 * @return Reference to the created directory node.
+	 * @throws IllegalStateException
+	 *             Thrown if network is not in state
+	 *             <code>NetworkState.CONFIGURING_NODES</code>.
+	 * @throws IllegalArgumentException
+	 *             Thrown if an invalid value is given as node name.
+	 */
+	public abstract DirectoryNode createDirectory(String nodeName);
+
+	/**
 	 * Creates a new directory node and adds it to the network, but does not yet
 	 * write its configuration to disk or start the corresponding Tor process.
 	 * This operation can only be invoked, if network status is
@@ -137,15 +157,31 @@
 			int controlPort, int socksPort, int orPort, int dirPort);
 
 	/**
-	 * Creates a new <code>ProxyNode</code> and adds it to the
-	 * <code>network</code>, but does not yet write its configuration to disk
-	 * or start the corresponding Tor process.
+	 * Creates a new <code>ProxyNode</code> with automatically assigned ports
+	 * and adds it to the network, but does not yet write its configuration to
+	 * disk or start the corresponding Tor process.
 	 * 
 	 * @param nodeName
 	 *            The name for this node, which is only used as name for the
 	 *            working directory and for logging purposes. May neither be
 	 *            <code>null</code> or have zero or more than 19 alpha-numeric
 	 *            characters.
+	 * @return Reference to the created proxy node.
+	 * @throws IllegalArgumentException
+	 *             Thrown if an invalid value is given as node name.
+	 */
+	public abstract ProxyNode createProxy(String nodeName);
+
+	/**
+	 * Creates a new <code>ProxyNode</code> and adds it to the network, but
+	 * does not yet write its configuration to disk or start the corresponding
+	 * Tor process.
+	 * 
+	 * @param nodeName
+	 *            The name for this node, which is only used as name for the
+	 *            working directory and for logging purposes. May neither be
+	 *            <code>null</code> or have zero or more than 19 alpha-numeric
+	 *            characters.
 	 * @param controlPort
 	 *            The TCP port on which the corresponding Tor process will wait
 	 *            for a controller. May not be negative or greater than 65535.
@@ -162,14 +198,31 @@
 			int socksPort);
 
 	/**
-	 * Creates a new router node and adds it to the network, but does not yet
-	 * write its configuration to disk or start the corresponding Tor process.
+	 * Creates a new <code>RouterNode</code> with automatically assigned ports
+	 * and adds it to the network, but does not yet write its configuration to
+	 * disk or start the corresponding Tor process.
 	 * 
 	 * @param nodeName
 	 *            The name for this node, which is used as name for the working
 	 *            directory, for logging purposes, and as node nickname. May
 	 *            neither be <code>null</code> or have zero or more than 19
 	 *            alpha-numeric characters.
+	 * @return Reference to the created router node.
+	 * @throws IllegalArgumentException
+	 *             Thrown if an invalid value is given as node name.
+	 */
+	public abstract RouterNode createRouter(String nodeName);
+
+	/**
+	 * Creates a new <code>RouterNode</code> and adds it to the network, but
+	 * does not yet write its configuration to disk or start the corresponding
+	 * Tor process.
+	 * 
+	 * @param nodeName
+	 *            The name for this node, which is used as name for the working
+	 *            directory, for logging purposes, and as node nickname. May
+	 *            neither be <code>null</code> or have zero or more than 19
+	 *            alpha-numeric characters.
 	 * @param controlPort
 	 *            The TCP port on which the corresponding Tor process will wait
 	 *            for a controller. May not be negative or greater than 65535.
@@ -195,13 +248,28 @@
 			int socksPort, int orPort, int dirPort);
 
 	/**
-	 * Creates a new server application, but does not start listening for
-	 * incoming requests.
+	 * Creates a new <code>ServerApplication</code> with automatically
+	 * assigned ports, but does not start listening for incoming requests.
 	 * 
 	 * @param serverApplicationName
 	 *            The name for this server application, which is used for
 	 *            logging purposes only. May neither be <code>null</code> or a
 	 *            zero-length string.
+	 * @return Reference to the created server application.
+	 * @throws IllegalArgumentException
+	 *             Thrown if an invalid value is given as server application
+	 *             name.
+	 */
+	public abstract ServerApplication createServer(String serverApplicationName);
+
+	/**
+	 * Creates a new <code>ServerApplication</code>, but does not start
+	 * listening for incoming requests.
+	 * 
+	 * @param serverApplicationName
+	 *            The name for this server application, which is used for
+	 *            logging purposes only. May neither be <code>null</code> or a
+	 *            zero-length string.
 	 * @param serverPort
 	 *            The TCP port on which the server will wait for incoming
 	 *            requests. May not be negative or greater than 65535.
@@ -228,6 +296,15 @@
 	public abstract NetworkState getNetworkState();
 
 	/**
+	 * Returns the <code>ProxyNode</code> with name <code>nodeName</code>.
+	 * 
+	 * @param nodeName
+	 *            The node name to look up.
+	 * @return The <code>ProxyNode</code> with name <code>nodeName</code>.
+	 */
+	public abstract ProxyNode getProxyNode(String nodeName);
+
+	/**
 	 * <p>
 	 * Sends a HUP signal to all nodes in the network in regular intervals and
 	 * blocks the invoking thread until all nodes have reported to have

Modified: puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/NetworkFactory.java
===================================================================
--- puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/NetworkFactory.java	2007-09-11 11:30:27 UTC (rev 11422)
+++ puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/NetworkFactory.java	2007-09-11 13:02:21 UTC (rev 11423)
@@ -29,4 +29,22 @@
 	public static Network createNetwork(String networkName) {
 		return new NetworkImpl(networkName);
 	}
+
+	/**
+	 * Creates a new network that is required for a test run. The new network is
+	 * initially unpopulated and creates its own working directory at
+	 * test-env/randomTestID/.
+	 * 
+	 * @param networkName
+	 *            Name of this network configuration.
+	 * @param startPort
+	 *            The initial value for automatically assigned port numbers of
+	 *            nodes created by this <code>Network</code>; must be a value
+	 *            between 1024 and 65535.
+	 * @return A new network instance.
+	 * @throws RemoteException
+	 */
+	public static Network createNetwork(String networkName, int startPort) {
+		return new NetworkImpl(networkName, startPort);
+	}
 }

Modified: puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/ProxyNode.java
===================================================================
--- puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/ProxyNode.java	2007-09-11 11:30:27 UTC (rev 11422)
+++ puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/ProxyNode.java	2007-09-11 13:02:21 UTC (rev 11423)
@@ -194,4 +194,11 @@
 	 */
 	public abstract int getSocksPort();
 
+	/**
+	 * Returns the control port of this node.
+	 * 
+	 * @return The control port of this node.
+	 */
+	public abstract int getControlPort();
+
 }

Modified: puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/RouterNode.java
===================================================================
--- puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/RouterNode.java	2007-09-11 11:30:27 UTC (rev 11422)
+++ puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/RouterNode.java	2007-09-11 13:02:21 UTC (rev 11423)
@@ -71,4 +71,11 @@
 	 */
 	public abstract int getDirPort();
 
+	/**
+	 * Returns the onion port of this node.
+	 * 
+	 * @return The onion port of this node.
+	 */
+	public abstract int getOrPort();
+
 }

Modified: puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/impl/NetworkImpl.java
===================================================================
--- puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/impl/NetworkImpl.java	2007-09-11 11:30:27 UTC (rev 11422)
+++ puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/impl/NetworkImpl.java	2007-09-11 13:02:21 UTC (rev 11423)
@@ -1,6 +1,7 @@
 package de.uniba.wiai.lspi.puppetor.impl;
 
 import java.io.File;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Set;
 import java.util.logging.Level;
@@ -193,7 +194,7 @@
 	 * work only with interface types to assure that all operations could also
 	 * be performed by the application itself.
 	 */
-	private Set<ProxyNode> nodes = new HashSet<ProxyNode>();
+	private HashMap<String, ProxyNode> nodes = new HashMap<String, ProxyNode>();
 
 	/**
 	 * Directory that contains status information of all nodes contained in this
@@ -202,14 +203,54 @@
 	private File workingDir;
 
 	/**
+	 * The counter for automatically assigned port numbers created by this
+	 * <code>Network</code>.
+	 */
+	private int portCounter = 7000;
+
+	/**
 	 * Creates an initially unpopulated Tor network and creates a new working
 	 * directory for it at test-env/randomTestID/.
 	 * 
 	 * @param networkName
 	 *            Name of this network configuration. May neither be
 	 *            <code>null</code> or a zero-length string.
+	 * @param startPort
+	 *            The initial value for automatically assigned port numbers of
+	 *            nodes created by this <code>Network</code>; must be a value
+	 *            between 1024 and 65535.
 	 * @throws IllegalArgumentException
 	 *             Thrown if the given <code>networkName</code> is either
+	 *             <code>null</code> or a zero-length string, or if an illegal
+	 *             number is given for <code>startPort</code>.
+	 */
+	public NetworkImpl(String networkName, int startPort) {
+
+		// initialize using overloaded constructor
+		this(networkName);
+
+		// check if start port is valid
+		if (startPort < 1024 || startPort > 65535) {
+			throw new IllegalArgumentException("Invalid startPort: "
+					+ startPort);
+		}
+
+		// remember parameter
+		this.portCounter = startPort;
+
+		// log exiting
+		this.logger.exiting(this.getClass().getName(), "NetworkImpl");
+	}
+
+	/**
+	 * Creates an initially unpopulated Tor network and creates a new working
+	 * directory for it at test-env/randomTestID/.
+	 * 
+	 * @param networkName
+	 *            Name of this network configuration. May neither be
+	 *            <code>null</code> or a zero-length string.
+	 * @throws IllegalArgumentException
+	 *             Thrown if the given <code>networkName</code> is either
 	 *             <code>null</code> or a zero-length string.
 	 */
 	public NetworkImpl(String networkName) {
@@ -271,7 +312,7 @@
 		}
 
 		// fail on first node that is not up
-		for (ProxyNode node : this.nodes) {
+		for (ProxyNode node : this.nodes.values()) {
 			if (!eventManager.hasEventOccured(node,
 					EventType.NODE_CIRCUIT_OPENED)) {
 
@@ -295,7 +336,7 @@
 		// start threads to determine fingerprints for all directories and
 		// routers in parallel
 		Set<FingerprintThread> fingerprintThreads = new HashSet<FingerprintThread>();
-		for (ProxyNode node : nodes) {
+		for (ProxyNode node : nodes.values()) {
 			if (node instanceof RouterNode) {
 				RouterNode dirOrRouterNode = (RouterNode) node;
 				FingerprintThread fingerprintThread = new FingerprintThread(
@@ -332,7 +373,7 @@
 		// have been read from disk before, so that this will perform really
 		// fast
 		this.authorizedDirectoriesFingerprints = new HashSet<String>();
-		for (ProxyNode node : this.nodes) {
+		for (ProxyNode node : this.nodes.values()) {
 			if (node instanceof DirectoryNode) {
 				DirectoryNode dirNode = (DirectoryNode) node;
 				this.authorizedDirectoriesFingerprints.add(dirNode
@@ -341,7 +382,7 @@
 		}
 
 		// configure nodes
-		for (ProxyNode node : this.nodes) {
+		for (ProxyNode node : this.nodes.values()) {
 			if (node.getNodeState() == NodeState.CONFIGURING) {
 				node
 						.configureDirServers(this.authorizedDirectoriesFingerprints);
@@ -352,7 +393,7 @@
 		// should have been read from disk before, so that this will perform
 		// really fast
 		this.approvedRoutersFingerprints = new HashSet<String>();
-		for (ProxyNode node : this.nodes) {
+		for (ProxyNode node : this.nodes.values()) {
 			if (node instanceof RouterNode) {
 				RouterNode routerOrDirNode = (RouterNode) node;
 				this.approvedRoutersFingerprints.add(routerOrDirNode
@@ -362,7 +403,7 @@
 
 		// write fingerprints for all directories and routers to
 		// approved-routers file
-		for (ProxyNode node : this.nodes) {
+		for (ProxyNode node : this.nodes.values()) {
 			if (node instanceof DirectoryNode) {
 				DirectoryNode dirNode = (DirectoryNode) node;
 				dirNode.writeApprovedRouters(this.approvedRoutersFingerprints);
@@ -412,31 +453,62 @@
 				socksPort, orPort, dirPort);
 
 		// add new directory node to nodes collection
-		this.nodes.add(dir);
+		this.nodes.put(dir.getNodeName(), dir);
 
 		// log exiting and return directory node
 		this.logger.exiting(this.getClass().getName(), "createDirectory", dir);
 		return dir;
 	}
 
-	public ProxyNode createProxy(String nodeName, int controlPort, int socksPort) {
+	public DirectoryNode createDirectory(String nodeName) {
 
 		// log entering
+		this.logger.entering(this.getClass().getName(), "createDirectory",
+				nodeName);
+
+		// invoke overloaded method
+		DirectoryNode dir = this.createDirectory(nodeName, portCounter++,
+				portCounter++, portCounter++, portCounter++);
+
+		// log exiting and return directory node
+		this.logger.exiting(this.getClass().getName(), "createDirectory", dir);
+		return dir;
+	}
+
+	public synchronized ProxyNode createProxy(String nodeName, int controlPort,
+			int socksPort) {
+
+		// log entering
 		this.logger.entering(this.getClass().getName(), "createProxy",
 				new Object[] { nodeName, controlPort, socksPort });
 
 		// create proxy node; parameter checking is done in constructor
-		ProxyNodeImpl proxy = new ProxyNodeImpl(this, nodeName, controlPort,
+		ProxyNode proxy = new ProxyNodeImpl(this, nodeName, controlPort,
 				socksPort);
 
 		// add new proxy node to nodes collection
-		this.nodes.add(proxy);
+		this.nodes.put(proxy.getNodeName(), proxy);
 
 		// log exiting and return proxy node
 		this.logger.exiting(this.getClass().getName(), "createProxy", proxy);
 		return proxy;
 	}
 
+	public ProxyNode createProxy(String nodeName) {
+
+		// log entering
+		this.logger
+				.entering(this.getClass().getName(), "createProxy", nodeName);
+
+		// invoke overloaded method
+		ProxyNode proxy = this.createProxy(nodeName, portCounter++,
+				portCounter++);
+
+		// log exiting and return proxy node
+		this.logger.exiting(this.getClass().getName(), "createProxy", proxy);
+		return proxy;
+	}
+
 	public RouterNode createRouter(String nodeName, int controlPort,
 			int socksPort, int orPort, int dirPort) {
 
@@ -450,13 +522,28 @@
 				socksPort, orPort, dirPort);
 
 		// add new router node to nodes collection
-		this.nodes.add(router);
+		this.nodes.put(router.getNodeName(), router);
 
 		// log exiting and return router node
 		this.logger.exiting(this.getClass().getName(), "createRouter", router);
 		return router;
 	}
 
+	public RouterNode createRouter(String nodeName) {
+
+		// log entering
+		this.logger.entering(this.getClass().getName(), "createRouter",
+				nodeName);
+
+		// invoke overloaded method
+		RouterNode router = this.createRouter(nodeName, portCounter++,
+				portCounter++, portCounter++, portCounter++);
+
+		// log exiting and return router node
+		this.logger.exiting(this.getClass().getName(), "createRouter", router);
+		return router;
+	}
+
 	public ServerApplication createServer(String serverApplicationName,
 			int serverPort) {
 
@@ -473,6 +560,21 @@
 		return server;
 	}
 
+	public ServerApplication createServer(String serverApplicationName) {
+
+		// log entering
+		this.logger.entering(this.getClass().getName(), "createServer",
+				serverApplicationName);
+
+		// invoke overloaded method
+		ServerApplication server = this.createServer(serverApplicationName,
+				portCounter++);
+
+		// log exiting and return server
+		this.logger.exiting(this.getClass().getName(), "createServer", server);
+		return server;
+	}
+
 	public EventManager getEventManager() {
 		return this.eventManager;
 	}
@@ -494,6 +596,10 @@
 		return this.workingDir;
 	}
 
+	public ProxyNode getProxyNode(String nodeName) {
+		return this.nodes.get(nodeName);
+	}
+
 	public boolean hupUntilUp(int tries, long hupInterval)
 			throws TorProcessException {
 
@@ -518,7 +624,7 @@
 
 		// create and register a new event handler for each node
 		final Thread sleepingThread = Thread.currentThread();
-		for (ProxyNode node : this.nodes) {
+		for (ProxyNode node : this.nodes.values()) {
 			eventManager.addEventListener(node, new EventListener() {
 				public void handleEvent(Event event) {
 					if (event.getType() == EventType.NODE_CIRCUIT_OPENED) {
@@ -559,7 +665,7 @@
 
 			this.logger.log(Level.FINE, "Sending HUP to nodes");
 			// send a HUP signal to all nodes
-			for (ProxyNode node : this.nodes) {
+			for (ProxyNode node : this.nodes.values()) {
 				this.logger.log(Level.FINE, "Sending HUP to node "
 						+ node.toString());
 				node.hup();
@@ -590,7 +696,7 @@
 		// continue shutting down the other nodes and throw the first exception
 		// subsequently
 		TorProcessException firstCaughtException = null;
-		for (ProxyNode node : this.nodes) {
+		for (ProxyNode node : this.nodes.values()) {
 			if (node.getNodeState() == NodeState.RUNNING) {
 				try {
 					node.shutdown();
@@ -643,7 +749,7 @@
 
 		// start nodes in parallel
 		Set<NodeStarter> allNodeStarters = new HashSet<NodeStarter>();
-		for (ProxyNode node : this.nodes) {
+		for (ProxyNode node : this.nodes.values()) {
 			NodeStarter nodeStarter = new NodeStarter(node,
 					maximumTimeToWaitInMillis);
 			allNodeStarters.add(nodeStarter);
@@ -723,7 +829,7 @@
 		}
 
 		// write configurations for all nodes
-		for (ProxyNode node : this.nodes) {
+		for (ProxyNode node : this.nodes.values()) {
 			if (node.getNodeState() == NodeState.CONFIGURING) {
 				node.writeConfiguration();
 			}

Modified: puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/impl/ProxyNodeImpl.java
===================================================================
--- puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/impl/ProxyNodeImpl.java	2007-09-11 11:30:27 UTC (rev 11422)
+++ puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/impl/ProxyNodeImpl.java	2007-09-11 13:02:21 UTC (rev 11423)
@@ -732,4 +732,8 @@
 	public int getSocksPort() {
 		return this.socksPort;
 	}
+
+	public int getControlPort() {
+		return this.controlPort;
+	}
 }

Modified: puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/impl/RouterNodeImpl.java
===================================================================
--- puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/impl/RouterNodeImpl.java	2007-09-11 11:30:27 UTC (rev 11422)
+++ puppetor/trunk/src/de/uniba/wiai/lspi/puppetor/impl/RouterNodeImpl.java	2007-09-11 13:02:21 UTC (rev 11423)
@@ -374,6 +374,10 @@
 		return this.dirPort;
 	}
 
+	public int getOrPort() {
+		return this.orPort;
+	}
+
 	public boolean startNode(long maximumTimeToWaitInMillis)
 			throws TorProcessException {
 



More information about the tor-commits mailing list