Example usage for io.netty.channel ChannelHandlerContext attr

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

Introduction

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

Prototype

@Deprecated
@Override
<T> Attribute<T> attr(AttributeKey<T> key);

Source Link

Usage

From source file:io.moquette.server.netty.metrics.BytesMetricsHandler.java

License:Open Source License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    Attribute<BytesMetrics> attr = ctx.attr(ATTR_KEY_METRICS);
    attr.set(new BytesMetrics());

    super.channelActive(ctx);
}

From source file:io.moquette.server.netty.metrics.BytesMetricsHandler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    BytesMetrics metrics = ctx.attr(ATTR_KEY_METRICS).get();
    metrics.incrementRead(((ByteBuf) msg).readableBytes());
    ctx.fireChannelRead(msg);/*from   w  ww.  j  a  va2s  .c om*/
}

From source file:io.moquette.server.netty.metrics.BytesMetricsHandler.java

License:Open Source License

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    BytesMetrics metrics = ctx.attr(ATTR_KEY_METRICS).get();
    metrics.incrementWrote(((ByteBuf) msg).writableBytes());
    ctx.write(msg, promise);/* w  w w.  ja v  a2  s .  c  o  m*/
}

From source file:io.moquette.server.netty.metrics.BytesMetricsHandler.java

License:Open Source License

@Override
public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
    BytesMetrics metrics = ctx.attr(ATTR_KEY_METRICS).get();
    m_collector.sumReadBytes(metrics.readBytes());
    m_collector.sumWroteBytes(metrics.wroteBytes());
    super.close(ctx, promise);
}

From source file:io.moquette.server.netty.metrics.MessageMetricsHandler.java

License:Open Source License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    Attribute<MessageMetrics> attr = ctx.attr(ATTR_KEY_METRICS);
    attr.set(new MessageMetrics());

    super.channelActive(ctx);
}

From source file:io.moquette.server.netty.metrics.MessageMetricsHandler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    MessageMetrics metrics = ctx.attr(ATTR_KEY_METRICS).get();
    metrics.incrementRead(1);/*from ww  w  .  ja va  2s.com*/
    ctx.fireChannelRead(msg);
}

From source file:io.moquette.server.netty.metrics.MessageMetricsHandler.java

License:Open Source License

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    MessageMetrics metrics = ctx.attr(ATTR_KEY_METRICS).get();
    metrics.incrementWrote(1);// ww  w.  jav a2  s .  c o m
    ctx.write(msg, promise);
}

From source file:io.moquette.server.netty.metrics.MessageMetricsHandler.java

License:Open Source License

@Override
public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
    MessageMetrics metrics = ctx.attr(ATTR_KEY_METRICS).get();
    m_collector.sumReadMessages(metrics.messagesRead());
    m_collector.sumWroteMessages(metrics.messagesWrote());
    super.close(ctx, promise);
}

From source file:io.mycat.netty.mysql.MySQLHandshakeHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    logger.info("MySQLHandshakeHandler channel Active");

    // maybe leak here
    ByteBuf out = ctx.alloc().buffer();/*  w ww .  j a  v  a 2  s  . c  o  m*/
    Handshake handshake = new Handshake();
    handshake.sequenceId = 0;
    handshake.protocolVersion = MySQLServer.PROTOCOL_VERSION;
    handshake.serverVersion = MySQLServer.SERVER_VERSION;
    handshake.connectionId = connIdGenerator.incrementAndGet();
    handshake.challenge1 = getRandomString(8);
    handshake.capabilityFlags = Flags.CLIENT_BASIC_FLAGS;
    handshake.characterSet = CharsetUtil.getIndex(MySQLServer.DEFAULT_CHARSET);
    handshake.statusFlags = Flags.SERVER_STATUS_AUTOCOMMIT;
    handshake.challenge2 = getRandomString(12);
    handshake.authPluginDataLength = 21;
    handshake.authPluginName = "mysql_native_password";
    // Remove some flags from the reply
    handshake.removeCapabilityFlag(Flags.CLIENT_COMPRESS);
    handshake.removeCapabilityFlag(Flags.CLIENT_IGNORE_SPACE);
    handshake.removeCapabilityFlag(Flags.CLIENT_LOCAL_FILES);
    handshake.removeCapabilityFlag(Flags.CLIENT_SSL);
    handshake.removeCapabilityFlag(Flags.CLIENT_TRANSACTIONS);
    handshake.removeCapabilityFlag(Flags.CLIENT_RESERVED);
    handshake.removeCapabilityFlag(Flags.CLIENT_REMEMBER_OPTIONS);

    MysqlFrontendSession temp = new MysqlFrontendSession();
    temp.setHandshake(handshake);
    temp.setAttachment(SEED_KEY, handshake.challenge1);
    ctx.attr(TMP_SESSION_KEY).set(temp);

    logger.info("prepare flush authentication, mysql handshake handler : {}", handshake);
    out.writeBytes(handshake.toPacket());
    ctx.writeAndFlush(out);

    //  ===========

    //        HandshakePacket handshake = new HandshakePacket();
    //        handshake.packetId = 0;
    //        handshake.protocolVersion = MySQLServer.PROTOCOL_VERSION;
    //        // TODO:
    //        handshake.serverVersion = MySQLServer.SERVER_VERSION.getBytes();
    //        handshake.threadId= connIdGenerator.incrementAndGet();
    //        // TODO:
    //        handshake.seed = getRandomString(8).getBytes();
    //        handshake.serverCapabilities = Flags.CLIENT_BASIC_FLAGS;
    //        handshake.serverCharsetIndex = (byte)CharsetUtil.getIndex(MySQLServer.DEFAULT_CHARSET);
    //        handshake.serverStatus = Flags.SERVER_STATUS_AUTOCOMMIT;
    //        // TODO:
    //        handshake.restOfScrambleBuff = getRandomString(12).getBytes();
    ////        handshake. = 21;
    ////        handshake.authPluginName = "mysql_native_password";
    //        // Remove some flags from the reply
    //        handshake.removeCapabilityFlag(Flags.CLIENT_COMPRESS);
    //        handshake.removeCapabilityFlag(Flags.CLIENT_IGNORE_SPACE);
    //        handshake.removeCapabilityFlag(Flags.CLIENT_LOCAL_FILES);
    //        handshake.removeCapabilityFlag(Flags.CLIENT_SSL);
    //        handshake.removeCapabilityFlag(Flags.CLIENT_TRANSACTIONS);
    //        handshake.removeCapabilityFlag(Flags.CLIENT_RESERVED);
    //        handshake.removeCapabilityFlag(Flags.CLIENT_REMEMBER_OPTIONS);
    //
    //
    //        MysqlFrontendSession temp = new MysqlFrontendSession();
    //        temp.setHandshake(handshake);
    //        temp.setAttachment(SEED_KEY, handshake.seed);
    //        ctx.attr(TMP_SESSION_KEY).set(temp);

    //        logger.info("prepare flush authentication, mysql handshake handler : {}", handshake);
    //        out.writeBytes(handshake.getPacket());
    //        ctx.writeAndFlush(out);
    //
}

From source file:io.netlibs.bgp.netty.handlers.UpdateAttributeChecker.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, Object e) throws Exception {
    boolean sentUpstream = false;

    if (e instanceof UpdatePacket) {
        PeerConnectionInformation connInfo = ctx.attr(BGPv4ClientEndpoint.PEER_CONNECTION_INFO).get();
        UpdatePacket update = (UpdatePacket) e;
        List<PathAttribute> attributeFlagsErrorList = new LinkedList<PathAttribute>();
        List<Class<? extends PathAttribute>> missingWellKnownList = new LinkedList<Class<? extends PathAttribute>>();
        Set<Class<? extends PathAttribute>> givenAttributes = new HashSet<Class<? extends PathAttribute>>();

        // check if passed optional / transitive bits match the presettings of the attribute type
        for (PathAttribute attribute : update.getPathAttributes()) {
            boolean badAttr = false;

            givenAttributes.add(attribute.getClass());

            switch (attribute.getCategory()) {
            case WELL_KNOWN_MANDATORY:
            case WELL_KNOWN_DISCRETIONARY:
                badAttr = attribute.isOptional() || !attribute.isTransitive();
                break;
            case OPTIONAL_NON_TRANSITIVE:
                badAttr = !attribute.isOptional() || attribute.isTransitive();
                break;
            case OPTIONAL_TRANSITIVE:
                badAttr = !attribute.isOptional() || !attribute.isTransitive();
                break;
            }/*www.j  a va 2  s.co m*/

            if (badAttr) {
                log.info("detected attribute " + attribute + " with invalid flags");

                attributeFlagsErrorList.add(attribute);
            }
        }

        // if we have any bad attribute, generate notification message and leave
        if (attributeFlagsErrorList.size() > 0) {
            NotificationHelper.sendNotification(ctx,
                    new AttributeFlagsNotificationPacket(serializeAttributes(attributeFlagsErrorList)),
                    new BgpEventFireChannelFutureListener(ctx));
        } else {
            // check presence of mandatory attributes
            Set<Class<? extends PathAttribute>> mandatoryAttributes;

            if (connInfo.isIBGPConnection())
                mandatoryAttributes = mandatoryIBGPAttributes;
            else
                mandatoryAttributes = mandatoryEBGPAttributes;

            for (Class<? extends PathAttribute> attrClass : mandatoryAttributes) {
                if (!givenAttributes.contains(attrClass)) {
                    missingWellKnownList.add(attrClass);
                }
            }

            if (missingWellKnownList.size() > 0) {
                Map<Class<? extends PathAttribute>, Integer> codeMap;
                List<NotificationPacket> notifications = new LinkedList<NotificationPacket>();

                if (connInfo.isAS4OctetsInUse())
                    codeMap = as4ClazzCodeMap;
                else
                    codeMap = as2ClazzCodeMap;

                if (connInfo.isAS4OctetsInUse())
                    codeMap = as4ClazzCodeMap;
                else
                    codeMap = as2ClazzCodeMap;

                for (Class<? extends PathAttribute> attrClass : missingWellKnownList) {
                    int code = codeMap.get(attrClass);

                    log.info("detected missing well-known atribute, type " + code);
                    notifications.add(new MissingWellKnownAttributeNotificationPacket(code));
                }

                NotificationHelper.sendNotifications(ctx, notifications,
                        new BgpEventFireChannelFutureListener(ctx));
            } else {
                boolean haveBougsWidth = false;

                // check path attributes for AS number width (2 or 4) settings which mismatch the connection configuration
                for (PathAttribute attribute : update.getPathAttributes()) {
                    if (attribute instanceof ASTypeAware) {
                        if (((ASTypeAware) attribute).getAsType() != connInfo.getAsTypeInUse()) {
                            haveBougsWidth = true;
                        }
                    }
                }

                if (haveBougsWidth) {
                    NotificationHelper.sendNotification(ctx, new MalformedAttributeListNotificationPacket(),
                            new BgpEventFireChannelFutureListener(ctx));
                } else
                    sentUpstream = true;
            }
        }
    } else
        sentUpstream = true;

    if (sentUpstream)
        ctx.fireChannelRead(e);
}