Ejemplo n.º 1
0
  public PeerSenderReply sendMessageTo(
      AbstractPeer aSendingPeer, WebPeer aWebPeer, String aMessage, int aTimeoutInSeconds)
      throws IOException {
    //    synchronized (aWebPeer.getPeerId()) {

    //      long t1 = System.currentTimeMillis();
    //      LOGGER.debug("Entering peer to web sender for message '" + aMessage + "' logcounter: " +
    // LOGCOUNTER++);
    //    AbstractURLConnectionHelper theConnectionHelper = new ApacheURLConnectionHelper( new
    // URL(aWebPeer.getURL(), ProtocolWebServer.CONTEXT_PROTOCOL), true );
    AbstractURLConnectionHelper theConnectionHelper =
        new URLConnectionHelper(
            new URL(aWebPeer.getURL(), ProtocolWebServer.CONTEXT_PROTOCOL), true);
    theConnectionHelper.scheduleClose(30, TimeUnit.SECONDS);
    try {
      theConnectionHelper.connectInputOutput();
      //        long t2 = System.currentTimeMillis();
      //        LOGGER.debug("Connecting to '" + aWebPeer.getURL()  + "' took " + (t2 - t1) + "
      // ms");
      theConnectionHelper.write("session", UUID.randomUUID().toString());
      theConnectionHelper.write("peerid", aSendingPeer.getPeerId());
      //      theConnectionHelper.write( "input", aMessage );
      theConnectionHelper.write("input", URLEncoder.encode(aMessage, "UTF-8"));
      //        long t3 = System.currentTimeMillis();
      //        LOGGER.debug("Writing to outputstream of '" + aWebPeer.getURL() + "' took " +
      // (t3-t2) +  " ms");
      //      theConnectionHelper.endLine();
      //      theConnectionHelper.flush();
      theConnectionHelper.endInput();
      String theLine = theConnectionHelper.readLine();
      //        long t4 = System.currentTimeMillis();
      //        LOGGER.debug("Reading from '" + aWebPeer.getURL()  + "' took " + (t4-t3) +  " ms");

      return new PeerSenderReply(theLine, HttpCommunicationInterface.getInstance());
    } catch (IOException e) {
      LOGGER.error(
          "Could not send message to web peer at endpoint: '"
              + aWebPeer.getEndPointRepresentation()
              + "'",
          e);
      // LOGGER.error("Could not send message to web peer at endpoint: '" +
      // aWebPeer.getEndPointRepresentation() + "'");
      throw e;
    } finally {
      theConnectionHelper.close();
    }
  }
  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) {
          }
        }
      }
    }
  }