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