Search in sources :

Example 1 with OpcUaBinaryStreamEncoder

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

the class BinarySerializationFixture method setUp.

@BeforeMethod
public void setUp() {
    buffer = Unpooled.buffer();
    writer = new OpcUaBinaryStreamEncoder(new TestSerializationContext()).setBuffer(buffer);
    reader = new OpcUaBinaryStreamDecoder(new TestSerializationContext()).setBuffer(buffer);
}
Also used : OpcUaBinaryStreamDecoder(org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamDecoder) TestSerializationContext(org.eclipse.milo.opcua.stack.core.serialization.TestSerializationContext) OpcUaBinaryStreamEncoder(org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamEncoder) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 2 with OpcUaBinaryStreamEncoder

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

the class BsdParserTest method assertRoundTrip.

protected void assertRoundTrip(String type, Object originalValue, OpcUaBinaryDataTypeCodec<Object> codec) {
    System.out.printf("--- assertRoundTrip Type: %s ---\n", type);
    System.out.println("originalValue:\t" + originalValue);
    ByteBuf buffer = Unpooled.buffer();
    codec.encode(context, new OpcUaBinaryStreamEncoder(context).setBuffer(buffer), originalValue);
    ByteBuf encodedValue = buffer.copy();
    System.out.println("encodedValue:\t" + ByteBufUtil.hexDump(encodedValue));
    Object decodedValue = codec.decode(context, new OpcUaBinaryStreamDecoder(context).setBuffer(buffer));
    assertEquals(decodedValue, originalValue);
    System.out.println("decodedValue:\t" + decodedValue);
}
Also used : OpcUaBinaryStreamDecoder(org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamDecoder) ByteBuf(io.netty.buffer.ByteBuf) OpcUaBinaryStreamEncoder(org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamEncoder)

Example 3 with OpcUaBinaryStreamEncoder

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

the class BsdParserTest method assertRoundTripUsingToString.

/**
 * A weaker version of {@link #assertRoundTrip(String, Object, OpcUaBinaryDataTypeCodec)} for values that don't
 * implement equals and hashcode or values that contain members not implementing equals and  hashcode.
 * <p>
 * Relies on toString() values to be implemented at all levels instead... not great, but since the built-in structs
 * don't implement equals/hashcode it's what we have.
 */
protected void assertRoundTripUsingToString(String type, Object originalValue, OpcUaBinaryDataTypeCodec<Object> codec) {
    System.out.printf("--- assertRoundTrip Type: %s ---\n", type);
    System.out.println("originalValue:\t" + originalValue);
    ByteBuf buffer = Unpooled.buffer();
    codec.encode(context, new OpcUaBinaryStreamEncoder(context).setBuffer(buffer), originalValue);
    ByteBuf encodedValue = buffer.copy();
    System.out.println("encodedValue:\t" + ByteBufUtil.hexDump(encodedValue));
    Object decodedValue = codec.decode(context, new OpcUaBinaryStreamDecoder(context).setBuffer(buffer));
    assertEquals(decodedValue.toString(), originalValue.toString());
    System.out.println("decodedValue:\t" + decodedValue);
}
Also used : OpcUaBinaryStreamDecoder(org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamDecoder) ByteBuf(io.netty.buffer.ByteBuf) OpcUaBinaryStreamEncoder(org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamEncoder)

Example 4 with OpcUaBinaryStreamEncoder

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

the class OpcClientHttpCodec method encode.

@Override
protected void encode(ChannelHandlerContext ctx, UaTransportRequest transportRequest, List<Object> out) throws Exception {
    logger.debug("encoding: " + transportRequest.getRequest());
    ctx.channel().attr(KEY_PENDING_REQUEST).set(transportRequest);
    ByteBuf content = Unpooled.buffer();
    switch(transportProfile) {
        case HTTPS_UABINARY:
            {
                OpcUaBinaryStreamEncoder encoder = new OpcUaBinaryStreamEncoder(client.getStaticSerializationContext());
                encoder.setBuffer(content);
                encoder.writeMessage(null, transportRequest.getRequest());
                break;
            }
        case HTTPS_UAXML:
            {
                // TODO put document into a SOAP message.
                throw new UaException(StatusCodes.Bad_InternalError, "no encoder for transport: " + transportProfile);
            }
        default:
            throw new UaException(StatusCodes.Bad_InternalError, "no encoder for transport: " + transportProfile);
    }
    String endpointUrl = endpoint.getEndpointUrl();
    FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, EndpointUtil.getPath(endpointUrl), content);
    httpRequest.headers().set(HttpHeaderNames.HOST, EndpointUtil.getHost(endpointUrl));
    httpRequest.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    httpRequest.headers().set(HttpHeaderNames.CONTENT_TYPE, UABINARY_CONTENT_TYPE);
    httpRequest.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());
    httpRequest.headers().set("OPCUA-SecurityPolicy", config.getEndpoint().getSecurityPolicyUri());
    out.add(httpRequest);
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) UaException(org.eclipse.milo.opcua.stack.core.UaException) ByteBuf(io.netty.buffer.ByteBuf) OpcUaBinaryStreamEncoder(org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamEncoder)

Example 5 with OpcUaBinaryStreamEncoder

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

the class OpcServerHttpRequestHandler method sendServiceResponse.

private void sendServiceResponse(ChannelHandlerContext ctx, UaRequestMessage request, UaResponseMessage response) {
    ByteBuf contentBuffer = BufferUtil.pooledBuffer();
    // TODO switch on transport profile for binary vs xml encoding
    OpcUaBinaryStreamEncoder binaryEncoder = new OpcUaBinaryStreamEncoder(stackServer.getSerializationContext());
    binaryEncoder.setBuffer(contentBuffer);
    binaryEncoder.writeMessage(null, response);
    FullHttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, contentBuffer);
    httpResponse.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    httpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, UABINARY_CONTENT_TYPE);
    httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, contentBuffer.readableBytes());
    ctx.writeAndFlush(httpResponse);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) ByteBuf(io.netty.buffer.ByteBuf) OpcUaBinaryStreamEncoder(org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamEncoder)

Aggregations

OpcUaBinaryStreamEncoder (org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamEncoder)8 ByteBuf (io.netty.buffer.ByteBuf)6 OpcUaBinaryStreamDecoder (org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamDecoder)3 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)2 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)2 TestSerializationContext (org.eclipse.milo.opcua.stack.core.serialization.TestSerializationContext)2 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)1 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1 UaException (org.eclipse.milo.opcua.stack.core.UaException)1 UaSerializationException (org.eclipse.milo.opcua.stack.core.UaSerializationException)1 OpcUaBinaryDataTypeCodec (org.eclipse.milo.opcua.stack.core.serialization.codecs.OpcUaBinaryDataTypeCodec)1 ExtensionObject (org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject)1 StatusCode (org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode)1 ResponseHeader (org.eclipse.milo.opcua.stack.core.types.structured.ResponseHeader)1 ServiceFault (org.eclipse.milo.opcua.stack.core.types.structured.ServiceFault)1 BeforeMethod (org.testng.annotations.BeforeMethod)1 BeforeTest (org.testng.annotations.BeforeTest)1