예제 #1
0
 public boolean connectionEstablishedTo(Address address) {
   lock.lock();
   try {
     TCPConnection conn = conns.get(address);
     return conn != null && conn.isConnected();
   } finally {
     lock.unlock();
   }
 }
예제 #2
0
 /** Returns a connection with the given service host. */
 private TCPConnection getConnection(ServiceAddress address) throws IOException {
   TCPConnection c;
   synchronized (connection_pool) {
     c = connection_pool.get(address);
     // If there isn't, establish a connection,
     if (c == null) {
       c = new TCPConnection();
       c.connect(password, network_interface, address);
       connection_pool.put(address, c);
     } else {
       c.addLock();
     }
   }
   return c;
 }
예제 #3
0
 public TCPConnection getConnection(Address dest) throws Exception {
   TCPConnection conn = null;
   getLock().lock();
   try {
     conn = conns.get(dest);
     if (conn != null && conn.isOpen()) return conn;
     try {
       conn = new TCPConnection(dest);
       conn.start(getThreadFactory());
       addConnection(dest, conn);
       if (log.isTraceEnabled()) log.trace("created socket to " + dest);
     } catch (Exception ex) {
       if (log.isTraceEnabled()) log.trace("failed creating connection to " + dest);
     }
   } finally {
     getLock().unlock();
   }
   return conn;
 }
예제 #4
0
 /**
  * Acceptor thread. Continuously accept new connections. Create a new thread for each new
  * connection and put it in conns. When the thread should stop, it is interrupted by the thread
  * creator.
  */
 public void run() {
   while (!srv_sock.isClosed() && !Thread.currentThread().isInterrupted()) {
     TCPConnection conn = null;
     Socket client_sock = null;
     try {
       client_sock = srv_sock.accept();
       conn = new TCPConnection(client_sock);
       Address peer_addr = conn.getPeerAddress();
       mapper.getLock().lock();
       try {
         boolean currentConnectionOpen = mapper.hasOpenConnection(peer_addr);
         boolean replaceWithNewConnection = false;
         if (currentConnectionOpen) {
           replaceWithNewConnection = peer_addr.compareTo(local_addr) > 0;
         }
         if (!currentConnectionOpen || replaceWithNewConnection) {
           mapper.removeConnection(peer_addr);
           mapper.addConnection(peer_addr, conn);
           conn.start(mapper.getThreadFactory()); // starts handler thread on this socket
         } else {
           Util.close(conn);
         }
       } finally {
         mapper.getLock().unlock();
       }
     } catch (SocketException se) {
       boolean threadExiting = srv_sock.isClosed() || Thread.currentThread().isInterrupted();
       if (threadExiting) {
         break;
       } else {
         if (log.isWarnEnabled()) log.warn("Could not accept connection from peer ", se);
         Util.close(conn);
         Util.close(client_sock);
       }
     } catch (Exception ex) {
       if (log.isWarnEnabled()) log.warn("Could not read accept connection from peer " + ex);
       Util.close(conn);
       Util.close(client_sock);
     }
   }
   if (log.isTraceEnabled()) log.trace(Thread.currentThread().getName() + " terminated");
 }
예제 #5
0
  public void send(Address dest, byte[] data, int offset, int length) throws Exception {
    if (dest == null) {
      if (log.isErrorEnabled()) log.error("destination is null");
      return;
    }

    if (data == null) {
      log.warn("data is null; discarding packet");
      return;
    }

    if (!running.get()) {
      if (log.isDebugEnabled())
        log.debug("connection table is not running, discarding message to " + dest);
      return;
    }

    if (dest.equals(local_addr)) {
      receive(local_addr, data, offset, length);
      return;
    }

    // 1. Try to obtain correct Connection (or create one if not yet existent)
    TCPConnection conn;
    conn = mapper.getConnection(dest);

    // 2. Send the message using that connection
    if (conn != null) {
      try {
        conn.send(data, offset, length);
      } catch (Exception ex) {
        mapper.removeConnection(dest);
        throw ex;
      }
    }
  }
예제 #6
0
 /** Releases a connection from some obligation, therefore permitting it to be released. */
 private void releaseConnection(TCPConnection c) {
   synchronized (connection_pool) {
     c.removeLock();
   }
 }