From 7b06b11c20d70f61e3a0a17c7e7a2fdcef0a8757 Mon Sep 17 00:00:00 2001 From: Determinant Date: Thu, 20 Feb 2020 23:42:43 -0500 Subject: ... --- test/test_p2p.cpp | 3 +-- test/test_p2p_tls.cpp | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/test_p2p.cpp b/test/test_p2p.cpp index 4b6d451..e289b45 100644 --- a/test/test_p2p.cpp +++ b/test/test_p2p.cpp @@ -120,9 +120,8 @@ struct Net { } void add_peer(const std::string &listen_addr) { - NetAddr addr(listen_addr); try { - net->add_peer(addr); + net->add_peer(NetAddr(listen_addr)); } catch (std::exception &err) { fprintf(stdout, "net %lu: got error during a sync call: %s\n", id, err.what()); } diff --git a/test/test_p2p_tls.cpp b/test/test_p2p_tls.cpp index 10dacff..59a7859 100644 --- a/test/test_p2p_tls.cpp +++ b/test/test_p2p_tls.cpp @@ -153,6 +153,22 @@ struct Net { } } + void set_peer_addr(const NetAddr &addr) { + try { + net->set_peer_addr(addr, addr); + } catch (std::exception &err) { + fprintf(stdout, "net %lu: got error during a sync call: %s\n", id, err.what()); + } + } + + void conn_peer(const NetAddr &addr) { + try { + net->conn_peer(addr); + } catch (std::exception &err) { + fprintf(stdout, "net %lu: got error during a sync call: %s\n", id, err.what()); + } + } + void del_peer(const std::string &listen_addr) { try { net->del_peer(NetAddr(listen_addr)); @@ -278,6 +294,46 @@ int main(int argc, char **argv) { it->second->del_peer(it2->second->listen_addr); }; + auto cmd_setpeeraddr = [](char *buff) { + int id = read_int(buff); + if (id < 0) return; + auto it = nets.find(id); + if (it == nets.end()) + { + fprintf(stdout, "net id does not exist\n"); + return; + } + int id2 = read_int(buff); + if (id2 < 0) return; + auto it2 = nets.find(id2); + if (it2 == nets.end()) + { + fprintf(stdout, "net id does not exist\n"); + return; + } + it->second->set_peer_addr(it2->second->listen_addr); + }; + + auto cmd_connpeer = [](char *buff) { + int id = read_int(buff); + if (id < 0) return; + auto it = nets.find(id); + if (it == nets.end()) + { + fprintf(stdout, "net id does not exist\n"); + return; + } + int id2 = read_int(buff); + if (id2 < 0) return; + auto it2 = nets.find(id2); + if (it2 == nets.end()) + { + fprintf(stdout, "net id does not exist\n"); + return; + } + it->second->conn_peer(it2->second->listen_addr); + }; + auto cmd_msg = [](char *buff) { int id = read_int(buff); if (id < 0) return; @@ -309,6 +365,8 @@ int main(int argc, char **argv) { fprintf(stdout, "add -- start a node (create a PeerNetwork instance)\n" "addpeer -- add a peer to a given node\n" + "setpeeraddr -- set the peer addr\n" + "connpeer -- try to connect to the peer\n" "delpeer -- add a peer to a given node\n" "del -- remove a node (destroy a PeerNetwork instance)\n" "msg -- send a text message to a node\n" @@ -321,6 +379,8 @@ int main(int argc, char **argv) { cmd_map.insert(std::make_pair("add", cmd_add)); cmd_map.insert(std::make_pair("addpeer", cmd_addpeer)); + cmd_map.insert(std::make_pair("setpeeraddr", cmd_setpeeraddr)); + cmd_map.insert(std::make_pair("connpeer", cmd_connpeer)); cmd_map.insert(std::make_pair("del", cmd_del)); cmd_map.insert(std::make_pair("delpeer", cmd_delpeer)); cmd_map.insert(std::make_pair("msg", cmd_msg)); -- cgit v1.2.3-70-g09d2