diff --git a/Makefile.am b/Makefile.am index b74e8332..dd8f07ea 100644 --- a/Makefile.am +++ b/Makefile.am @@ -42,11 +42,6 @@ libaedis_a_SOURCES += $(top_srcdir)/src/aedis.cpp libaedis_a_CPPFLAGS = $(MY_CPPFLAGS) noinst_PROGRAMS = -noinst_PROGRAMS += pubsub -pubsub_SOURCES = $(top_srcdir)/examples/pubsub.cpp -pubsub_CPPFLAGS = $(MY_CPPFLAGS) -pubsub_LDADD = $(MY_LDADD) - noinst_PROGRAMS += advanced advanced_SOURCES = $(top_srcdir)/examples/advanced.cpp advanced_CPPFLAGS = $(MY_CPPFLAGS) @@ -67,6 +62,11 @@ basic3_SOURCES = $(top_srcdir)/examples/basic3.cpp basic3_CPPFLAGS = $(MY_CPPFLAGS) basic3_LDADD = $(MY_LDADD) +noinst_PROGRAMS += basic4 +basic4_SOURCES = $(top_srcdir)/examples/basic4.cpp +basic4_CPPFLAGS = $(MY_CPPFLAGS) +basic4_LDADD = $(MY_LDADD) + noinst_PROGRAMS += containers containers_SOURCES = $(top_srcdir)/examples/containers.cpp containers_CPPFLAGS = $(MY_CPPFLAGS) diff --git a/examples/advanced.cpp b/examples/advanced.cpp index 0a3ab5bc..4d867b3c 100644 --- a/examples/advanced.cpp +++ b/examples/advanced.cpp @@ -128,7 +128,7 @@ net::awaitable reader(tcp_socket& socket, std::queue& reqs) net::awaitable advanced() { - auto socket = co_await make_connection(); + auto socket = co_await make_connection("127.0.0.1", "6379"); std::queue reqs; reqs.push({}); diff --git a/examples/basic1.cpp b/examples/basic1.cpp index 9a42b9ab..e0634118 100644 --- a/examples/basic1.cpp +++ b/examples/basic1.cpp @@ -20,15 +20,15 @@ using aedis::resp3::async_read; namespace net = aedis::net; -/** A simple example that illustrates the basic principles. Three commands are - * sent in the same request +/** A simple example that illustrates the basic principles. Three + * commands are sent in the same request * * 1. hello (always required) * 2. ping * 3. quit * - * The responses are then read in sequence. For simplification we read all - * responses on the same object. + * The responses are then read individually and for simplification in + * the same response object. */ net::awaitable ping() { @@ -38,7 +38,7 @@ net::awaitable ping() req.push(command::ping); req.push(command::quit); - auto socket = co_await make_connection(); + auto socket = co_await make_connection("127.0.0.1", "6379"); co_await async_write(socket, req); std::string buffer; diff --git a/examples/basic2.cpp b/examples/basic2.cpp index 047cf89e..8639be42 100644 --- a/examples/basic2.cpp +++ b/examples/basic2.cpp @@ -21,7 +21,7 @@ namespace net = aedis::net; /* Similar to the basic1 example but * - * 1. Reads the response in a loop. + * 1. Reads the responses in a loop. * 2. Prints the command to which the response belongs to. */ net::awaitable ping() @@ -32,14 +32,18 @@ net::awaitable ping() req.push(command::ping); req.push(command::quit); - auto socket = co_await make_connection(); + auto socket = co_await make_connection("127.0.0.1", "6379"); co_await async_write(socket, req); std::string buffer; while (!std::empty(req.commands)) { response resp; co_await async_read(socket, buffer, resp); - std::cout << req.commands.front() << "\n" << resp << std::endl; + + std::cout + << req.commands.front() << "\n" + << resp << std::endl; + req.commands.pop(); } } catch (std::exception const& e) { diff --git a/examples/basic3.cpp b/examples/basic3.cpp index d19748cc..09a0f81d 100644 --- a/examples/basic3.cpp +++ b/examples/basic3.cpp @@ -21,20 +21,21 @@ namespace net = aedis::net; /* A slightly more elaborate way dealing with requests and responses. * - * This time we send the ping + quit only after the hello command has - * arrived. We also separate the application logic out the coroutine for - * clarity. - * - * This can be used as a starting point for more complex applications. + * This time we send the ping + quit only after the response to the + * hello command has been received. We also separate the application + * logic out the coroutine for clarity. */ -// Adds a new element in the queue if necessary. +/// Adds a new element in the queue if necessary. void prepare_next(std::queue& reqs) { if (std::empty(reqs) || std::size(reqs) == 1) reqs.push({}); } +/** The function that processes the response has been factored out of + * the coroutine to simplify application logic. + */ void process_response(std::queue& reqs, response& resp) { std::cout @@ -58,7 +59,7 @@ net::awaitable ping() reqs.push({}); reqs.back().push(command::hello, 3); - auto socket = co_await make_connection(); + auto socket = co_await make_connection("127.0.0.1", "6379"); std::string buffer; while (!std::empty(reqs)) { diff --git a/examples/pubsub.cpp b/examples/basic4.cpp similarity index 59% rename from examples/pubsub.cpp rename to examples/basic4.cpp index 0e62a917..842a810d 100644 --- a/examples/pubsub.cpp +++ b/examples/basic4.cpp @@ -14,28 +14,24 @@ using namespace aedis; -/** Publisher: A coroutine that will pusblish on two channels and exit. - */ -net::awaitable publisher() -{ - resp3::request req; - req.push(command::hello, 3); - req.push(command::publish, "channel1", "Message to channel1"); - req.push(command::publish, "channel2", "Message to channel2"); - req.push(command::quit); - - auto socket = co_await make_connection(); - co_await async_write(socket, req); - - std::string buffer; - resp3::response_base ignore; - co_await async_read(socket, buffer, ignore); - co_await async_read(socket, buffer, ignore); - co_await async_read(socket, buffer, ignore); -} - -/** Subscriber: Will subscribe to two channels and listen for messages +/** In previous examples we sent the command we were interested in and + * quit (closed) the connection. In this example we send a + * subscription to a channel and start reading for message * indefinitely. + * + * Notice we store the id of the connection as seem by redis to be + * able to identify it. + * + * After starting the example you can send messages with the + * redis-client like this + * + * $ redis-cli -3 + * 127.0.0.1:6379> PUBLISH channel1 mmmm + * (integer) 3 + * 127.0.0.1:6379> + * + * The messages will then appear on the terminal you are running the + * example. */ net::awaitable subscriber() { @@ -43,7 +39,7 @@ net::awaitable subscriber() req.push(command::hello, "3"); req.push(command::subscribe, "channel1", "channel2"); - auto socket = co_await make_connection(); + auto socket = co_await make_connection("127.0.0.1", "6379"); co_await async_write(socket, req); std::string buffer; @@ -62,7 +58,10 @@ net::awaitable subscriber() for (;;) { resp.clear(); co_await async_read(socket, buffer, resp); - std::cout << "Subscriber " << id << ":\n" << resp << std::endl; + + std::cout + << "Subscriber " << id << ":\n" + << resp << std::endl; } } @@ -72,6 +71,5 @@ int main() co_spawn(ioc, subscriber(), net::detached); co_spawn(ioc, subscriber(), net::detached); co_spawn(ioc, subscriber(), net::detached); - co_spawn(ioc, publisher(), net::detached); ioc.run(); } diff --git a/examples/utils.ipp b/examples/utils.ipp index 14fa648d..cc811c4b 100644 --- a/examples/utils.ipp +++ b/examples/utils.ipp @@ -11,28 +11,16 @@ #include "types.hpp" -aedis::net::awaitable make_connection() +aedis::net::awaitable +make_connection( + std::string const& host, + std::string const& port) { auto ex = co_await aedis::net::this_coro::executor; tcp_resolver resolver{ex}; - auto const res = co_await resolver.async_resolve("127.0.0.1", "6379"); + auto const res = co_await resolver.async_resolve(host, port); tcp_socket socket{ex}; co_await aedis::net::async_connect(socket, res); co_return std::move(socket); } -void print_command_raw(std::string const& data, int n) -{ - for (int i = 0; i < n; ++i) { - if (data[i] == '\n') { - std::cout << "\\n"; - continue; - } - if (data[i] == '\r') { - std::cout << "\\r"; - continue; - } - std::cout << data[i]; - } -} -