Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
refactor(parity-cpp-example) : cpp:ify
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasad1 committed Nov 20, 2018
1 parent 7f4d280 commit d8d5e66
Showing 1 changed file with 33 additions and 27 deletions.
60 changes: 33 additions & 27 deletions parity-clib-examples/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <chrono>
#include <parity.h>
#include <regex>
#include <string>
#include <thread>

void* parity_light_run();
void* parity_full_run();
Expand All @@ -33,10 +31,10 @@ const int SUBSCRIPTION_ID_LEN = 18;
static int g_rpc_counter = 0;

// global string for callbacks
static char g_str[60];
static std::string g_str;

// list of rpc queries
static const char* rpc_queries[] = {
static std::vector<std::string> rpc_queries {
"{\"method\":\"parity_versionInfo\",\"params\":[],\"id\":1,\"jsonrpc\":\"2.0\"}",
"{\"method\":\"eth_getTransactionReceipt\",\"params\":[\"0x444172bef57ad978655171a8af2cfd89baa02a97fcb773067aef7794d6913fff\"],\"id\":1,\"jsonrpc\":\"2.0\"}",
"{\"method\":\"eth_estimateGas\",\"params\":[{\"from\":\"0x0066Dc48bb833d2B59f730F33952B3c29fE926F5\"}],\"id\":1,\"jsonrpc\":\"2.0\"}",
Expand All @@ -50,8 +48,9 @@ void on_restart(void*, const char*, size_t) {}
void ws_response(void* _unused, const char* response, size_t len) {
printf("ws_response: %s\r\n", response);
std::regex is_subscription ("\\{\"jsonrpc\":\"2.0\",\"result\":\"0[xX][a-fA-F0-9]{16}\",\"id\":1\\}");
if (std::regex_match(response, is_subscription) == true) {
strncpy(g_str, response, 55);
// assume only one subscription is used
if (std::regex_match(response, is_subscription) == true && g_str.empty()) {
g_str = response;
}
}

Expand Down Expand Up @@ -107,13 +106,12 @@ int parity_rpc_queries(void* parity) {
return 1;
}

size_t num_queries = sizeof(rpc_queries) / sizeof(rpc_queries[0]);
size_t num_queries = rpc_queries.size();
size_t timeout = 1000;
g_rpc_counter = num_queries;


for (int i = 0; i < num_queries; i++) {
if (parity_rpc(parity, rpc_queries[i], strlen(rpc_queries[i]), timeout, rpc_response) != 0) {
for (auto query : rpc_queries) {
if (parity_rpc(parity, query.c_str(), query.length(), timeout, rpc_response) != 0) {
return 1;
}
}
Expand All @@ -130,24 +128,24 @@ int parity_subscribe_to_websocket(void* parity) {

size_t timeout = 1000;
int num_queries = 1;
g_str[0] = 0;
g_str.clear();

char subscribe[] = "{\"method\":\"eth_subscribe\",\"params\":[\"newHeads\"],\"id\":1,\"jsonrpc\":\"2.0\"}";
char unsubscribe[] = "{\"method\":\"eth_unsubscribe\",\"params\":[\"0x1234567891234567\"],\"id\":1,\"jsonrpc\":\"2.0\"}";
std::string subscribe = "{\"method\":\"eth_subscribe\",\"params\":[\"newHeads\"],\"id\":1,\"jsonrpc\":\"2.0\"}";
std::string unsubscribe = "{\"method\":\"eth_unsubscribe\",\"params\":[\"0x1234567891234567\"],\"id\":1,\"jsonrpc\":\"2.0\"}";

const void *const handle = parity_subscribe_ws(parity, subscribe, strlen(subscribe), ws_response);
const void *const handle = parity_subscribe_ws(parity, subscribe.c_str(), subscribe.length(), ws_response);

if (!handle) {
return 1;
}

while(g_str[0] == 0);
sleep(60);
while(g_str.empty());
std::this_thread::sleep_for(std::chrono::seconds(60));

// Replace subscription_id with the id we got in the callback
// (this is not a good practice use your favorite JSON parser)
strncpy(&unsubscribe[39], &g_str[27], SUBSCRIPTION_ID_LEN);
if (parity_unsubscribe_ws(parity, handle, unsubscribe, strlen(unsubscribe), timeout, ws_response) != 0) {
unsubscribe.replace(39, SUBSCRIPTION_ID_LEN, g_str, 27, SUBSCRIPTION_ID_LEN);
if (parity_unsubscribe_ws(parity, handle, unsubscribe.c_str(), unsubscribe.length(), timeout, ws_response) != 0) {
return 1;
}

Expand All @@ -161,10 +159,14 @@ void* parity_full_run() {
.on_client_restart_cb_custom = nullptr
};

const char* args[] = {"--no-ipc" , "--jsonrpc-apis=all", "--chain", "kovan"};
size_t str_lens[] = {strlen(args[0]), strlen(args[1]), strlen(args[2]), strlen(args[3])};
std::vector<const char*> args = {"--no-ipc" , "--jsonrpc-apis=all", "--chain", "kovan"};
std::vector<size_t> strs_len;

for (auto arg: args) {
strs_len.push_back(std::strlen(arg));
}

if (parity_config_from_cli(args, str_lens, sizeof(str_lens) / sizeof(str_lens[0]), &cfg.configuration) != 0) {
if (parity_config_from_cli(&args[0], &strs_len[0], args.size(), &cfg.configuration) != 0) {
return nullptr;
}

Expand All @@ -183,10 +185,14 @@ void* parity_light_run() {
.on_client_restart_cb_custom = nullptr
};

const char* args[] = {"--light", "--no-ipc","--chain", "kovan", "--jsonrpc-apis=all"};
size_t str_lens[] = {strlen(args[0]), strlen(args[1]), strlen(args[2]), strlen(args[3]), strlen(args[4])};
std::vector<const char*> args = {"--no-ipc" , "--jsonrpc-apis=all", "--chain", "kovan"};
std::vector<size_t> str_lens;

for (auto arg: args) {
str_lens.push_back(std::strlen(arg));
}

if (parity_config_from_cli(args, str_lens, sizeof(str_lens) / sizeof(str_lens[0]), &cfg.configuration) != 0) {
if (parity_config_from_cli(&args[0], &str_lens[0], str_lens.size(), &cfg.configuration) != 0) {
return nullptr;
}

Expand Down

0 comments on commit d8d5e66

Please sign in to comment.