Example usage for org.apache.thrift.protocol TBinaryProtocol readStructBegin

List of usage examples for org.apache.thrift.protocol TBinaryProtocol readStructBegin

Introduction

In this page you can find the example usage for org.apache.thrift.protocol TBinaryProtocol readStructBegin.

Prototype

@Override
    public TStruct readStructBegin() throws TException 

Source Link

Usage

From source file:com.sample.HelloWorldWithUnity3d.PingVerticle.java

License:Apache License

void startWebsocketServerWithThrift() {
    final EventBus eventBus = vertx.eventBus();
    final Pattern chatUrlPattern = Pattern.compile("/chat/(\\w+)");

    final ThriftHandler<Protocol, ServerWebSocket> thandler = new ThriftHandler<Protocol, ServerWebSocket>();
    thandler.AddHandler(TestReq.class, Protocol.Test1, new ThriftHandler.Handler<ServerWebSocket, TestReq>() {

        @Override//from   www  .  j  ava 2 s  .  c o m
        public void DoHandler(ServerWebSocket ws, TestReq req) {
            // TODO Auto-generated method stub
            ByteArrayOutputStream outStream = new ByteArrayOutputStream(256);
            TBinaryProtocol tProtocol = new TBinaryProtocol(new TIOStreamTransport(null, outStream));

            try {
                TestAck testAck = new TestAck();
                testAck.header = new Header();
                testAck.header.key = Protocol.Test1;
                testAck.header.ok = 1;
                //testAck.setIvalue(true);
                testAck.value = "aa";

                testAck.write(tProtocol);
            } catch (TException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            ws.writeBinaryFrame(new Buffer(outStream.toByteArray()));
        }
    });

    thandler.AddHandler(TestReq.class, Protocol.Test2, new ThriftHandler.Handler<ServerWebSocket, TestReq2>() {

        @Override
        public void DoHandler(ServerWebSocket ws, TestReq2 req) {
            // TODO Auto-generated method stub
            ByteArrayOutputStream outStream = new ByteArrayOutputStream(256);
            TBinaryProtocol tProtocol = new TBinaryProtocol(new TIOStreamTransport(null, outStream));

            try {
                Test2Ack testAck = new Test2Ack();
                testAck.header = new Header();
                testAck.header.key = Protocol.Test1;
                testAck.header.ok = 1;
                //testAck.setIvalue(true);

                testAck.write(tProtocol);
            } catch (TException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            ws.writeBinaryFrame(new Buffer(outStream.toByteArray()));
        }
    });

    vertx.createHttpServer().websocketHandler(new Handler<ServerWebSocket>() {

        @Override
        public void handle(final ServerWebSocket ws) {
            final Matcher m = chatUrlPattern.matcher(ws.path());
            if (!m.matches()) {
                ws.reject();
                return;
            }

            final String chatRoom = m.group(1);
            final String id = ws.textHandlerID();
            System.out.println("registering new connection with id: " + id + " for chat-room: " + chatRoom);
            vertx.sharedData().getSet("chat.room." + chatRoom).add(id);

            ws.closeHandler(new Handler<Void>() {
                @Override
                public void handle(final Void event) {
                    System.out.println(
                            "un-registering connection with id: " + id + " from chat-room: " + chatRoom);
                    vertx.sharedData().getSet("chat.room." + chatRoom).remove(id);
                }
            });

            ws.dataHandler(new Handler<Buffer>() {
                @Override
                public void handle(final Buffer data) {

                    try {
                        ByteArrayInputStream stream = new ByteArrayInputStream(data.getBytes());
                        TProtocol tProtocol = new TBinaryProtocol(new TIOStreamTransport(stream, null));
                        tProtocol.readStructBegin();
                        tProtocol.readFieldBegin();
                        Header header = new Header();
                        header.read(tProtocol);
                        stream.reset();

                        thandler.DoHandle(ws, Protocol.Test1, tProtocol);

                    } catch (Exception e) {
                        ws.reject();
                    }
                }
            });

        }
    }).listen(8091);

}

From source file:zipkin.reporter.libthrift.InternalScribeCodec.java

License:Apache License

static boolean parseResponse(TBinaryProtocol iprot) throws TException {
    iprot.readStructBegin();
    TField schemeField;// w  w  w.  jav  a2s  . c  om
    while ((schemeField = iprot.readFieldBegin()).type != TType.STOP) {
        if (schemeField.id == 0 /* SUCCESS */ && schemeField.type == TType.I32) {
            return iprot.readI32() == 0;
        } else {
            TProtocolUtil.skip(iprot, schemeField.type);
        }
    }
    throw new TApplicationException(MISSING_RESULT, "Log failed: unknown result");
}