Example usage for io.netty.util Attribute compareAndSet

List of usage examples for io.netty.util Attribute compareAndSet

Introduction

In this page you can find the example usage for io.netty.util Attribute compareAndSet.

Prototype

boolean compareAndSet(T oldValue, T newValue);

Source Link

Document

Atomically sets the value to the given updated value if the current value == the expected value.

Usage

From source file:at.yawk.dbus.protocol.codec.Local.java

/**
 * Generate a serial for the given channel.
 *///  ww w  .  j  a  v  a  2  s .  c om
static int generateSerial(ChannelHandlerContext ctx) {
    int serial;
    Attribute<Integer> attr = ctx.attr(LAST_SERIAL);
    Integer lastSerial;
    do {
        lastSerial = attr.get();
        serial = (lastSerial == null ? 0 : lastSerial) + 1;
        if (serial == 0) {
            serial = 1;
        }
    } while (!attr.compareAndSet(lastSerial, serial));
    return serial;
}

From source file:com.corundumstudio.socketio.handler.EncoderHandler.java

License:Apache License

private void handleHTTP(OutPacketMessage msg, ChannelHandlerContext ctx) throws IOException {
    Channel channel = ctx.channel();
    Attribute<Boolean> attr = channel.attr(WRITE_ONCE);

    Queue<Packet> queue = msg.getClientHead().getPacketsQueue(msg.getTransport());

    if (!channel.isActive() || queue.isEmpty() || !attr.compareAndSet(null, true)) {
        return;//w w w .j  a  v a  2  s  .  c  o m
    }

    ByteBuf out = encoder.allocateBuffer(ctx.alloc());
    Boolean b64 = ctx.channel().attr(EncoderHandler.B64).get();
    if (b64 != null && b64) {
        Integer jsonpIndex = ctx.channel().attr(EncoderHandler.JSONP_INDEX).get();
        encoder.encodeJsonP(jsonpIndex, queue, out, ctx.alloc(), 50);
        String type = "application/javascript";
        if (jsonpIndex == null) {
            type = "text/plain";
        }
        sendMessage(msg, channel, out, type);
    } else {
        encoder.encodePackets(queue, out, ctx.alloc(), 50);
        sendMessage(msg, channel, out, "application/octet-stream");
    }
}

From source file:com.dinstone.rpc.netty.client.SessionUtil.java

License:Apache License

@SuppressWarnings("unchecked")
public static Map<Integer, CallFuture> getCallFutureMap(Channel session) {
    Attribute<Object> attr = session.attr(CALL_KEY);
    Map<Integer, CallFuture> cfMap = (Map<Integer, CallFuture>) attr.get();
    if (cfMap == null) {
        cfMap = new ConcurrentHashMap<Integer, CallFuture>();
        if (!attr.compareAndSet(null, cfMap)) {
            cfMap = (Map<Integer, CallFuture>) attr.get();
        }//from  w  ww. j  a va 2 s . c  om
    }

    return cfMap;
}