use of org.vertx.java.core.net.NetSocket 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());
}
}
});
}
use of org.vertx.java.core.net.NetSocket 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();
}
}
Aggregations