Search in sources :

Example 1 with TTupleProtocol

use of org.apache.thrift.protocol.TTupleProtocol in project providence by morimekta.

the class TTupleProtocolSerializer method serialize.

@Override
public <Message extends PMessage<Message, Field>, Field extends PField> int serialize(@Nonnull OutputStream output, @Nonnull PServiceCall<Message, Field> call) throws IOException {
    CountingOutputStream wrapper = new CountingOutputStream(output);
    TTransport transport = new TIOStreamTransport(wrapper);
    try {
        TTupleProtocol protocol = (TTupleProtocol) protocolFactory.getProtocol(transport);
        TMessage tm = new TMessage(call.getMethod(), (byte) call.getType().asInteger(), call.getSequence());
        protocol.writeMessageBegin(tm);
        writeMessage(call.getMessage(), protocol);
        protocol.writeMessageEnd();
        transport.flush();
        wrapper.flush();
        return wrapper.getByteCount();
    } catch (TException e) {
        throw new SerializerException(e, e.getMessage());
    }
}
Also used : TException(org.apache.thrift.TException) CountingOutputStream(net.morimekta.util.io.CountingOutputStream) TMessage(org.apache.thrift.protocol.TMessage) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) TTransport(org.apache.thrift.transport.TTransport) SerializerException(net.morimekta.providence.serializer.SerializerException) TTupleProtocol(org.apache.thrift.protocol.TTupleProtocol)

Example 2 with TTupleProtocol

use of org.apache.thrift.protocol.TTupleProtocol in project providence by morimekta.

the class TTupleProtocolSerializer method serialize.

@Override
public <Message extends PMessage<Message, Field>, Field extends PField> int serialize(@Nonnull OutputStream output, @Nonnull Message message) throws IOException {
    CountingOutputStream wrapper = new CountingOutputStream(output);
    TTransport transport = new TIOStreamTransport(wrapper);
    try {
        TTupleProtocol protocol = (TTupleProtocol) protocolFactory.getProtocol(transport);
        writeMessage(message, protocol);
        transport.flush();
        wrapper.flush();
        return wrapper.getByteCount();
    } catch (TException e) {
        throw new SerializerException(e, e.getMessage());
    }
}
Also used : TException(org.apache.thrift.TException) CountingOutputStream(net.morimekta.util.io.CountingOutputStream) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) TTransport(org.apache.thrift.transport.TTransport) SerializerException(net.morimekta.providence.serializer.SerializerException) TTupleProtocol(org.apache.thrift.protocol.TTupleProtocol)

Example 3 with TTupleProtocol

use of org.apache.thrift.protocol.TTupleProtocol in project providence by morimekta.

the class TTupleProtocolSerializer method deserialize.

@Nonnull
@Override
@SuppressWarnings("unchecked")
public <Message extends PMessage<Message, Field>, Field extends PField> PServiceCall<Message, Field> deserialize(@Nonnull InputStream input, @Nonnull PService service) throws SerializerException {
    TMessage tm = null;
    PServiceCallType type = null;
    try {
        TTransport transport = new TIOStreamTransport(input);
        TTupleProtocol protocol = (TTupleProtocol) protocolFactory.getProtocol(transport);
        tm = protocol.readMessageBegin();
        type = PServiceCallType.findById(tm.type);
        if (type == null) {
            throw new SerializerException("Unknown call type for id " + tm.type);
        } else if (type == PServiceCallType.EXCEPTION) {
            PApplicationException exception = readMessage(protocol, PApplicationException.kDescriptor);
            return new PServiceCall(tm.name, type, tm.seqid, exception);
        }
        PServiceMethod method = service.getMethod(tm.name);
        if (method == null) {
            throw new SerializerException("No such method " + tm.name + " on " + service.getQualifiedName());
        }
        PMessageDescriptor<Message, Field> descriptor = isRequestCallType(type) ? method.getRequestType() : method.getResponseType();
        Message message = readMessage(protocol, descriptor);
        protocol.readMessageEnd();
        return new PServiceCall<>(tm.name, type, tm.seqid, message);
    } catch (TTransportException e) {
        throw new SerializerException(e, "Unable to serialize into transport protocol").setExceptionType(PApplicationExceptionType.findById(e.getType())).setCallType(type).setMethodName(tm != null ? tm.name : "").setSequenceNo(tm != null ? tm.seqid : 0);
    } catch (TException e) {
        throw new SerializerException(e, "Transport exception in protocol").setExceptionType(PApplicationExceptionType.PROTOCOL_ERROR).setCallType(type).setMethodName(tm != null ? tm.name : "").setSequenceNo(tm != null ? tm.seqid : 0);
    }
}
Also used : TException(org.apache.thrift.TException) PMessage(net.morimekta.providence.PMessage) TMessage(org.apache.thrift.protocol.TMessage) PServiceCallType(net.morimekta.providence.PServiceCallType) TTransportException(org.apache.thrift.transport.TTransportException) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) SerializerException(net.morimekta.providence.serializer.SerializerException) TTupleProtocol(org.apache.thrift.protocol.TTupleProtocol) PField(net.morimekta.providence.descriptor.PField) TMessage(org.apache.thrift.protocol.TMessage) PApplicationException(net.morimekta.providence.PApplicationException) PServiceCall(net.morimekta.providence.PServiceCall) TTransport(org.apache.thrift.transport.TTransport) PServiceMethod(net.morimekta.providence.descriptor.PServiceMethod) Nonnull(javax.annotation.Nonnull)

Example 4 with TTupleProtocol

use of org.apache.thrift.protocol.TTupleProtocol in project providence by morimekta.

the class TTupleProtocolSerializer method deserialize.

@Nonnull
@Override
public <Message extends PMessage<Message, Field>, Field extends PField> Message deserialize(@Nonnull InputStream input, @Nonnull PMessageDescriptor<Message, Field> descriptor) throws IOException {
    try {
        TTransport transport = new TIOStreamTransport(input);
        TTupleProtocol protocol = (TTupleProtocol) protocolFactory.getProtocol(transport);
        return readMessage(protocol, descriptor);
    } catch (TTransportException e) {
        throw new SerializerException(e, "Unable to serialize into transport protocol");
    } catch (TException e) {
        throw new SerializerException(e, "Transport exception in protocol");
    }
}
Also used : TException(org.apache.thrift.TException) TTransportException(org.apache.thrift.transport.TTransportException) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) TTransport(org.apache.thrift.transport.TTransport) SerializerException(net.morimekta.providence.serializer.SerializerException) TTupleProtocol(org.apache.thrift.protocol.TTupleProtocol) Nonnull(javax.annotation.Nonnull)

Aggregations

SerializerException (net.morimekta.providence.serializer.SerializerException)4 TException (org.apache.thrift.TException)4 TTupleProtocol (org.apache.thrift.protocol.TTupleProtocol)4 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)4 TTransport (org.apache.thrift.transport.TTransport)4 Nonnull (javax.annotation.Nonnull)2 CountingOutputStream (net.morimekta.util.io.CountingOutputStream)2 TMessage (org.apache.thrift.protocol.TMessage)2 TTransportException (org.apache.thrift.transport.TTransportException)2 PApplicationException (net.morimekta.providence.PApplicationException)1 PMessage (net.morimekta.providence.PMessage)1 PServiceCall (net.morimekta.providence.PServiceCall)1 PServiceCallType (net.morimekta.providence.PServiceCallType)1 PField (net.morimekta.providence.descriptor.PField)1 PServiceMethod (net.morimekta.providence.descriptor.PServiceMethod)1