List of usage examples for io.netty.buffer ByteBufUtil hexDump
public static String hexDump(byte[] array)
From source file:org.redisson.transaction.RedissonTransaction.java
License:Apache License
private Map<HashKey, HashValue> disableLocalCache(String requestId, Set<String> localCaches, List<TransactionalOperation> operations) { if (localCaches.isEmpty()) { return Collections.emptyMap(); }//from ww w . j ava 2 s. c om Map<HashKey, HashValue> hashes = new HashMap<>(localCaches.size()); RedissonBatch batch = new RedissonBatch(null, commandExecutor.getConnectionManager(), BatchOptions.defaults()); for (TransactionalOperation transactionalOperation : operations) { if (localCaches.contains(transactionalOperation.getName())) { MapOperation mapOperation = (MapOperation) transactionalOperation; RedissonLocalCachedMap<?, ?> map = (RedissonLocalCachedMap<?, ?>) mapOperation.getMap(); HashKey hashKey = new HashKey(transactionalOperation.getName(), transactionalOperation.getCodec()); byte[] key = map.toCacheKey(mapOperation.getKey()).getKeyHash(); HashValue value = hashes.get(hashKey); if (value == null) { value = new HashValue(); hashes.put(hashKey, value); } value.getKeyIds().add(key); String disabledKeysName = RedissonObject.suffixName(transactionalOperation.getName(), RedissonLocalCachedMap.DISABLED_KEYS_SUFFIX); RMultimapCacheAsync<LocalCachedMapDisabledKey, String> multimap = batch .getListMultimapCache(disabledKeysName, transactionalOperation.getCodec()); LocalCachedMapDisabledKey localCacheKey = new LocalCachedMapDisabledKey(requestId, options.getResponseTimeout()); multimap.putAsync(localCacheKey, ByteBufUtil.hexDump(key)); multimap.expireKeyAsync(localCacheKey, options.getResponseTimeout(), TimeUnit.MILLISECONDS); } } try { batch.execute(); } catch (Exception e) { throw new TransactionException( "Unable to execute transaction over local cached map objects: " + localCaches, e); } CountDownLatch latch = new CountDownLatch(hashes.size()); List<RTopic> topics = new ArrayList<>(); for (Entry<HashKey, HashValue> entry : hashes.entrySet()) { RTopic topic = new RedissonTopic(LocalCachedMessageCodec.INSTANCE, commandExecutor, RedissonObject .suffixName(entry.getKey().getName(), requestId + RedissonLocalCachedMap.DISABLED_ACK_SUFFIX)); topics.add(topic); topic.addListener(Object.class, new MessageListener<Object>() { @Override public void onMessage(CharSequence channel, Object msg) { AtomicInteger counter = entry.getValue().getCounter(); if (counter.decrementAndGet() == 0) { latch.countDown(); } } }); } RedissonBatch publishBatch = new RedissonBatch(null, commandExecutor.getConnectionManager(), BatchOptions.defaults()); for (Entry<HashKey, HashValue> entry : hashes.entrySet()) { String disabledKeysName = RedissonObject.suffixName(entry.getKey().getName(), RedissonLocalCachedMap.DISABLED_KEYS_SUFFIX); RMultimapCacheAsync<LocalCachedMapDisabledKey, String> multimap = publishBatch .getListMultimapCache(disabledKeysName, entry.getKey().getCodec()); LocalCachedMapDisabledKey localCacheKey = new LocalCachedMapDisabledKey(requestId, options.getResponseTimeout()); multimap.removeAllAsync(localCacheKey); RTopicAsync topic = publishBatch.getTopic( RedissonObject.suffixName(entry.getKey().getName(), RedissonLocalCachedMap.TOPIC_SUFFIX), LocalCachedMessageCodec.INSTANCE); RFuture<Long> future = topic.publishAsync(new LocalCachedMapDisable(requestId, entry.getValue().getKeyIds().toArray(new byte[entry.getValue().getKeyIds().size()][]), options.getResponseTimeout())); future.onComplete((res, e) -> { if (e != null) { return; } int receivers = res.intValue(); AtomicInteger counter = entry.getValue().getCounter(); if (counter.addAndGet(receivers) == 0) { latch.countDown(); } }); } try { publishBatch.execute(); } catch (Exception e) { throw new TransactionException( "Unable to execute transaction over local cached map objects: " + localCaches, e); } for (RTopic topic : topics) { topic.removeAllListeners(); } try { latch.await(options.getResponseTimeout(), TimeUnit.MILLISECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } return hashes; }
From source file:org.redisson.transaction.RedissonTransaction.java
License:Apache License
private RFuture<Map<HashKey, HashValue>> disableLocalCacheAsync(String requestId, Set<String> localCaches, List<TransactionalOperation> operations) { if (localCaches.isEmpty()) { return RedissonPromise.newSucceededFuture(Collections.emptyMap()); }//w w w . j a va 2s. co m RPromise<Map<HashKey, HashValue>> result = new RedissonPromise<>(); Map<HashKey, HashValue> hashes = new HashMap<>(localCaches.size()); RedissonBatch batch = new RedissonBatch(null, commandExecutor.getConnectionManager(), BatchOptions.defaults()); for (TransactionalOperation transactionalOperation : operations) { if (localCaches.contains(transactionalOperation.getName())) { MapOperation mapOperation = (MapOperation) transactionalOperation; RedissonLocalCachedMap<?, ?> map = (RedissonLocalCachedMap<?, ?>) mapOperation.getMap(); HashKey hashKey = new HashKey(transactionalOperation.getName(), transactionalOperation.getCodec()); byte[] key = map.toCacheKey(mapOperation.getKey()).getKeyHash(); HashValue value = hashes.get(hashKey); if (value == null) { value = new HashValue(); hashes.put(hashKey, value); } value.getKeyIds().add(key); String disabledKeysName = RedissonObject.suffixName(transactionalOperation.getName(), RedissonLocalCachedMap.DISABLED_KEYS_SUFFIX); RMultimapCacheAsync<LocalCachedMapDisabledKey, String> multimap = batch .getListMultimapCache(disabledKeysName, transactionalOperation.getCodec()); LocalCachedMapDisabledKey localCacheKey = new LocalCachedMapDisabledKey(requestId, options.getResponseTimeout()); multimap.putAsync(localCacheKey, ByteBufUtil.hexDump(key)); multimap.expireKeyAsync(localCacheKey, options.getResponseTimeout(), TimeUnit.MILLISECONDS); } } RFuture<BatchResult<?>> batchListener = batch.executeAsync(); batchListener.onComplete((res, e) -> { if (e != null) { result.tryFailure(e); return; } CountableListener<Map<HashKey, HashValue>> listener = new CountableListener<>(result, hashes, hashes.size()); RPromise<Void> subscriptionFuture = new RedissonPromise<>(); CountableListener<Void> subscribedFutures = new CountableListener<>(subscriptionFuture, null, hashes.size()); List<RTopic> topics = new ArrayList<>(); for (Entry<HashKey, HashValue> entry : hashes.entrySet()) { String disabledAckName = RedissonObject.suffixName(entry.getKey().getName(), requestId + RedissonLocalCachedMap.DISABLED_ACK_SUFFIX); RTopic topic = new RedissonTopic(LocalCachedMessageCodec.INSTANCE, commandExecutor, disabledAckName); topics.add(topic); RFuture<Integer> topicFuture = topic.addListenerAsync(Object.class, new MessageListener<Object>() { @Override public void onMessage(CharSequence channel, Object msg) { AtomicInteger counter = entry.getValue().getCounter(); if (counter.decrementAndGet() == 0) { listener.decCounter(); } } }); topicFuture.onComplete((r, ex) -> { subscribedFutures.decCounter(); }); } subscriptionFuture.onComplete((r, ex) -> { RedissonBatch publishBatch = new RedissonBatch(null, commandExecutor.getConnectionManager(), BatchOptions.defaults()); for (Entry<HashKey, HashValue> entry : hashes.entrySet()) { String disabledKeysName = RedissonObject.suffixName(entry.getKey().getName(), RedissonLocalCachedMap.DISABLED_KEYS_SUFFIX); RMultimapCacheAsync<LocalCachedMapDisabledKey, String> multimap = publishBatch .getListMultimapCache(disabledKeysName, entry.getKey().getCodec()); LocalCachedMapDisabledKey localCacheKey = new LocalCachedMapDisabledKey(requestId, options.getResponseTimeout()); multimap.removeAllAsync(localCacheKey); RTopicAsync topic = publishBatch.getTopic(RedissonObject.suffixName(entry.getKey().getName(), RedissonLocalCachedMap.TOPIC_SUFFIX), LocalCachedMessageCodec.INSTANCE); RFuture<Long> publishFuture = topic.publishAsync(new LocalCachedMapDisable(requestId, entry.getValue().getKeyIds().toArray(new byte[entry.getValue().getKeyIds().size()][]), options.getResponseTimeout())); publishFuture.onComplete((receivers, exc) -> { if (ex != null) { return; } AtomicInteger counter = entry.getValue().getCounter(); if (counter.addAndGet(receivers.intValue()) == 0) { listener.decCounter(); } }); } RFuture<BatchResult<?>> publishFuture = publishBatch.executeAsync(); publishFuture.onComplete((res2, ex2) -> { result.onComplete((res3, ex3) -> { for (RTopic topic : topics) { topic.removeAllListeners(); } }); if (ex2 != null) { result.tryFailure(ex2); return; } commandExecutor.getConnectionManager().newTimeout(new TimerTask() { @Override public void run(Timeout timeout) throws Exception { result.tryFailure(new TransactionTimeoutException( "Unable to execute transaction within " + options.getResponseTimeout() + "ms")); } }, options.getResponseTimeout(), TimeUnit.MILLISECONDS); }); }); }); return result; }
From source file:org.redisson.transaction.RedissonTransaction.java
License:Apache License
protected static String generateId() { byte[] id = new byte[16]; ThreadLocalRandom.current().nextBytes(id); return ByteBufUtil.hexDump(id); }
From source file:org.springframework.core.codec.support.JsonObjectDecoder.java
License:Apache License
@Override public Flux<DataBuffer> decode(Publisher<DataBuffer> inputStream, ResolvableType type, MimeType mimeType, Object... hints) {//w w w.jav a2 s . c o m return Flux.from(inputStream).flatMap(new Function<DataBuffer, Publisher<? extends DataBuffer>>() { int openBraces; int index; int state; boolean insideString; ByteBuf input; Integer writerIndex; @Override public Publisher<? extends DataBuffer> apply(DataBuffer b) { List<DataBuffer> chunks = new ArrayList<>(); if (this.input == null) { this.input = Unpooled.copiedBuffer(b.asByteBuffer()); this.writerIndex = this.input.writerIndex(); } else { this.input = Unpooled.copiedBuffer(this.input, Unpooled.copiedBuffer(b.asByteBuffer())); this.writerIndex = this.input.writerIndex(); } if (this.state == ST_CORRUPTED) { this.input.skipBytes(this.input.readableBytes()); return Flux.error(new IllegalStateException("Corrupted stream")); } if (this.writerIndex > maxObjectLength) { // buffer size exceeded maxObjectLength; discarding the complete buffer. this.input.skipBytes(this.input.readableBytes()); reset(); return Flux.error(new IllegalStateException("object length exceeds " + maxObjectLength + ": " + this.writerIndex + " bytes discarded")); } for (/* use current index */; this.index < this.writerIndex; this.index++) { byte c = this.input.getByte(this.index); if (this.state == ST_DECODING_NORMAL) { decodeByte(c, this.input, this.index); // All opening braces/brackets have been closed. That's enough to conclude // that the JSON object/array is complete. if (this.openBraces == 0) { ByteBuf json = extractObject(this.input, this.input.readerIndex(), this.index + 1 - this.input.readerIndex()); if (json != null) { chunks.add(allocator.wrap(json.nioBuffer())); } // The JSON object/array was extracted => discard the bytes from // the input buffer. this.input.readerIndex(this.index + 1); // Reset the object state to get ready for the next JSON object/text // coming along the byte stream. reset(); } } else if (this.state == ST_DECODING_ARRAY_STREAM) { decodeByte(c, this.input, this.index); if (!this.insideString && (this.openBraces == 1 && c == ',' || this.openBraces == 0 && c == ']')) { // skip leading spaces. No range check is needed and the loop will terminate // because the byte at position index is not a whitespace. for (int i = this.input.readerIndex(); Character .isWhitespace(this.input.getByte(i)); i++) { this.input.skipBytes(1); } // skip trailing spaces. int idxNoSpaces = this.index - 1; while (idxNoSpaces >= this.input.readerIndex() && Character.isWhitespace(this.input.getByte(idxNoSpaces))) { idxNoSpaces--; } ByteBuf json = extractObject(this.input, this.input.readerIndex(), idxNoSpaces + 1 - this.input.readerIndex()); if (json != null) { chunks.add(allocator.wrap(json.nioBuffer())); } this.input.readerIndex(this.index + 1); if (c == ']') { reset(); } } // JSON object/array detected. Accumulate bytes until all braces/brackets are closed. } else if (c == '{' || c == '[') { initDecoding(c, streamArrayElements); if (this.state == ST_DECODING_ARRAY_STREAM) { // Discard the array bracket this.input.skipBytes(1); } // Discard leading spaces in front of a JSON object/array. } else if (Character.isWhitespace(c)) { this.input.skipBytes(1); } else { this.state = ST_CORRUPTED; return Flux.error(new IllegalStateException("invalid JSON received at byte position " + this.index + ": " + ByteBufUtil.hexDump(this.input))); } } if (this.input.readableBytes() == 0) { this.index = 0; } return Flux.fromIterable(chunks); } /** * Override this method if you want to filter the json objects/arrays that * get passed through the pipeline. */ @SuppressWarnings("UnusedParameters") protected ByteBuf extractObject(ByteBuf buffer, int index, int length) { return buffer.slice(index, length).retain(); } private void decodeByte(byte c, ByteBuf input, int index) { if ((c == '{' || c == '[') && !this.insideString) { this.openBraces++; } else if ((c == '}' || c == ']') && !this.insideString) { this.openBraces--; } else if (c == '"') { // start of a new JSON string. It's necessary to detect strings as they may // also contain braces/brackets and that could lead to incorrect results. if (!this.insideString) { this.insideString = true; // If the double quote wasn't escaped then this is the end of a string. } else if (input.getByte(index - 1) != '\\') { this.insideString = false; } } } private void initDecoding(byte openingBrace, boolean streamArrayElements) { this.openBraces = 1; if (openingBrace == '[' && streamArrayElements) { this.state = ST_DECODING_ARRAY_STREAM; } else { this.state = ST_DECODING_NORMAL; } } private void reset() { this.insideString = false; this.state = ST_INIT; this.openBraces = 0; } }); }
From source file:org.springframework.http.codec.json.JsonObjectDecoder.java
License:Apache License
@Override public Flux<DataBuffer> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) { return Flux.from(inputStream).flatMap(new Function<DataBuffer, Publisher<? extends DataBuffer>>() { int openBraces; int index; int state; boolean insideString; ByteBuf input;/*from ww w.j av a 2s . co m*/ Integer writerIndex; @Override public Publisher<? extends DataBuffer> apply(DataBuffer buffer) { List<DataBuffer> chunks = new ArrayList<>(); if (this.input == null) { this.input = Unpooled.copiedBuffer(buffer.asByteBuffer()); DataBufferUtils.release(buffer); this.writerIndex = this.input.writerIndex(); } else { this.index = this.index - this.input.readerIndex(); this.input = Unpooled.copiedBuffer(this.input, Unpooled.copiedBuffer(buffer.asByteBuffer())); DataBufferUtils.release(buffer); this.writerIndex = this.input.writerIndex(); } if (this.state == ST_CORRUPTED) { this.input.skipBytes(this.input.readableBytes()); return Flux.error(new IllegalStateException("Corrupted stream")); } if (this.writerIndex > maxObjectLength) { // buffer size exceeded maxObjectLength; discarding the complete buffer. this.input.skipBytes(this.input.readableBytes()); reset(); return Flux.error(new IllegalStateException("object length exceeds " + maxObjectLength + ": " + this.writerIndex + " bytes discarded")); } DataBufferFactory dataBufferFactory = buffer.factory(); for (/* use current index */; this.index < this.writerIndex; this.index++) { byte c = this.input.getByte(this.index); if (this.state == ST_DECODING_NORMAL) { decodeByte(c, this.input, this.index); // All opening braces/brackets have been closed. That's enough to conclude // that the JSON object/array is complete. if (this.openBraces == 0) { ByteBuf json = extractObject(this.input, this.input.readerIndex(), this.index + 1 - this.input.readerIndex()); if (json != null) { chunks.add(dataBufferFactory.wrap(json.nioBuffer())); } // The JSON object/array was extracted => discard the bytes from // the input buffer. this.input.readerIndex(this.index + 1); // Reset the object state to get ready for the next JSON object/text // coming along the byte stream. reset(); } } else if (this.state == ST_DECODING_ARRAY_STREAM) { decodeByte(c, this.input, this.index); if (!this.insideString && (this.openBraces == 1 && c == ',' || this.openBraces == 0 && c == ']')) { // skip leading spaces. No range check is needed and the loop will terminate // because the byte at position index is not a whitespace. for (int i = this.input.readerIndex(); Character .isWhitespace(this.input.getByte(i)); i++) { this.input.skipBytes(1); } // skip trailing spaces. int idxNoSpaces = this.index - 1; while (idxNoSpaces >= this.input.readerIndex() && Character.isWhitespace(this.input.getByte(idxNoSpaces))) { idxNoSpaces--; } ByteBuf json = extractObject(this.input, this.input.readerIndex(), idxNoSpaces + 1 - this.input.readerIndex()); if (json != null) { chunks.add(dataBufferFactory.wrap(json.nioBuffer())); } this.input.readerIndex(this.index + 1); if (c == ']') { reset(); } } // JSON object/array detected. Accumulate bytes until all braces/brackets are closed. } else if (c == '{' || c == '[') { initDecoding(c, streamArrayElements); if (this.state == ST_DECODING_ARRAY_STREAM) { // Discard the array bracket this.input.skipBytes(1); } // Discard leading spaces in front of a JSON object/array. } else if (Character.isWhitespace(c)) { this.input.skipBytes(1); } else { this.state = ST_CORRUPTED; return Flux.error(new IllegalStateException("invalid JSON received at byte position " + this.index + ": " + ByteBufUtil.hexDump(this.input))); } } return Flux.fromIterable(chunks); } /** * Override this method if you want to filter the json objects/arrays that * get passed through the pipeline. */ protected ByteBuf extractObject(ByteBuf buffer, int index, int length) { return buffer.slice(index, length).retain(); } private void decodeByte(byte c, ByteBuf input, int index) { if ((c == '{' || c == '[') && !this.insideString) { this.openBraces++; } else if ((c == '}' || c == ']') && !this.insideString) { this.openBraces--; } else if (c == '"') { // start of a new JSON string. It's necessary to detect strings as they may // also contain braces/brackets and that could lead to incorrect results. if (!this.insideString) { this.insideString = true; // If the double quote wasn't escaped then this is the end of a string. } else if (input.getByte(index - 1) != '\\') { this.insideString = false; } } } private void initDecoding(byte openingBrace, boolean streamArrayElements) { this.openBraces = 1; if (openingBrace == '[' && streamArrayElements) { this.state = ST_DECODING_ARRAY_STREAM; } else { this.state = ST_DECODING_NORMAL; } } private void reset() { this.insideString = false; this.state = ST_INIT; this.openBraces = 0; } }); }
From source file:org.springframework.reactive.codec.decoder.JsonObjectDecoder.java
License:Apache License
@Override public Publisher<ByteBuffer> decode(Publisher<ByteBuffer> inputStream, ResolvableType type, MediaType mediaType, Object... hints) {//from www . j ava 2 s. c o m return Streams.wrap(inputStream).flatMap(new Function<ByteBuffer, Publisher<? extends ByteBuffer>>() { int openBraces; int idx; int state; boolean insideString; ByteBuf in; Integer wrtIdx; @Override public Publisher<? extends ByteBuffer> apply(ByteBuffer b) { List<ByteBuffer> chunks = new ArrayList<>(); if (in == null) { in = Unpooled.copiedBuffer(b); wrtIdx = in.writerIndex(); } else { in = Unpooled.copiedBuffer(in, Unpooled.copiedBuffer(b)); wrtIdx = in.writerIndex(); } if (state == ST_CORRUPTED) { in.skipBytes(in.readableBytes()); return Streams.fail(new IllegalStateException("Corrupted stream")); } if (wrtIdx > maxObjectLength) { // buffer size exceeded maxObjectLength; discarding the complete buffer. in.skipBytes(in.readableBytes()); reset(); return Streams.fail(new IllegalStateException( "object length exceeds " + maxObjectLength + ": " + wrtIdx + " bytes discarded")); } for (/* use current idx */; idx < wrtIdx; idx++) { byte c = in.getByte(idx); if (state == ST_DECODING_NORMAL) { decodeByte(c, in, idx); // All opening braces/brackets have been closed. That's enough to conclude // that the JSON object/array is complete. if (openBraces == 0) { ByteBuf json = extractObject(in, in.readerIndex(), idx + 1 - in.readerIndex()); if (json != null) { chunks.add(json.nioBuffer()); } // The JSON object/array was extracted => discard the bytes from // the input buffer. in.readerIndex(idx + 1); // Reset the object state to get ready for the next JSON object/text // coming along the byte stream. reset(); } } else if (state == ST_DECODING_ARRAY_STREAM) { decodeByte(c, in, idx); if (!insideString && (openBraces == 1 && c == ',' || openBraces == 0 && c == ']')) { // skip leading spaces. No range check is needed and the loop will terminate // because the byte at position idx is not a whitespace. for (int i = in.readerIndex(); Character.isWhitespace(in.getByte(i)); i++) { in.skipBytes(1); } // skip trailing spaces. int idxNoSpaces = idx - 1; while (idxNoSpaces >= in.readerIndex() && Character.isWhitespace(in.getByte(idxNoSpaces))) { idxNoSpaces--; } ByteBuf json = extractObject(in, in.readerIndex(), idxNoSpaces + 1 - in.readerIndex()); if (json != null) { chunks.add(json.nioBuffer()); } in.readerIndex(idx + 1); if (c == ']') { reset(); } } // JSON object/array detected. Accumulate bytes until all braces/brackets are closed. } else if (c == '{' || c == '[') { initDecoding(c, streamArrayElements); if (state == ST_DECODING_ARRAY_STREAM) { // Discard the array bracket in.skipBytes(1); } // Discard leading spaces in front of a JSON object/array. } else if (Character.isWhitespace(c)) { in.skipBytes(1); } else { state = ST_CORRUPTED; return Streams.fail(new IllegalStateException( "invalid JSON received at byte position " + idx + ": " + ByteBufUtil.hexDump(in))); } } if (in.readableBytes() == 0) { idx = 0; } return Streams.from(chunks); } /** * Override this method if you want to filter the json objects/arrays that get passed through the pipeline. */ @SuppressWarnings("UnusedParameters") protected ByteBuf extractObject(ByteBuf buffer, int index, int length) { return buffer.slice(index, length).retain(); } private void decodeByte(byte c, ByteBuf in, int idx) { if ((c == '{' || c == '[') && !insideString) { openBraces++; } else if ((c == '}' || c == ']') && !insideString) { openBraces--; } else if (c == '"') { // start of a new JSON string. It's necessary to detect strings as they may // also contain braces/brackets and that could lead to incorrect results. if (!insideString) { insideString = true; // If the double quote wasn't escaped then this is the end of a string. } else if (in.getByte(idx - 1) != '\\') { insideString = false; } } } private void initDecoding(byte openingBrace, boolean streamArrayElements) { openBraces = 1; if (openingBrace == '[' && streamArrayElements) { state = ST_DECODING_ARRAY_STREAM; } else { state = ST_DECODING_NORMAL; } } private void reset() { insideString = false; state = ST_INIT; openBraces = 0; } }); }
From source file:org.traccar.ExtendedObjectDecoder.java
License:Apache License
private void saveOriginal(Object decodedMessage, Object originalMessage) { if (Context.getConfig().getBoolean("database.saveOriginal") && decodedMessage instanceof Position) { Position position = (Position) decodedMessage; if (originalMessage instanceof ByteBuf) { ByteBuf buf = (ByteBuf) originalMessage; position.set(Position.KEY_ORIGINAL, ByteBufUtil.hexDump(buf)); } else if (originalMessage instanceof String) { position.set(Position.KEY_ORIGINAL, DataConverter.printHex(((String) originalMessage).getBytes(StandardCharsets.US_ASCII))); }//from w ww . j a v a 2 s .c o m } }
From source file:org.traccar.handler.StandardLoggingHandler.java
License:Apache License
public void log(ChannelHandlerContext ctx, boolean downstream, SocketAddress remoteAddress, ByteBuf buf) { StringBuilder message = new StringBuilder(); message.append("[").append(ctx.channel().id().asShortText()).append(": "); message.append(protocol);//from w w w. j av a2 s. co m if (downstream) { message.append(" > "); } else { message.append(" < "); } if (remoteAddress instanceof InetSocketAddress) { message.append(((InetSocketAddress) remoteAddress).getHostString()); } else { message.append("unknown"); } message.append("]"); message.append(" HEX: "); message.append(ByteBufUtil.hexDump(buf)); LOGGER.info(message.toString()); }
From source file:org.traccar.protocol.AnytrekProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg;/* ww w.j ava2 s .c o m*/ buf.skipBytes(2); // header buf.readUnsignedShortLE(); // size int type = buf.readUnsignedByte(); String imei = ByteBufUtil.hexDump(buf.readSlice(8)).substring(2); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); if (deviceSession == null) { return null; } Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_VERSION_FW, buf.readUnsignedShortLE()); position.set(Position.KEY_BATTERY, buf.readUnsignedShortLE() * 0.01); position.set(Position.KEY_RSSI, buf.readUnsignedByte()); DateBuilder dateBuilder = new DateBuilder() .setDate(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()) .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()); position.setTime(dateBuilder.getDate()); position.set(Position.KEY_SATELLITES, BitUtil.to(buf.readUnsignedByte(), 4)); double latitude = buf.readUnsignedIntLE() / 1800000.0; double longitude = buf.readUnsignedIntLE() / 1800000.0; position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte())); int flags = buf.readUnsignedShortLE(); position.setCourse(BitUtil.to(flags, 10)); position.setValid(BitUtil.check(flags, 12)); if (!BitUtil.check(flags, 10)) { latitude = -latitude; } if (BitUtil.check(flags, 11)) { longitude = -longitude; } position.setLatitude(latitude); position.setLongitude(longitude); buf.readUnsignedIntLE(); // info index buf.readUnsignedIntLE(); // setting index flags = buf.readUnsignedByte(); position.set(Position.KEY_CHARGE, BitUtil.check(flags, 0)); position.set(Position.KEY_IGNITION, BitUtil.check(flags, 1)); position.set(Position.KEY_ALARM, BitUtil.check(flags, 4) ? Position.ALARM_GENERAL : null); buf.readUnsignedShortLE(); // charge current position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE()); sendResponse(channel, remoteAddress, type); return position; }
From source file:org.traccar.protocol.AplicomProtocolDecoder.java
License:Apache License
private void decodeCanData(ByteBuf buf, Position position) { buf.readUnsignedMedium(); // packet identifier position.set(Position.KEY_VERSION_FW, buf.readUnsignedByte()); int count = buf.readUnsignedByte(); buf.readUnsignedByte(); // batch count buf.readUnsignedShort(); // selector bit buf.readUnsignedInt(); // timestamp buf.skipBytes(8);//from ww w .j av a 2 s .co m ArrayList<ByteBuf> values = new ArrayList<>(count); for (int i = 0; i < count; i++) { values.add(buf.readSlice(8)); } for (int i = 0; i < count; i++) { ByteBuf value = values.get(i); switch (buf.readInt()) { case 0x20D: position.set(Position.KEY_RPM, value.readShortLE()); position.set("dieselTemperature", value.readShortLE() * 0.1); position.set("batteryVoltage", value.readShortLE() * 0.01); position.set("supplyAirTempDep1", value.readShortLE() * 0.1); break; case 0x30D: position.set("activeAlarm", ByteBufUtil.hexDump(value)); break; case 0x40C: position.set("airTempDep1", value.readShortLE() * 0.1); position.set("airTempDep2", value.readShortLE() * 0.1); break; case 0x40D: position.set("coldUnitState", ByteBufUtil.hexDump(value)); break; case 0x50C: position.set("defrostTempDep1", value.readShortLE() * 0.1); position.set("defrostTempDep2", value.readShortLE() * 0.1); break; case 0x50D: position.set("condenserPressure", value.readShortLE() * 0.1); position.set("suctionPressure", value.readShortLE() * 0.1); break; case 0x58C: value.readByte(); value.readShort(); // index switch (value.readByte()) { case 0x01: position.set("setpointZone1", value.readIntLE() * 0.1); break; case 0x02: position.set("setpointZone2", value.readIntLE() * 0.1); break; case 0x05: position.set("unitType", value.readIntLE()); break; case 0x13: position.set("dieselHours", value.readIntLE() / 60 / 60); break; case 0x14: position.set("electricHours", value.readIntLE() / 60 / 60); break; case 0x17: position.set("serviceIndicator", value.readIntLE()); break; case 0x18: position.set("softwareVersion", value.readIntLE() * 0.01); break; default: break; } break; default: LOGGER.warn("Aplicom CAN decoding error", new UnsupportedOperationException()); break; } } }