C++でthriftの最初の例を書きます

4624 ワード

Table of Contents
  • 1. thriftファイル
  • の作成
  • 2. C++コード
  • を生成
  • 3. C++クライアントコード作成
  • 4. サンプルアイテム
  • 4.1. プロジェクトディレクトリ
  • 4.2. コンパイルサービス
  • 4.3. コンパイルクライアント
  • 4.4. 運転

  • 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サービスとして使用しており、性能は低いが、実現は簡単で、プロセス管理クラスのインタフェースに適している.これは今私に必要なものです.私のプログラムの一つは、大量のデータを渡すのではなく、ワークプロセスをリモートでステータスクエリーとタスク制御する必要があるからです.次に、自動生成コードを示します.
    // 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 
    #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  ::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
    "); } }; int main(int argc, char **argv) { int port = 9090; shared_ptr handler(new WorkerManagerHandler()); shared_ptr processor(new WorkerManagerProcessor(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; }

    3 C++クライアントコードの作成
    クライアント・プログラムは自分で作成する必要があります.
    #include 
    
    #include 
    #include 
    #include 
    
    #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 socket(new TSocket("localhost", 9090));
      boost::shared_ptr transport(new TBufferedTransport(socket));
      boost::shared_ptr 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クライアントのコンパイル
    クライアントディレクトリの下の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