Example usage for io.netty.buffer ByteBufUtil hexDump

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

Introduction

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

Prototype

public static String hexDump(byte[] array) 

Source Link

Document

Returns a <a href="http://en.wikipedia.org/wiki/Hex_dump">hex dump</a> of the specified byte array.

Usage

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;//from  w  w w. jav a2  s.com

    buf.skipBytes(1); // header

    String id = ByteBufUtil.hexDump(buf.readSlice(6));
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id);
    if (deviceSession == null) {
        return null;
    }

    int type = buf.readUnsignedShort();
    buf.readUnsignedShort(); // length

    if (type == 0x2086 || type == 0x2084 || type == 0x2082) {

        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());

        buf.readUnsignedByte(); // data type
        buf.readUnsignedShort(); // trip id

        position.setTime(decodeDate(buf));

        position.setLatitude(decodeCoordinate(BcdUtil.readInteger(buf, 8)));
        position.setLongitude(decodeCoordinate(BcdUtil.readInteger(buf, 9)));

        int flags = buf.readUnsignedByte();
        position.setValid(BitUtil.check(flags, 0));
        if (!BitUtil.check(flags, 1)) {
            position.setLatitude(-position.getLatitude());
        }
        if (!BitUtil.check(flags, 2)) {
            position.setLongitude(-position.getLongitude());
        }

        position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
        position.setCourse(buf.readUnsignedByte() * 2);

        position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
        position.set(Position.KEY_RSSI, buf.readUnsignedByte());
        position.set(Position.KEY_ODOMETER, buf.readUnsignedInt() * 1000);
        position.set(Position.KEY_STATUS, buf.readUnsignedInt());

        // additional data

        return position;

    } else if (type == 0x3088) {

        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());

        getLastLocation(position, null);

        buf.readUnsignedShort(); // trip id
        buf.skipBytes(8); // imei
        buf.skipBytes(8); // imsi

        position.set("tripStart", decodeDate(buf).getTime());
        position.set("tripEnd", decodeDate(buf).getTime());
        position.set("drivingTime", buf.readUnsignedShort());

        position.set(Position.KEY_FUEL_CONSUMPTION, buf.readUnsignedInt());
        position.set(Position.KEY_ODOMETER_TRIP, buf.readUnsignedInt());

        position.set("maxSpeed", UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
        position.set("maxRpm", buf.readUnsignedShort());
        position.set("maxTemp", buf.readUnsignedByte() - 40);
        position.set("hardAccelerationCount", buf.readUnsignedByte());
        position.set("hardBrakingCount", buf.readUnsignedByte());

        for (String speedType : Arrays.asList("over", "high", "normal", "low")) {
            position.set(speedType + "SpeedTime", buf.readUnsignedShort());
            position.set(speedType + "SpeedDistance", buf.readUnsignedInt());
            position.set(speedType + "SpeedFuel", buf.readUnsignedInt());
        }

        position.set("idleTime", buf.readUnsignedShort());
        position.set("idleFuel", buf.readUnsignedInt());

        position.set("hardCorneringCount", buf.readUnsignedByte());
        position.set("overspeedCount", buf.readUnsignedByte());
        position.set("overheatCount", buf.readUnsignedShort());
        position.set("laneChangeCount", buf.readUnsignedByte());
        position.set("emergencyRefueling", buf.readUnsignedByte());

        return position;

    }

    return null;
}

From source file:se.sics.kompics.network.netty.DatagramHandler.java

License:Open Source License

@Override
protected void messageReceived(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
    try {/*from   www . j a v a 2 s .  c  o m*/
        Object m = Serializers.fromBinary(msg.content(), Optional.absent());
        if (m instanceof Msg) {
            component.deliverMessage((Msg) m);
        } else {
            NettyNetwork.LOG.warn("Got unexpected Datagram message type: {} -> {}",
                    m.getClass().getCanonicalName(), m);
        }
    } catch (Exception e) { // Catch anything...the Serializer could through any kind of weird exception if you get message that were send by someone else
        NettyNetwork.LOG.warn("Got weird Datagram message, ignoring it: {}",
                ByteBufUtil.hexDump(msg.content()));
    }
}

From source file:se.sics.kompics.network.netty.MessageEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, Msg msg, List<Object> outL) throws Exception {
    ByteBuf out = ctx.alloc().buffer(NettyNetwork.INITIAL_BUFFER_SIZE, NettyNetwork.SEND_BUFFER_SIZE);
    NettyNetwork.LOG.trace("Trying to encode outgoing data to {} from {}.", ctx.channel().remoteAddress(),
            ctx.channel().localAddress());
    int startIdx = out.writerIndex();
    out.writeBytes(LENGTH_PLACEHOLDER);//from ww  w . j  av  a2s .  co  m

    Serializers.toBinary(msg, out);

    int endIdx = out.writerIndex();
    int diff = endIdx - startIdx - LENGTH_PLACEHOLDER.length;
    if (diff > 65532) { //2^16 - 2bytes for the length header (snappy wants no more than 65536 bytes uncompressed)
        throw new Exception("Can't encode message longer than 65532 bytes!");
    }
    out.setShort(startIdx, diff);
    NettyNetwork.LOG.trace("Encoded outgoing {} bytes of data to {}: {}.",
            new Object[] { diff, ctx.channel().remoteAddress(), ByteBufUtil.hexDump(out) });
    outL.add(out);
}

From source file:se.sics.kompics.network.netty.serialization.SerializationTest.java

License:Open Source License

@Test
public void byteTest() {
    byte[] some = new byte[] { 1, 2, 3, 4 };
    ByteBuf buf = Unpooled.directBuffer();
    Serializers.toBinary(some, buf);//from w ww. j av  a  2 s.  c o m
    System.out.println("Bytes: " + ByteBufUtil.hexDump(buf));
    byte[] someRes = (byte[]) Serializers.fromBinary(buf, Optional.absent());
    assertArrayEquals(some, someRes);
}

From source file:se.sics.kompics.network.netty.serialization.SerializationTest.java

License:Open Source License

@Test
public void nullTest() {
    Object some = null;/*from w  ww.j a  v a2 s.  c  o  m*/
    ByteBuf buf = Unpooled.directBuffer();
    Serializers.toBinary(some, buf);
    System.out.println("Nulls: " + ByteBufUtil.hexDump(buf));
    Object someRes = Serializers.fromBinary(buf, Optional.absent());
    assertEquals(some, someRes);
}

From source file:se.sics.kompics.network.netty.serialization.SerializationTest.java

License:Open Source License

@Test
public void intTest() {
    Integer some = 1234;/*from w w  w. jav a2s  . co m*/
    ByteBuf buf = Unpooled.directBuffer();
    Serializers.toBinary(some, buf);
    System.out.println("Ints: " + ByteBufUtil.hexDump(buf) + " : " + ByteBufUtil.hexDump(buf).length());
    Integer someRes = (Integer) Serializers.fromBinary(buf, Optional.absent());
    assertEquals(some, someRes);

    int someI = 1234;
    Serializers.toBinary(someI, buf);
    System.out.println("Ints2: " + ByteBufUtil.hexDump(buf) + " : " + ByteBufUtil.hexDump(buf).length());
    int someResI = (int) Serializers.fromBinary(buf, Optional.absent());
    assertEquals(someI, someResI);
}

From source file:se.sics.kompics.network.netty.serialization.SerializationTest.java

License:Open Source License

@Test
public void serializableTest() {
    SomeSerializable some = new SomeSerializable();
    ByteBuf buf = Unpooled.directBuffer();
    Serializers.toBinary(some, buf);/*from ww  w. j a v  a 2s  .c  o m*/
    System.out.println(
            "SomeSerializable: " + ByteBufUtil.hexDump(buf) + " : " + ByteBufUtil.hexDump(buf).length());
    SomeSerializable someRes = (SomeSerializable) Serializers.fromBinary(buf, Optional.absent());
    assertEquals(some.getField(), someRes.getField());
}

From source file:se.sics.kompics.network.netty.serialization.SerializationTest.java

License:Open Source License

@Test
public void parentSerializableTest() {
    SomeSerializable someI = new SomeSerializable();
    ParentSome some = new ParentSome(someI);
    ByteBuf buf = Unpooled.directBuffer();
    Serializers.toBinary(some, buf);/*from   w  w  w.  ja  va2 s.  com*/
    System.out.println("SomeParent: " + ByteBufUtil.hexDump(buf) + " : " + ByteBufUtil.hexDump(buf).length());
    ParentSome someRes = (ParentSome) Serializers.fromBinary(buf, Optional.absent());
    assertEquals(some.getMySer().getField(), someRes.getMySer().getField());
}

From source file:se.sics.kompics.network.netty.serialization.SerializationTest.java

License:Open Source License

@Test
public void addressTest() {
    try {/*  w  ww .j ava2s . c  om*/
        Address addr = new Address(InetAddress.getByName("127.0.0.1"), 1234, new byte[] { 1, 2, 3, 4 });
        Address hostAddr = addr.hostAddress();
        ByteBuf buf = Unpooled.directBuffer();
        Serializers.toBinary(addr, buf);
        System.out.println("Address: " + ByteBufUtil.hexDump(buf));
        Address someRes = (Address) Serializers.fromBinary(buf, Optional.absent());
        assertEquals(addr, someRes);

        buf.clear();

        Serializers.toBinary(hostAddr, buf);
        System.out.println("HostAddress: " + ByteBufUtil.hexDump(buf));
        Address someHostRes = (Address) Serializers.fromBinary(buf, Optional.absent());
        assertEquals(hostAddr, someHostRes);

        buf.release();
    } catch (UnknownHostException ex) {
        Assert.fail(ex.getMessage());
    }
}

From source file:se.sics.kompics.network.netty.serialization.SerializationTest.java

License:Open Source License

@Test
public void disambTest() {
    for (Transport proto : Transport.values()) {
        try {/*from  www  . j  a v  a  2 s  .c o  m*/
            Address src = new Address(InetAddress.getByName("127.0.0.1"), 1234, new byte[] { 1, 2, 3, 4 });
            Address dst = new Address(InetAddress.getByName("127.0.0.1"), 5678, new byte[] { 5, 6, 7, 8 });
            DisambiguateConnection.Req req = new DisambiguateConnection.Req(src, dst, proto, 1234, 5678);
            DisambiguateConnection.Resp resp = new DisambiguateConnection.Resp(src, dst, proto, 1234, 5678,
                    9876);
            ByteBuf buf = Unpooled.directBuffer();
            Serializers.toBinary(req, buf);
            System.out.println("DisambReq: " + ByteBufUtil.hexDump(buf));
            Object someRes = Serializers.fromBinary(buf, Optional.absent());
            Assert.assertNotNull(someRes);
            someRes = null;
            buf.clear();
            Serializers.toBinary(resp, buf);
            System.out.println("DisambResp: " + ByteBufUtil.hexDump(buf));
            someRes = Serializers.fromBinary(buf, Optional.absent());
            Assert.assertNotNull(someRes);
        } catch (UnknownHostException ex) {
            Assert.fail(ex.getMessage());
        }
    }
}