Ejemplo n.º 1
0
  public void init() throws ServletException {
    super.init();
    try {
      if (getServletContext().getAttribute("SessionData") == null) {
        getServletContext().setAttribute("SessionData", new SessionData());
      }

      if (getServletContext().getAttribute("PeerIpMap") == null) {
        getServletContext().setAttribute("PeerIpMap", new HashMap<String, String>());
      }

      if (getServletContext().getAttribute("ProtocolContainer") == null) {
        PropertyMap thePropertyMap = new PropertyMap();
        thePropertyMap.setProperty("routingprotocol.exchangedelay", "60");
        thePropertyMap.setProperty(
            "routingprotocol.persist", "true".equalsIgnoreCase(getInitParameter("persist")));

        Set<String> theSupportedProtocols = new HashSet<String>();
        theSupportedProtocols.add(RoutingProtocol.ID);
        theSupportedProtocols.add(EchoProtocol.ID);
        theSupportedProtocols.add(MessageProtocol.ID);
        theSupportedProtocols.add(AsyncMessageProcotol.ID);

        ProtocolContainer theProtocolContainer =
            new ProtocolContainer(new ProtocolFactory(thePropertyMap), theSupportedProtocols);

        getServletContext().setAttribute("ProtocolContainer", theProtocolContainer);
      }

      ServerInfo theServerInfo = new ServerInfo(Type.WEB);
      theServerInfo.setServerURL(getServletConfig().getInitParameter("serverurl"));

      getProtocolContainer().setServerInfo(theServerInfo);

      WebPeer theWebPeer =
          (WebPeer)
              ((RoutingProtocol) getProtocolContainer().getProtocol(RoutingProtocol.ID))
                  .getRoutingTable()
                  .getEntryForLocalPeer()
                  .getPeer();
      theWebPeer.setEndPointContainer(
          (EndPointContainer2) getServletContext().getAttribute("EndPoints"));

      try {
        RoutingProtocol theRoutingProtocol =
            (RoutingProtocol) getProtocolContainer().getProtocol(RoutingProtocol.ID);
        WebRoutingTableInspecter theInspector =
            new WebRoutingTableInspecter(getSessionData(), getPeerIpMap());
        theRoutingProtocol.setRoutingTableInspector(theInspector);
      } catch (Exception e) {
        LOGGER.error("Unable to get routingprotocol", e);
      }

    } catch (Exception e) {
      throw new ServletException("Could not init p2p servlet", e);
    }
  }
Ejemplo n.º 2
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();
    }
  }