Search in sources :

Example 1 with OpcUaBinaryDataTypeCodec

use of org.eclipse.milo.opcua.stack.core.serialization.codecs.OpcUaBinaryDataTypeCodec in project milo by eclipse.

the class BinaryDecoderTest method testDecodeNullArray.

@Test(description = "a null array, once encoded, should decode to a null array")
public void testDecodeNullArray() {
    Argument argument = new Argument("test", Identifiers.Int16, 1, null, LocalizedText.NULL_VALUE);
    @SuppressWarnings("unchecked") OpcUaBinaryDataTypeCodec<Argument> codec = (OpcUaBinaryDataTypeCodec<Argument>) OpcUaDataTypeManager.getInstance().getCodec(OpcUaDefaultBinaryEncoding.ENCODING_NAME, Argument.TYPE_ID.toNodeId(new NamespaceTable()).get());
    assertNotNull(codec);
    codec.encode(new TestSerializationContext(), writer, argument);
    Argument decoded = codec.decode(new TestSerializationContext(), reader);
    assertEquals(decoded.getName(), argument.getName());
    assertNull(decoded.getArrayDimensions());
}
Also used : Argument(org.eclipse.milo.opcua.stack.core.types.structured.Argument) NamespaceTable(org.eclipse.milo.opcua.stack.core.NamespaceTable) TestSerializationContext(org.eclipse.milo.opcua.stack.core.serialization.TestSerializationContext) OpcUaBinaryDataTypeCodec(org.eclipse.milo.opcua.stack.core.serialization.codecs.OpcUaBinaryDataTypeCodec) Test(org.testng.annotations.Test)

Example 2 with OpcUaBinaryDataTypeCodec

use of org.eclipse.milo.opcua.stack.core.serialization.codecs.OpcUaBinaryDataTypeCodec in project milo by eclipse.

the class OpcUaBinaryStreamDecoder method readMessage.

@Override
public UaMessage readMessage(String field) throws UaSerializationException {
    NodeId encodingId = readNodeId();
    OpcUaBinaryDataTypeCodec<?> binaryCodec = (OpcUaBinaryDataTypeCodec<?>) context.getDataTypeManager().getCodec(encodingId);
    if (binaryCodec != null) {
        return (UaMessage) binaryCodec.decode(context, this);
    } else {
        throw new UaSerializationException(StatusCodes.Bad_DecodingError, "no codec registered: " + encodingId);
    }
}
Also used : UaSerializationException(org.eclipse.milo.opcua.stack.core.UaSerializationException) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) ExpandedNodeId(org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId) OpcUaBinaryDataTypeCodec(org.eclipse.milo.opcua.stack.core.serialization.codecs.OpcUaBinaryDataTypeCodec)

Example 3 with OpcUaBinaryDataTypeCodec

use of org.eclipse.milo.opcua.stack.core.serialization.codecs.OpcUaBinaryDataTypeCodec in project milo by eclipse.

the class OpcUaBinaryStreamEncoder method writeStruct.

@Override
public void writeStruct(String field, Object value, NodeId dataTypeId) throws UaSerializationException {
    try {
        @SuppressWarnings("unchecked") OpcUaBinaryDataTypeCodec<Object> codec = (OpcUaBinaryDataTypeCodec<Object>) context.getDataTypeManager().getCodec(OpcUaDefaultBinaryEncoding.ENCODING_NAME, dataTypeId);
        if (codec == null) {
            throw new UaSerializationException(StatusCodes.Bad_EncodingError, "no codec registered: " + dataTypeId);
        }
        codec.encode(context, this, value);
    } catch (ClassCastException e) {
        throw new UaSerializationException(StatusCodes.Bad_EncodingError, e);
    }
}
Also used : UaSerializationException(org.eclipse.milo.opcua.stack.core.UaSerializationException) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject) OpcUaBinaryDataTypeCodec(org.eclipse.milo.opcua.stack.core.serialization.codecs.OpcUaBinaryDataTypeCodec)

Example 4 with OpcUaBinaryDataTypeCodec

use of org.eclipse.milo.opcua.stack.core.serialization.codecs.OpcUaBinaryDataTypeCodec in project milo by eclipse.

the class OpcUaDefaultBinaryEncoding method decode.

@Override
public Object decode(SerializationContext context, Object encodedBody, NodeId encodingId) {
    try {
        @SuppressWarnings("unchecked") OpcUaBinaryDataTypeCodec<Object> codec = (OpcUaBinaryDataTypeCodec<Object>) context.getDataTypeManager().getCodec(encodingId);
        if (codec == null) {
            throw new UaSerializationException(StatusCodes.Bad_DecodingError, "no codec registered for encodingId=" + encodingId);
        }
        ByteString binaryBody = (ByteString) encodedBody;
        byte[] bs = binaryBody.bytesOrEmpty();
        ByteBuf buffer = Unpooled.wrappedBuffer(bs);
        OpcUaBinaryStreamDecoder reader = new OpcUaBinaryStreamDecoder(context).setBuffer(buffer);
        return codec.decode(context, reader);
    } catch (ClassCastException e) {
        throw new UaSerializationException(StatusCodes.Bad_DecodingError, e);
    }
}
Also used : UaSerializationException(org.eclipse.milo.opcua.stack.core.UaSerializationException) OpcUaBinaryStreamDecoder(org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamDecoder) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject) ByteBuf(io.netty.buffer.ByteBuf) OpcUaBinaryDataTypeCodec(org.eclipse.milo.opcua.stack.core.serialization.codecs.OpcUaBinaryDataTypeCodec)

Example 5 with OpcUaBinaryDataTypeCodec

use of org.eclipse.milo.opcua.stack.core.serialization.codecs.OpcUaBinaryDataTypeCodec in project milo by eclipse.

the class OpcUaBinaryStreamDecoder method readStructArray.

@Override
public Object[] readStructArray(String field, NodeId dataTypeId) throws UaSerializationException {
    int length = readInt32();
    if (length == -1) {
        return null;
    } else {
        checkArrayLength(length);
        OpcUaBinaryDataTypeCodec<?> binaryCodec = (OpcUaBinaryDataTypeCodec<?>) context.getDataTypeManager().getCodec(OpcUaDefaultBinaryEncoding.ENCODING_NAME, dataTypeId);
        if (binaryCodec == null) {
            throw new UaSerializationException(StatusCodes.Bad_DecodingError, "no codec registered: " + dataTypeId);
        }
        Class<?> clazz = binaryCodec.getType();
        Object array = Array.newInstance(clazz, length);
        for (int i = 0; i < length; i++) {
            Object value = binaryCodec.decode(context, this);
            Array.set(array, i, value);
        }
        return (Object[]) array;
    }
}
Also used : UaSerializationException(org.eclipse.milo.opcua.stack.core.UaSerializationException) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) OpcUaBinaryDataTypeCodec(org.eclipse.milo.opcua.stack.core.serialization.codecs.OpcUaBinaryDataTypeCodec)

Aggregations

OpcUaBinaryDataTypeCodec (org.eclipse.milo.opcua.stack.core.serialization.codecs.OpcUaBinaryDataTypeCodec)7 UaSerializationException (org.eclipse.milo.opcua.stack.core.UaSerializationException)6 ExtensionObject (org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject)4 ByteBuf (io.netty.buffer.ByteBuf)2 ExpandedNodeId (org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId)2 NodeId (org.eclipse.milo.opcua.stack.core.types.builtin.NodeId)2 NamespaceTable (org.eclipse.milo.opcua.stack.core.NamespaceTable)1 OpcUaBinaryStreamDecoder (org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamDecoder)1 OpcUaBinaryStreamEncoder (org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamEncoder)1 TestSerializationContext (org.eclipse.milo.opcua.stack.core.serialization.TestSerializationContext)1 ByteString (org.eclipse.milo.opcua.stack.core.types.builtin.ByteString)1 Unsigned.uint (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint)1 Argument (org.eclipse.milo.opcua.stack.core.types.structured.Argument)1 Test (org.testng.annotations.Test)1