安装过程已经在上一篇中介绍过,搞了好几天终于弄好了,一开始装了个0.8.0版本的thrift,有错误,后来换成0.7.0版本的thrift就可以了......
(1) 写thrift配置文件 thrift_configure.thrift
service Test{
string recognize(1: string bar_code)
}
还可以定义结构体。
(2) 生成c++文件
/usr/local/thrift/bin/thrift --gen cpp thrift_configure.thrift
就会看到新生成了一个文件夹gen-cpp
打开文件夹会有7个文件,我们要修改的文件则是:Test_server.cpp
(3) server端
打开文件Test_server.cpp,修改如下:
#include "Test.h"
#include <config.h>
#include <protocol/TCompactProtocol.h>
#include <server/TSimpleServer.h>
#include <transport/TServerSocket.h>
#include <transport/TBufferTransports.h>
#include <concurrency/ThreadManager.h>
#include <concurrency/PosixThreadFactory.h>
#include <server/TThreadPoolServer.h>
#include <server/TThreadedServer.h>
#include "test.h" // 包括了自己的逻辑
#include <string>
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;
using namespace ::apache::thrift::concurrency;
using boost::shared_ptr;
class BarcodeRecognitionServerHandler : virtual public TestServerIf {
public:
TestHandler() {
// Your initialization goes here
}
void recognize(std::string& _return, const std::string& bar_code) {
// Your implementation goes here 在这儿添加自己的逻辑调用
printf("recognize:%s\t\n",bar_code.c_str());
_return = recognize_c(bar_code);
}
};
int main(int argc, char **argv) {
shared_ptr<TestHandler> handler(new TestHandler());
shared_ptr<TProcessor> processor(new TestProcessor(handler));
shared_ptr<TProtocolFactory> protocolFactory(new TCompactProtocolFactory());
shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
shared_ptr<TServerTransport> serverTransport(new TServerSocket(9090));
shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(10);
shared_ptr<PosixThreadFactory> threadFactory = shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
threadManager->threadFactory(threadFactory);
threadManager->start();
TThreadPoolServer server(processor, serverTransport, transportFactory, protocolFactory, threadManager);
server.serve();
return 0;
}
(4) client端 新建文件Test_client.cpp
#include "Test.h"
#include <config.h>
#include <transport/TSocket.h>
#include <transport/TBufferTransports.h>
#include <protocol/TCompactProtocol.h>
#include <string>
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace std;
using boost::shared_ptr;
int main(int argc, char **argv) {
boost::shared_ptr<TSocket> socket(new TSocket("localhost", 9090));
boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
boost::shared_ptr<TProtocol> protocol(new TCompactProtocol(transport));
transport->open();
TestClient client(protocol);
string path="test";
client.recognize(path,path);
transport->close();
}
(5) makefile内容如下:
BOOST_DIR = /usr/include/boost
THRIFT_DIR = /usr/local/thrift/include/thrift
LIB_DIR = /usr/local/lib
THRIFT_LIB=/usr/local/thrift/lib/
GEN_SRC = ./gen-cpp/test_types.cpp ./gen-cpp/test_constants.cpp ./gen-cpp/Test.cpp
default: Server Client
Server : Test_Server.cpp
g++ -g -o Server -I${THRIFT_DIR} -I${BOOST_DIR} -I./gen-cpp -L${LIB_DIR} -L${THRIFT_LIB} -lthrift Test_Server.cpp ${GEN_SRC} -g
Client : Test_Client.cpp
g++ -g -o Client -I${THRIFT_DIR} -I${BOOST_DIR} -I./gen-cpp -L${LIB_DIR} -L${THRIFT_LIB} -lthrift Test_Client.cpp ${GEN_SRC} -g
clean:
$(RM) -r Server Client
(6) 编译通过,测试;