Java Code Examples for org.apache.thrift.transport.TTransport#read()

The following examples show how to use org.apache.thrift.transport.TTransport#read() . 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: ServerTrans.java    From ThriftBook with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) 
       throws TTransportException, UnsupportedEncodingException {
    final String msg = "Hello Thrift!\n";
    final String stop_cmd = "STOP";
    final int buf_size = 1024*8;
    byte[] buf = new byte[buf_size];
    final int port = 9090;

    TServerTransport acceptor = new TServerSocket(9090);
    acceptor.listen();
    System.out.println("[Server] listening on port: " + port);
    
    String input;
    do {
        TTransport trans = acceptor.accept();
        int len = trans.read(buf, 0, buf_size);
        input = new String(buf, 0, len,"UTF-8");
        System.out.println("[Server] handling request: " + input);
        trans.write(msg.getBytes());
        trans.flush();
        trans.close();
    } while (! stop_cmd.regionMatches(0, input, 0, 4)); 

    System.out.println("[Server] exiting");
    acceptor.close();
}
 
Example 2
Source File: SockTrans.java    From ThriftBook with Apache License 2.0 6 votes vote down vote up
public static void read_trans(TTransport trans) {
    final int buf_size = 1024*8;
    byte[] buf = new byte[buf_size];

    while (true) {
        try {							
            int bytes_read = trans.read(buf, 0, buf_size);
            if (bytes_read <= 0 || buf_size < bytes_read) {
               break;
            }
            System.out.print(new String(buf, 0, bytes_read, "UTF-8"));
        } catch (Throwable t) {
            break;
        }
    }
}
 
Example 3
Source File: TransExcep.java    From ThriftBook with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
     try {
         TTransport trans = new TSimpleFileTransport("data", false, true);
         Trade trade = new Trade();
         trade.symbol = "F";
         trade.price = 13.10;
         trade.size = 2500;
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         ObjectOutputStream oos = new ObjectOutputStream(baos);
         oos.writeObject(trade);
         trans.write(baos.toByteArray());
trans.close();

         trans = new TSimpleFileTransport("data",
                                      ((args.length==0) ? true : false), 
                                      true);			
         byte[] buf = new byte[128];
         int iBytesRead = trans.read(buf, 0, buf.length);
         ByteArrayInputStream bais = new ByteArrayInputStream(buf);
         ObjectInputStream ois = new ObjectInputStream(bais);
         trade = (Trade) ois.readObject();
         System.out.println("Trade(" + iBytesRead + "): " + trade.symbol + " " + 
                            trade.size + " @ " + trade.price);
     } catch (TTransportException tte) {				
         System.out.println("TTransportException(" + tte.getType() + 
                            "): " + tte);
     } catch (TException te) {						
         System.out.println("TException: " + te);
     } catch (Exception e) {						
         System.out.println("Exception: " + e);
     } catch (Throwable t) {						
         System.out.println("Throwable: " + t);			
     }
 }
 
Example 4
Source File: ServerFrame.java    From ThriftBook with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) 
         throws TTransportException, UnsupportedEncodingException, SocketException {
     final String msg = "Hello Thrift!\n";
     final String stop_cmd = "STOP";
     final int buf_size = 1024*8;
     byte[] buf = new byte[buf_size];
     final int port = 9090;

     TServerTransport acceptor = new TServerSocket(port);
     acceptor.listen();
     System.out.println("[Server] listening on port " + port);

     while (true) {
         TTransport trans_ep = acceptor.accept();
TTransport trans = new TFramedTransport(trans_ep);
         int len = trans.read(buf, 0, buf_size);
         String input = new String(buf, 0, len, "UTF-8");
         System.out.println("[Server] handling request: " + input);
         trans.write(msg.getBytes());
         trans.flush();
         trans.close();
         if (stop_cmd.regionMatches(0, input, 0, 4)) {
            break;
         }
     }
     System.out.println("[Server] exiting");
     acceptor.close();
 }
 
Example 5
Source File: ThriftCodecTest.java    From dubbo-2.6.5 with Apache License 2.0 2 votes vote down vote up
@Test
public void testEncodeRequest() throws Exception {

    Request request = createRequest();

    ChannelBuffer output = ChannelBuffers.dynamicBuffer(1024);

    codec.encode(channel, output, request);

    byte[] bytes = new byte[output.readableBytes()];
    output.readBytes(bytes);

    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);

    TTransport transport = new TIOStreamTransport(bis);

    TBinaryProtocol protocol = new TBinaryProtocol(transport);

    // frame
    byte[] length = new byte[4];
    transport.read(length, 0, 4);

    if (bis.markSupported()) {
        bis.mark(0);
    }

    // magic
    Assert.assertEquals(ThriftCodec.MAGIC, protocol.readI16());

    // message length
    int messageLength = protocol.readI32();
    Assert.assertEquals(messageLength + 4, bytes.length);

    // header length
    short headerLength = protocol.readI16();
    // version
    Assert.assertEquals(ThriftCodec.VERSION, protocol.readByte());
    // service name
    Assert.assertEquals(Demo.Iface.class.getName(), protocol.readString());
    // dubbo request id
    Assert.assertEquals(request.getId(), protocol.readI64());

    // test message header length
    if (bis.markSupported()) {
        bis.reset();
        bis.skip(headerLength);
    }

    TMessage message = protocol.readMessageBegin();

    Demo.echoString_args args = new Demo.echoString_args();

    args.read(protocol);

    protocol.readMessageEnd();

    Assert.assertEquals("echoString", message.name);

    Assert.assertEquals(TMessageType.CALL, message.type);

    Assert.assertEquals("Hello, World!", args.getArg());

}
 
Example 6
Source File: ThriftCodecTest.java    From dubbox with Apache License 2.0 2 votes vote down vote up
@Test
public void testEncodeRequest() throws Exception {

    Request request = createRequest();

    ChannelBuffer output = ChannelBuffers.dynamicBuffer(1024);

    codec.encode( channel, output, request );

    byte[] bytes = new byte[output.readableBytes()];
    output.readBytes(bytes);

    ByteArrayInputStream bis = new ByteArrayInputStream( bytes );

    TTransport transport = new TIOStreamTransport( bis );

    TBinaryProtocol protocol = new TBinaryProtocol( transport );

    // frame
    byte[] length = new byte[4];
    transport.read( length, 0, 4 );

    if ( bis.markSupported() ) {
        bis.mark( 0 );
    }

    // magic
    Assert.assertEquals( ThriftCodec.MAGIC, protocol.readI16() );

    // message length
    int messageLength = protocol.readI32();
    Assert.assertEquals( messageLength + 4, bytes.length );

    // header length
    short headerLength = protocol.readI16();
    // version
    Assert.assertEquals( ThriftCodec.VERSION, protocol.readByte() );
    // service name
    Assert.assertEquals( Demo.Iface.class.getName(), protocol.readString() );
    // dubbo request id
    Assert.assertEquals( request.getId(), protocol.readI64() );

    // test message header length
    if ( bis.markSupported() ) {
        bis.reset();
        bis.skip( headerLength );
    }

    TMessage message = protocol.readMessageBegin();

    Demo.echoString_args args = new Demo.echoString_args();

    args.read( protocol );

    protocol.readMessageEnd();

    Assert.assertEquals( "echoString", message.name );

    Assert.assertEquals( TMessageType.CALL, message.type );

    Assert.assertEquals( "Hello, World!", args.getArg() );

}
 
Example 7
Source File: ThriftCodecTest.java    From dubbox-hystrix with Apache License 2.0 2 votes vote down vote up
@Test
public void testEncodeRequest() throws Exception {

    Request request = createRequest();

    ChannelBuffer output = ChannelBuffers.dynamicBuffer(1024);

    codec.encode( channel, output, request );

    byte[] bytes = new byte[output.readableBytes()];
    output.readBytes(bytes);

    ByteArrayInputStream bis = new ByteArrayInputStream( bytes );

    TTransport transport = new TIOStreamTransport( bis );

    TBinaryProtocol protocol = new TBinaryProtocol( transport );

    // frame
    byte[] length = new byte[4];
    transport.read( length, 0, 4 );

    if ( bis.markSupported() ) {
        bis.mark( 0 );
    }

    // magic
    Assert.assertEquals( ThriftCodec.MAGIC, protocol.readI16() );

    // message length
    int messageLength = protocol.readI32();
    Assert.assertEquals( messageLength + 4, bytes.length );

    // header length
    short headerLength = protocol.readI16();
    // version
    Assert.assertEquals( ThriftCodec.VERSION, protocol.readByte() );
    // service name
    Assert.assertEquals( Demo.Iface.class.getName(), protocol.readString() );
    // dubbo request id
    Assert.assertEquals( request.getId(), protocol.readI64() );

    // test message header length
    if ( bis.markSupported() ) {
        bis.reset();
        bis.skip( headerLength );
    }

    TMessage message = protocol.readMessageBegin();

    Demo.echoString_args args = new Demo.echoString_args();

    args.read( protocol );

    protocol.readMessageEnd();

    Assert.assertEquals( "echoString", message.name );

    Assert.assertEquals( TMessageType.CALL, message.type );

    Assert.assertEquals( "Hello, World!", args.getArg() );

}
 
Example 8
Source File: ThriftCodecTest.java    From dubbox with Apache License 2.0 2 votes vote down vote up
@Test
public void testEncodeRequest() throws Exception {

    Request request = createRequest();

    ChannelBuffer output = ChannelBuffers.dynamicBuffer(1024);

    codec.encode( channel, output, request );

    byte[] bytes = new byte[output.readableBytes()];
    output.readBytes(bytes);

    ByteArrayInputStream bis = new ByteArrayInputStream( bytes );

    TTransport transport = new TIOStreamTransport( bis );

    TBinaryProtocol protocol = new TBinaryProtocol( transport );

    // frame
    byte[] length = new byte[4];
    transport.read( length, 0, 4 );

    if ( bis.markSupported() ) {
        bis.mark( 0 );
    }

    // magic
    Assert.assertEquals( ThriftCodec.MAGIC, protocol.readI16() );

    // message length
    int messageLength = protocol.readI32();
    Assert.assertEquals( messageLength + 4, bytes.length );

    // header length
    short headerLength = protocol.readI16();
    // version
    Assert.assertEquals( ThriftCodec.VERSION, protocol.readByte() );
    // service name
    Assert.assertEquals( Demo.Iface.class.getName(), protocol.readString() );
    // dubbo request id
    Assert.assertEquals( request.getId(), protocol.readI64() );

    // test message header length
    if ( bis.markSupported() ) {
        bis.reset();
        bis.skip( headerLength );
    }

    TMessage message = protocol.readMessageBegin();

    Demo.echoString_args args = new Demo.echoString_args();

    args.read( protocol );

    protocol.readMessageEnd();

    Assert.assertEquals( "echoString", message.name );

    Assert.assertEquals( TMessageType.CALL, message.type );

    Assert.assertEquals( "Hello, World!", args.getArg() );

}
 
Example 9
Source File: ThriftCodecTest.java    From dubbox with Apache License 2.0 2 votes vote down vote up
@Test
public void testEncodeRequest() throws Exception {

    Request request = createRequest();

    ChannelBuffer output = ChannelBuffers.dynamicBuffer(1024);

    codec.encode( channel, output, request );

    byte[] bytes = new byte[output.readableBytes()];
    output.readBytes(bytes);

    ByteArrayInputStream bis = new ByteArrayInputStream( bytes );

    TTransport transport = new TIOStreamTransport( bis );

    TBinaryProtocol protocol = new TBinaryProtocol( transport );

    // frame
    byte[] length = new byte[4];
    transport.read( length, 0, 4 );

    if ( bis.markSupported() ) {
        bis.mark( 0 );
    }

    // magic
    Assert.assertEquals( ThriftCodec.MAGIC, protocol.readI16() );

    // message length
    int messageLength = protocol.readI32();
    Assert.assertEquals( messageLength + 4, bytes.length );

    // header length
    short headerLength = protocol.readI16();
    // version
    Assert.assertEquals( ThriftCodec.VERSION, protocol.readByte() );
    // service name
    Assert.assertEquals( Demo.Iface.class.getName(), protocol.readString() );
    // dubbo request id
    Assert.assertEquals( request.getId(), protocol.readI64() );

    // test message header length
    if ( bis.markSupported() ) {
        bis.reset();
        bis.skip( headerLength );
    }

    TMessage message = protocol.readMessageBegin();

    Demo.echoString_args args = new Demo.echoString_args();

    args.read( protocol );

    protocol.readMessageEnd();

    Assert.assertEquals( "echoString", message.name );

    Assert.assertEquals( TMessageType.CALL, message.type );

    Assert.assertEquals( "Hello, World!", args.getArg() );

}