Example usage for io.netty.handler.codec.mqtt MqttDecoder MqttDecoder

List of usage examples for io.netty.handler.codec.mqtt MqttDecoder MqttDecoder

Introduction

In this page you can find the example usage for io.netty.handler.codec.mqtt MqttDecoder MqttDecoder.

Prototype

public MqttDecoder() 

Source Link

Usage

From source file:com.caricah.iotracah.server.mqttserver.netty.MqttServerInitializer.java

License:Apache License

@Override
protected void customizePipeline(EventExecutorGroup eventExecutorGroup, ChannelPipeline pipeline) {
    pipeline.addLast("decoder", new MqttDecoder());
    pipeline.addLast("encoder", new MqttEncoder());

    // we finally have the chance to add some business logic.
    pipeline.addLast(eventExecutorGroup, "iotracah-mqtt",
            new MqttServerHandler((MqttServerImpl) getServerImpl()));
}

From source file:io.crate.mqtt.netty.Client.java

License:Open Source License

public void connect() {
    LOGGER.debug("[mqtt-client] connect");
    handler = new ClientNettyMQTTHandler();
    workerGroup = new NioEventLoopGroup(1);
    try {//  ww w.j av a 2 s.  co  m
        Bootstrap b = new Bootstrap();
        b.group(workerGroup);
        b.channel(NioSocketChannel.class);
        b.option(ChannelOption.SO_KEEPALIVE, true);
        b.handler(new ChannelInitializer<SocketChannel>() {
            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast("decoder", new MqttDecoder());
                pipeline.addLast("encoder", MqttEncoder.INSTANCE);
                pipeline.addLast("handler", handler);
            }
        });

        // Start the client.
        channel = b.connect(host, port).sync().channel();
    } catch (Exception ex) {
        LOGGER.error("[mqtt-client] Error in client setup: " + ex.getMessage());
        workerGroup.shutdownGracefully();
        Throwables.rethrow(ex);
    }
}

From source file:io.crate.mqtt.netty.Netty4MqttServerTransport.java

@Override
protected void doStart() {
    if (isEnterprise == false || isEnabled == false) {
        return;//from   w  w w . j  a v a 2  s  . com
    }

    mqttIngestService.initialize();
    serverBootstrap = CrateChannelBootstrapFactory.newChannelBootstrap("mqtt", settings);
    serverBootstrap.childHandler(new ChannelInitializer<Channel>() {
        @Override
        protected void initChannel(Channel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            final MqttProcessor processor = new MqttProcessor(mqttIngestService);
            final MqttNettyHandler handler = new MqttNettyHandler(processor);
            final MqttNettyIdleTimeoutHandler timeoutHandler = new MqttNettyIdleTimeoutHandler();
            final IdleStateHandler defaultIdleHandler = new IdleStateHandler(0L, 0L,
                    defaultIdleTimeout.seconds(), TimeUnit.SECONDS);

            pipeline.addFirst("idleStateHandler", defaultIdleHandler)
                    .addAfter("idleStateHandler", "idleEventHandler", timeoutHandler)
                    .addLast("decoder", new MqttDecoder()).addLast("encoder", MqttEncoder.INSTANCE)
                    .addLast("messageLogger", mqttMessageLogger).addLast("handler", handler);

            if (isMQTTSslEnabled(settings)) {
                SslHandler sslHandler = sslContextProvider.get().newHandler(pipeline.channel().alloc());
                pipeline.addFirst(sslHandler);
            }
        }
    });
    serverBootstrap.validate();

    boolean success = false;
    try {
        boundAddress = resolveBindAddress();
        logger.info("{}", boundAddress);
        success = true;
    } finally {
        if (!success) {
            doStop(); // stop boss/worker threads to avoid leaks
        }
    }
}

From source file:io.netty.example.mqtt.heartBeat.MqttHeartBeatBroker.java

License:Apache License

public static void main(String[] args) throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();

    try {/*from  w w w .  j a  v a 2  s.co  m*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup);
        b.option(ChannelOption.SO_BACKLOG, 1024);
        b.channel(NioServerSocketChannel.class);
        b.childHandler(new ChannelInitializer<SocketChannel>() {
            protected void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast("encoder", MqttEncoder.INSTANCE);
                ch.pipeline().addLast("decoder", new MqttDecoder());
                ch.pipeline().addLast("heartBeatHandler", new IdleStateHandler(45, 0, 0, TimeUnit.SECONDS));
                ch.pipeline().addLast("handler", MqttHeartBeatBrokerHandler.INSTANCE);
            }
        });

        ChannelFuture f = b.bind(1883).sync();
        System.out.println("Broker initiated...");

        f.channel().closeFuture().sync();
    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }
}

From source file:io.netty.example.mqtt.heartBeat.MqttHeartBeatClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    EventLoopGroup workerGroup = new NioEventLoopGroup();

    try {//from   w  w w  . java2  s.c o m
        Bootstrap b = new Bootstrap();
        b.group(workerGroup);
        b.channel(NioSocketChannel.class);
        b.handler(new ChannelInitializer<SocketChannel>() {
            protected void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast("encoder", MqttEncoder.INSTANCE);
                ch.pipeline().addLast("decoder", new MqttDecoder());
                ch.pipeline().addLast("heartBeatHandler", new IdleStateHandler(0, 20, 0, TimeUnit.SECONDS));
                ch.pipeline().addLast("handler",
                        new MqttHeartBeatClientHandler(CLIENT_ID, USER_NAME, PASSWORD));
            }
        });

        ChannelFuture f = b.connect(HOST, PORT).sync();
        System.out.println("Client connected");
        f.channel().closeFuture().sync();
    } finally {
        workerGroup.shutdownGracefully();
    }
}

From source file:net.anyflow.lannister.client.MqttClient.java

License:Apache License

public MqttConnectReturnCode connect() throws InterruptedException {
    group = new NioEventLoopGroup(1, new DefaultThreadFactory("lannister/client"));

    bootstrap.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
        @Override/*from  www. j a  v a2  s .c  o m*/
        protected void initChannel(SocketChannel ch) throws Exception {
            if ("mqtts".equalsIgnoreCase(uri.getScheme())) {
                SslContext sslCtx = SslContextBuilder.forClient().trustManager(trustManagerFactory).build();

                ch.pipeline().addLast(sslCtx.newHandler(ch.alloc(), uri.getHost(), uri.getPort()));
            }

            ch.pipeline().addLast(MqttDecoder.class.getName(), new MqttDecoder());
            ch.pipeline().addLast(MqttEncoder.class.getName(), MqttEncoder.INSTANCE);
            ch.pipeline().addLast(MqttPacketReceiver.class.getName(),
                    new MqttPacketReceiver(MqttClient.this, receiver, sharedObject));
        }
    });

    channel = bootstrap.connect(uri.getHost(), uri.getPort()).sync().channel();

    normalizeMessage(options.will());
    send(MessageFactory.connect(options));

    synchronized (sharedObject.locker()) {
        int timeout = Settings.SELF.getInt("lannister.client.responseTimeoutSeconds", 15);

        sharedObject.locker().wait(timeout * 1000);
    }
    if (sharedObject.receivedMessage() == null) {
        return null;
    }

    return ((MqttConnAckMessage) sharedObject.receivedMessage()).variableHeader().connectReturnCode();
}