Example usage for io.netty.channel ChannelHandlerContext fireChannelRead

List of usage examples for io.netty.channel ChannelHandlerContext fireChannelRead

Introduction

In this page you can find the example usage for io.netty.channel ChannelHandlerContext fireChannelRead.

Prototype

@Override
    ChannelHandlerContext fireChannelRead(Object msg);

Source Link

Usage

From source file:org.tiger.netty.rpc.all.client.HeartBeatReqHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    System.out.println("HeartBeatReqHandler channelRead ..." + System.currentTimeMillis());
    System.out.println(msg);/* www . j  ava 2 s  .  c o m*/
    NettyMessage message = (NettyMessage) msg;
    // ?????
    if (message.getHeader() != null && message.getHeader().getType() == MessageType.LOGIN_RESP.value()) {
        heartBeat = ctx.executor().scheduleAtFixedRate(new HeartBeatTask(ctx), 0, 5000, TimeUnit.MILLISECONDS);
    } else if (message.getHeader() != null
            && message.getHeader().getType() == MessageType.HEARTBEAT_RESP.value()) {
        System.out.println("Client receive server heart beat message : ---> " + message);
    } else
        ctx.fireChannelRead(msg);
}

From source file:org.tiger.netty.rpc.all.client.LoginAuthReqHandler.java

License:Apache License

/**
 * Calls {@link ChannelHandlerContext#fireChannelRead(Object)} to forward to
 * the next {@link ChannelHandler} in the {@link ChannelPipeline}.
 *
 * Sub-classes may override this method to change behavior.
 *//*  www . ja v  a  2 s  .c  o m*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    System.out.println("LoginAuthReqHandler channelRead ..." + System.currentTimeMillis());
    NettyMessage message = (NettyMessage) msg;

    // ??????
    if (message.getHeader() != null && message.getHeader().getType() == MessageType.LOGIN_RESP.value()) {
        Byte loginResult = (Byte) message.getBody();
        if (loginResult != (byte) 0) {
            // ?
            ctx.close();
        } else {
            System.out.println("Login is ok : " + message);
            //InboundHandler
            ctx.fireChannelRead(msg);
        }
    } else
        ctx.fireChannelRead(msg);
}

From source file:org.tiger.netty.rpc.all.server.LoginAuthRespHandler.java

License:Apache License

/**
 * Calls {@link ChannelHandlerContext#fireChannelRead(Object)} to forward to
 * the next {@link ChannelHandler} in the {@link ChannelPipeline}.
 *
 * Sub-classes may override this method to change behavior.
 *///from w  ww . j  a v  a 2  s . c om
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    NettyMessage message = (NettyMessage) msg;

    // ?????
    if (message.getHeader() != null && message.getHeader().getType() == MessageType.LOGIN_REQ.value()) {
        String nodeIndex = ctx.channel().remoteAddress().toString();
        NettyMessage loginResp = null;
        // ???
        if (nodeCheck.containsKey(nodeIndex)) {
            loginResp = buildResponse((byte) -1);
        } else {
            InetSocketAddress address = (InetSocketAddress) ctx.channel().remoteAddress();
            String ip = address.getAddress().getHostAddress();
            boolean isOK = false;
            //IP???????IP?
            for (String WIP : whitekList) {
                if (WIP.equals(ip)) {
                    isOK = true;
                    break;
                }
            }
            loginResp = isOK ? buildResponse((byte) 0) : buildResponse((byte) -1);
            if (isOK)
                nodeCheck.put(nodeIndex, true);
        }
        System.out.println("The login response is : " + loginResp + " body [" + loginResp.getBody() + "]");
        ctx.writeAndFlush(loginResp);
    } else {
        ctx.fireChannelRead(msg);
    }
}

From source file:org.traccar.BaseDataHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof Position) {
        Position position = handlePosition((Position) msg);
        if (position != null) {
            ctx.fireChannelRead(position);
        }/*w  w  w. ja v  a2 s . c  o m*/
    } else {
        super.channelRead(ctx, msg);
    }
}

From source file:org.traccar.ExtendedObjectDecoder.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    NetworkMessage networkMessage = (NetworkMessage) msg;
    Object originalMessage = networkMessage.getMessage();
    try {/*ww  w.j ava 2  s.c  om*/
        Object decodedMessage = decode(ctx.channel(), networkMessage.getRemoteAddress(), originalMessage);
        onMessageEvent(ctx.channel(), networkMessage.getRemoteAddress(), originalMessage, decodedMessage);
        if (decodedMessage == null) {
            decodedMessage = handleEmptyMessage(ctx.channel(), networkMessage.getRemoteAddress(),
                    originalMessage);
        }
        if (decodedMessage != null) {
            if (decodedMessage instanceof Collection) {
                for (Object o : (Collection) decodedMessage) {
                    saveOriginal(o, originalMessage);
                    ctx.fireChannelRead(o);
                }
            } else {
                saveOriginal(decodedMessage, originalMessage);
                ctx.fireChannelRead(decodedMessage);
            }
        }
    } finally {
        ReferenceCountUtil.release(originalMessage);
    }
}

From source file:org.traccar.handler.GeocoderHandler.java

License:Apache License

@Override
public void channelRead(final ChannelHandlerContext ctx, Object message) {
    if (message instanceof Position && !ignorePositions) {
        final Position position = (Position) message;
        if (processInvalidPositions || position.getValid()) {
            if (geocoderReuseDistance != 0) {
                Position lastPosition = identityManager.getLastPosition(position.getDeviceId());
                if (lastPosition != null && lastPosition.getAddress() != null
                        && position.getDouble(Position.KEY_DISTANCE) <= geocoderReuseDistance) {
                    position.setAddress(lastPosition.getAddress());
                    ctx.fireChannelRead(position);
                    return;
                }//from  w w  w.  j av  a  2  s .  co m
            }

            if (statisticsManager != null) {
                statisticsManager.registerGeocoderRequest();
            }

            geocoder.getAddress(position.getLatitude(), position.getLongitude(),
                    new Geocoder.ReverseGeocoderCallback() {
                        @Override
                        public void onSuccess(String address) {
                            position.setAddress(address);
                            ctx.fireChannelRead(position);
                        }

                        @Override
                        public void onFailure(Throwable e) {
                            LOGGER.warn("Geocoding failed", e);
                            ctx.fireChannelRead(position);
                        }
                    });
        } else {
            ctx.fireChannelRead(position);
        }
    } else {
        ctx.fireChannelRead(message);
    }
}

From source file:org.traccar.handler.GeolocationHandler.java

License:Apache License

@Override
public void channelRead(final ChannelHandlerContext ctx, Object message) {
    if (message instanceof Position) {
        final Position position = (Position) message;
        if ((position.getOutdated() || processInvalidPositions && !position.getValid())
                && position.getNetwork() != null) {
            if (statisticsManager != null) {
                statisticsManager.registerGeolocationRequest();
            }//from  w  w  w  .ja  v a 2s  .c o  m

            geolocationProvider.getLocation(position.getNetwork(),
                    new GeolocationProvider.LocationProviderCallback() {
                        @Override
                        public void onSuccess(double latitude, double longitude, double accuracy) {
                            position.set(Position.KEY_APPROXIMATE, true);
                            position.setValid(true);
                            position.setFixTime(position.getDeviceTime());
                            position.setLatitude(latitude);
                            position.setLongitude(longitude);
                            position.setAccuracy(accuracy);
                            position.setAltitude(0);
                            position.setSpeed(0);
                            position.setCourse(0);
                            position.set(Position.KEY_RSSI, 0);
                            ctx.fireChannelRead(position);
                        }

                        @Override
                        public void onFailure(Throwable e) {
                            LOGGER.warn("Geolocation network error", e);
                            ctx.fireChannelRead(position);
                        }
                    });
        } else {
            ctx.fireChannelRead(position);
        }
    } else {
        ctx.fireChannelRead(message);
    }
}

From source file:org.traccar.handler.NetworkMessageHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    if (ctx.channel() instanceof DatagramChannel) {
        DatagramPacket packet = (DatagramPacket) msg;
        ctx.fireChannelRead(new NetworkMessage(packet.content(), packet.sender()));
    } else if (msg instanceof ByteBuf) {
        ByteBuf buffer = (ByteBuf) msg;/*w w  w  .  j  a  v a  2 s  .  c o m*/
        ctx.fireChannelRead(new NetworkMessage(buffer, ctx.channel().remoteAddress()));
    }
}

From source file:org.traccar.handler.RemoteAddressHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {

    InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress();
    String hostAddress = remoteAddress != null ? remoteAddress.getAddress().getHostAddress() : null;

    if (msg instanceof Position) {
        Position position = (Position) msg;
        position.set(Position.KEY_IP, hostAddress);
    }/*from   ww w.j ava  2s . c o  m*/

    ctx.fireChannelRead(msg);
}

From source file:org.traccar.handler.TimeHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {

    if (msg instanceof Position && (protocols == null
            || protocols.contains(ctx.pipeline().get(BaseProtocolDecoder.class).getProtocolName()))) {

        Position position = (Position) msg;
        if (useServerTime) {
            position.setDeviceTime(position.getServerTime());
        }//  ww w. j a v  a  2s. c  o m
        position.setFixTime(position.getDeviceTime());

    }
    ctx.fireChannelRead(msg);
}