Search in sources :

Example 1 with FrameParser

use of io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameParser in project vertx-tcp-eventbus-bridge by vert-x3.

the class TcpEventBusBridgeInteropTest method testSendMessageWithReplyBacktrack.

@Test
public void testSendMessageWithReplyBacktrack(TestContext context) {
    // Send a request and get a response
    NetClient client = vertx.createNetClient();
    final Async async = context.async();
    client.connect(7000, "localhost", conn -> {
        context.assertFalse(conn.failed());
        NetSocket socket = conn.result();
        final FrameParser parser = new FrameParser(parse -> {
            context.assertTrue(parse.succeeded());
            JsonObject frame = parse.result();
            context.assertNotEquals("err", frame.getString("type"));
            context.assertEquals("Hello vert.x", frame.getJsonObject("body").getString("value"));
            client.close();
            async.complete();
        });
        socket.handler(parser);
        FrameHelper.sendFrame("send", "hello", "#backtrack", new JsonObject().put("value", "vert.x"), socket);
    });
}
Also used : NetSocket(io.vertx.core.net.NetSocket) 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 2 with FrameParser

use of io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameParser in project vertx-tcp-eventbus-bridge by vert-x3.

the class TcpEventBusBridgeInteropTest method testRegister.

@Test
public void testRegister(TestContext context) {
    // Send a request and get a response
    NetClient client = vertx.createNetClient();
    final Async async = context.async();
    client.connect(7000, "localhost", conn -> {
        context.assertFalse(conn.failed());
        NetSocket socket = conn.result();
        // 1 reply will arrive
        // MESSAGE for echo
        final FrameParser parser = new FrameParser(parse -> {
            context.assertTrue(parse.succeeded());
            JsonObject frame = parse.result();
            context.assertNotEquals("err", frame.getString("type"));
            context.assertEquals("Vert.x", frame.getJsonObject("body").getString("value"));
            client.close();
            async.complete();
        });
        socket.handler(parser);
        FrameHelper.sendFrame("register", "echo", null, socket);
        // now try to publish a message so it gets delivered both to the consumer registred on the startup and to this
        // remote consumer
        FrameHelper.sendFrame("publish", "echo", new JsonObject().put("value", "Vert.x"), socket);
    });
}
Also used : NetSocket(io.vertx.core.net.NetSocket) 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 3 with FrameParser

use of io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameParser in project vertx-tcp-eventbus-bridge by vert-x3.

the class TcpEventBusBridgeTest method testRegister.

@Test
public void testRegister(TestContext context) {
    // Send a request and get a response
    NetClient client = vertx.createNetClient();
    final Async async = context.async();
    client.connect(7000, "localhost", conn -> {
        context.assertFalse(conn.failed());
        NetSocket socket = conn.result();
        // 1 reply will arrive
        // MESSAGE for echo
        final FrameParser parser = new FrameParser(parse -> {
            context.assertTrue(parse.succeeded());
            JsonObject frame = parse.result();
            context.assertNotEquals("err", frame.getString("type"));
            context.assertEquals(false, frame.getBoolean("send"));
            context.assertEquals("Vert.x", frame.getJsonObject("body").getString("value"));
            client.close();
            async.complete();
        });
        socket.handler(parser);
        FrameHelper.sendFrame("register", "echo", null, socket);
        // now try to publish a message so it gets delivered both to the consumer registred on the startup and to this
        // remote consumer
        FrameHelper.sendFrame("publish", "echo", new JsonObject().put("value", "Vert.x"), socket);
    });
}
Also used : NetSocket(io.vertx.core.net.NetSocket) 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 4 with FrameParser

use of io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameParser 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", conn -> {
        context.assertFalse(conn.failed());
        NetSocket socket = conn.result();
        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 : 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 5 with FrameParser

use of io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameParser in project vertx-tcp-eventbus-bridge by vert-x3.

the class TcpEventBusBridgeImpl method handler.

private void handler(NetSocket socket) {
    final Map<String, MessageConsumer<?>> registry = new ConcurrentHashMap<>();
    final Map<String, Message<JsonObject>> replies = new ConcurrentHashMap<>();
    // create a protocol parser
    final FrameParser parser = new FrameParser(res -> {
        if (res.failed()) {
            // could not parse the message properly
            log.error(res.cause());
            return;
        }
        final JsonObject msg = res.result();
        // short reference
        // default to message
        final String type = msg.getString("type", "message");
        final String address = msg.getString("address");
        BridgeEventType eventType = parseType(type);
        checkCallHook(() -> new BridgeEventImpl(eventType, msg, socket), () -> {
            if (eventType == BridgeEventType.SOCKET_PING) {
            // No specific action
            } else if (address == null) {
                sendErrFrame("missing_address", socket);
                log.error("msg does not have address: " + msg.toString());
                return;
            }
            doSendOrPub(true, socket, address, msg, registry, replies);
        }, () -> {
            sendErrFrame("blocked by bridgeEvent handler", socket);
        });
        if ("ping".equals(type)) {
            // discard
            return;
        }
        if (address == null) {
            sendErrFrame("address_required", socket);
            return;
        }
    // doSendOrPub
    });
    socket.handler(parser);
    socket.exceptionHandler(t -> {
        log.error(t.getMessage(), t);
        registry.values().forEach(MessageConsumer::unregister);
        registry.clear();
        socket.close();
    });
    socket.endHandler(v -> {
        registry.values().forEach(MessageConsumer::unregister);
        registry.clear();
    });
}
Also used : MessageConsumer(io.vertx.core.eventbus.MessageConsumer) Message(io.vertx.core.eventbus.Message) JsonObject(io.vertx.core.json.JsonObject) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) FrameParser(io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameParser) BridgeEventType(io.vertx.ext.bridge.BridgeEventType)

Aggregations

JsonObject (io.vertx.core.json.JsonObject)10 FrameParser (io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameParser)10 NetClient (io.vertx.core.net.NetClient)9 NetSocket (io.vertx.core.net.NetSocket)9 Async (io.vertx.ext.unit.Async)9 Test (org.junit.Test)9 Message (io.vertx.core.eventbus.Message)2 MessageConsumer (io.vertx.core.eventbus.MessageConsumer)1 BridgeEventType (io.vertx.ext.bridge.BridgeEventType)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1