Пример #1
0
  private void x_startTorrent() {
    boolean ok = _util.connect();
    if (!ok) fatal("Unable to connect to I2P");
    if (coordinator == null) {
      I2PServerSocket serversocket = _util.getServerSocket();
      if (serversocket == null) fatal("Unable to listen for I2P connections");
      else {
        Destination d = serversocket.getManager().getSession().getMyDestination();
        if (_log.shouldLog(Log.INFO))
          _log.info(
              "Listening on I2P destination "
                  + d.toBase64()
                  + " / "
                  + d.calculateHash().toBase64());
      }
      if (_log.shouldLog(Log.INFO))
        _log.info("Starting PeerCoordinator, ConnectionAcceptor, and TrackerClient");
      activity = "Collecting pieces";
      coordinator = new PeerCoordinator(_util, id, infoHash, meta, storage, this, this);
      coordinator.setUploaded(savedUploaded);
      if (_peerCoordinatorSet != null) {
        // multitorrent
        _peerCoordinatorSet.add(coordinator);
      } else {
        // single torrent
        acceptor = new ConnectionAcceptor(_util, new PeerAcceptor(coordinator));
      }
      // TODO pass saved closest DHT nodes to the tracker? or direct to the coordinator?
      trackerclient = new TrackerClient(_util, meta, additionalTrackerURL, coordinator, this);
    }
    // ensure acceptor is running when in multitorrent
    if (_peerCoordinatorSet != null && acceptor != null) {
      acceptor.startAccepting();
    }

    stopped = false;
    if (coordinator.halted()) {
      coordinator.restart();
      if (_peerCoordinatorSet != null) _peerCoordinatorSet.add(coordinator);
    }
    if (!trackerclient.started()) {
      trackerclient.start();
    } else if (trackerclient.halted()) {
      if (storage != null) {
        try {
          storage.reopen();
        } catch (IOException ioe) {
          try {
            storage.close();
          } catch (IOException ioee) {
            ioee.printStackTrace();
          }
          fatal("Could not reopen storage", ioe);
        }
      }
      trackerclient.start();
    } else {
      if (_log.shouldLog(Log.INFO)) _log.info("NOT starting TrackerClient???");
    }
  }
Пример #2
0
  @Override
  public void setUp() throws Exception {
    routedConnectionFactory =
        LimeTestUtils.createInjector().getInstance(RoutedConnectionFactory.class);

    ACCEPTOR = new ConnectionAcceptor();
    ACCEPTOR.start();
    ConnectionSettings.ALLOW_WHILE_DISCONNECTED.setValue(true);
    ConnectionSettings.PREFERENCING_ACTIVE.setValue(false);
  }
Пример #3
0
 public void testNonBlockingBadHandshake() throws Exception {
   ACCEPTOR.getObserver().setBadHandshake(true);
   RoutedConnection rc =
       routedConnectionFactory.createRoutedConnection("127.0.0.1", LISTEN_PORT, ConnectType.PLAIN);
   StubGnetConnectObserver observer = new StubGnetConnectObserver();
   rc.initialize(observer);
   observer.waitForResponse(10000);
   assertFalse(observer.isShutdown());
   assertTrue(observer.isBadHandshake());
   assertFalse(observer.isConnect());
   assertFalse(observer.isNoGOK());
 }
    public void delegate(final String word, final Socket sock, boolean newThread) {
      boolean localHost = NetworkUtils.isLocalHost(sock);
      boolean drop = false;
      if (localOnly && !localHost) drop = true;
      if (!localOnly && localHost && ConnectionSettings.LOCAL_IS_PRIVATE.getValue()) drop = true;

      if (drop) {
        IOUtils.close(sock);
        return;
      }

      if (blocking && newThread) {
        Runnable r =
            new Runnable() {
              public void run() {
                acceptor.acceptConnection(word, sock);
              }
            };
        ThreadFactory.startThread(r, "IncomingConnection");
      } else acceptor.acceptConnection(word, sock);
    }
Пример #5
0
  /**
   * Main operation of the Reactor:
   *
   * <UL>
   *   <LI>Uses the <CODE>Selector.select()</CODE> method to find new requests from clients
   *   <LI>For each request in the selection set:
   *       <UL>
   *         If it is <B>acceptable</B>, use the ConnectionAcceptor to accept it, create a new
   *         ConnectionHandler for it register it to the Selector
   *         <LI>If it is <B>readable</B>, use the ConnectionHandler to read it, extract messages
   *             and insert them to the ThreadPool
   *       </UL>
   */
  public void run() {
    // Create & start the ThreadPool
    ExecutorService executor = Executors.newFixedThreadPool(_poolSize);
    Selector selector = null;
    ServerSocketChannel ssChannel = null;

    try {
      selector = Selector.open();
      ssChannel = createServerSocket(_port);
    } catch (IOException e) {
      logger.info("cannot create the selector -- server socket is busy?");
      return;
    }

    _data = new ReactorData<T>(executor, selector, _protocolFactory, _tokenizerFactory);
    ConnectionAcceptor<T> connectionAcceptor = new ConnectionAcceptor<T>(ssChannel, _data);

    // Bind the server socket channel to the selector, with the new
    // acceptor as attachment

    try {
      ssChannel.register(selector, SelectionKey.OP_ACCEPT, connectionAcceptor);
    } catch (ClosedChannelException e) {
      logger.info("server channel seems to be closed!");
      return;
    }

    while (_shouldRun && selector.isOpen()) {
      // Wait for an event
      try {
        selector.select();
      } catch (IOException e) {
        logger.info("trouble with selector: " + e.getMessage());
        continue;
      }

      // Get list of selection keys with pending events
      Iterator<SelectionKey> it = selector.selectedKeys().iterator();

      // Process each key
      while (it.hasNext()) {
        // Get the selection key
        SelectionKey selKey = (SelectionKey) it.next();

        // Remove it from the list to indicate that it is being
        // processed. it.remove removes the last item returned by next.
        it.remove();

        // Check if it's a connection request
        if (selKey.isValid() && selKey.isAcceptable()) {
          logger.info("Accepting a connection");
          ConnectionAcceptor<T> acceptor = (ConnectionAcceptor<T>) selKey.attachment();
          try {
            acceptor.accept();
          } catch (IOException e) {
            logger.info("problem accepting a new connection: " + e.getMessage());
          }
          continue;
        }
        // Check if a message has been sent
        if (selKey.isValid() && selKey.isReadable()) {
          ConnectionHandler<T> handler = (ConnectionHandler<T>) selKey.attachment();
          logger.info("Channel is ready for reading");
          handler.read();
        }
        // Check if there are messages to send
        if (selKey.isValid() && selKey.isWritable()) {
          ConnectionHandler<T> handler = (ConnectionHandler<T>) selKey.attachment();
          logger.info("Channel is ready for writing");
          handler.write();
        }
      }
    }
    stopReactor();
  }
Пример #6
0
  @Override
  public void tearDown() throws Exception {
    ACCEPTOR.shutdown();

    Thread.sleep(1000);
  }
Пример #7
0
 /**
  * @return true if restarted
  * @since 0.8.4
  */
 public boolean restartAcceptor() {
   if (acceptor == null) return false;
   acceptor.restart();
   return true;
 }