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();
    }
  }