use of io.vertx.core.net.NetClient in project vertx-tcp-eventbus-bridge by vert-x3.
the class TcpEventBusBridgeTest method testNoAddress.
@Test
public void testNoAddress(TestContext context) {
NetClient client = vertx.createNetClient();
final Async async = context.async();
final AtomicBoolean errorOnce = new AtomicBoolean(false);
final FrameParser parser = new FrameParser(parse -> {
context.assertTrue(parse.succeeded());
JsonObject frame = parse.result();
if (!errorOnce.compareAndSet(false, true)) {
context.fail("Client gets error message twice!");
} else {
context.assertEquals("err", frame.getString("type"));
context.assertEquals("missing_address", frame.getString("message"));
vertx.setTimer(200, l -> {
client.close();
async.complete();
});
}
});
client.connect(7000, "localhost", context.asyncAssertSuccess(socket -> {
socket.handler(parser);
FrameHelper.sendFrame("send", socket);
}));
}
use of io.vertx.core.net.NetClient in project vertx-tcp-eventbus-bridge by vert-x3.
the class TcpEventBusBridgeTest method testErrorReply.
@Test
public void testErrorReply(TestContext context) {
// Send a request and get a response
NetClient client = vertx.createNetClient();
final Async async = context.async();
vertx.eventBus().consumer("test", (Message<JsonObject> msg) -> {
msg.fail(0, "oops!");
});
client.connect(7000, "localhost", context.asyncAssertSuccess(socket -> {
final FrameParser parser = new FrameParser(parse -> {
context.assertTrue(parse.succeeded());
JsonObject frame = parse.result();
context.assertEquals("err", frame.getString("type"));
context.assertEquals("#backtrack", frame.getString("address"));
client.close();
async.complete();
});
socket.handler(parser);
FrameHelper.sendFrame("send", "test", "#backtrack", new JsonObject().put("value", "vert.x"), socket);
}));
}
use of io.vertx.core.net.NetClient in project vertx-examples by vert-x3.
the class StarwarsCommand method start.
@Override
public void start() throws Exception {
Command starwars = CommandBuilder.command("starwars").processHandler(process -> {
// Connect the client
NetClient client = process.vertx().createNetClient();
client.connect(23, "towel.blinkenlights.nl", ar -> {
if (ar.succeeded()) {
NetSocket socket = ar.result();
// Ctrl-C closes the socket
process.interruptHandler(v -> {
socket.close();
});
//
socket.handler(buff -> {
// Push the data to the Shell
process.write(buff.toString("UTF-8"));
}).exceptionHandler(err -> {
err.printStackTrace();
socket.close();
});
// When socket closes, end the command
socket.closeHandler(v -> {
process.end();
});
} else {
process.write("Could not connect to remote Starwars server\n").end();
}
});
}).build(vertx);
ShellService service = ShellService.create(vertx, new ShellServiceOptions().setTelnetOptions(new TelnetTermOptions().setHost("localhost").setPort(3000)));
CommandRegistry.getShared(vertx).registerCommand(starwars);
service.start(ar -> {
if (!ar.succeeded()) {
ar.cause().printStackTrace();
}
});
}
use of io.vertx.core.net.NetClient in project vert.x by eclipse.
the class DummyMetricsTest method testDummyNetClientMetrics.
@Test
public void testDummyNetClientMetrics() {
NetClient client = vertx.createNetClient(new NetClientOptions());
assertFalse(client.isMetricsEnabled());
}
use of io.vertx.core.net.NetClient in project vert.x by eclipse.
the class Socks4Proxy method start.
/**
* Start the server.
*
* @param vertx
* Vertx instance to use for creating the server and client
* @param finishedHandler
* will be called when the start has started
*/
@Override
public void start(Vertx vertx, Handler<Void> finishedHandler) {
NetServerOptions options = new NetServerOptions();
options.setHost("localhost").setPort(PORT);
server = vertx.createNetServer(options);
server.connectHandler(socket -> {
socket.handler(buffer -> {
if (!buffer.getBuffer(0, clientRequest.length()).equals(clientRequest)) {
throw new IllegalStateException("expected " + toHex(clientRequest) + ", got " + toHex(buffer));
}
log.debug("got request: " + toHex(buffer));
int port = buffer.getUnsignedShort(2);
String ip = getByte4(buffer.getBuffer(4, 8));
String authUsername = getString(buffer.getBuffer(8, buffer.length()));
if (username != null && !authUsername.equals(username)) {
log.debug("auth failed");
log.debug("writing: " + toHex(errorResponse));
socket.write(errorResponse);
socket.close();
} else {
String host;
if (ip.equals("0.0.0.1")) {
host = getString(buffer.getBuffer(9 + authUsername.length(), buffer.length()));
} else {
host = ip;
}
log.debug("connect: " + host + ":" + port);
socket.handler(null);
lastUri = host + ":" + port;
if (forceUri != null) {
host = forceUri.substring(0, forceUri.indexOf(':'));
port = Integer.valueOf(forceUri.substring(forceUri.indexOf(':') + 1));
}
log.debug("connecting to " + host + ":" + port);
NetClient netClient = vertx.createNetClient(new NetClientOptions());
netClient.connect(port, host, result -> {
if (result.succeeded()) {
log.debug("writing: " + toHex(connectResponse));
socket.write(connectResponse);
log.debug("connected, starting pump");
NetSocket clientSocket = result.result();
socket.closeHandler(v -> clientSocket.close());
clientSocket.closeHandler(v -> socket.close());
Pump.pump(socket, clientSocket).start();
Pump.pump(clientSocket, socket).start();
} else {
log.error("exception", result.cause());
socket.handler(null);
log.debug("writing: " + toHex(errorResponse));
socket.write(errorResponse);
socket.close();
}
});
}
});
});
server.listen(result -> {
log.debug("socks4a server started");
finishedHandler.handle(null);
});
}
Aggregations