Thrift应用(linux)

安装过程已经在上一篇中介绍过,搞了好几天终于弄好了,一开始装了个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) 编译通过,测试;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值