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

📄 socketbuffer.java

📁 pastry的java实现的2.0b版
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*************************************************************************"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 22, 2006 */package rice.pastry.socket;import java.io.*;import java.nio.ByteBuffer;import rice.environment.logging.Logger;import rice.p2p.commonapi.rawserialization.*;import rice.p2p.util.MathUtils;import rice.pastry.*;import rice.pastry.messaging.*;import rice.pastry.routing.*;/** * Holds 1 serialized message for receiving or sending. Has specialized code for * RouteMessage, Liveness Message, and byte arrays. * * @version $Id: pretty.settings 2305 2005-03-11 20:22:33Z jeffh $ * @author Jeff Hoye */public class SocketBuffer implements RawMessageDelivery {  private MessageDeserializer defaultDeserializer;  private NodeHandleFactory nhf;  private SocketPastryNode spn;  private int address;  private short type;  byte priority;  private NodeHandle sender;  // RouteMessage stuff  private SendOptions sendOpts;  short rmSubType = -2;  int rmSubAddress = -2;  SocketDataInputStream str;  private ByteBuffer buffer;  ExposedDataOutputStream o;  ExposedByteArrayOutputStream ebaos;  /**   * True if was just part of session initiation.   */  boolean discard = false;  // low level stuff  /**   * DESCRIBE THE FIELD   */  public final static int DEFAULT_BUFFER_SIZE = 1024;  // Hack to not have to allocate buffers I know to be zero  /**   * DESCRIBE THE FIELD   */  public final static byte[] ZERO = new byte[8];  // writing bytes  /**   * Constructor for SocketBuffer.   *   * @param output DESCRIBE THE PARAMETER   */  public SocketBuffer(byte[] output) {    buffer = ByteBuffer.wrap(output);    priority = -1;    discard = true;  }  // from a read  /**   * Constructor for SocketBuffer.   *   * @param input DESCRIBE THE PARAMETER   * @param spn DESCRIBE THE PARAMETER   * @exception IOException DESCRIBE THE EXCEPTION   */  public SocketBuffer(byte[] input, SocketPastryNode spn) throws IOException {    str = new SocketDataInputStream(new ByteArrayInputStream(input));    nhf = spn;    this.spn = spn;//  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+//  +            Appl   Address    (0 is the router)                +//  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+//  +  HasSender?   +   Priority    +  Type (Application specifc)   + // zero is java serialization//  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+//  +            NodeHandle sender                                  +//  +                                                               +//                    ...  flexable size//  +                                                               +//  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+    address = str.readInt();    boolean hasSender = str.readBoolean();    priority = str.readByte();    type = str.readShort();    if (hasSender) {      if (spn == null) {        sender = SocketNodeHandle.build(str);      } else {        sender = spn.readNodeHandle(str);      }    }  }  /**   * For quick write   *   * @param rm   * @exception IOException DESCRIBE THE EXCEPTION   */  public SocketBuffer(PRawMessage rm) throws IOException {    initialize(DEFAULT_BUFFER_SIZE);    serialize(rm, true);  }  /**   * For a quick write.   *   * @param address DESCRIBE THE PARAMETER   * @param path DESCRIBE THE PARAMETER   * @exception IOException DESCRIBE THE EXCEPTION   * @throws IOException   */  public SocketBuffer(EpochInetSocketAddress address, SourceRoute path) throws IOException {    initialize(DEFAULT_BUFFER_SIZE);    o.write(SocketCollectionManager.PASTRY_MAGIC_NUMBER);    o.writeInt(0);    // version//    o.write(PingManager.HEADER_PING, 0, PingManager.HEADER_PING.length);    o.writeByte((byte) 1);    int numHops = path.getNumHops() + 1;    o.writeByte((byte) (numHops));    address.serialize(o);    for (int i = 0; i < path.getNumHops(); i++) {      path.getHop(i).serialize(o);    }  }  /**   * Constructor for SocketBuffer.   *   * @param address DESCRIBE THE PARAMETER   * @param path DESCRIBE THE PARAMETER   * @param msg DESCRIBE THE PARAMETER   * @exception IOException DESCRIBE THE EXCEPTION   */  public SocketBuffer(EpochInetSocketAddress address, SourceRoute path, PRawMessage msg) throws IOException {    this(address, path);//    System.out.println("SB "+msg);    serialize(msg, false);  }  /**   * Constructor for SocketBuffer.   *   * @param path DESCRIBE THE PARAMETER   * @param appId DESCRIBE THE PARAMETER   * @exception IOException DESCRIBE THE EXCEPTION   */  public SocketBuffer(SourceRoute path, int appId) throws IOException {    initialize(DEFAULT_BUFFER_SIZE);    o.write(SocketCollectionManager.PASTRY_MAGIC_NUMBER);    o.writeInt(0);    // version    for (int i = 1; i < path.getNumHops(); i++) {      o.write(SocketCollectionManager.HEADER_SOURCE_ROUTE, 0, SocketCollectionManager.HEADER_SOURCE_ROUTE.length);      path.getHop(i).serialize(o);    }    o.write(SocketCollectionManager.HEADER_DIRECT, 0, SocketCollectionManager.HEADER_DIRECT.length);    o.write(MathUtils.intToByteArray(appId), 0, 4);  }  /**   * Main Constructor   *   * @param defaultDeserializer   * @param nhf DESCRIBE THE PARAMETER   */  public SocketBuffer(MessageDeserializer defaultDeserializer, NodeHandleFactory nhf) {    this.defaultDeserializer = defaultDeserializer;    this.nhf = nhf;    initialize(DEFAULT_BUFFER_SIZE);  }  /**   * Gets the RouteMessage attribute of the SocketBuffer object   *   * @return The RouteMessage value   */  public boolean isRouteMessage() {    return address == RouterAddress.getCode() && type == RouteMessage.TYPE;  }  /**   * Gets the Options attribute of the SocketBuffer object   *   * @return The Options value   */  public SendOptions getOptions() {    return sendOpts;  }  /**   * Gets the RouteMessage attribute of the SocketBuffer object   *   * @return The RouteMessage value   */  public RouteMessage getRouteMessage() {    try {      return (RouteMessage) deserialize(null);    } catch (IOException ioe) {      throw new RuntimeException(ioe);    }  }//  public void serialize(PRawMessage msg) {//    address = msg.getDestination();//    type = msg.getType();//    priority = msg.getPriority();//    sender = msg.getSender();//  }  /**   * Gets the Buffer attribute of the SocketBuffer object   *   * @return The Buffer value   */  public ByteBuffer getBuffer() {    if (buffer != null) {      return buffer;    }    buffer = (ByteBuffer) ByteBuffer.wrap(ebaos.buf()).limit(o.bytesWritten());    return buffer;  }  /**   * Gets the Address attribute of the SocketBuffer object   *   * @return The Address value   */  public int getAddress() {    return address;  }  /**   * Gets the Type attribute of the SocketBuffer object   *   * @return The Type value   */  public short getType() {    return type;  }  /**   * Sets the Type attribute of the SocketBuffer object   *   * @param type The new Type value   */  public void setType(short type) {    this.type = type;  }  /**   * DESCRIBE THE METHOD   *

⌨️ 快捷键说明

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