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);
});
}
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);
});
}
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);
});
}
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);
});
}
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();
});
}
Aggregations