Published 15 October 2018

Framework

Framework

For API methods calls, we first create a "framework" that will establish a connection to the node running on the local machine. The C++ code is shown below. In the future we will simply change the call code between the comments  /** api code begin */ and /** api code end */

#include <thrift\stdcxx.h>

#include <thrift\transport\TSocket.h>

#include <thrift\transport\TBufferTransports.h>

#include <thrift\protocol\TBinaryProtocol.h>

 

#include <APIHandler.h>

 

using namespace apache::thrift::transport;

using namespace apache::thrift::protocol;

using namespace api;

 

int main()

{

         std::shared_ptr<TSocket> socket = std::shared_ptr<TSocket>(new TSocket("127.0.0.1", 9090));

         std::shared_ptr<TTransport> transport = std::shared_ptr<TTransport>(new TBufferedTransport(socket));

         std::shared_ptr<TProtocol> protocol = std::shared_ptr<TProtocol>(new TBinaryProtocol(transport));

         std::shared_ptr<APIClient> api = std::shared_ptr<APIClient>(new APIClient(protocol));

 

         try

         {

                  transport->open();

         }

         catch (...)

         {

                  printf("thrift error: failed connect to node\n");

         }

 

         if (transport->isOpen())

         {

                  /** api code begin */

 

                  // do something

 

                  /** api code end */

 

                  transport->close();

         }

 

         system("pause");

         return 0;

}

21 Useful article

Contents