Package org.xsocket.connection

Examples of org.xsocket.connection.INonBlockingConnection


   
    if ((isSSL == true) && !isSSLSupported) {
      throw new IOException("ssl connection are not supported (use pool sslContext parameter constructor)");
    }
   
    INonBlockingConnection tcpConnection = null;
   
    if (isPooled) {
      try {
        tcpConnection = pool.getNonBlockingConnection(host, port, isSSL);
       
View Full Code Here


    if (idx != -1) {
      forwardPort = Integer.parseInt(host.substring(idx + 1, host.length()));
      forwardHost = host.substring(0, idx);
    }

    INonBlockingConnection con = new NonBlockingConnection(securedProxyHost, securedProxyPort, sslContext, false);
    con.setHandler(new DataHandler(exchange));

   
    StringBuilder sb = new StringBuilder();
   
    sb.append("CONNECT " + forwardHost + ":" + forwardPort + " HTTP/1.1\r\n" +
          "Host: " + host + "\r\n" +
          "User-Agent: xLightweb/" + HttpUtils.getImplementationVersion() + "\r\n");
   
   
    if (proxyUser != null) {
      if (proxyUserPassword != null) {
        sb.append("Proxy-Authorization: Basic " + proxyUserPassword + "\r\n");
       
      } else {
        exchange.sendError(401, "proxy user password is not set (hint: usage <HttpClient>.setProxyPassword(...)");
        return;
      }
    }

    sb.append("Proxy-Connection: keep-alive\r\n" +
          "\r\n");
   
    con.write(sb.toString());
  }
View Full Code Here

   
    NonBlockingConnectionPool pool = new NonBlockingConnectionPool();
   
    for (int i = 0; i < 5; i++) {
     
      INonBlockingConnection con = pool.getNonBlockingConnection("localhost", server.getLocalPort());
      HttpClientConnection httpCon = new HttpClientConnection(con);
     
      IHttpResponse response =  httpCon.call(new GetRequest("http://localhost:" + server.getLocalPort()+ "/"));
      Assert.assertEquals(200, response.getStatus());
      Assert.assertEquals("12", response.getBody().readString());
    }
   
    QAUtil.sleep(1000);
    Assert.assertEquals(0, pool.getNumActive());
    Assert.assertEquals(5, pool.getNumDestroyed());
   
   
    INonBlockingConnection con = pool.getNonBlockingConnection("localhost", server.getLocalPort());
    HttpClientConnection httpCon = new HttpClientConnection(con);
    IHttpResponse response =  httpCon.call(new GetRequest("http://localhost:" + server.getLocalPort()+ "/"));
   
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals("12", response.getBody().readString());
View Full Code Here

     * @param targetHost       the target host
     * @param targetPort       the target port
     * @throws IOException if an exception occurs
     */
    public static void establishTcpTunnel(IHttpConnection httpConnection, String targetHost, int targetPort) throws IOException {
        INonBlockingConnection forwardCon = new NonBlockingConnection(targetHost, targetPort);
       
        INonBlockingConnection tcpCon = httpConnection.getUnderlyingTcpConnection();

        forwardCon.setAttachment(tcpCon);
        tcpCon.setAttachment(forwardCon);
           
        forwardCon.setFlushmode(FlushMode.ASYNC);
        forwardCon.setAutoflush(false);
        tcpCon.setFlushmode(FlushMode.ASYNC);
        tcpCon.setAutoflush(false);

        forwardCon.setHandler(new TcpProxyHandler());
        tcpCon.setHandler(new TcpProxyHandler());
    }
View Full Code Here

   
   
    private static class TcpProxyHandler implements IDataHandler, IDisconnectHandler {
       
        public boolean onDisconnect(INonBlockingConnection connection) throws IOException {
            INonBlockingConnection reverseConnection = (INonBlockingConnection) connection.getAttachment();
            if (reverseConnection != null) {
                connection.setAttachment(null);
                NonBlockingConnectionPool.destroy(reverseConnection);
            }
            return true;
View Full Code Here

            return true;
        }
       
       
        public boolean onData(INonBlockingConnection connection) throws IOException, BufferUnderflowException, MaxReadSizeExceededException {
            INonBlockingConnection forwardConnection = (INonBlockingConnection) connection.getAttachment();
               
            int available = connection.available();
            if (available > 0) {
                ByteBuffer[] data = connection.readByteBufferByLength(connection.available());
                forwardConnection.write(data);
                forwardConnection.flush();
               
            } else if (available == -1) {
                NonBlockingConnectionPool.destroy(connection);
            }
           
View Full Code Here

        public void onComplete() throws IOException {
            try {
                int available = ds.available();
                ByteBuffer[] data = ds.readByteBufferByLength(available);
               
                INonBlockingConnection con = registry.retrieveTcpConnection(channelId, 1000);
               
                IDataHandler dh = new IDataHandler() {
                  public boolean onData(INonBlockingConnection connection) throws IOException {
                        connection.resetToReadMark();
                        connection.markReadPosition();
                        int length = connection.readInt();
                        ByteBuffer[] data = connection.readByteBufferByLength(length);
                        connection.removeReadMark();
                       
                        registry.addTcpConnection(channelId, connection);
                        exchange.send(new HttpResponse(200, "text/plain", data));
                        return false;
                   
                };
                con.setHandler(dh);
               
                con.write(available);
                con.write(data);
               
            } catch (SocketTimeoutException ste) {
                exchange.sendError(404);
            }
           
View Full Code Here

                    } catch (InterruptedException ignore) { }
                } else {
                    if (cons.isEmpty()) {
                        openConnections.remove(channelId);
                    } else {
                        INonBlockingConnection con = cons.remove(getRandomId(cons.size() - 1));
                        if (cons.isEmpty()) {
                            openConnections.remove(channelId);
                        }
                        return con;
                    }
View Full Code Here

        public void onComplete() throws IOException {
            try {
                int available = ds.available();
                ByteBuffer[] data = ds.readByteBufferByLength(available);
               
                INonBlockingConnection con = registry.retrieveTcpConnection(channelId, 1000);
               
                IDataHandler dh = new IDataHandler() {
                  public boolean onData(INonBlockingConnection connection) throws IOException {
                        connection.resetToReadMark();
                        connection.markReadPosition();
                        int length = connection.readInt();
                        ByteBuffer[] data = connection.readByteBufferByLength(length);
                        connection.removeReadMark();
                       
                        registry.addTcpConnection(channelId, connection);
                        exchange.send(new HttpResponse(200, "text/plain", data));
                        return false;
                   
                };
                con.setHandler(dh);
               
                con.write(available);
                con.write(data);
               
            } catch (SocketTimeoutException ste) {
                exchange.sendError(404);
            }
           
View Full Code Here

                    } catch (InterruptedException ignore) { }
                } else {
                    if (cons.isEmpty()) {
                        openConnections.remove(channelId);
                    } else {
                        INonBlockingConnection con = cons.remove(getRandomId(cons.size() - 1));
                        if (cons.isEmpty()) {
                            openConnections.remove(channelId);
                        }
                        return con;
                    }
View Full Code Here

TOP

Related Classes of org.xsocket.connection.INonBlockingConnection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.