📄 multiringregrtest.java
字号:
/*************************************************************************"FreePastry" Peer-to-Peer Application Development Substrate Copyright 2002, Rice University. All rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions aremet:- Redistributions of source code must retain the above copyrightnotice, this list of conditions and the following disclaimer.- Redistributions in binary form must reproduce the above copyrightnotice, this list of conditions and the following disclaimer in thedocumentation and/or other materials provided with the distribution.- Neither the name of Rice University (RICE) nor the names of itscontributors may be used to endorse or promote products derived fromthis software without specific prior written permission.This software is provided by RICE and the contributors on an "as is"basis, without any representations or warranties of any kind, expressor implied including, but not limited to, representations orwarranties of non-infringement, merchantability or fitness for aparticular purpose. In no event shall RICE or contributors be liablefor any direct, indirect, incidental, special, exemplary, orconsequential damages (including, but not limited to, procurement ofsubstitute goods or services; loss of use, data, or profits; orbusiness interruption) however caused and on any theory of liability,whether in contract, strict liability, or tort (including negligenceor otherwise) arising in any way out of the use of this software, evenif advised of the possibility of such damage.********************************************************************************/package rice.p2p.multiring.testing;import java.io.IOException;import java.net.*;import java.util.*;import rice.environment.Environment;import rice.environment.params.simple.SimpleParameters;import rice.environment.random.RandomSource;import rice.environment.time.simulated.DirectTimeSource;import rice.p2p.commonapi.*;import rice.p2p.commonapi.Id;import rice.p2p.commonapi.NodeHandle;import rice.p2p.multiring.MultiringNode;import rice.pastry.*;import rice.pastry.commonapi.PastryIdFactory;import rice.pastry.direct.*;import rice.pastry.dist.*;import rice.pastry.standard.RandomNodeIdFactory;/** * Provides regression testing setup for applications written on top of the * commonapi. Currently is written to use Pastry nodes, but this will be * abstracted away. * * @version $Id: MultiringRegrTest.java 3274 2006-05-15 16:17:47Z jeffh $ * @author Alan Mislove */public class MultiringRegrTest { // ----- VARAIBLES ----- // the collection of nodes which have been created /** * DESCRIBE THE FIELD */ protected MultiringNode[] globalNodes; // the collection of nodes which have been created /** * DESCRIBE THE FIELD */ protected MultiringNode[][] organizationalNodes; // the test applications on the global nodes /** * DESCRIBE THE FIELD */ protected MultiringTestApp[] globalApps; // the collection of apps on org nodes /** * DESCRIBE THE FIELD */ protected MultiringTestApp[][] organizationalApps; // the global ring id /** * DESCRIBE THE FIELD */ protected Id globalRingId; // the list of ringIds for organizations /** * DESCRIBE THE FIELD */ protected Id[] ringIds; // ----- PASTRY SPECIFIC VARIABLES ----- // the factory for creating pastry nodes /** * DESCRIBE THE FIELD */ protected PastryNodeFactory factory; // the factory for creating random node ids /** * DESCRIBE THE FIELD */ protected IdFactory idFactory; // the simulator, in case of direct /** * DESCRIBE THE FIELD */ protected NetworkSimulator simulator; /** * DESCRIBE THE FIELD */ public Environment environment; // ----- STATIC FIELDS ----- // the number of nodes to create /** * DESCRIBE THE FIELD */ public static int NUM_GLOBAL_NODES = 20; // the number of organizations to create /** * DESCRIBE THE FIELD */ public static int NUM_ORGANIZATIONS = 5; // the number of internal nodes in each organization /** * DESCRIBE THE FIELD */ public static int NUM_INTERNAL_NODES = 3; // the number gateway nodes in each org /** * DESCRIBE THE FIELD */ public static int NUM_GATEWAY_NODES = NUM_GLOBAL_NODES / NUM_ORGANIZATIONS; // the number of nodes in each organization /** * DESCRIBE THE FIELD */ public static int NUM_ORGANIZATIONAL_NODES = NUM_GATEWAY_NODES + NUM_INTERNAL_NODES; // ----- TESTING SPECIFIC FIELDS ----- // the text to print to the screen /** * DESCRIBE THE FIELD */ public final static String SUCCESS = "SUCCESS"; /** * DESCRIBE THE FIELD */ public final static String FAILURE = "FAILURE"; // the width to pad the output /** * DESCRIBE THE FIELD */ protected final static int PAD_SIZE = 60; // the direct protocol /** * DESCRIBE THE FIELD */ public final static int PROTOCOL_DIRECT = -138; // the possible network simulation models /** * DESCRIBE THE FIELD */ public final static int SIMULATOR_SPHERE = -1; /** * DESCRIBE THE FIELD */ public final static int SIMULATOR_EUCLIDEAN = -2; // ----- PASTRY SPECIFIC FIELDS ----- // the port to begin creating nodes on /** * DESCRIBE THE FIELD */ public static int PORT = 5009; // the host to boot the first node off of /** * DESCRIBE THE FIELD */ public static String BOOTSTRAP_HOST = "localhost"; // the port on the bootstrap to contact /** * DESCRIBE THE FIELD */ public static int BOOTSTRAP_PORT = 5009; // the procotol to use when creating nodes /** * DESCRIBE THE FIELD */ public static int PROTOCOL = DistPastryNodeFactory.PROTOCOL_DEFAULT; // the simulator to use in the case of direct /** * DESCRIBE THE FIELD */ public static int SIMULATOR = SIMULATOR_SPHERE; // the instance name to use /** * DESCRIBE THE FIELD */ public static String INSTANCE_NAME = "MultiringRegrTest"; // ----- EXTERNALLY AVAILABLE METHODS ----- /** * Constructor, which takes no arguments and sets up the factories in * preparation for node creation. * * @param env DESCRIBE THE PARAMETER * @exception IOException DESCRIBE THE EXCEPTION */ public MultiringRegrTest(Environment env) throws IOException { this.environment = env; if (PROTOCOL == PROTOCOL_DIRECT) { if (SIMULATOR == SIMULATOR_SPHERE) { simulator = new SphereNetwork(env); } else { simulator = new EuclideanNetwork(env); } factory = new DirectPastryNodeFactory(new RandomNodeIdFactory(environment), simulator, env); } else { factory = DistPastryNodeFactory.getFactory(new RandomNodeIdFactory(environment), PROTOCOL, PORT, env); } NUM_GATEWAY_NODES = NUM_GLOBAL_NODES / NUM_ORGANIZATIONS; NUM_ORGANIZATIONAL_NODES = NUM_GATEWAY_NODES + NUM_INTERNAL_NODES; idFactory = new PastryIdFactory(env); globalRingId = idFactory.buildId(new byte[20]); ringIds = new Id[NUM_ORGANIZATIONS]; globalNodes = new MultiringNode[NUM_GLOBAL_NODES]; organizationalNodes = new MultiringNode[NUM_ORGANIZATIONS][NUM_ORGANIZATIONAL_NODES]; globalApps = new MultiringTestApp[NUM_GLOBAL_NODES]; organizationalApps = new MultiringTestApp[NUM_ORGANIZATIONS][NUM_ORGANIZATIONAL_NODES]; } /** * Gets a handle to a bootstrap node. * * @param bootstrap DESCRIBE THE PARAMETER * @return handle to bootstrap node, or null. */ protected rice.pastry.NodeHandle getBootstrap(Node bootstrap) { if (PROTOCOL == PROTOCOL_DIRECT) { return ((DirectPastryNode) bootstrap).getLocalHandle(); } else { InetSocketAddress address = new InetSocketAddress(BOOTSTRAP_HOST, BOOTSTRAP_PORT); return ((DistPastryNodeFactory) factory).getNodeHandle(((DistNodeHandle) ((DistPastryNode) bootstrap).getLocalHandle()).getAddress()); } } /** * Method which creates the nodes */ public void createNodes() { for (int i = 0; i < NUM_GLOBAL_NODES; i++) { globalNodes[i] = createNode(globalRingId, globalNodes[0]); simulate(); globalApps[i] = new MultiringTestApp(globalNodes[i]); simulate(); System.out.println("Created node " + i + " in the global ring with id " + globalNodes[i].getId()); } for (int i = 0; i < NUM_ORGANIZATIONS; i++) { ringIds[i] = generateId(16 * (i + 1)); for (int j = 0; j < NUM_GATEWAY_NODES; j++) { organizationalNodes[i][j] = createNode(globalNodes[i * NUM_GATEWAY_NODES + j], ringIds[i], organizationalNodes[i][0]); simulate(); organizationalApps[i][j] = new MultiringTestApp(organizationalNodes[i][j]); simulate(); System.out.println("Created gateway node " + j + " in ring " + ringIds[i] + " with id " + organizationalNodes[i][j].getId()); } for (int j = NUM_GATEWAY_NODES; j < NUM_ORGANIZATIONAL_NODES; j++) { organizationalNodes[i][j] = createNode(ringIds[i], organizationalNodes[i][0]); simulate(); organizationalApps[i][j] = new MultiringTestApp(organizationalNodes[i][j]); simulate(); System.out.println("Created internal node " + (j - NUM_GATEWAY_NODES) + " in ring " + ringIds[i] + " with id " + organizationalNodes[i][j].getId()); } } } /** * Method which starts the creation of nodes */ public void start() { createNodes(); System.out.println("\nTest Beginning\n"); runTest(); } // ----- INTERNAL METHODS ----- /** * In case we're using the direct simulator, this method simulates the message * passing. */ protected void simulate() { try { Thread.sleep(300); } catch (InterruptedException ie) { }// if (PROTOCOL == PROTOCOL_DIRECT) {// while (simulator.simulate()) {}// } else {// pause(500);// } } /** * Method which creates a single node, given it's node number * * @param ringId DESCRIBE THE PARAMETER * @param bootstrap DESCRIBE THE PARAMETER * @return The created node */ protected MultiringNode createNode(Id ringId, MultiringNode bootstrap) { MultiringNode mn; if (bootstrap == null) { mn = new MultiringNode(ringId, factory.newNode(null)); } else { mn = new MultiringNode(ringId, factory.newNode(getBootstrap(bootstrap.getNode()))); } PastryNode pn = (PastryNode) mn.getNode(); synchronized (pn) { while (!pn.isReady()) { try { pn.wait(500); } catch (InterruptedException ie) { return null; } } if (!pn.isReady()) { System.out.println("Still waiting for node " + pn + " in ring " + ringId + " to be ready."); } } return mn; } /** * Method which creates a single node, given it's node number * * @param existing DESCRIBE THE PARAMETER * @param ringId DESCRIBE THE PARAMETER * @param bootstrap DESCRIBE THE PARAMETER * @return The created node */ protected MultiringNode createNode(MultiringNode existing, Id ringId, MultiringNode bootstrap) { if (existing == null) { throw new IllegalArgumentException("EXISTING WAS NULL! " + ringId + " " + bootstrap); } if (bootstrap == null) { return new MultiringNode(ringId, factory.newNode(null, (rice.pastry.Id) existing.getNodeId()), existing); } else { return new MultiringNode(ringId, factory.newNode(getBootstrap(bootstrap.getNode()), (rice.pastry.Id) existing.getNodeId()), existing); } } /** * Method which pauses for the provided number of milliseconds * * @param ms The number of milliseconds to pause */ protected synchronized void pause(int ms) { if (PROTOCOL != PROTOCOL_DIRECT) { try { wait(ms); } catch (InterruptedException e) { } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -