10. Thrift Example
struct UserProfile {
1: i32 uid,
class UserStorageHandler : virtual public UserStorageIf {
2: string name,
public:
3: string blurb
UserStorageHandler() {
}
// Your initialization goes here
service UserStorage {
}
void store(1: UserProfile user),
UserProfile retrieve(1: i32 uid)
void store(const UserProfile& user) {
}
// Your implementation goes here
printf("storen");
}
void retrieve(UserProfile& _return, const int32_t uid) {
# Make an object // Your implementation goes here
up = UserProfile(uid=1, printf("retrieven");
name="Mark Slee", }
blurb="I'll find something to put here.") };
# Talk to a server via TCP sockets, using a binary protocol int main(int argc, char **argv) {
transport = TSocket.TSocket("localhost", 9090) int port = 9090;
transport.open() shared_ptr<UserStorageHandler> handler(new UserStorageHandler());
protocol = TBinaryProtocol.TBinaryProtocol(transport) shared_ptr<TProcessor> processor(new UserStorageProcessor(handler));
shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
# Use the service we already defined shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFac
service = UserStorage.Client(protocol) shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory(
service.store(up) TSimpleServer server(processor, serverTransport, transportFactory, proto
server.serve();
# Retrieve something as well return 0;
up2 = service.retrieve(2) }
22. server runner
def thread_pool_server_runner(app, global_conf, **kwargs):
for name in ['port', 'pool_size']:
if name in kwargs:
kwargs[name] = int(kwargs[name])
pool_size = kwargs.pop('pool_size')
host = kwargs.pop('host', '0.0.0.0')
transport = TSocket.TServerSocket(**kwargs)
transport.host = host
tfactory = TTransport.TBufferedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
server = ThreadPoolServer(app, transport, tfactory, pfactory)
if pool_size:
server.threads = pool_size
server.serve()