Java Code Examples for io.netty.handler.timeout.IdleStateEvent#state()

The following examples show how to use io.netty.handler.timeout.IdleStateEvent#state() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HeartbeatServerHandler.java    From nuls-v2 with MIT License 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

    if (evt instanceof IdleStateEvent) {
        SocketChannel channel = (SocketChannel) ctx.channel();
        String nodeId = IpUtil.getNodeId(channel.remoteAddress());
        LoggerUtil.COMMON_LOG.info("{}====userEventTriggered  IdleStateEvent==", nodeId);
        IdleStateEvent event = (IdleStateEvent) evt;
        String type = "";
        if (event.state() == IdleState.READER_IDLE) {
            type = "read idle";
        } else if (event.state() == IdleState.WRITER_IDLE) {
            type = "write idle";
        } else if (event.state() == IdleState.ALL_IDLE) {
            type = "all idle";
        }
        LoggerUtil.COMMON_LOG.info("localInfo: " + channel.localAddress().getHostString() + ":" + channel.localAddress().getPort());
        LoggerUtil.COMMON_LOG.info("remoteInfo: " + channel.remoteAddress().getHostString() + ":" + channel.remoteAddress().getPort());
        ctx.channel().close();
    } else {
        super.userEventTriggered(ctx, evt);
    }
}
 
Example 2
Source File: MonitorClientHandler.java    From tajo with Apache License 2.0 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
  if (enableMonitor && evt instanceof IdleStateEvent) {
    IdleStateEvent e = (IdleStateEvent) evt;
    if (e.state() == IdleState.READER_IDLE && !e.isFirst()) {
      /* trigger expired event */
      LOG.info("Server has not respond " + ctx.channel());
      ctx.fireUserEventTriggered(MonitorStateEvent.MONITOR_EXPIRED_STATE_EVENT);
    } else if (e.state() == IdleState.WRITER_IDLE) {
      /* send ping packet to remote server */
      if(LOG.isDebugEnabled()){
        LOG.debug("sending ping request " + ctx.channel());
      }
      ctx.writeAndFlush(ping.duplicate().retain());
    }
  }
  super.userEventTriggered(ctx, evt);
}
 
Example 3
Source File: MyChannelHandler.java    From spring_boot with Apache License 2.0 6 votes vote down vote up
/**
 * 这里是保持服务器与客户端长连接  进行心跳检测 避免连接断开
 * @param ctx
 * @param evt
 * @throws Exception
 */
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if(evt instanceof IdleStateEvent){
        IdleStateEvent stateEvent = (IdleStateEvent) evt;
        PingWebSocketFrame ping = new PingWebSocketFrame();
        switch (stateEvent.state()){
            //读空闲(服务器端)
            case READER_IDLE:
                LOGGER.info("【"+ctx.channel().remoteAddress()+"】读空闲(服务器端)");
                ctx.writeAndFlush(ping);
                break;
                //写空闲(客户端)
            case WRITER_IDLE:
                LOGGER.info("【"+ctx.channel().remoteAddress()+"】写空闲(客户端)");
                ctx.writeAndFlush(ping);
                break;
            case ALL_IDLE:
                LOGGER.info("【"+ctx.channel().remoteAddress()+"】读写空闲");
                break;
        }
    }
}
 
Example 4
Source File: SocketHandler.java    From sds with Apache License 2.0 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    //心跳配置
    if (IdleStateEvent.class.isAssignableFrom(evt.getClass())&& socketService.getSocketEventService().hasOpenHeartCheck()) {
        IdleStateEvent event = (IdleStateEvent) evt;
        String uniqueKey = ctx.channel().remoteAddress().toString();
        if (event.state() == IdleState.READER_IDLE) {
            //表示已经多久没有收到数据了

            socketService.getSocketEventService().onHeartNoReadDataListener(ctx,uniqueKey);

        } else if (event.state() == IdleState.WRITER_IDLE) {
            //表示已经多久没有发送数据了

            socketService.getSocketEventService().onHeartNoWriteDataListener(ctx,uniqueKey);

        } else if (event.state() == IdleState.ALL_IDLE) {
            //表示已经多久既没有收到也没有发送数据了

        }
    }
}
 
Example 5
Source File: AbstractHeartbeatHandler.java    From jim-framework with Apache License 2.0 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent e = (IdleStateEvent) evt;
        switch (e.state()) {
            case READER_IDLE:
                this.handleReaderIdle(ctx);
                break;
            case WRITER_IDLE:
                this.handleWriterIdle(ctx);
                break;
            case ALL_IDLE:
                this.handleAllIdle(ctx);
                break;
            default:
                break;
        }
    }
}
 
Example 6
Source File: OpSelectorHandler.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void userEventTriggered(final ChannelHandlerContext ctx, final Object evt) throws Exception {
    // only need to handle this event if the idle monitor is on
    if (!channelizer.supportsIdleMonitor()) return;

    if (evt instanceof IdleStateEvent) {
        final IdleStateEvent e = (IdleStateEvent) evt;

        // if no requests (reader) then close, if no writes from server to client then ping. clients should
        // periodically ping the server, but coming from this direction allows the server to kill channels that
        // have dead clients on the other end
        if (e.state() == IdleState.READER_IDLE) {
            logger.info("Closing channel - client is disconnected after idle period of " + settings.idleConnectionTimeout + " " + ctx.channel());
            ctx.close();
        } else if (e.state() == IdleState.WRITER_IDLE && settings.keepAliveInterval > 0) {
            logger.info("Checking channel - sending ping to client after idle period of " + settings.keepAliveInterval + " " + ctx.channel());
            ctx.writeAndFlush(channelizer.createIdleDetectionMessage());
        }
    }
}
 
Example 7
Source File: DefaultClientChannelInboundHandler.java    From hermes with Apache License 2.0 5 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
	if (evt instanceof IdleStateEvent) {
		IdleStateEvent e = (IdleStateEvent) evt;
		switch (e.state()) {
		case ALL_IDLE:
		case READER_IDLE:
		case WRITER_IDLE:
			m_endpointClient.removeChannel(m_endpoint, m_endpointChannel, true);
			break;
		}
	}
}
 
Example 8
Source File: SelfCheckHttpServer.java    From krpc with Apache License 2.0 5 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent e = (IdleStateEvent) evt;
        if (e.state() == IdleState.ALL_IDLE) {
            String connId = getConnId(ctx);
            ctx.close();
            log.error("connection timeout, connId={}", connId);
        }
    }
}
 
Example 9
Source File: IdleStateTrigger.java    From netty-pubsub with MIT License 5 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if(evt instanceof IdleStateEvent){
        IdleStateEvent event= (IdleStateEvent) evt;
        switch (event.state()){
            case READER_IDLE:
            	 //服务端触发事件,说明客户端已经掉线,关闭失效连接
            	LOGGER.error("【客户端已断开】"+ctx.channel().remoteAddress()+"  "+DateUtils.getCurrentDateTime());
                ctx.close();
                break;
        }
    }
    super.userEventTriggered(ctx, evt);
}
 
Example 10
Source File: UptimeClientHandler.java    From tools-journey with Apache License 2.0 5 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
    if (!(evt instanceof IdleStateEvent)) {
        return;
    }

    IdleStateEvent e = (IdleStateEvent) evt;
    if (e.state() == IdleState.READER_IDLE) {
        // The connection was OK but there was no traffic for last period.
        println("Disconnecting due to no inbound traffic");
        ctx.close();
    }
}
 
Example 11
Source File: RegisterNodeHandler.java    From litchi with Apache License 2.0 5 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object e) {
    if (e instanceof IdleStateEvent) {
        IdleStateEvent event = (IdleStateEvent) e;
        if (event.state() == IdleState.ALL_IDLE) {
            RPC_LOGGER.info("channel ALL_IDLE to close. {}", ctx);
            ctx.close();
        }
    }
}
 
Example 12
Source File: TCPServerHandler.java    From jt808-server with Apache License 2.0 5 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
    if (IdleStateEvent.class.isAssignableFrom(evt.getClass())) {
        IdleStateEvent event = (IdleStateEvent) evt;
        if (event.state() == IdleState.READER_IDLE) {
            Session session = this.sessionManager.removeBySessionId(Session.buildId(ctx.channel()));
            logger.logEvent("服务器主动断开连接", session);
            ctx.close();
        }
    }
}
 
Example 13
Source File: NettyHttpServletHandler.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent e = (IdleStateEvent) evt;
        if (e.state() == IdleState.READER_IDLE || e.state() == IdleState.WRITER_IDLE) {
            LOG.log(Level.FINE, "Closing idle channel: {}", e.state());
            ctx.close();
        }
    }
}
 
Example 14
Source File: ExceptionHandler.java    From ovsdb with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
    if (evt instanceof IdleStateEvent) {
        LOG.debug("Get idle state event");
        IdleStateEvent event = (IdleStateEvent) evt;
        if (event.state() == IdleState.READER_IDLE) {
            LOG.debug("Reader idle state. Send echo message to peer");
            //Send echo message to peer
            OvsdbClient client = ovsdbConnectionService.getClient(ctx.channel());
            client.echo();
        }
    }
}
 
Example 15
Source File: HeartBeatHandler.java    From iot-dc with Apache License 2.0 5 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent event = (IdleStateEvent) evt;
        if (event.state() == IdleState.READER_IDLE) {
            loss_connect_time++;
            if (loss_connect_time > 2) {
                ctx.channel().close();
            }
        } else {
            super.userEventTriggered(ctx, evt);
        }
    }
}
 
Example 16
Source File: TransferHandler.java    From ratel with Apache License 2.0 5 votes vote down vote up
@Override  
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {  
    if (evt instanceof IdleStateEvent) {  
        IdleStateEvent event = (IdleStateEvent) evt;  
        if (event.state() == IdleState.READER_IDLE) {  
            try{
            	clientOfflineEvent(ctx.channel());
            	ctx.channel().close();
            }catch(Exception e){
            }
        }  
    } else {  
        super.userEventTriggered(ctx, evt);  
    }  
}
 
Example 17
Source File: BasicClient.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws Exception {
  if (evt.state() == IdleState.WRITER_IDLE) {
    ctx.writeAndFlush(PING_MESSAGE)
        .addListener(pingFailedListener);
  }
}
 
Example 18
Source File: NettyServerMessageHandler.java    From Raincat with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void userEventTriggered(final ChannelHandlerContext ctx, final Object evt) {
    //心跳配置
    if (IdleStateEvent.class.isAssignableFrom(evt.getClass())) {
        IdleStateEvent event = (IdleStateEvent) evt;
        if (event.state() == IdleState.READER_IDLE) {
            ctx.close();
        }
    }
}
 
Example 19
Source File: IpCameraHandler.java    From IpCamera with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void userEventTriggered(@Nullable ChannelHandlerContext ctx, @Nullable Object evt) throws Exception {
    if (ctx == null) {
        return;
    }
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent e = (IdleStateEvent) evt;
        // If camera does not use the channel for X amount of time it will close.
        if (e.state() == IdleState.READER_IDLE) {
            lock.lock();
            try {
                byte indexInLists = (byte) listOfChannels.indexOf(ctx.channel());
                if (indexInLists >= 0) {
                    String urlToKeepOpen;
                    switch (thing.getThingTypeUID().getId()) {
                        case "DAHUA":
                            urlToKeepOpen = listOfRequests.get(indexInLists);
                            if ("/cgi-bin/eventManager.cgi?action=attach&codes=[All]"
                                    .contentEquals(urlToKeepOpen)) {
                                return;
                            }
                            break;
                        case "HIKVISION":
                            urlToKeepOpen = listOfRequests.get(indexInLists);
                            if ("/ISAPI/Event/notification/alertStream".contentEquals(urlToKeepOpen)) {
                                return;
                            }
                            break;
                        case "DOORBIRD":
                            urlToKeepOpen = listOfRequests.get(indexInLists);
                            if ("/bha-api/monitor.cgi?ring=doorbell,motionsensor"
                                    .contentEquals(urlToKeepOpen)) {
                                return;
                            }
                            break;
                    }
                    logger.debug("! Channel was found idle for more than 15 seconds so closing it down. !");
                    listOfChStatus.set(indexInLists, (byte) 0);
                } else {
                    logger.warn("!?! Channel that was found idle could not be located in our tracking. !?!");
                }
            } finally {
                lock.unlock();
            }
            ctx.close();
        }
    }
}
 
Example 20
Source File: BasicClient.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws Exception {
  if (evt.state() == IdleState.WRITER_IDLE) {
    ctx.writeAndFlush(PING_MESSAGE).addListener(pingFailedHandler);
  }
}