Chinaunix首页 | 论坛 | 博客
  • 博客访问: 283458
  • 博文数量: 40
  • 博客积分: 1807
  • 博客等级: 上尉
  • 技术积分: 350
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-03 15:42
文章分类

全部博文(40)

文章存档

2011年(18)

2010年(20)

2009年(2)

我的朋友

分类: LINUX

2010-09-25 15:31:46

Getting started

The first thing you need to know is that the C++ code generated by Thrift compiles only on Unix based systems, although some success has been reported using Cygwin on Win32 in .

Requirements

Make sure that your system meets the requirements as noted in

  • Thrift library files
  • Thrift header files.

Installing the Thrift library

Installing the Thrift library is trivial to link with the generated code.

  1. Download a snapshot of Thrift and extract if you haven't done so already -

  2. Navigate to lib/cpp using the terminal of your choice

  3. Run  make to compile

  4. Run  make install to install the library. Your user needs root permissions.

Generating the server code

In this example we use an imaginary file called your_thrift_file.thrift:

#!/usr/local/bin/thrift --gen cpp

namespace cpp Test

service Something {
i32 ping()
}

Now run:

thrift --gen cpp your_thrift_file.thrift

Exploring the generated code - The Server

The generated code should be under the gen-cpp directory. You will see a number of generated C++ and header files along with an automatically generated server skeleton code (in bold).

  • Something.cpp
  • Something.h
  • Something_server.skeleton.cpp

  • your_thrift_file_constants.cpp
  • your_thrift_file_constants.h
  • your_thrift_file_types.cpp
  • your_thrift_file_types.h

Implementing the Server

Copy the generated server skeleton to a file named Something_server.cpp and keep the original:

cp Something_server.skeleton.cpp Something_server.cpp

When this server is run in console it prints "ping" on the console window each time the function is called from a client.

Here's the autogenerated skeleton file to illustrate how to write a server: Something_server.cpp

#include "Something.h"
#include
#include
#include
#include

using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace apache::thrift::server;

using boost::shared_ptr;

using namespace Test;

class SomethingHandler : virtual public SomethingIf {
public:
SomethingHandler() {
// Your initialization goes here
}

int32_t ping() {
// Your implementation goes here
printf("ping\n");
}

};

int main(int argc, char **argv) {
int port = 9090;
shared_ptr handler(new SomethingHandler());
shared_ptr processor(new SomethingProcessor(handler));
shared_ptr serverTransport(new TServerSocket(port));
shared_ptr transportFactory(new TBufferedTransportFactory());
shared_ptr protocolFactory(new TBinaryProtocolFactory());

TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
server.serve();
return 0;
}

Compiling/Building the server

To quickly build a binary using a single command use:

g++ -Wall -I/usr/local/include/thrift *.cpp -lthrift -o something

Compiling

You need to point your compiler to the thrift include path (CXX flag:  -I/usr/local/include/thrift)

g++ -Wall -I/usr/local/include/thrift -c Something.cpp -o something.o
g++ -Wall -I/usr/local/include/thrift -c Something_server.cpp -o server.o
g++ -Wall -I/usr/local/include/thrift -c your_thrift_file_constants.cpp -o constants.o
g++ -Wall -I/usr/local/include/thrift -c your_thrift_file_types.cpp -o types.o

Linking

You need to point your linker to the thrift library. (Linker flag:  -lthrift  or  -l/usr/local/lib/libthrift.so 

g++ -L/usr/local/lib -lthrift *.o -o Something_server

Writing the client code

thrift generates a client interface, but you have to hook it up a bit on your own.

#include "Something.h"  // As an example

#include
#include
#include

using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;

using namespace Test;

int main(int argc, char **argv) {
boost::shared_ptr socket(new TSocket("localhost", 9090));
boost::shared_ptr transport(new TBufferedTransport(socket));
boost::shared_ptr protocol(new TBinaryProtocol(transport));

SomethingClient client(protocol);
transport->open();
client.ping();
transport->close();

return 0;
}

Compiling

g++ -Wall -I/usr/local/include/thrift -c something_client.cpp -o client.o

Linking

g++ -L/usr/local/lib -lthrift client.o Something.o constants.o types.o -o Something_client

Compiling/Building everything with Makefile

GEN_SRC := Something.cpp your_thrift_file_constants.cpp your_thrift_file_types.cpp
GEN_OBJ := $(patsubst %.cpp,%.o, $(GEN_SRC))

THRIFT_DIR := /usr/local/include/thrift
BOOST_DIR := /usr/local/include

INC := -I$(THRIFT_DIR) -I$(BOOST_DIR)

.PHONY: all clean

all: something_server something_client

%.o: %.cpp
$(CXX) -Wall $(INC) -c $< -o $@

something_server: Something_server.o $(GEN_OBJ)
$(CXX) -L/usr/local/lib -lthrift $^ -o $@

something_client: Something_client.o $(GEN_OBJ)
$(CXX) -L/usr/local/lib -lthrift $^ -o $@

clean:
$(RM) *.o something_server something_client
阅读(1559) | 评论(0) | 转发(0) |
0

上一篇:KeyValue DB之redis

下一篇:scribe using thrift

给主人留下些什么吧!~~