Java Code Examples for org.apache.thrift.protocol.TProtocol#readFieldBegin()

The following examples show how to use org.apache.thrift.protocol.TProtocol#readFieldBegin() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ThriftIDLSerializer.java    From octo-rpc with Apache License 2.0 6 votes vote down vote up
protected RpcResult doDeserializeResponse(DefaultResponse response, TMessage message,
                                          TProtocol protocol) throws Exception {
    RpcResult rpcResult = new RpcResult();
    if (message.type == TMessageType.REPLY) {
        Object realResult = deserializeResult(protocol, response.getServiceInterface().getName(), message.name);
        if (realResult instanceof Exception) {
            // 服务端自定义异常
            response.setException((Exception) realResult);
        }
        rpcResult.setReturnVal(realResult);
    } else if (message.type == TMessageType.EXCEPTION) {
        TApplicationException exception = TApplicationException.read(protocol);
        MetaUtil.wrapException(exception, response);
    }
    if (!response.isOctoProtocol() && hasOldRequestHeader(protocol)) {
        // 解析老协议的Header
        RequestHeader requestHeader = new RequestHeader();
        protocol.readFieldBegin();
        requestHeader.read(protocol);
        protocol.readFieldEnd();
    }
    return rpcResult;
}
 
Example 2
Source File: AbstractThriftBase.java    From ikasoa with MIT License 6 votes vote down vote up
/**
 * 读取操作
 */
@Override
public void read(TProtocol iprot) throws TException {
	if (!StringUtil.equals("org.apache.thrift.scheme.StandardScheme", iprot.getScheme().getName()))
		throw new TApplicationException("Service scheme must be 'org.apache.thrift.scheme.StandardScheme' !");
	TField schemeField;
	iprot.readStructBegin();
	while (true) {
		schemeField = iprot.readFieldBegin();
		if (ObjectUtil.same(schemeField.type, TType.STOP))
			break;
		if (ObjectUtil.same(schemeField.type, TType.STRING))
			str = iprot.readString();
		else
			throw new TApplicationException("field type must be 'String' !");
		iprot.readFieldEnd();
	}
	iprot.readStructEnd();
}
 
Example 3
Source File: ThriftIDLSerializer.java    From octo-rpc with Apache License 2.0 5 votes vote down vote up
protected RpcInvocation doDeserializeRequest(DefaultRequest request, TMessage message,
                                             TProtocol protocol) throws Exception {
    if (message.type != TMessageType.CALL) {
        throw new ProtocolException("Thrift deserialize request: message type is invalid.");
    }

    ThriftMessageInfo thriftMessageInfo = new ThriftMessageInfo(message.name, message.seqid);
    if (!request.isOctoProtocol()) {
        if (hasOldRequestHeader(protocol)) {
            // 解析老协议的header
            RequestHeader requestHeader = new RequestHeader();
            protocol.readFieldBegin();
            requestHeader.read(protocol);
            protocol.readFieldEnd();
            request = MetaUtil.convertOldProtocolHeaderToRequest(requestHeader, request);
            thriftMessageInfo.setOldProtocol(true);
        }
        request.setServiceName(ThriftUtil.getIDLClassName(request.getServiceInterface()));
    }

    List<Object> arguments = new ArrayList<>();
    List<Class<?>> parameterTypes = new ArrayList<>();
    deserializeArguments(request.getServiceInterface().getName(), message.name, protocol, arguments, parameterTypes);

    Class<?>[] parameterTypeArray = parameterTypes.toArray(new Class[parameterTypes.size()]);
    Method method = obtainMethod(request.getServiceInterface(), message.name, parameterTypeArray);

    RpcInvocation invocation = new RpcInvocation(request.getServiceInterface(), method, arguments.toArray(), parameterTypeArray);
    request.setThriftMsgInfo(thriftMessageInfo);
    return invocation;
}
 
Example 4
Source File: TApplicationExceptions.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * Reads a {@link TApplicationException} from the specified {@link TProtocol}.
 *
 * <p>Note: This has been copied from {@link TApplicationException#read(TProtocol)} due to API differences
 * between libthrift 0.9.x and 0.10.x.
 */
public static TApplicationException read(TProtocol iprot) throws TException {
    TField field;
    iprot.readStructBegin();

    String message = null;
    int type = TApplicationException.UNKNOWN;

    while (true) {
        field = iprot.readFieldBegin();
        if (field.type == TType.STOP) {
            break;
        }
        switch (field.id) {
            case 1:
                if (field.type == TType.STRING) {
                    message = iprot.readString();
                } else {
                    TProtocolUtil.skip(iprot, field.type);
                }
                break;
            case 2:
                if (field.type == TType.I32) {
                    type = iprot.readI32();
                } else {
                    TProtocolUtil.skip(iprot, field.type);
                }
                break;
            default:
                TProtocolUtil.skip(iprot, field.type);
                break;
        }
        iprot.readFieldEnd();
    }
    iprot.readStructEnd();

    return new TApplicationException(type, message);
}
 
Example 5
Source File: BinFileRead.java    From ThriftBook with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws TException {
    TTransport trans = new TSimpleFileTransport("data", true, false);
    TProtocol proto = new TBinaryProtocol(trans);		

    Trade trade_read = new Trade();
    TField field = new TField();

    TStruct struct_obj = proto.readStructBegin();		
    while(true) {						
        field = proto.readFieldBegin();			
        if (field.id == TType.STOP) {				
            break;
        }
        switch(field.id) {					
        case 1:
            trade_read.symbol = proto.readString();	
            break;
        case 2:
            trade_read.price = proto.readDouble();
            break;
        case 3:
            trade_read.size = proto.readI32();
            break;
        default:
            TProtocolUtil.skip(proto,field.type);	
            break;
        }
        proto.readFieldEnd();				
    }
    proto.readStructEnd();				

    System.out.println("Trade: " + trade_read.symbol + " " +
                       trade_read.size + " @ " + trade_read.price);
}
 
Example 6
Source File: ProtocolReadToWrite.java    From parquet-mr with Apache License 2.0 5 votes vote down vote up
private void readOneStruct(TProtocol in, TProtocol out) throws TException {
  final TStruct struct = in.readStructBegin();
  out.writeStructBegin(struct);
  TField field;
  while ((field = in.readFieldBegin()).type != TType.STOP) {
    out.writeFieldBegin(field);
    readOneValue(in, out, field.type);
    in.readFieldEnd();
    out.writeFieldEnd();
  }
  out.writeFieldStop();
  in.readStructEnd();
  out.writeStructEnd();
}