Пример #1
0
  /*
   * Test method for 'jKad.builders.RPCFactory.buildRPC(byte[])'
   */
  public void testBuildFindNodeRPC() {
    RPC rpc;
    ByteBuffer data;

    data = ByteBuffer.allocate(FindNodeRPC.TOTAL_AREA_LENGTH);
    data.put(KadProtocol.FIND_NODE);
    data.put((byte) 0);

    byte[] rpcID = this.buildData(KadProtocol.RPC_ID_LENGTH / 4, Integer.MAX_VALUE - 10);
    byte[] destinationID = this.buildData(KadProtocol.NODE_ID_LENGTH / 4, Integer.MAX_VALUE - 20);
    byte[] senderID = this.buildData(KadProtocol.NODE_ID_LENGTH / 4, Integer.MAX_VALUE - 30);
    byte[] searchedID = this.buildData(KadProtocol.NODE_ID_LENGTH / 4, Integer.MAX_VALUE - 40);
    this.buildBasicInfo(data, rpcID, senderID, destinationID);
    data.position(FindNodeRPC.SEARCHED_NODE_AREA);
    data.put(searchedID);
    try {
      FindNodeRPC findNodeRPC = (FindNodeRPC) factory.buildRPC(data.array());
      assertEquals(findNodeRPC.getClass().getName(), FindNodeRPC.class.getName());
      assertEquals(rpcID, findNodeRPC.getRPCID().toByteArray());
      assertEquals(destinationID, findNodeRPC.getDestinationNodeID().toByteArray());
      assertEquals(senderID, findNodeRPC.getSenderNodeID().toByteArray());
      assertEquals(searchedID, findNodeRPC.getSearchedNodeID().toByteArray());
    } catch (KadProtocolException e) {
      e.printStackTrace();
      fail();
    }
  }
Пример #2
0
  /*
   * Test method for 'jKad.builders.RPCFactory.buildRPC(byte[])'
   */
  public void testBuildFindValueResponse() {
    RPC rpc;
    ByteBuffer data;

    data = ByteBuffer.allocate(FindValueResponse.TOTAL_AREA_LENGTH);
    data.put(KadProtocol.FIND_VALUE);
    data.put((byte) 1);

    byte[] rpcID = this.buildData(KadProtocol.RPC_ID_LENGTH / 4, Integer.MAX_VALUE - 10);
    byte[] destinationID = this.buildData(KadProtocol.NODE_ID_LENGTH / 4, Integer.MAX_VALUE - 20);
    byte[] senderID = this.buildData(KadProtocol.NODE_ID_LENGTH / 4, Integer.MAX_VALUE - 30);
    byte piece = Byte.MAX_VALUE - 40;
    byte pieceTotal = Byte.MAX_VALUE - 50;
    byte[] value = this.buildData(KadProtocol.VALUE_LENGTH / 4, Integer.MAX_VALUE - 70);
    this.buildBasicInfo(data, rpcID, senderID, destinationID);
    data.position(FindValueResponse.PIECE_AREA);
    data.put(piece);
    data.position(FindValueResponse.PIECE_TOTAL_AREA);
    data.put(pieceTotal);
    data.position(FindValueResponse.VALUE_AREA);
    data.put(value);
    try {
      FindValueResponse findValueResponse = (FindValueResponse) factory.buildRPC(data.array());
      assertEquals(findValueResponse.getClass().getName(), FindValueResponse.class.getName());
      assertEquals(rpcID, findValueResponse.getRPCID().toByteArray());
      assertEquals(destinationID, findValueResponse.getDestinationNodeID().toByteArray());
      assertEquals(senderID, findValueResponse.getSenderNodeID().toByteArray());
      assertEquals(piece, findValueResponse.getPiece());
      assertEquals(pieceTotal, findValueResponse.getPieceTotal());
      assertEquals(value, findValueResponse.getValue().toByteArray());
    } catch (KadProtocolException e) {
      e.printStackTrace();
      fail();
    }
  }
Пример #3
0
  /*
   * Test method for 'jKad.builders.RPCFactory.buildRPC(byte[])'
   */
  public void testBuildFindNodeResponse() {
    RPC rpc;
    ByteBuffer data;

    data = ByteBuffer.allocate(FindNodeResponse.TOTAL_AREA_LENGTH);
    data.put(KadProtocol.FIND_NODE);
    data.put((byte) 1);

    byte[] rpcID = this.buildData(KadProtocol.RPC_ID_LENGTH / 4, Integer.MAX_VALUE - 10);
    byte[] destinationID = this.buildData(KadProtocol.NODE_ID_LENGTH / 4, Integer.MAX_VALUE - 20);
    byte[] senderID = this.buildData(KadProtocol.NODE_ID_LENGTH / 4, Integer.MAX_VALUE - 30);
    byte[] ip = this.buildData(FindNodeResponse.IP_ADDRESS_LENGTH / 4, Integer.MAX_VALUE - 40);
    byte[] port = this.buildData(FindNodeResponse.PORT_LENGTH / 4, Integer.MAX_VALUE - 50);
    this.buildBasicInfo(data, rpcID, senderID, destinationID);
    data.position(FindNodeResponse.IP_AREA);
    data.put(ip);
    data.position(FindNodeResponse.PORT_AREA);
    data.put(port);
    try {
      FindNodeResponse findNodeResponse = (FindNodeResponse) factory.buildRPC(data.array());
      assertEquals(findNodeResponse.getClass().getName(), FindNodeResponse.class.getName());
      assertEquals(rpcID, findNodeResponse.getRPCID().toByteArray());
      assertEquals(destinationID, findNodeResponse.getDestinationNodeID().toByteArray());
      assertEquals(senderID, findNodeResponse.getSenderNodeID().toByteArray());
      assertEquals(ip, findNodeResponse.getIpAddress().toByteArray());
      assertEquals(port, findNodeResponse.getPort().toByteArray());
    } catch (KadProtocolException e) {
      e.printStackTrace();
      fail();
    }
  }
Пример #4
0
 public static Packet createRPCPacket(NetworkManager m, RPC rpc) throws IOException {
   if (!rpc.isInitialized()) {
     if (T.t) {
       T.error("RPC not initialized!");
     }
   }
   Packet p = m.createPacketForSend();
   p.writeByte(RPCFactory.getPacketIdFor(rpc));
   p = rpc.serializeTo(p);
   return p;
 }
Пример #5
0
 public void received(int fromGuid, int hops, Packet packet, RPC outerRPC) throws IOException {
   int id = packet.readByte();
   RPC rpc = RPCFactory.newInstance(id);
   if (rpc == null) {
     if (T.t) {
       T.warn("Skipping unknown RPC ID: " + id + "!!!");
     }
     packet.skip(packet.getAvailable()); // skip contents of packet
     return;
   } else {
     if (T.t) {
       if (outerRPC == null) {
         T.debug(
             "<-- Recived "
                 + rpc
                 + " ("
                 + (packet.getAvailable() + 1)
                 + " bytes) from "
                 + netMan.getFriendManager().getNode(fromGuid)
                 + " (con: "
                 + getRemoteFriend()
                 + ")"); // +1 because we read one byte above
       } else {
         T.debug(
             "<----- Recived "
                 + rpc
                 + " ("
                 + (packet.getAvailable() + 1)
                 + " bytes) from "
                 + netMan.getFriendManager().getNode(fromGuid)
                 + " (con: "
                 + getRemoteFriend()
                 + ", outer: "
                 + outerRPC
                 + ")"); // +1 because we read one byte above
       }
     }
     rpc.init(this, fromGuid, hops);
     rpc.execute(packet);
     signalReceived(rpc);
   }
 }
Пример #6
0
  /*
   * Test method for 'jKad.builders.RPCFactory.buildRPC(byte[])'
   */
  public void testBuildStoreRPC() {
    RPC rpc;
    ByteBuffer data;

    data = ByteBuffer.allocate(StoreRPC.TOTAL_AREA_LENGTH);
    data.put(KadProtocol.STORE);
    data.put((byte) 0);

    byte[] rpcID = this.buildData(KadProtocol.RPC_ID_LENGTH / 4, Integer.MAX_VALUE - 10);
    byte[] destinationID = this.buildData(KadProtocol.NODE_ID_LENGTH / 4, Integer.MAX_VALUE - 20);
    byte[] senderID = this.buildData(KadProtocol.NODE_ID_LENGTH / 4, Integer.MAX_VALUE - 30);
    byte piece = Byte.MAX_VALUE - 40;
    byte pieceTotal = Byte.MAX_VALUE - 50;
    byte[] key = this.buildData(KadProtocol.KEY_LENGTH / 4, Integer.MAX_VALUE - 60);
    byte[] value = this.buildData(KadProtocol.VALUE_LENGTH / 4, Integer.MAX_VALUE - 70);
    this.buildBasicInfo(data, rpcID, senderID, destinationID);
    data.position(StoreRPC.PIECE_AREA);
    data.put(piece);
    data.position(StoreRPC.PIECE_TOTAL_AREA);
    data.put(pieceTotal);
    data.position(StoreRPC.KEY_AREA);
    data.put(key);
    data.position(StoreRPC.VALUE_AREA);
    data.put(value);
    try {
      StoreRPC storeRPC = (StoreRPC) factory.buildRPC(data.array());
      assertEquals(storeRPC.getClass().getName(), StoreRPC.class.getName());
      assertEquals(rpcID, storeRPC.getRPCID().toByteArray());
      assertEquals(destinationID, storeRPC.getDestinationNodeID().toByteArray());
      assertEquals(senderID, storeRPC.getSenderNodeID().toByteArray());
      assertEquals(piece, storeRPC.getPiece());
      assertEquals(pieceTotal, storeRPC.getPieceTotal());
      assertEquals(key, storeRPC.getKey().toByteArray());
      assertEquals(value, storeRPC.getValue().toByteArray());
    } catch (KadProtocolException e) {
      e.printStackTrace();
      fail();
    }
  }
Пример #7
0
  /*
   * Test method for 'jKad.builders.RPCFactory.buildRPC(byte[])'
   */
  public void testBuildPingResponse() {
    RPC rpc;
    ByteBuffer data;

    data = ByteBuffer.allocate(PingResponse.TOTAL_AREA_LENGTH);
    data.put(KadProtocol.PING);
    data.put((byte) 1);

    byte[] rpcID = this.buildData(KadProtocol.RPC_ID_LENGTH / 4, Integer.MAX_VALUE - 10);
    byte[] destinationID = this.buildData(KadProtocol.NODE_ID_LENGTH / 4, Integer.MAX_VALUE - 20);
    byte[] senderID = this.buildData(KadProtocol.NODE_ID_LENGTH / 4, Integer.MAX_VALUE - 30);
    this.buildBasicInfo(data, rpcID, senderID, destinationID);
    try {
      PingResponse pingResponse = (PingResponse) factory.buildRPC(data.array());
      assertEquals(pingResponse.getClass().getName(), PingResponse.class.getName());
      assertEquals(rpcID, pingResponse.getRPCID().toByteArray());
      assertEquals(destinationID, pingResponse.getDestinationNodeID().toByteArray());
      assertEquals(senderID, pingResponse.getSenderNodeID().toByteArray());
    } catch (KadProtocolException e) {
      e.printStackTrace();
      fail();
    }
  }
Пример #8
0
 public void setUp() {
   RPCFactory.setRPCFactoryClass(RPCFactoryImp.class);
   factory = RPCFactory.newInstance();
 }