⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multiringregrtest.java

📁 pastry的java实现的2.0b版
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
  /**   * Method which kills the specified node   *   * @param n The node to kill   */  protected void kill(int n) {//   if (PROTOCOL == PROTOCOL_DIRECT)//     simulator.setAlive((rice.pastry.NodeId) nodes[n].getId(), false);  }  /**   * Private method which generates a random Id   *   * @return A new random Id   */  private Id generateId() {    byte[] data = new byte[20];    environment.getRandomSource().nextBytes(data);    return idFactory.buildId(data);  }  /**   * Private method which generates a Id with a prefix   *   * @param i DESCRIBE THE PARAMETER   * @return A new random Id   */  private Id generateId(int i) {    byte[] data = new byte[20];    data[data.length - 1] = (byte) 2;    data[data.length - 2] = (byte) i;    return idFactory.buildId(data);  }  // ----- METHODS TO BE PROVIDED BY IMPLEMENTATIONS -----  /**   * Method which should run the test - this is called once all of the nodes   * have been created and are ready.   */  protected void runTest() {    RandomSource rng = environment.getRandomSource();    for (int i = 0; i < 20; i++) {      int si = rng.nextInt(NUM_ORGANIZATIONS);      int sj = rng.nextInt(NUM_ORGANIZATIONAL_NODES);      int di = rng.nextInt(NUM_ORGANIZATIONS);      int dj = rng.nextInt(NUM_ORGANIZATIONAL_NODES);      MultiringTestApp sourceApp = organizationalApps[si][sj];      Id source = organizationalNodes[si][sj].getId();      Id dest = organizationalNodes[di][dj].getId();      System.out.println("SENDING FROM " + source + " TO " + dest);      sourceApp.send(dest);      try {        Thread.sleep(500);      } catch (Exception e) {      }    }  }  // ----- TESTING UTILITY METHODS -----  /**   * Method which prints the beginning of a test section.   *   * @param name The name of section   */  protected final void sectionStart(String name) {    System.out.println(name);  }  /**   * Method which prints the end of a test section.   */  protected final void sectionDone() {    System.out.println();  }  /**   * Method which prints the beginning of a test section step.   *   * @param name The name of step   */  protected final void stepStart(String name) {    System.out.print(pad("  " + name));  }  /**   * Method which prints the end of a test section step, with an assumed   * success.   */  protected final void stepDone() {    stepDone(SUCCESS);  }  /**   * Method which prints the end of a test section step.   *   * @param status The status of step   */  protected final void stepDone(String status) {    stepDone(status, "");  }  /**   * Method which prints the end of a test section step, as well as a message.   *   * @param status The status of section   * @param message The message   */  protected final void stepDone(String status, String message) {    System.out.println("[" + status + "]");    if ((message != null) && (!message.equals(""))) {      System.out.println("     " + message);    }    if (status.equals(FAILURE)) {      System.exit(0);    }  }  /**   * Method which prints an exception which occured during testing.   *   * @param e The exception which was thrown   */  protected final void stepException(Exception e) {    System.out.println("\nException " + e + " occurred during testing.");    e.printStackTrace();    System.exit(0);  }  /**   * Method which pads a given string with "." characters.   *   * @param start The string   * @return The result.   */  private final String pad(String start) {    if (start.length() >= PAD_SIZE) {      return start.substring(0, PAD_SIZE);    } else {      int spaceLength = PAD_SIZE - start.length();      char[] spaces = new char[spaceLength];      Arrays.fill(spaces, '.');      return start.concat(new String(spaces));    }  }  /**   * Throws an exception if the test condition is not met.   *   * @param intention DESCRIBE THE PARAMETER   * @param test DESCRIBE THE PARAMETER   */  protected final void assertTrue(String intention, boolean test) {    if (!test) {      stepDone(FAILURE, "Assertion '" + intention + "' failed.");    }  }  /**   * Thows an exception if expected is not equal to actual.   *   * @param description DESCRIBE THE PARAMETER   * @param expected DESCRIBE THE PARAMETER   * @param actual DESCRIBE THE PARAMETER   */  protected final void assertEquals(String description,                                    Object expected,                                    Object actual) {    if (!expected.equals(actual)) {      stepDone(FAILURE, "Assertion '" + description +        "' failed, expected: '" + expected +        "' got: " + actual + "'");    }  }  // ----- COMMAND LINE PARSING METHODS -----  /**   * process command line args   *   * @param args DESCRIBE THE PARAMETER   */  protected static void parseArgs(String args[]) {    // process command line arguments    for (int i = 0; i < args.length; i++) {      if (args[i].equals("-help")) {        System.out.println("Usage: DistCommonAPITest [-port p] [-protocol (rmi|wire)] [-bootstrap host[:port]] [-help]");        System.exit(1);      }    }    for (int i = 0; i < args.length; i++) {      if (args[i].equals("-globalNodes") && i + 1 < args.length) {        int p = Integer.parseInt(args[i + 1]);        if (p > 0) {          NUM_GLOBAL_NODES = p;        }        break;      }    }    for (int i = 0; i < args.length; i++) {      if (args[i].equals("-organizations") && i + 1 < args.length) {        int p = Integer.parseInt(args[i + 1]);        if (p > 0) {          NUM_ORGANIZATIONS = p;        }        break;      }    }    for (int i = 0; i < args.length; i++) {      if (args[i].equals("-internalNodes") && i + 1 < args.length) {        int p = Integer.parseInt(args[i + 1]);        if (p > 0) {          NUM_INTERNAL_NODES = p;        }        break;      }    }    for (int i = 0; i < args.length; i++) {      if (args[i].equals("-port") && i + 1 < args.length) {        int p = Integer.parseInt(args[i + 1]);        if (p > 0) {          PORT = p;        }        break;      }    }    BOOTSTRAP_PORT = PORT;    for (int i = 0; i < args.length; i++) {      if (args[i].equals("-bootstrap") && i + 1 < args.length) {        String str = args[i + 1];        int index = str.indexOf(':');        if (index == -1) {          BOOTSTRAP_HOST = str;          BOOTSTRAP_PORT = PORT;        } else {          BOOTSTRAP_HOST = str.substring(0, index);          BOOTSTRAP_PORT = Integer.parseInt(str.substring(index + 1));          if (BOOTSTRAP_PORT <= 0) {            BOOTSTRAP_PORT = PORT;          }        }        break;      }    }    for (int i = 0; i < args.length; i++) {      if (args[i].equals("-protocol") && i + 1 < args.length) {        String s = args[i + 1];//        if (s.equalsIgnoreCase("wire"))//          PROTOCOL = DistPastryNodeFactory.PROTOCOL_WIRE;//        else if (s.equalsIgnoreCase("rmi"))//          PROTOCOL = DistPastryNodeFactory.PROTOCOL_RMI;//        else        if (s.equalsIgnoreCase("socket")) {          PROTOCOL = DistPastryNodeFactory.PROTOCOL_SOCKET;        } else if (s.equalsIgnoreCase("direct")) {          PROTOCOL = PROTOCOL_DIRECT;        } else {          System.out.println("ERROR: Unsupported protocol: " + s);        }        break;      }    }    for (int i = 0; i < args.length; i++) {      if (args[i].equals("-simulator") && i + 1 < args.length) {        String s = args[i + 1];        if (s.equalsIgnoreCase("sphere")) {          SIMULATOR = SIMULATOR_SPHERE;        } else if (s.equalsIgnoreCase("euclidean")) {          PROTOCOL = SIMULATOR_EUCLIDEAN;        } else {          System.out.println("ERROR: Unsupported simulator: " + s);        }        break;      }    }  }  /**   * The main program for the MultiringRegrTest class   *   * @param args The command line arguments   * @exception IOException DESCRIBE THE EXCEPTION   */  public static void main(String[] args) throws IOException {    parseArgs(args);    Environment env;    if (PROTOCOL == PROTOCOL_DIRECT) {      env = Environment.directEnvironment();    } else {      env = new Environment();    }    MultiringRegrTest test = new MultiringRegrTest(env);    test.start();    env.destroy();  }  /**   * DESCRIBE THE CLASS   *   * @version $Id: pretty.settings 2305 2005-03-11 20:22:33Z jeffh $   * @author jeffh   */  public class MultiringTestApp implements Application {    /**     * DESCRIBE THE FIELD     */    protected Endpoint endpoint;    /**     * Constructor for MultiringTestApp.     *     * @param node DESCRIBE THE PARAMETER     */    public MultiringTestApp(Node node) {      this.endpoint = node.buildEndpoint(this, "BLAH");      this.endpoint.register();    }    /**     * DESCRIBE THE METHOD     *     * @param target DESCRIBE THE PARAMETER     */    public void send(Id target) {      endpoint.route(target, new MultiringTestMessage(endpoint.getId()), null);    }    /**     * DESCRIBE THE METHOD     *     * @param message DESCRIBE THE PARAMETER     * @return DESCRIBE THE RETURN VALUE     */    public boolean forward(RouteMessage message) {      return true;    }    /**     * DESCRIBE THE METHOD     *     * @param id DESCRIBE THE PARAMETER     * @param message DESCRIBE THE PARAMETER     */    public void deliver(Id id, Message message) {      System.out.println("RECEIVED MESSSAGE FROM " + ((MultiringTestMessage) message).source + " FOR TARGET " + id + " AT NODE " + endpoint.getId());    }    /**     * DESCRIBE THE METHOD     *     * @param handle DESCRIBE THE PARAMETER     * @param joined DESCRIBE THE PARAMETER     */    public void update(NodeHandle handle, boolean joined) {    }  }  /**   * DESCRIBE THE CLASS   *   * @version $Id: pretty.settings 2305 2005-03-11 20:22:33Z jeffh $   * @author jeffh   */  public static class MultiringTestMessage implements Message {    /**     * DESCRIBE THE FIELD     */    public Id source;    /**     * Constructor for MultiringTestMessage.     *     * @param source DESCRIBE THE PARAMETER     */    public MultiringTestMessage(Id source) {      this.source = source;    }    /**     * Gets the Priority attribute of the MultiringTestMessage object     *     * @return The Priority value     */    public byte getPriority() {      return MEDIUM_PRIORITY;    }  }  // ----- ATTEMPT TO LOAD LOCAL HOSTNAME -----  static {    try {      BOOTSTRAP_HOST = InetAddress.getLocalHost().getHostName();    } catch (UnknownHostException e) {      System.out.println("Error determining local host: " + e);    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -