Example #1
0
  public Connection getConnection() throws NoServersAvailableException {
    if (goodConnectionList.size() == 0) {
      checkDeadConnections();
    }

    synchronized (connectionLock) {
      Connection connection = null;

      while (goodConnectionList.size() > 0) {
        // Simple round-robin for now, get the first one, if it fails, remove it
        // Otherwise, increment the counter to the next connection's index
        if (connectionIndex.get() >= goodConnectionList.size()) {
          connectionIndex.set(0);
        }

        connection = goodConnectionList.get(connectionIndex.getAndIncrement());

        if (connection.isHealthy()) {
          return connection;
        } else {
          LOG.warn("Connection to " + connection.toString() + " is unhealthy, marking as bad.");
          badConnectionList.add(connection);
          goodConnectionList.remove(connection);
        }
      }
    }

    throw new NoServersAvailableException();
  }
Example #2
0
 public void checkDeadConnections() {
   synchronized (connectionLock) {
     if (badConnectionList.size() != 0) {
       for (Iterator<Connection> it = badConnectionList.iterator(); it.hasNext(); ) {
         Connection c = it.next();
         if (c.isHealthy()) {
           LOG.info("Connection " + c.toString() + " is good again!");
           goodConnectionList.add(c);
           it.remove();
         }
       }
     }
   }
 }
Example #3
0
  private boolean isValid(final Connection connection) {
    if (connection == null) {
      return false;
    }

    if (status != PwmService.STATUS.OPEN) {
      return false;
    }

    try {
      final Method getFreeSpaceMethod = File.class.getMethod("isValid");
      final Object rawResult = getFreeSpaceMethod.invoke(connection, 10);
      return (Boolean) rawResult;
    } catch (NoSuchMethodException e) {
      /* no error, pre java 1.6 doesn't have this method */
    } catch (Exception e) {
      LOGGER.debug(
          "error checking for isValid for " + connection.toString() + ",: " + e.getMessage());
    }

    final StringBuilder sb = new StringBuilder();
    sb.append("SELECT * FROM ")
        .append(DatabaseTable.PWM_META.toString())
        .append(" WHERE " + KEY_COLUMN + " = ?");
    PreparedStatement statement = null;
    ResultSet resultSet = null;
    try {
      statement = connection.prepareStatement(sb.toString());
      statement.setString(1, KEY_ENGINE_START_PREFIX + instanceID);
      statement.setMaxRows(1);
      resultSet = statement.executeQuery();
      if (resultSet.next()) {
        resultSet.getString(VALUE_COLUMN);
      }
    } catch (SQLException e) {
      final ErrorInformation errorInformation =
          new ErrorInformation(
              PwmError.ERROR_DB_UNAVAILABLE, "isValid operation failed: " + e.getMessage());
      lastError = errorInformation;
      LOGGER.error(errorInformation.toDebugStr());
      return false;
    } finally {
      close(statement);
      close(resultSet);
    }
    return true;
  }
 /*
  * Handle websocket open a connection
  * (non-Javadoc)
  * @see org.eclipse.jetty.websocket.WebSocket#onOpen(org.eclipse.jetty.websocket.WebSocket.Connection)
  */
 @Override
 public void onOpen(Connection connection) {
   this.connection = connection;
   // this.connection.setMaxIdleTime(5*60*1000);
   this.connection.setMaxIdleTime(0);
   System.out.println("onOpen: " + connection + "|Time: " + new Date());
   try {
     connection.sendMessage("onOpen: " + connection);
     CopyOnWriteArrayList<CommunicationWebsocketBusiness> a =
         new CopyOnWriteArrayList<CommunicationWebsocketBusiness>();
     a.add(this);
     CommunicationWebsocketServlet.connections.put(connection.toString(), a);
   } catch (IOException e) {
     this.connection.close();
   } catch (Exception ex) {
     this.connection.close();
   }
 }
Example #5
0
  /** Marks a flag in the ConnectionObject to indicate this connection is no longer in use */
  public synchronized void removeConnection(Connection c) {
    if (c == null) return;

    ConnectionObject co = null;
    for (int i = 0; i < connections.size(); i++) {
      co = (ConnectionObject) connections.elementAt(i);
      if (c == co.connection) {
        try {
          c.close();
          connections.removeElementAt(i);
          trace("Removed " + c.toString());
        } catch (Exception e) {
          e.printStackTrace();
        }

        break;
      }
    }
  }
Example #6
0
  private void receiveUnknownCon(Packet packet, long sendId, boolean queueIfNoConn) {
    if (packet.isFlagSet(Packet.FLAG_ECHO)) {
      if (packet.getSendStreamId() > 0) {
        if (_manager.answerPings()) receivePing(packet);
        else if (_log.shouldLog(Log.WARN))
          _log.warn("Dropping Echo packet on unknown con: " + packet);
      } else if (packet.getReceiveStreamId() > 0) {
        receivePong(packet);
      } else {
        if (_log.shouldLog(Log.WARN))
          _log.warn("Echo packet received with no stream IDs: " + packet);
      }
      packet.releasePayload();
    } else {
      if (_log.shouldLog(Log.WARN) && !packet.isFlagSet(Packet.FLAG_SYNCHRONIZE))
        _log.warn("Packet received on an unknown stream (and not an ECHO or SYN): " + packet);
      if (sendId <= 0) {
        Connection con = _manager.getConnectionByOutboundId(packet.getReceiveStreamId());
        if (con != null) {
          if ((con.getHighestAckedThrough() <= 5) && (packet.getSequenceNum() <= 5)) {
            if (_log.shouldLog(Log.WARN))
              _log.warn(
                  "Received additional packet w/o SendStreamID after the syn on "
                      + con
                      + ": "
                      + packet);
            receiveKnownCon(con, packet);
            return;
          } else {
            if (_log.shouldLog(Log.WARN))
              _log.warn(
                  "hrmph, received while ack of syn was in flight on "
                      + con
                      + ": "
                      + packet
                      + " acked: "
                      + con.getAckedPackets());
            // allow unlimited packets without a SendStreamID for now
            receiveKnownCon(con, packet);
            return;
          }
        }
      }

      if (packet.isFlagSet(Packet.FLAG_SYNCHRONIZE)) {
        // logTCPDump() will be called in ConnectionManager.receiveConnection(),
        // which is called by ConnectionHandler.receiveNewSyn(),
        // after we have a new conn, which makes the logging better.
        _manager.getConnectionHandler().receiveNewSyn(packet);
      } else if (queueIfNoConn) {
        // don't call logTCPDump() here, wait for it to find a conn

        // We can get here on the 2nd+ packet if the 1st (SYN) packet
        // is still on the _synQueue in the ConnectionHandler, and
        // ConnectionManager.receiveConnection() hasn't run yet to put
        // the StreamID on the getConnectionByOutboundId list.
        // Then the 2nd packet gets discarded and has to be retransmitted.
        //
        // We fix this by putting this packet on the syn queue too!
        // Then ConnectionHandler.accept() will check the connection list
        // and call receivePacket() above instead of receiveConnection().
        if (_log.shouldLog(Log.WARN)) {
          _log.warn("Packet belongs to no other cons, putting on the syn queue: " + packet);
        }
        if (_log.shouldLog(Log.DEBUG)) {
          StringBuilder buf = new StringBuilder(128);
          for (Connection con : _manager.listConnections()) {
            buf.append(con.toString()).append(" ");
          }
          _log.debug(
              "connections: "
                  + buf.toString()
                  + " sendId: "
                  + (sendId > 0 ? Packet.toId(sendId) : " unknown"));
        }
        // packet.releasePayload();
        _manager.getConnectionHandler().receiveNewSyn(packet);
      } else {
        // log it here, just before we kill it - dest will be unknown
        if (I2PSocketManagerFull.pcapWriter != null
            && _context.getBooleanProperty(I2PSocketManagerFull.PROP_PCAP)) packet.logTCPDump(null);
        // don't queue again (infinite loop!)
        sendReset(packet);
        packet.releasePayload();
      }
    }
  }
 @Override
 public String toString() {
   return delegate.toString();
 }
Example #8
0
File: Call.java Project: vozon/DTF
 @Override
 public String toString() {
   return rpcRequest.toString() + " from " + connection.toString();
 }
Example #9
0
 @Override
 public String toString() {
   return param.toString() + " from " + connection.toString();
 }
 public String toString() {
   return connection.toString();
 }