Example usage for io.netty.buffer ByteBufUtil decodeHexDump

List of usage examples for io.netty.buffer ByteBufUtil decodeHexDump

Introduction

In this page you can find the example usage for io.netty.buffer ByteBufUtil decodeHexDump.

Prototype

public static byte[] decodeHexDump(CharSequence hexDump) 

Source Link

Document

Decodes a string generated by #hexDump(byte[])

Usage

From source file:org.redisson.cache.LocalCacheListener.java

License:Apache License

public void add(Cache<?, ?> cache) {
    this.cache = cache;

    invalidationTopic = new RedissonTopic(LocalCachedMessageCodec.INSTANCE, commandExecutor,
            getInvalidationTopicName());

    if (options.getReconnectionStrategy() != ReconnectionStrategy.NONE) {
        reconnectionListenerId = invalidationTopic.addListener(new BaseStatusListener() {
            @Override//  www . j  a  v a2 s  .  com
            public void onSubscribe(String channel) {
                if (options.getReconnectionStrategy() == ReconnectionStrategy.CLEAR) {
                    cache.clear();
                }
                if (options.getReconnectionStrategy() == ReconnectionStrategy.LOAD
                        // check if instance has already been used
                        && lastInvalidate > 0) {

                    loadAfterReconnection();
                }
            }
        });
    }

    if (options.getSyncStrategy() != SyncStrategy.NONE) {
        syncListenerId = invalidationTopic.addListener(Object.class, new MessageListener<Object>() {
            @Override
            public void onMessage(CharSequence channel, Object msg) {
                if (msg instanceof LocalCachedMapDisable) {
                    LocalCachedMapDisable m = (LocalCachedMapDisable) msg;
                    String requestId = m.getRequestId();
                    Set<CacheKey> keysToDisable = new HashSet<CacheKey>();
                    for (byte[] keyHash : ((LocalCachedMapDisable) msg).getKeyHashes()) {
                        CacheKey key = new CacheKey(keyHash);
                        keysToDisable.add(key);
                    }

                    disableKeys(requestId, keysToDisable, m.getTimeout());

                    RedissonTopic topic = new RedissonTopic(LocalCachedMessageCodec.INSTANCE, commandExecutor,
                            RedissonObject.suffixName(name, requestId + DISABLED_ACK_SUFFIX));
                    topic.publishAsync(new LocalCachedMapDisableAck());
                }

                if (msg instanceof LocalCachedMapEnable) {
                    LocalCachedMapEnable m = (LocalCachedMapEnable) msg;
                    for (byte[] keyHash : m.getKeyHashes()) {
                        CacheKey key = new CacheKey(keyHash);
                        disabledKeys.remove(key, m.getRequestId());
                    }
                }

                if (msg instanceof LocalCachedMapClear) {
                    LocalCachedMapClear clearMsg = (LocalCachedMapClear) msg;
                    cache.clear();

                    RSemaphore semaphore = getClearSemaphore(clearMsg.getRequestId());
                    semaphore.releaseAsync();
                }

                if (msg instanceof LocalCachedMapInvalidate) {
                    LocalCachedMapInvalidate invalidateMsg = (LocalCachedMapInvalidate) msg;
                    if (!Arrays.equals(invalidateMsg.getExcludedId(), instanceId)) {
                        for (byte[] keyHash : invalidateMsg.getKeyHashes()) {
                            CacheKey key = new CacheKey(keyHash);
                            cache.remove(key);
                        }
                    }
                }

                if (msg instanceof LocalCachedMapUpdate) {
                    LocalCachedMapUpdate updateMsg = (LocalCachedMapUpdate) msg;

                    for (LocalCachedMapUpdate.Entry entry : updateMsg.getEntries()) {
                        ByteBuf keyBuf = Unpooled.wrappedBuffer(entry.getKey());
                        ByteBuf valueBuf = Unpooled.wrappedBuffer(entry.getValue());
                        try {
                            updateCache(keyBuf, valueBuf);
                        } catch (IOException e) {
                            log.error("Can't decode map entry", e);
                        } finally {
                            keyBuf.release();
                            valueBuf.release();
                        }
                    }

                }

                if (options.getReconnectionStrategy() == ReconnectionStrategy.LOAD) {
                    lastInvalidate = System.currentTimeMillis();
                }
            }

        });

        String disabledKeysName = RedissonObject.suffixName(name, DISABLED_KEYS_SUFFIX);
        RListMultimapCache<LocalCachedMapDisabledKey, String> multimap = new RedissonListMultimapCache<LocalCachedMapDisabledKey, String>(
                null, codec, commandExecutor, disabledKeysName);

        for (LocalCachedMapDisabledKey key : multimap.readAllKeySet()) {
            Set<CacheKey> keysToDisable = new HashSet<CacheKey>();
            for (String hash : multimap.getAll(key)) {
                CacheKey cacheKey = new CacheKey(ByteBufUtil.decodeHexDump(hash));
                keysToDisable.add(cacheKey);
            }

            disableKeys(key.getRequestId(), keysToDisable, key.getTimeout());
        }
    }
}

From source file:org.redisson.remote.RequestId.java

License:Apache License

public RequestId(String id) {
    this(ByteBufUtil.decodeHexDump(id));
}

From source file:org.traccar.protocol.AutoTrackProtocolDecoder.java

License:Apache License

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {

    ByteBuf buf = (ByteBuf) msg;/*  www  . j a va2  s. c  o m*/

    buf.skipBytes(4); // sync
    int type = buf.readUnsignedByte();
    buf.readUnsignedShortLE(); // length

    switch (type) {
    case MSG_LOGIN_REQUEST:
        String imei = ByteBufUtil.hexDump(buf.readBytes(8));
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
        if (deviceSession == null) {
            return null;
        }
        int fuelConst = buf.readUnsignedShortLE();
        int tripConst = buf.readUnsignedShortLE();
        if (channel != null) {
            ByteBuf response = Unpooled.buffer();
            response.writeInt(0xF1F1F1F1); // sync
            response.writeByte(MSG_LOGIN_CONFIRM);
            response.writeShortLE(12); // length
            response.writeBytes(ByteBufUtil.decodeHexDump(imei));
            response.writeShortLE(fuelConst);
            response.writeShortLE(tripConst);
            response.writeShort(Checksum.crc16(Checksum.CRC16_XMODEM, response.nioBuffer()));
            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
        }
        return null;
    case MSG_TELEMETRY_1:
    case MSG_TELEMETRY_2:
    case MSG_TELEMETRY_3:
        deviceSession = getDeviceSession(channel, remoteAddress);
        if (deviceSession == null) {
            return null;
        }
        return decodeTelemetry(channel, remoteAddress, deviceSession, buf);
    default:
        return null;
    }
}