diff options
author | Determinant <[email protected]> | 2020-02-18 19:26:55 -0500 |
---|---|---|
committer | Determinant <[email protected]> | 2020-02-18 19:26:55 -0500 |
commit | b9913ffd42258f17818c911d0f249aa1877af438 (patch) | |
tree | 44bee2503ea0a70077eb311a9cfc474ddd5e401a /test/test_p2p.cpp | |
parent | c18cf986facd3c37b85bf20087bdf04537b555f2 (diff) |
get it almost working
Diffstat (limited to 'test/test_p2p.cpp')
-rw-r--r-- | test/test_p2p.cpp | 65 |
1 files changed, 63 insertions, 2 deletions
diff --git a/test/test_p2p.cpp b/test/test_p2p.cpp index aff712a..4b6d451 100644 --- a/test/test_p2p.cpp +++ b/test/test_p2p.cpp @@ -120,8 +120,25 @@ struct Net { } void add_peer(const std::string &listen_addr) { + NetAddr addr(listen_addr); try { - net->add_peer(NetAddr(listen_addr)); + net->add_peer(addr); + } catch (std::exception &err) { + fprintf(stdout, "net %lu: got error during a sync call: %s\n", id, err.what()); + } + } + + 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()); } @@ -249,6 +266,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; @@ -267,7 +324,7 @@ int main(int argc, char **argv) { return; } scanf("%64s", buff); - it->second->net->send_msg(MsgText(id, buff), it2->second->listen_addr); + it->second->net->send_msg(MsgText(id, buff), NetAddr(it2->second->listen_addr)); }; auto cmd_sleep = [](char *buff) { @@ -280,6 +337,8 @@ int main(int argc, char **argv) { fprintf(stdout, "add <node-id> <port> -- start a node (create a PeerNetwork instance)\n" "addpeer <node-id> <peer-id> -- add a peer to a given node\n" + "setpeeraddr <node-id> <peer-id> -- set the peer addr\n" + "connpeer <node-id> <peer-id> -- try to connect to the peer\n" "delpeer <node-id> <peer-id> -- add a peer to a given node\n" "del <node-id> -- remove a node (destroy a PeerNetwork instance)\n" "msg <node-id> <peer-id> <msg> -- send a text message to a node\n" @@ -292,6 +351,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)); |