Processing Apache Thrift lists / maps in C ++

Firstly, I will say that I am not the most competent C ++ programmer, but I study and enjoy the power of Trottft.

I implemented a Thrift service with some basic functions that return void, i32 and a list. I am using a Python client controlled by a Django web application to make RPC calls and it works very well. The generated code is pretty straight forward, with the exception of the returned lists:

. crack description file:

namespace cpp Remote enum N_PROTO { N_TCP, N_UDP, N_ANY } service Rcon { i32 ping() i32 KillFlows() i32 RestartDispatch() i32 PrintActiveFlows() i32 PrintActiveListeners(1:i32 proto) list<string> ListAllFlows() } 

Generated Signatures from Rcon.h:

  int32_t ping(); int32_t KillFlows(); int32_t RestartDispatch(); int32_t PrintActiveFlows(); int32_t PrintActiveListeners(const int32_t proto); int64_t ListenerBytesReceived(const int32_t id); void ListAllFlows(std::vector<std::string> & _return); 

As you can see, the generated ListAllFlows () function accepts a link to a row vector. I think I expect it to return a row vector, as described in the .thrift description.

I am wondering if I should provide the function with a row vector to modify, and then Thrift will handle its return to my client, even though the function returns void.

I can not find absolutely no resources or examples of using lists of Thrift <> types in C ++. Any recommendations would be appreciated.

+2
source share
2 answers

Ok, I figured it out. It is pretty simple.

 void ListAllFlows(std::vector<std::string> & _return) { for(int x = 0; x < 5; x++) { _return.push_back(std::string("hi")); } } 

Then, the Python client simply calls it, as it looks in the .thrift file:

 result = client.ListAllFlows() print result # ['hi', 'hi', 'hi', 'hi', 'hi'] 
+3
source

Returning the container class directly works, but has several drawbacks, which I described in more detail here .

0
source

Source: https://habr.com/ru/post/1490614/


All Articles