From b9913ffd42258f17818c911d0f249aa1877af438 Mon Sep 17 00:00:00 2001 From: Determinant Date: Tue, 18 Feb 2020 19:26:55 -0500 Subject: get it almost working --- test/test_msg.cpp | 2 +- test/test_p2p.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++++++++-- test/test_p2p_tls.cpp | 2 +- 3 files changed, 65 insertions(+), 4 deletions(-) (limited to 'test') diff --git a/test/test_msg.cpp b/test/test_msg.cpp index ee13166..d21b7f3 100644 --- a/test/test_msg.cpp +++ b/test/test_msg.cpp @@ -64,7 +64,7 @@ struct MsgTest { const opcode_t MsgTest::opcode; int main() { - salticidae::MsgBase msg(MsgTest(10)); + salticidae::MsgBase msg(MsgTest(10), 0x0); printf("%s\n", std::string(msg).c_str()); MsgTest parse(msg.get_payload()); return 0; 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 -- 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" @@ -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)); diff --git a/test/test_p2p_tls.cpp b/test/test_p2p_tls.cpp index 93cefac..10dacff 100644 --- a/test/test_p2p_tls.cpp +++ b/test/test_p2p_tls.cpp @@ -296,7 +296,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) { -- cgit v1.2.3