コード例 #1
0
  public void setUp() throws Exception {
    super.setUp();
    // p1 <--> p2 <--> p3 peer 1 cannot reach peer 3
    myProtocolContainer1 = getProtocolContainer(-1, false, "1");
    myServer1 = getP2PServer(myProtocolContainer1, RoutingProtocol.START_PORT);
    RoutingProtocol theRoutingProtocol1 =
        (RoutingProtocol) myProtocolContainer1.getProtocol(RoutingProtocol.ID);
    myTransferProtocl1 =
        ((AsyncTransferProtocol) myProtocolContainer1.getProtocol(AsyncTransferProtocol.ID));

    myProtocolContainer2 = getProtocolContainer(-1, false, "2");
    myServer2 = getP2PServer(myProtocolContainer2, RoutingProtocol.START_PORT + 1);
    RoutingProtocol theRoutingProtocol2 =
        (RoutingProtocol) myProtocolContainer2.getProtocol(RoutingProtocol.ID);
    myTransferProtocol2 =
        ((AsyncTransferProtocol) myProtocolContainer2.getProtocol(AsyncTransferProtocol.ID));

    assertTrue(myServer1.start());
    assertTrue(myServer2.start());

    theRoutingProtocol1.scanLocalSystem();
    theRoutingProtocol2.scanLocalSystem();

    Thread.sleep(SLEEP_AFTER_SCAN);

    for (int i = 0; i < 5; i++) {
      theRoutingProtocol1.exchangeRoutingTable();
      theRoutingProtocol2.exchangeRoutingTable();
    }

    RoutingTable theRoutingTable1 =
        ((RoutingProtocol) myProtocolContainer1.getProtocol(RoutingProtocol.ID)).getRoutingTable();
    RoutingTable theRoutingTable2 =
        ((RoutingProtocol) myProtocolContainer2.getProtocol(RoutingProtocol.ID)).getRoutingTable();

    RoutingTableEntry thePeer2 =
        theRoutingTable1.getEntryForPeer(theRoutingTable2.getLocalPeerId());
    assertNotNull(thePeer2.getPeer());
    assertTrue(thePeer2.isReachable());
  }
コード例 #2
0
  public void sendFile(File aFile, String aPeerid) throws FileTransferException {
    String theResponse = null;

    ProtocolContainer theProtocolContainer = findProtocolContainer();
    AbstractPeer thePeer = null;
    try {
      thePeer = getRoutingTable().getEntryForPeer(aPeerid).getPeer();
      MessageProtocol theMessageProtocol =
          (MessageProtocol) theProtocolContainer.getProtocol("MSG");
      Message theMessage = new Message();
      theMessage.setDestination(thePeer);
      theMessage.setMessage(createMessage(Command.FILE.name() + " " + aFile.getName()));
      theMessage.setProtocolMessage(true);
      theMessage.setMessageTimeoutInSeconds(-1);
      theResponse = theMessageProtocol.sendMessage(theMessage);
    } catch (ProtocolException e) {
      throw new FileTransferException(
          "Could not transfer file because message protocol is not known", e);
    } catch (MessageException e) {
      throw new FileTransferException(
          "Could not transfer file because message could not be delivered", e);
    } catch (UnknownPeerException e) {
      throw new FileTransferException(
          "Could not transfer file because given peer id is not known", e);
    }

    if (!theResponse.startsWith(Response.ACCEPTED.name())) {
      throw new FileTransferException(
          "The file: '"
              + aFile.getName()
              + "' was refused by peer: '"
              + thePeer.getPeerId()
              + "' with response '"
              + theResponse
              + "'");
    } else {
      String theFileId = theResponse.split(" ")[1];
      // TODO we probably should not just cast to SocketPeer
      Pipe thePipe = new Pipe((SocketPeer) thePeer);
      thePipe.setPipeDescription("FILE:" + theFileId + ":" + aFile.length());
      FileInputStream theInputStream = null;
      try {
        PipeProtocol thePipeProtocol = getPipeProtocol();
        thePipeProtocol.openPipe(thePipe);
        theInputStream = new FileInputStream(aFile);
        IOTools.copyStream(theInputStream, thePipe.getSocket().getOutputStream());
        thePipeProtocol.closePipe(thePipe);

        theResponse =
            getPeerSender()
                .send(
                    thePeer,
                    createMessage(
                        Command.WAIT_FOR_FILE.name() + " " + theFileId + " " + aFile.length()))
                .getReply();

        if (theResponse.equalsIgnoreCase(Response.BAD_FILE_SIZE.name())) {
          throw new FileTransferException("Received file had bad file size");
        }

        if (theResponse.equalsIgnoreCase(Response.FILE_NOK.name())) {
          throw new FileTransferException("Sending file failed");
        }
      } catch (Exception e) {
        throw new FileTransferException(
            "Transferring file to peer: '" + thePeer.getPeerId() + "' could not be initiated", e);
      } finally {
        if (theInputStream != null) {
          try {
            theInputStream.close();
          } catch (IOException e) {
          }
        }
      }
    }
  }