Search in sources :

Example 6 with BridgeEventType

use of io.vertx.ext.bridge.BridgeEventType 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<?>> 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 && address == null) {
                sendErrFrame("missing_address", socket);
                log.error("msg does not have address: " + msg);
                return;
            }
            doSendOrPub(socket, address, msg, registry, replies);
        }, () -> sendErrFrame("blocked by bridgeEvent handler", socket));
    });
    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 : 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

BridgeEventType (io.vertx.ext.bridge.BridgeEventType)6 Bucket (io.github.bucket4j.Bucket)2 StartService (in.erail.glue.annotation.StartService)1 Bandwidth (io.github.bucket4j.Bandwidth)1 Refill (io.github.bucket4j.Refill)1 JsonObject (io.vertx.core.json.JsonObject)1 FrameParser (io.vertx.ext.eventbus.bridge.tcp.impl.protocol.FrameParser)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1