Search in sources :

Example 26 with NetClient

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);
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) RunWith(org.junit.runner.RunWith) Vertx(io.vertx.core.Vertx) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Message(io.vertx.core.eventbus.Message) Test(org.junit.Test) BridgeOptions(io.vertx.ext.bridge.BridgeOptions) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) NetServerOptions(io.vertx.core.net.NetServerOptions) FrameParser(io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameParser) FrameHelper(io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameHelper) After(org.junit.After) BridgeEventType(io.vertx.ext.bridge.BridgeEventType) JsonObject(io.vertx.core.json.JsonObject) Handler(io.vertx.core.Handler) NetClient(io.vertx.core.net.NetClient) PermittedOptions(io.vertx.ext.bridge.PermittedOptions) Before(org.junit.Before) NetSocket(io.vertx.core.net.NetSocket) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NetClient(io.vertx.core.net.NetClient) Async(io.vertx.ext.unit.Async) JsonObject(io.vertx.core.json.JsonObject) FrameParser(io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameParser) Test(org.junit.Test)

Example 27 with NetClient

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);
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) RunWith(org.junit.runner.RunWith) Vertx(io.vertx.core.Vertx) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Message(io.vertx.core.eventbus.Message) Test(org.junit.Test) BridgeOptions(io.vertx.ext.bridge.BridgeOptions) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) NetServerOptions(io.vertx.core.net.NetServerOptions) FrameParser(io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameParser) FrameHelper(io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameHelper) After(org.junit.After) BridgeEventType(io.vertx.ext.bridge.BridgeEventType) JsonObject(io.vertx.core.json.JsonObject) Handler(io.vertx.core.Handler) NetClient(io.vertx.core.net.NetClient) PermittedOptions(io.vertx.ext.bridge.PermittedOptions) Before(org.junit.Before) NetSocket(io.vertx.core.net.NetSocket) NetClient(io.vertx.core.net.NetClient) Message(io.vertx.core.eventbus.Message) Async(io.vertx.ext.unit.Async) JsonObject(io.vertx.core.json.JsonObject) FrameParser(io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameParser) Test(org.junit.Test)

Example 28 with NetClient

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();
        }
    });
}
Also used : TelnetTermOptions(io.vertx.ext.shell.term.TelnetTermOptions) Command(io.vertx.ext.shell.command.Command) ShellServiceOptions(io.vertx.ext.shell.ShellServiceOptions) CommandRegistry(io.vertx.ext.shell.command.CommandRegistry) AbstractVerticle(io.vertx.core.AbstractVerticle) ShellService(io.vertx.ext.shell.ShellService) CommandBuilder(io.vertx.ext.shell.command.CommandBuilder) NetClient(io.vertx.core.net.NetClient) Runner(io.vertx.example.util.Runner) NetSocket(io.vertx.core.net.NetSocket) NetSocket(io.vertx.core.net.NetSocket) ShellService(io.vertx.ext.shell.ShellService) NetClient(io.vertx.core.net.NetClient) TelnetTermOptions(io.vertx.ext.shell.term.TelnetTermOptions) Command(io.vertx.ext.shell.command.Command) ShellServiceOptions(io.vertx.ext.shell.ShellServiceOptions)

Example 29 with NetClient

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());
}
Also used : NetClientOptions(io.vertx.core.net.NetClientOptions) NetClient(io.vertx.core.net.NetClient) Test(org.junit.Test)

Example 30 with NetClient

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);
    });
}
Also used : NetSocket(io.vertx.core.net.NetSocket) NetClientOptions(io.vertx.core.net.NetClientOptions) NetClient(io.vertx.core.net.NetClient) NetServerOptions(io.vertx.core.net.NetServerOptions)

Aggregations

NetClient (io.vertx.core.net.NetClient)52 NetSocket (io.vertx.core.net.NetSocket)35 Test (org.junit.Test)34 NetServerOptions (io.vertx.core.net.NetServerOptions)24 Vertx (io.vertx.core.Vertx)23 JsonObject (io.vertx.core.json.JsonObject)21 Async (io.vertx.ext.unit.Async)20 Handler (io.vertx.core.Handler)19 FrameParser (io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameParser)19 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)19 NetClientOptions (io.vertx.core.net.NetClientOptions)17 BridgeOptions (io.vertx.ext.bridge.BridgeOptions)16 PermittedOptions (io.vertx.ext.bridge.PermittedOptions)16 Before (org.junit.Before)16 Message (io.vertx.core.eventbus.Message)15 BridgeEventType (io.vertx.ext.bridge.BridgeEventType)15 FrameHelper (io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameHelper)15 TestContext (io.vertx.ext.unit.TestContext)15 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)15 After (org.junit.After)15