Example #1
0
  public void connect(String host, int port) throws Exception {
    //
    if (!this.connected.compareAndSet(false, true)) {
      return;
    }

    //
    if (isVerbose() && LOGGER.isInfoEnabled()) {
      LOGGER.info("connecting to host: {}, port: {}", host, port);
    }

    //
    this.socket = this.socketFactory.create(host, port);
    this.os = new TransportOutputStreamImpl(this.socket.getOutputStream());
    if (this.level2BufferSize <= 0) {
      this.is = new TransportInputStreamImpl(this.socket.getInputStream(), this.level1BufferSize);
    } else {
      this.is =
          new TransportInputStreamImpl(
              new ActiveBufferedInputStream(this.socket.getInputStream(), this.level2BufferSize),
              this.level1BufferSize);
    }

    //
    final Packet packet = this.is.readPacket();
    if (packet.getPacketBody()[0] == ErrorPacket.PACKET_MARKER) {
      final ErrorPacket error = ErrorPacket.valueOf(packet);
      LOGGER.info(
          "failed to connect to host: {}, port: {}, error", new Object[] {host, port, error});
      throw new TransportException(error);
    } else {
      //
      final GreetingPacket greeting = GreetingPacket.valueOf(packet);
      this.context.setServerHost(host);
      this.context.setServerPort(port);
      this.context.setServerStatus(greeting.getServerStatus());
      this.context.setServerVersion(greeting.getServerVersion().toString());
      this.context.setServerCollation(greeting.getServerCollation());
      this.context.setServerCapabilities(greeting.getServerCapabilities());
      this.context.setThreadId(greeting.getThreadId());
      this.context.setProtocolVersion(greeting.getProtocolVersion());
      this.context.setScramble(
          greeting.getScramble1().toString() + greeting.getScramble2().toString());

      //
      if (isVerbose() && LOGGER.isInfoEnabled()) {
        LOGGER.info(
            "connected to host: {}, port: {}, context: {}",
            new Object[] {host, port, this.context});
      }
    }

    //
    this.authenticator.login(this);
  }
 public TransportException(ErrorPacket ep) {
   super(ep.getErrorMessage().toString());
   this.errorCode = ep.getErrorCode();
   this.sqlState = ep.getSqlState().toString();
   this.errorMessage = ep.getErrorMessage().toString();
 }