/**
   * Connect this socket wrapper to remote server and start main loop on AprSocketSource stdout
   * link, to watch for incoming data, and AprSocketSink stdin link, to pull for outgoing data.
   */
  @Override
  public void connect(InetSocketAddress address) throws IOException {
    try {
      inetAddress =
          Address.info(address.getHostName(), Socket.APR_UNSPEC, address.getPort(), 0, pool);
      socket =
          Socket.create(
              Address.getInfo(inetAddress).family, Socket.SOCK_STREAM, Socket.APR_PROTO_TCP, pool);
    } catch (Exception e) {
      throw new IOException(
          "[" + this + "] ERROR: Cannot create socket for \"" + address + "\".", e);
    }

    int ret = Socket.connect(socket, inetAddress);
    if (ret != 0)
      throw new IOException(
          "["
              + this
              + "] ERROR: Cannot connect to remote host \""
              + address
              + "\": "
              + Error.strerror(ret));

    source.setSocket(socket);
    sink.setSocket(socket);

    // Start polling for data to send to remote sever
    runMainLoop(IN, STDIN, true, true);

    // Push incoming data from server to handlers
    runMainLoop(OUT, STDOUT, false, false);
  }
Exemplo n.º 2
0
 /**
  * transform an APR error number in a more fancy exception
  *
  * @param code APR error code
  * @throws java.io.IOException the produced exception for the given APR error number
  */
 private static void throwException(int code) throws IOException {
   throw new IOException(org.apache.tomcat.jni.Error.strerror(-code) + " (code: " + code + ")");
 }