From 161d969e0eabfecccd69a6b9ed2d03919cf89cb5 Mon Sep 17 00:00:00 2001 From: Determinant Date: Sun, 18 Nov 2018 16:24:30 -0500 Subject: improve the test programs --- README.rst | 277 ++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 144 insertions(+), 133 deletions(-) (limited to 'README.rst') diff --git a/README.rst b/README.rst index 2a40304..23158e2 100644 --- a/README.rst +++ b/README.rst @@ -1,4 +1,4 @@ -Salticidae: a Minimal C++ asynchronous network library. +Salticidae: Minimal C++ asynchronous network library. ======================================================= .. image:: https://img.shields.io/badge/License-MIT-yellow.svg @@ -67,135 +67,146 @@ Example (MsgNetwork layer) -------------------------- .. code-block:: cpp - #include - #include - #include - - #include "salticidae/msg.h" - #include "salticidae/event.h" - #include "salticidae/network.h" - #include "salticidae/stream.h" - - using salticidae::NetAddr; - using salticidae::DataStream; - using salticidae::MsgNetwork; - using salticidae::htole; - using salticidae::letoh; - using std::placeholders::_1; - using std::placeholders::_2; - - /** Hello Message. */ - struct MsgHello { - static const uint8_t opcode = 0x0; - DataStream serialized; - std::string name; - std::string text; - /** Defines how to serialize the msg. */ - MsgHello(const std::string &name, - const std::string &text) { - serialized << htole((uint32_t)name.length()); - serialized << name << text; - } - /** Defines how to parse the msg. */ - MsgHello(DataStream &&s) { - uint32_t len; - s >> len; - len = letoh(len); - name = std::string((const char *)s.get_data_inplace(len), len); - len = s.size(); - text = std::string((const char *)s.get_data_inplace(len), len); - } - }; - - /** Acknowledgement Message. */ - struct MsgAck { - static const uint8_t opcode = 0x1; - DataStream serialized; - MsgAck() {} - MsgAck(DataStream &&s) {} - }; - - const uint8_t MsgHello::opcode; - const uint8_t MsgAck::opcode; - - using MsgNetworkByteOp = MsgNetwork; - - struct MyNet: public MsgNetworkByteOp { - const std::string name; - const NetAddr peer; - - MyNet(const salticidae::EventContext &ec, - const std::string name, - const NetAddr &peer): - MsgNetwork(ec, 10, 1.0, 4096), - name(name), - peer(peer) { - /* message handler could be a bound method */ - reg_handler(salticidae::generic_bind( - &MyNet::on_receive_hello, this, _1, _2)); - - reg_conn_handler([this](ConnPool::Conn &conn, bool connected) { - if (connected) - { - if (conn.get_mode() == ConnPool::Conn::ACTIVE) - { - printf("[%s] Connected, sending hello.\n", - this->name.c_str()); - /* send the first message through this connection */ - send_msg(MsgHello(this->name, "Hello there!"), - static_cast(conn)); - } - else - printf("[%s] Accepted, waiting for greetings.\n", - this->name.c_str()); - } - else - { - printf("[%s] Disconnected, retrying.\n", this->name.c_str()); - /* try to reconnect to the same address */ - connect(conn.get_addr()); - } - }); - } - - void on_receive_hello(MsgHello &&msg, MyNet::Conn &conn) { - printf("[%s] %s says %s\n", - name.c_str(), - msg.name.c_str(), msg.text.c_str()); - /* send acknowledgement */ - send_msg(MsgAck(), conn); - } - }; - - - void on_receive_ack(MsgAck &&msg, MyNet::Conn &conn) { - auto net = static_cast(conn.get_net()); - printf("[%s] the peer knows\n", net->name.c_str()); - } - - salticidae::EventContext ec; - NetAddr alice_addr("127.0.0.1:12345"); - NetAddr bob_addr("127.0.0.1:12346"); - - int main() { - /* test two nodes */ - MyNet alice(ec, "Alice", bob_addr); - MyNet bob(ec, "Bob", alice_addr); - - /* message handler could be a normal function */ - alice.reg_handler(on_receive_ack); - bob.reg_handler(on_receive_ack); - - alice.start(); - bob.start(); - - alice.listen(alice_addr); - bob.listen(bob_addr); - - /* first attempt */ - alice.connect(bob_addr); - bob.connect(alice_addr); - - ec.dispatch(); - return 0; - } + + #include + #include + #include + + #include "salticidae/msg.h" + #include "salticidae/event.h" + #include "salticidae/network.h" + #include "salticidae/stream.h" + + using salticidae::NetAddr; + using salticidae::DataStream; + using salticidae::MsgNetwork; + using salticidae::htole; + using salticidae::letoh; + using std::placeholders::_1; + using std::placeholders::_2; + + /** Hello Message. */ + struct MsgHello { + static const uint8_t opcode = 0x0; + DataStream serialized; + std::string name; + std::string text; + /** Defines how to serialize the msg. */ + MsgHello(const std::string &name, + const std::string &text) { + serialized << htole((uint32_t)name.length()); + serialized << name << text; + } + /** Defines how to parse the msg. */ + MsgHello(DataStream &&s) { + uint32_t len; + s >> len; + len = letoh(len); + name = std::string((const char *)s.get_data_inplace(len), len); + len = s.size(); + text = std::string((const char *)s.get_data_inplace(len), len); + } + }; + + /** Acknowledgement Message. */ + struct MsgAck { + static const uint8_t opcode = 0x1; + DataStream serialized; + MsgAck() {} + MsgAck(DataStream &&s) {} + }; + + const uint8_t MsgHello::opcode; + const uint8_t MsgAck::opcode; + + using MsgNetworkByteOp = MsgNetwork; + + struct MyNet: public MsgNetworkByteOp { + const std::string name; + const NetAddr peer; + + MyNet(const salticidae::EventContext &ec, + const std::string name, + const NetAddr &peer): + MsgNetwork(ec, MsgNetwork::Config()), + name(name), + peer(peer) { + /* message handler could be a bound method */ + reg_handler( + salticidae::generic_bind(&MyNet::on_receive_hello, this, _1, _2)); + + reg_conn_handler([this](const ConnPool::conn_t &conn, bool connected) { + if (connected) + { + if (conn->get_mode() == ConnPool::Conn::ACTIVE) + { + printf("[%s] Connected, sending hello.\n", + this->name.c_str()); + /* send the first message through this connection */ + send_msg(MsgHello(this->name, "Hello there!"), + salticidae::static_pointer_cast(conn)); + } + else + printf("[%s] Accepted, waiting for greetings.\n", + this->name.c_str()); + } + else + { + printf("[%s] Disconnected, retrying.\n", this->name.c_str()); + /* try to reconnect to the same address */ + connect(conn->get_addr()); + } + }); + } + + void on_receive_hello(MsgHello &&msg, const MyNet::conn_t &conn) { + printf("[%s] %s says %s\n", + name.c_str(), + msg.name.c_str(), msg.text.c_str()); + /* send acknowledgement */ + send_msg(MsgAck(), conn); + } + }; + + + void on_receive_ack(MsgAck &&msg, const MyNet::conn_t &conn) { + auto net = static_cast(conn->get_net()); + printf("[%s] the peer knows\n", net->name.c_str()); + } + + int main() { + salticidae::EventContext ec; + NetAddr alice_addr("127.0.0.1:12345"); + NetAddr bob_addr("127.0.0.1:12346"); + + /* test two nodes in the same main loop */ + MyNet alice(ec, "Alice", bob_addr); + MyNet bob(ec, "Bob", alice_addr); + + /* message handler could be a normal function */ + alice.reg_handler(on_receive_ack); + bob.reg_handler(on_receive_ack); + + /* start all threads */ + alice.start(); + bob.start(); + + /* accept incoming connections */ + alice.listen(alice_addr); + bob.listen(bob_addr); + + /* try to connect once */ + alice.connect(bob_addr); + bob.connect(alice_addr); + + /* the main loop can be shutdown by ctrl-c or kill */ + auto shutdown = [&](int) {ec.stop();}; + salticidae::SigEvent ev_sigint(ec, shutdown); + salticidae::SigEvent ev_sigterm(ec, shutdown); + ev_sigint.add(SIGINT); + ev_sigterm.add(SIGTERM); + + /* enter the main loop */ + ec.dispatch(); + return 0; + } -- cgit v1.2.3