1. 程式人生 > >用C++編寫thrift第一個例子

用C++編寫thrift第一個例子

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!

               


Table of Contents

1 建立thrift檔案

thrift檔案非常簡單,一個WorkerManager提供了一個ping方法,讓客戶端通過RPC方式遠端呼叫,模擬icmp協議的ping,看看服務端是否正常.

# worker.thrift# Dean Chen ([email protected]
)#/** * Thrift files can namespace, package, or prefix their output in various * target languages. */namespace cpp freebird/** * Defining a removed class named WorkerManager */service WorkerManager {  /**   * client calls ping method to make sure service process is active or dead   */   void ping()}

2 生成C++程式碼

用thrift命令列就可以生成C++程式碼,包括伺服器端程式碼:

thrift -r --gen cpp -o ../ worker.thrift

上面的命令會在../目錄建立gen-cpp目錄,裡面包含了所有生成的C++程式碼.

worker_constants.cpp  worker_constants.h  WorkerManager.cpp  WorkerManager.h  WorkerManager_server.skeleton.cpp  worker_types.cpp  worker_types.h

WorkerManager_server.skeleton.cpp就是C++服務端的main函式入口檔案,裡面使用了TSimpleServer作為TCP服務,效能較低,但是實現簡單,比較合適做程序管理類的介面. 這目前就是我需要的.因為我的一個程式中就是需要能夠遠端對worker程序進行狀態查詢和任務控制,而不是傳遞大量的資料. 下面是自動產生的程式碼:

// This autogenerated skeleton file illustrates how to build a server.// You should copy it to another filename to avoid overwriting it.#include "WorkerManager.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;using namespace  ::freebird;class WorkerManagerHandler : virtual public WorkerManagerIf { public:  WorkerManagerHandler() {    // Your initialization goes here  }  /**   * client calls ping method to make sure service process is active or dead   */  void ping() {    // Your implementation goes here    printf("ping\n");  }};int main(int argc, char **argv) {  int port = 9090;  shared_ptr<WorkerManagerHandler> handler(new WorkerManagerHandler());  shared_ptr<TProcessor> processor(new WorkerManagerProcessor(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);  server.serve();  return 0;}

3 編寫C++客戶端程式碼

客戶端程式需要自己編寫:

#include <iostream>#include <thrift/protocol/TBinaryProtocol.h>#include <thrift/transport/TSocket.h>#include <thrift/transport/TTransportUtils.h>#include "../../server/gen-cpp/WorkerManager.h"using namespace std;using namespace apache::thrift;using namespace apache::thrift::protocol;using namespace apache::thrift::transport;using namespace freebird;int main() {  boost::shared_ptr<TTransport> socket(new TSocket("localhost", 9090));  boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));  boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));  WorkerManagerClient client(protocol);  try {    transport->open();    client.ping();    cout << "ping()" << endl;    transport->close();  } catch (TException& tx) {    cout << "ERROR: " << tx.what() << endl;  }}

4 例子專案

專案路徑

4.1 專案目錄

thrift_base$ tree -L 2.├── client│   ├── builder│   ├── include│   └── src└── server    ├── builder    ├── gen-cpp    └── thrift

4.2 編譯服務端

sever目錄下除了thrift目錄裡的thrift檔案外,沒有任何額外的C++程式碼gen-cpp是命令列產生的. 用下面的命令進行編譯

cd builder./rebuild.lsp debug_config.lsp

builder/bin目錄下的thrift_server是可執行程式

4.3 編譯客戶端

client目錄下src目錄裡的main.cc檔案是唯一手寫的程式碼,編譯後在builder/bin目錄下出現thrift_client可執行程式. 用下面的命令進行編譯

cd builder./rebuild.lsp debug_config.lsp

4.4 執行

先啟動服務端程式,監聽9099埠,然後啟動客戶端程式,呼叫服務端的ping方法,服務端控制檯打印出文字. 一切工作正常.

Author: dean

Created: 2015-12-15 二 20:38

Validate


           

給我老師的人工智慧教程打call!http://blog.csdn.net/jiangjunshow

這裡寫圖片描述