Example usage for io.netty.channel ChannelHandlerContext fireUserEventTriggered

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

Introduction

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

Prototype

@Override
    ChannelHandlerContext fireUserEventTriggered(Object evt);

Source Link

Usage

From source file:io.vertx.core.net.impl.VertxHandler.java

License:Open Source License

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent && ((IdleStateEvent) evt).state() == IdleState.ALL_IDLE) {
        ctx.close();//from  ww w.  j a v a2s.c  o m
    }
    ctx.fireUserEventTriggered(evt);
}

From source file:jp.ac.keio.sfc.ht.memsys.ghost.server.GhostRequestServerHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    GhostRequest m = (GhostRequest) msg;
    GhostResponse res = null;//w  w  w  .  j a v a 2s .co m
    if (m.TYPE.equals(GhostRequestTypes.INIT)) {
        String appId = gateway.registerApplication(m.PARAMS.getData(BundleKeys.APP_NAME));
        Bundle bundle = new Bundle();
        bundle.putData(BundleKeys.APP_ID, appId);
        res = new GhostResponse(GhostResponseTypes.SUCCESS, GhostRequestTypes.INIT, bundle);
    } else if (m.TYPE.equals(GhostRequestTypes.REGISTERTASK)) {
        Timeout timeout = new Timeout(20, TimeUnit.SECONDS);
        Future<Object> f = gateway.registerTask(m);
        GhostResponse result = (GhostResponse) Await.result(f, timeout.duration());

        res = new GhostResponse(GhostResponseTypes.SUCCESS, GhostRequestTypes.REGISTERTASK, null);
    } else if (m.TYPE.equals(GhostRequestTypes.EXECUTE)) {

        Future<Object> f = gateway.executeTask(m);
        ctx.fireUserEventTriggered((Object) f);

        //GhostResponse result = (GhostResponse) Await.result(f, timeout.duration());
        //res = new GhostResponse(GhostResponseTypes.SUCCESS, GhostRequestTypes.EXECUTE, null);
    } else {
        System.out.println("[Ghost Request Server Handler] UNKNOWN REQUEST");
    }
    if (res != null) {
        ctx.fireChannelRead(res);
    }
}

From source file:octoteam.tahiti.server.pipeline.RequestRateLimitHandler.java

@Override
protected void messageReceived(ChannelHandlerContext ctx, Message msg) throws Exception {
    if (msg.getDirection() != Message.DirectionCode.REQUEST || msg.getService() != serviceCode) {
        ctx.fireChannelRead(msg);/*from  ww  w.j a v  a 2  s  .c  o m*/
        return;
    }
    License quotaLimiter = (License) PipelineHelper.getSession(ctx).get(sessionKey);
    if (quotaLimiter == null) {
        quotaLimiter = this.quotaLimiterFactory.call();
        PipelineHelper.getSession(ctx).put(sessionKey, quotaLimiter);
    }
    if (quotaLimiter.use() == License.Availability.AVAILABLE) {
        ctx.fireChannelRead(msg);
    } else {
        RateLimitExceededEvent evt = new RateLimitExceededEvent(name);
        ctx.fireUserEventTriggered(evt);
        ctx.writeAndFlush(buildExceededMsg(msg));
    }
}

From source file:org.apache.flink.runtime.io.network.netty.PartitionRequestQueue.java

License:Apache License

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg.getClass() == SequenceNumberingSubpartitionView.class) {
        boolean triggerWrite = queue.isEmpty();

        queue.add((SequenceNumberingSubpartitionView) msg);

        if (triggerWrite) {
            writeAndFlushNextMessageIfPossible(ctx.channel());
        }/* w  ww. jav  a2 s. co m*/
    } else if (msg.getClass() == InputChannelID.class) {
        InputChannelID toCancel = (InputChannelID) msg;

        if (released.contains(toCancel)) {
            return;
        }

        // Cancel the request for the input channel
        if (currentPartitionQueue != null && currentPartitionQueue.getReceiverId().equals(toCancel)) {
            currentPartitionQueue.releaseAllResources();
            markAsReleased(currentPartitionQueue.receiverId);
            currentPartitionQueue = null;
        } else {
            int size = queue.size();

            for (int i = 0; i < size; i++) {
                SequenceNumberingSubpartitionView curr = queue.poll();

                if (curr.getReceiverId().equals(toCancel)) {
                    curr.releaseAllResources();
                    markAsReleased(curr.receiverId);
                } else {
                    queue.add(curr);
                }
            }
        }
    } else {
        ctx.fireUserEventTriggered(msg);
    }
}

From source file:org.apache.hadoop.hbase.ipc.BufferCallBeforeInitHandler.java

License:Apache License

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof BufferCallEvent) {
        BufferCallEvent bcEvt = (BufferCallBeforeInitHandler.BufferCallEvent) evt;
        switch (bcEvt.action) {
        case FLUSH:
            for (Call call : id2Call.values()) {
                ctx.write(call);// ww w .  ja v a2 s.c o m
            }
            break;
        case FAIL:
            for (Call call : id2Call.values()) {
                call.setException(bcEvt.error);
            }
            break;
        }
        ctx.flush();
        ctx.pipeline().remove(this);
    } else if (evt instanceof CallEvent) {
        // just remove the call for now until we add other call event other than timeout and cancel.
        id2Call.remove(((CallEvent) evt).call.id);
    } else {
        ctx.fireUserEventTriggered(evt);
    }
}

From source file:org.apache.hadoop.hbase.ipc.NettyRpcDuplexHandler.java

License:Apache License

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent idleEvt = (IdleStateEvent) evt;
        switch (idleEvt.state()) {
        case WRITER_IDLE:
            if (id2Call.isEmpty()) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("shutdown connection to " + conn.remoteId().address
                            + " because idle for a long time");
                }/*from w  w  w .jav a  2 s .  co m*/
                // It may happen that there are still some pending calls in the event loop queue and
                // they will get a closed channel exception. But this is not a big deal as it rarely
                // rarely happens and the upper layer could retry immediately.
                conn.shutdown();
            }
            break;
        default:
            LOG.warn("Unrecognized idle state " + idleEvt.state());
            break;
        }
    } else if (evt instanceof CallEvent) {
        // just remove the call for now until we add other call event other than timeout and cancel.
        id2Call.remove(((CallEvent) evt).call.id);
    } else {
        ctx.fireUserEventTriggered(evt);
    }
}

From source file:org.apache.jackrabbit.oak.plugins.segment.standby.codec.ReplyDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {

    switch (state()) {
    case HEADER: {
        length = in.readInt();//from ww w. jav  a2 s . c o m
        type = in.readByte();
        switch (type) {
        case Messages.HEADER_SEGMENT:
            checkpoint(DecodingState.SEGMENT);
            break;
        case Messages.HEADER_BLOB:
            checkpoint(DecodingState.BLOB);
            break;
        default:
            throw new Exception("Unknown type: " + type);
        }
        return;
    }

    case SEGMENT: {
        Segment s = decodeSegment(in, length, type);
        if (s != null) {
            out.add(SegmentReply.empty());
            ctx.fireUserEventTriggered(new SegmentReply(s));
            reset();
        }
        return;
    }

    case BLOB: {
        IdArrayBasedBlob b = decodeBlob(in, length, type);
        if (b != null) {
            out.add(SegmentReply.empty());
            ctx.fireUserEventTriggered(new SegmentReply(b));
            reset();
        }
        return;
    }

    default:
        throw new Exception("Unknown decoding state: " + state());
    }
}

From source file:org.apache.spark.network.server.TransportChannelHandler.java

License:Apache License

/** Triggered based on events from an {@link io.netty.handler.timeout.IdleStateHandler}. */
@Override//from  w w  w . java 2 s .  co  m
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent e = (IdleStateEvent) evt;
        // See class comment for timeout semantics. In addition to ensuring we only timeout while
        // there are outstanding requests, we also do a secondary consistency check to ensure
        // there's no race between the idle timeout and incrementing the numOutstandingRequests
        // (see SPARK-7003).
        //
        // To avoid a race between TransportClientFactory.createClient() and this code which could
        // result in an inactive client being returned, this needs to run in a synchronized block.
        synchronized (this) {
            boolean isActuallyOverdue = System.nanoTime()
                    - responseHandler.getTimeOfLastRequestNs() > requestTimeoutNs;
            if (e.state() == IdleState.ALL_IDLE && isActuallyOverdue) {
                if (responseHandler.numOutstandingRequests() > 0) {
                    String address = getRemoteAddress(ctx.channel());
                    logger.error("Connection to {} has been quiet for {} ms while there are outstanding "
                            + "requests. Assuming connection is dead; please adjust spark.network.timeout if "
                            + "this is wrong.", address, requestTimeoutNs / 1000 / 1000);
                    client.timeOut();
                    ctx.close();
                } else if (closeIdleConnections) {
                    // While CloseIdleConnections is enable, we also close idle connection
                    client.timeOut();
                    ctx.close();
                }
            }
        }
    }
    ctx.fireUserEventTriggered(evt);
}

From source file:org.apache.tajo.rpc.MonitorClientHandler.java

License:Apache License

@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());
            }//from w  ww .  j ava  2  s  . co  m
            ctx.writeAndFlush(ping.duplicate().retain());
        }
    }
    super.userEventTriggered(ctx, evt);
}

From source file:org.dcache.xrootd.plugins.AccessLogHandler.java

License:Open Source License

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof LoginEvent) {
        LoginReply loginReply = ((LoginEvent) evt).getLoginReply();
        Subject subject = loginReply.getSubject();
        NetLoggerBuilder log = new NetLoggerBuilder(INFO, "org.dcache.xrootd.login").omitNullValues();
        log.add("session", CDC.getSession());
        log.add("user.dn", Subjects.getDn(subject));
        log.add("user.mapped", subject);
        log.toLogger(logger);//from  ww  w  .j  a  v  a 2  s. c  om
    }
    ctx.fireUserEventTriggered(evt);
}