Example usage for org.apache.thrift.transport TTransport read

List of usage examples for org.apache.thrift.transport TTransport read

Introduction

In this page you can find the example usage for org.apache.thrift.transport TTransport read.

Prototype

public abstract int read(byte[] buf, int off, int len) throws TTransportException;

Source Link

Document

Reads up to len bytes into buffer buf, starting at offset off.

Usage

From source file:com.alibaba.dubbo.rpc.protocol.thrift.ThriftCodecTest.java

License:Open Source License

@Test
public void testEncodeRequest() throws Exception {

    Request request = createRequest();

    ByteArrayOutputStream output = new ByteArrayOutputStream(1024);

    codec.encode(channel, output, request);

    byte[] bytes = output.toByteArray();

    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);

    TTransport transport = new TIOStreamTransport(bis);

    TBinaryProtocol protocol = new TBinaryProtocol(transport);

    // frame/*  w w w.ja  v  a2s .c o m*/
    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());

}

From source file:org.apache.accumulo.monitor.ZooKeeperStatus.java

License:Apache License

@Override
public void run() {

    while (!stop) {

        TreeSet<ZooKeeperState> update = new TreeSet<>();

        String zookeepers[] = SiteConfiguration.getInstance().get(Property.INSTANCE_ZK_HOST).split(",");
        for (String keeper : zookeepers) {
            int clients = 0;
            String mode = "unknown";

            String[] parts = keeper.split(":");
            TTransport transport = null;
            try {
                HostAndPort addr;//from  ww  w  . ja  v a 2 s .  c  o m
                if (parts.length > 1)
                    addr = HostAndPort.fromParts(parts[0], Integer.parseInt(parts[1]));
                else
                    addr = HostAndPort.fromParts(parts[0], 2181);

                transport = TTimeoutTransport.create(addr, 10 * 1000l);
                transport.write("stat\n".getBytes(UTF_8), 0, 5);
                StringBuilder response = new StringBuilder();
                try {
                    transport.flush();
                    byte[] buffer = new byte[1024 * 100];
                    int n = 0;
                    while ((n = transport.read(buffer, 0, buffer.length)) > 0) {
                        response.append(new String(buffer, 0, n, UTF_8));
                    }
                } catch (TTransportException ex) {
                    // happens at EOF
                }
                for (String line : response.toString().split("\n")) {
                    if (line.startsWith(" "))
                        clients++;
                    if (line.startsWith("Mode"))
                        mode = line.split(":")[1];
                }
                update.add(new ZooKeeperState(keeper, mode, clients));
            } catch (Exception ex) {
                log.info("Exception talking to zookeeper " + keeper, ex);
                update.add(new ZooKeeperState(keeper, "Down", -1));
            } finally {
                if (transport != null) {
                    try {
                        transport.close();
                    } catch (Exception ex) {
                        log.error("Exception", ex);
                    }
                }
            }
        }
        status = update;
        UtilWaitThread.sleep(5 * 1000);
    }
}

From source file:org.apache.accumulo.server.monitor.ZooKeeperStatus.java

License:Apache License

@Override
public void run() {

    while (!stop) {

        TreeSet<ZooKeeperState> update = new TreeSet<ZooKeeperState>();

        String zookeepers[] = ServerConfiguration.getSiteConfiguration().get(Property.INSTANCE_ZK_HOST)
                .split(",");
        for (String keeper : zookeepers) {
            int clients = 0;
            String mode = "unknown";

            String[] parts = keeper.split(":");
            TTransport transport = null;
            try {
                HostAndPort addr;//from ww w.  j av  a  2 s  .co m
                if (parts.length > 1)
                    addr = HostAndPort.fromParts(parts[0], Integer.parseInt(parts[1]));
                else
                    addr = HostAndPort.fromParts(parts[0], 2181);

                transport = TTimeoutTransport.create(addr, 10 * 1000l);
                transport.write("stat\n".getBytes(), 0, 5);
                StringBuilder response = new StringBuilder();
                try {
                    transport.flush();
                    byte[] buffer = new byte[1024 * 100];
                    int n = 0;
                    while ((n = transport.read(buffer, 0, buffer.length)) > 0) {
                        response.append(new String(buffer, 0, n));
                    }
                } catch (TTransportException ex) {
                    // happens at EOF
                }
                for (String line : response.toString().split("\n")) {
                    if (line.startsWith(" "))
                        clients++;
                    if (line.startsWith("Mode"))
                        mode = line.split(":")[1];
                }
                update.add(new ZooKeeperState(keeper, mode, clients));
            } catch (Exception ex) {
                log.info("Exception talking to zookeeper " + keeper, ex);
                update.add(new ZooKeeperState(keeper, "Down", -1));
            } finally {
                if (transport != null) {
                    try {
                        transport.close();
                    } catch (Exception ex) {
                        log.error(ex, ex);
                    }
                }
            }
        }
        status = update;
        UtilWaitThread.sleep(1000);
    }
}

From source file:org.apache.dubbo.rpc.protocol.thrift.ThriftCodecTest.java

License:Apache License

@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);/*from w  w  w. j a v a 2  s  .  c o m*/

    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());

}