Last active
February 17, 2016 18:32
-
-
Save wilbeibi/ee05f49c00d33d973558 to your computer and use it in GitHub Desktop.
union test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.wilbeibi.test; | |
import org.apache.thrift.TException; | |
import org.apache.thrift.protocol.TBinaryProtocol; | |
import org.apache.thrift.protocol.TProtocol; | |
import org.apache.thrift.transport.TSocket; | |
import org.apache.thrift.transport.TTransport; | |
import org.apache.thrift.transport.TTransportException; | |
import com.wilbeibi.thrift.*; | |
public class Client { | |
public void startClient() { | |
TTransport transport; | |
try { | |
transport = new TSocket("localhost", 9090); | |
TProtocol protocol = new TBinaryProtocol(transport); | |
MyTest.Client client = new MyTest.Client(protocol); | |
transport.open(); | |
Box box = client.echoUnion(1); | |
System.out.println(box.toString()); | |
Box box2 = client.echoUnion(2); | |
System.out.println(box2.toString()); | |
transport.close(); | |
} catch (TTransportException e) { | |
e.printStackTrace(); | |
} catch (TException e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void main(String[] args) { | |
Client client = new Client(); | |
client.startClient(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace java com.wilbeibi.thrift | |
union Value { | |
1: i16 i16_v, | |
2: string str_v, | |
} | |
struct Box { | |
1: Value value; | |
} | |
service MyTest { | |
Box echoUnion(1: i32 number); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This autogenerated skeleton file illustrates how to build a server. | |
// You should copy it to another filename to avoid overwriting it. | |
#include "MyTest.h" | |
#include <thrift/protocol/TBinaryProtocol.h> | |
#include <thrift/server/TSimpleServer.h> | |
#include <thrift/transport/TServerSocket.h> | |
#include <thrift/transport/TBufferTransports.h> | |
using namespace ::apache::thrift; | |
using namespace ::apache::thrift::protocol; | |
using namespace ::apache::thrift::transport; | |
using namespace ::apache::thrift::server; | |
using boost::shared_ptr; | |
class MyTestHandler : virtual public MyTestIf { | |
public: | |
MyTestHandler() { | |
// Your initialization goes here | |
} | |
void echoUnion(Box& _return, const int32_t number) { | |
// Your implementation goes here | |
printf("Into echoUnion\n"); | |
if (number % 2 == 0) { | |
Value v; | |
v.__set_i16_v(100); | |
v.__isset.i16_v = true; | |
_return.__set_value(v); | |
printf("Even number set int32\n"); | |
} else { | |
Value v; | |
v.__set_str_v("String value"); | |
v.__isset.str_v = true; | |
_return.__set_value(v); | |
printf("Odd number set string\n"); | |
} | |
printf("echoUnion\n"); | |
} | |
}; | |
int main(int argc, char **argv) { | |
int port = 9090; | |
shared_ptr<MyTestHandler> handler(new MyTestHandler()); | |
shared_ptr<TProcessor> processor(new MyTestProcessor(handler)); | |
shared_ptr<TServerTransport> serverTransport(new TServerSocket(port)); | |
shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory()); | |
shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory()); | |
TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory); | |
printf("Server is running on %d\n", port); | |
server.serve(); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment