Search in sources :

Example 1 with NetClient

use of org.vertx.java.core.net.NetClient in project fabric8 by jboss-fuse.

the class DetectingGateway method createClient.

/**
 * Creates a new client for the given URL and handler
 */
private NetClient createClient(final ConnectionParameters params, final SocketWrapper socketFromClient, final URI url, final Buffer received) {
    final NetClient netClient = vertx.createNetClient();
    socketFromClient.readStream().pause();
    return netClient.connect(url.getPort(), url.getHost(), new Handler<AsyncResult<NetSocket>>() {

        public void handle(final AsyncResult<NetSocket> asyncSocket) {
            if (!asyncSocket.succeeded()) {
                handleConnectFailure(socketFromClient, String.format("Could not connect to '%s'", url));
            } else {
                socketFromClient.readStream().resume();
                final NetSocket socketToServer = asyncSocket.result();
                successfulConnectionAttempts.incrementAndGet();
                boolean removed = socketsConnecting.remove(socketFromClient);
                assert removed;
                final ConnectedSocketInfo connectedInfo = new ConnectedSocketInfo(params, url, socketFromClient, netClient);
                boolean added = socketsConnected.add(connectedInfo);
                assert added;
                Handler<Void> endHandler = new Handler<Void>() {

                    @Override
                    public void handle(Void event) {
                        handleShutdown(connectedInfo);
                    }
                };
                Handler<Throwable> exceptionHandler = new Handler<Throwable>() {

                    @Override
                    public void handle(Throwable event) {
                        handleShutdown(connectedInfo);
                    }
                };
                socketFromClient.readStream().endHandler(endHandler);
                socketFromClient.readStream().exceptionHandler(exceptionHandler);
                socketToServer.endHandler(endHandler);
                socketToServer.exceptionHandler(exceptionHandler);
                if (LOG.isTraceEnabled()) {
                    LOG.trace("Sending out to destination socket: {}", received);
                }
                socketToServer.write(received);
                Pump.createPump(socketToServer, socketFromClient.writeStream()).start();
                Pump.createPump(socketFromClient.readStream(), socketToServer).start();
                LOG.debug("socketFromClient {} has been connected to socketToServer {}", socketFromClient.remoteAddress(), socketToServer.remoteAddress());
            }
        }
    });
}
Also used : NetSocket(org.vertx.java.core.net.NetSocket) NetClient(org.vertx.java.core.net.NetClient) Handler(org.vertx.java.core.Handler) AsyncResult(org.vertx.java.core.AsyncResult)

Example 2 with NetClient

use of org.vertx.java.core.net.NetClient in project fabric8 by jboss-fuse.

the class TcpGatewayHandler method handle.

@Override
public void handle(final NetSocket socket) {
    NetClient client = null;
    List<String> paths = serviceMap.getPaths();
    TcpClientRequestFacade requestFacade = new TcpClientRequestFacade(socket);
    String path = pathLoadBalancer.choose(paths, requestFacade);
    if (path != null) {
        List<ServiceDetails> services = serviceMap.getServices(path);
        if (!services.isEmpty()) {
            ServiceDetails serviceDetails = serviceLoadBalancer.choose(services, requestFacade);
            if (serviceDetails != null) {
                List<String> urlStrings = serviceDetails.getServices();
                for (String urlString : urlStrings) {
                    if (Strings.notEmpty(urlString)) {
                        // lets create a client for this request...
                        try {
                            URI uri = new URI(urlString);
                            // URL url = new URL(urlString);
                            String urlProtocol = uri.getScheme();
                            if (Objects.equal(protocol, urlProtocol)) {
                                Handler<AsyncResult<NetSocket>> handler = new Handler<AsyncResult<NetSocket>>() {

                                    public void handle(final AsyncResult<NetSocket> asyncSocket) {
                                        socket.resume();
                                        NetSocket clientSocket = asyncSocket.result();
                                        Pump.createPump(clientSocket, socket).start();
                                        Pump.createPump(socket, clientSocket).start();
                                    }
                                };
                                client = createClient(socket, uri, handler);
                                break;
                            }
                        } catch (MalformedURLException e) {
                            LOG.warn("Failed to parse URL: " + urlString + ". " + e, e);
                        } catch (URISyntaxException e) {
                            LOG.warn("Failed to parse URI: " + urlString + ". " + e, e);
                        }
                    }
                }
            }
        }
    }
    if (client == null) {
        // fail to route
        LOG.info("No service available for protocol " + protocol + " for paths " + paths);
        socket.close();
    }
}
Also used : NetSocket(org.vertx.java.core.net.NetSocket) MalformedURLException(java.net.MalformedURLException) Handler(org.vertx.java.core.Handler) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) NetClient(org.vertx.java.core.net.NetClient) ServiceDetails(io.fabric8.gateway.ServiceDetails) AsyncResult(org.vertx.java.core.AsyncResult)

Example 3 with NetClient

use of org.vertx.java.core.net.NetClient in project fabric8 by jboss-fuse.

the class TcpGatewayHandler method createClient.

/**
 * Creates a new client for the given URL and handler
 */
protected NetClient createClient(NetSocket socket, URI url, Handler<AsyncResult<NetSocket>> handler) throws MalformedURLException {
    NetClient client = vertx.createNetClient();
    int port = url.getPort();
    String host = url.getHost();
    LOG.info("Connecting " + socket.remoteAddress() + " to host " + host + " port " + port + " protocol " + protocol);
    socket.pause();
    return client.connect(port, host, handler);
}
Also used : NetClient(org.vertx.java.core.net.NetClient)

Example 4 with NetClient

use of org.vertx.java.core.net.NetClient in project fabric8 by jboss-fuse.

the class DetectingGateway method route.

public void route(final SocketWrapper socket, ConnectionParameters params, final Buffer received) {
    NetClient client = null;
    if (params.protocolVirtualHost == null) {
        params.protocolVirtualHost = defaultVirtualHost;
    }
    HashSet<String> schemes = new HashSet<String>(Arrays.asList(params.protocolSchemes));
    if (params.protocolVirtualHost != null) {
        List<ServiceDetails> services = serviceMap.getServices(params.protocolVirtualHost);
        // Lets try again with the defaultVirtualHost
        if (services.isEmpty() && !params.protocolVirtualHost.equals(defaultVirtualHost)) {
            params.protocolVirtualHost = defaultVirtualHost;
            services = serviceMap.getServices(params.protocolVirtualHost);
        }
        LOG.debug(String.format("%d services match the virtual host", services.size()));
        if (!services.isEmpty()) {
            ClientRequestFacade clientRequestFacade = clientRequestFacadeFactory.create(socket, params);
            ServiceDetails serviceDetails = serviceLoadBalancer.choose(services, clientRequestFacade);
            if (serviceDetails != null) {
                List<String> urlStrings = serviceDetails.getServices();
                LOG.debug("Selected service exposes the following URLS: {}", urlStrings);
                for (String urlString : urlStrings) {
                    if (Strings.notEmpty(urlString)) {
                        // lets create a client for this request...
                        try {
                            URI uri = new URI(urlString);
                            // URL url = new URL(urlString);
                            String urlProtocol = uri.getScheme();
                            if (schemes.contains(urlProtocol)) {
                                if (!socket.remoteAddress().toString().equals(clientRequestFacade.getClientRequestKey())) {
                                    LOG.info(String.format("Connecting client from '%s' (with key '%s') requesting virtual host '%s' to '%s:%d' using the %s protocol", socket.remoteAddress(), clientRequestFacade.getClientRequestKey(), params.protocolVirtualHost, uri.getHost(), uri.getPort(), params.protocol));
                                } else {
                                    LOG.info(String.format("Connecting client from '%s' requesting virtual host '%s' to '%s:%d' using the %s protocol", socket.remoteAddress(), params.protocolVirtualHost, uri.getHost(), uri.getPort(), params.protocol));
                                }
                                client = createClient(params, socket, uri, received);
                                break;
                            }
                        } catch (URISyntaxException e) {
                            LOG.warn("Failed to parse URI: " + urlString + ". " + e, e);
                        }
                    }
                }
            }
        }
    }
    if (client == null) {
        // failed to route
        handleConnectFailure(socket, String.format("No endpoint available for virtual host '%s' and protocol %s", params.protocolVirtualHost, params.protocol));
    }
}
Also used : NetClient(org.vertx.java.core.net.NetClient) ServiceDetails(io.fabric8.gateway.ServiceDetails) ClientRequestFacade(io.fabric8.gateway.loadbalancer.ClientRequestFacade) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Aggregations

NetClient (org.vertx.java.core.net.NetClient)4 ServiceDetails (io.fabric8.gateway.ServiceDetails)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 AsyncResult (org.vertx.java.core.AsyncResult)2 Handler (org.vertx.java.core.Handler)2 NetSocket (org.vertx.java.core.net.NetSocket)2 ClientRequestFacade (io.fabric8.gateway.loadbalancer.ClientRequestFacade)1 MalformedURLException (java.net.MalformedURLException)1