Example usage for io.netty.buffer ByteBuf writeShort

List of usage examples for io.netty.buffer ByteBuf writeShort

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf writeShort.

Prototype

public abstract ByteBuf writeShort(int value);

Source Link

Document

Sets the specified 16-bit short integer at the current writerIndex and increases the writerIndex by 2 in this buffer.

Usage

From source file:org.eclipse.neoscada.protocol.iec60870.asdu.types.TypeHelper.java

License:Open Source License

public static void encodeTimestamp(final ProtocolOptions options, final ByteBuf out, final long timestamp) {
    final Calendar c = new GregorianCalendar(options.getTimeZone());
    c.setTimeInMillis(timestamp);//  w  w  w  .j a va2 s  . co  m

    final int ms = c.get(Calendar.SECOND) * 1_000 + c.get(Calendar.MILLISECOND);
    final int minutes = c.get(Calendar.MINUTE);
    final int hours = c.get(Calendar.HOUR_OF_DAY);
    final int dayOfMonth = c.get(Calendar.DAY_OF_MONTH);
    final int month = c.get(Calendar.MONTH) + 1;
    final int year = c.get(Calendar.YEAR) % 100;

    byte hourField = (byte) (hours & 0b00011111);

    if (!options.isIgnoreDaylightSavingTime()) {
        if (c.get(Calendar.DST_OFFSET) > 0) {
            hourField |= 0b10000000;
        }
    }

    out.writeShort(ms);
    out.writeByte(minutes); // we implicitly set "valid"
    out.writeByte(hourField);
    out.writeByte(dayOfMonth); // we implicitly set dayOfWeek to zero here
    out.writeByte(month);
    out.writeByte(year);
}

From source file:org.eclipse.neoscada.protocol.iec60870.asdu.types.TypeHelper.java

License:Open Source License

/**
 * Encode Short integer number with quality descriptor
 *//*from ww w .  ja  va  2  s .  c  o m*/
public static void encodeScaledValue(final ProtocolOptions options, final ByteBuf out, final Value<Short> value,
        final boolean withTimestamp) {
    final byte qds = (byte) (value.isOverflow() ? 0b00000001 : 0b00000000);
    final byte siq = value.getQualityInformation().apply(qds);

    out.writeShort(value.getValue());
    out.writeByte(siq);

    if (withTimestamp) {
        encodeTimestamp(options, out, value.getTimestamp());
    }
}

From source file:org.eclipse.neoscada.protocol.iec60870.asdu.types.TypeHelper.java

License:Open Source License

/**
 * Encode Double as normalized value with quality descriptor
 */// w  w  w.  j  a v  a 2  s. c  om
public static void encodeNormalizedValue(final ProtocolOptions options, final ByteBuf out,
        final Value<Double> value, final boolean withTimestamp) {
    final boolean isOverflow;
    final short normalizedValue;
    if (value.getValue() < MIN_NORMALIZED_VALUE) {
        normalizedValue = Short.MIN_VALUE;
        isOverflow = true;
    } else if (value.getValue() > MAX_NORMALIZED_VALUE) {
        normalizedValue = Short.MAX_VALUE;
        isOverflow = true;
    } else {
        normalizedValue = (short) (value.getValue() * 32768);
        isOverflow = false;
    }

    final byte qds = (byte) ((value.isOverflow() || isOverflow) ? 0b00000001 : 0b00000000);
    final byte siq = value.getQualityInformation().apply(qds);

    out.writeShort(normalizedValue);
    out.writeByte(siq);

    if (withTimestamp) {
        encodeTimestamp(options, out, value.getTimestamp());
    }
}

From source file:org.eclipse.scada.protocol.iec60870.asdu.message.SetPointCommandScaledValue.java

License:Open Source License

@Override
public void encode(final ProtocolOptions options, final ByteBuf out) {
    EncodeHelper.encodeHeader(this, options, null, this.header, out);

    this.informationObjectAddress.encode(options, out);

    out.writeShort(this.value);

    byte b = 0;/*ww w. jav  a 2s  .  com*/

    b |= this.type & 0b011111111;
    b |= this.execute ? 0 : 0b100000000;

    out.writeByte(b);
}

From source file:org.eclipse.scada.protocol.iec60870.asdu.types.ASDUAddress.java

License:Open Source License

@Override
public String toString() {
    final ByteBuf buf = Unpooled.buffer(2);
    buf.writeShort(this.address);
    return isBroadcast() ? "[BCAST]"
            : String.format("[%d-%d # %d]", buf.getUnsignedByte(0), buf.getUnsignedByte(1), this.address);
}

From source file:org.evilco.mc.defense.common.network.DefenseStationRegisterUserPacket.java

License:Apache License

/**
 * {@inheritDoc}//from   ww w  . j  a  va  2  s  .  c  o m
 */
@Override
public void write(ByteBuf buffer) {
    // convert string to byte[]
    byte[] usernameRaw = this.username.getBytes(Charsets.UTF_8);

    // write coordinates
    buffer.writeInt(this.xCoord);
    buffer.writeInt(this.yCoord);
    buffer.writeInt(this.zCoord);

    // write mode
    buffer.writeBoolean(this.blacklist);

    // write length
    buffer.writeShort(usernameRaw.length);

    // write data
    buffer.writeBytes(usernameRaw);
}

From source file:org.evilco.mc.defense.common.network.DefenseStationUnregisterPacket.java

License:Apache License

/**
 * {@inheritDoc}//from w  w w. j  ava 2s .c o m
 */
@Override
public void write(ByteBuf buffer) {
    // write coordinates
    buffer.writeInt(this.xCoord);
    buffer.writeInt(this.yCoord);
    buffer.writeInt(this.zCoord);

    // create buffer
    byte[] buf = this.userID.toString().getBytes(Charsets.UTF_8);

    // write length
    buffer.writeShort(buf.length);

    // write bytes
    buffer.writeBytes(buf);
}

From source file:org.hawkular.metrics.clients.ptrans.collectd.packet.PacketDecodingTest.java

License:Apache License

static ByteBuf createStringPartBuffer(String value, PartType partType) {
    ByteBuf buffer = Unpooled.buffer();
    buffer.writeShort(partType.getId());
    ByteBuf src = Unpooled.copiedBuffer(value, CharsetUtil.US_ASCII);
    buffer.writeShort(4 + src.readableBytes() + 1);
    buffer.writeBytes(src);// w  w  w  .  java  2  s  .co m
    buffer.writeByte(0);
    return buffer;
}

From source file:org.hawkular.metrics.clients.ptrans.collectd.packet.PacketDecodingTest.java

License:Apache License

static ByteBuf createNumericPartBuffer(Long value, PartType partType) {
    ByteBuf buffer = Unpooled.buffer();
    buffer.writeShort(partType.getId());
    buffer.writeShort(12);//from ww  w .j a  va2  s.  co  m
    buffer.writeLong(value);
    return buffer;
}

From source file:org.hawkular.metrics.clients.ptrans.collectd.packet.PacketDecodingTest.java

License:Apache License

static ByteBuf createValuesPartBuffer(Values values) {
    List<Number> data = values.getData();
    ListIterator<Number> dataIterator = data.listIterator();
    List<DataType> dataTypes = values.getDataTypes();
    ListIterator<DataType> dataTypeIterator = dataTypes.listIterator();

    ByteBuf payloadBuffer = Unpooled.buffer();

    while (dataTypeIterator.hasNext()) {
        payloadBuffer.writeByte(dataTypeIterator.next().getId());
    }//w  ww.  ja v  a2  s  .c o m

    dataTypeIterator = dataTypes.listIterator();
    while (dataIterator.hasNext()) {
        DataType dataType = dataTypeIterator.next();
        Number number = dataIterator.next();
        switch (dataType) {
        case COUNTER:
        case ABSOLUTE:
            BigInteger bigInteger = (BigInteger) number;
            payloadBuffer.writeBytes(bigInteger.toByteArray());
            break;
        case DERIVE:
            payloadBuffer.writeLong((Long) number);
            break;
        case GAUGE:
            payloadBuffer.writeLong(ByteBufUtil.swapLong(Double.doubleToLongBits((Double) number)));
            break;
        default:
            fail("Unknown data type: " + dataType);
        }
    }

    ByteBuf headerBuffer = Unpooled.buffer();
    headerBuffer.writeShort(VALUES.getId());
    headerBuffer.writeShort(6 + payloadBuffer.writerIndex());
    headerBuffer.writeShort(data.size());

    ByteBuf buffer = Unpooled.buffer();
    buffer.writeBytes(headerBuffer.duplicate()).writeBytes(payloadBuffer.duplicate());
    return buffer;
}