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

📄 myapp.java

📁 pastry的java实现的2.0b版
💻 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.********************************************************************************//* *  Created on Feb 15, 2005 * *  TODO To change the template for this generated file go to *  Window - Preferences - Java - Code Style - Code Templates */package rice.tutorial.appsocket;import java.io.IOException;import java.nio.ByteBuffer;import rice.p2p.commonapi.*;import rice.p2p.commonapi.appsocket.*;/** * A very simple application. * * @version $Id: pretty.settings 2305 2005-03-11 20:22:33Z jeffh $ * @author Jeff Hoye */public class MyApp implements Application {  /**   * The Endpoint represents the underlieing node. By making calls on the   * Endpoint, it assures that the message will be delivered to a MyApp on   * whichever node the message is intended for.   */  protected Endpoint endpoint;  /**   * The node we were constructed on.   */  protected Node node;  ByteBuffer[] outs;  ByteBuffer out;  ByteBuffer[] ins;  ByteBuffer in;  int MSG_LENGTH;  /**   * Constructor for MyApp.   *   * @param node DESCRIBE THE PARAMETER   * @param factory DESCRIBE THE PARAMETER   */  public MyApp(Node node, final IdFactory factory) {    // We are only going to use one instance of this application on each PastryNode    this.endpoint = node.buildEndpoint(this, "myinstance");    this.node = node;    MSG_LENGTH = node.getLocalNodeHandle().getId().toByteArray().length;    outs = new ByteBuffer[1];    out = ByteBuffer.wrap(node.getLocalNodeHandle().getId().toByteArray());    outs[0] = out;    ins = new ByteBuffer[1];    in = ByteBuffer.allocate(MSG_LENGTH);    ins[0] = in;    // example receiver interface    endpoint.accept(      new AppSocketReceiver() {        /**         * Called if we have a problem.         *         * @param socket DESCRIBE THE PARAMETER         * @param e DESCRIBE THE PARAMETER         */        public void receiveException(AppSocket socket, Exception e) {          e.printStackTrace();        }        public void receiveSelectResult(AppSocket socket, boolean canRead, boolean canWrite) {          in.clear();          try {            long ret = socket.read(ins, 0, ins.length);            if (ret != MSG_LENGTH) {              // if you sent any kind of long message, you would need to handle this case better              System.out.println("Error, we only received part of a message.");              return;            }            System.out.println(MyApp.this.node.getLocalNodeHandle() + " Received message from " + factory.buildId(in.array()));          } catch (IOException ioe) {            ioe.printStackTrace();          }          // only need to do this if expecting more messages//        socket.register(true, false, 3000, this);        }        /**         * When we accept a new socket.         *         * @param socket DESCRIBE THE PARAMETER         */        public void receiveSocket(AppSocket socket) {//        System.out.println("Accepted socket: "+socket);          socket.register(true, false, 30000, this);          // it's critical to call this to be able to accept multiple times          endpoint.accept(this);        }      });    endpoint.register();  }  /**   * Getter for the node.   *   * @return The Node value   */  public Node getNode() {    return node;  }  /**   * Called to directly send a message to the nh   *   * @param nh DESCRIBE THE PARAMETER   */  public void sendMyMsgDirect(NodeHandle nh) {    System.out.println(this + " opening to " + nh);    endpoint.connect(nh,      new AppSocketReceiver() {        /**         * Called if there is a problem.         *         * @param socket DESCRIBE THE PARAMETER         * @param e DESCRIBE THE PARAMETER         */        public void receiveException(AppSocket socket, Exception e) {          e.printStackTrace();        }        /**         * Example of how to write some bytes         *         * @param socket DESCRIBE THE PARAMETER         * @param canRead DESCRIBE THE PARAMETER         * @param canWrite DESCRIBE THE PARAMETER         */        public void receiveSelectResult(AppSocket socket, boolean canRead, boolean canWrite) {          try {            long ret = socket.write(outs, 0, outs.length);//          System.out.println("WROTE:"+ret);            // see if we are done            if (!out.hasRemaining()) {              socket.close();              out.clear();            } else {              // keep writing              socket.register(false, true, 30000, this);            }          } catch (IOException ioe) {            ioe.printStackTrace();          }        }        /**         * Called when the socket comes available.         *         * @param socket DESCRIBE THE PARAMETER         */        public void receiveSocket(AppSocket socket) {//        System.out.println("Connected socket: "+socket);          socket.register(false, true, 30000, this);        }      }, 30000);  }  /**   * Called when we receive a message.   *   * @param id DESCRIBE THE PARAMETER   * @param message DESCRIBE THE PARAMETER   */  public void deliver(Id id, Message message) {    System.out.println(this + " received " + message);  }  /**   * Called when you hear about a new neighbor. Don't worry about this method   * for now.   *   * @param handle DESCRIBE THE PARAMETER   * @param joined DESCRIBE THE PARAMETER   */  public void update(NodeHandle handle, boolean joined) {  }  /**   * Called a message travels along your path. Don't worry about this method for   * now.   *   * @param message DESCRIBE THE PARAMETER   * @return DESCRIBE THE RETURN VALUE   */  public boolean forward(RouteMessage message) {    return true;  }  /**   * DESCRIBE THE METHOD   *   * @return DESCRIBE THE RETURN VALUE   */  public String toString() {    return "MyApp " + endpoint.getId();  }}

⌨️ 快捷键说明

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