CppWAMP
C++11 client library for the WAMP protocol
examples/coro20timeservice/main.cpp
/*------------------------------------------------------------------------------
Copyright Butterfly Energy Systems 2022.
Distributed under the Boost Software License, Version 1.0.
http://www.boost.org/LICENSE_1_0.txt
------------------------------------------------------------------------------*/
//******************************************************************************
// Example WAMP service provider app using C++20 coroutines.
//******************************************************************************
#include <chrono>
#include <ctime>
#include <iostream>
#include <boost/asio/awaitable.hpp>
#include <boost/asio/co_spawn.hpp>
#include <boost/asio/detached.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/asio/use_awaitable.hpp>
#include <cppwamp/json.hpp>
#include <cppwamp/tcp.hpp>
const std::string realm = "cppwamp.demo.time";
const std::string address = "localhost";
const short port = 54321u;
//------------------------------------------------------------------------------
namespace wamp
{
// Convert a std::tm to/from an object variant.
template <typename TConverter>
void convert(TConverter& conv, std::tm& t)
{
conv ("sec", t.tm_sec)
("min", t.tm_min)
("hour", t.tm_hour)
("mday", t.tm_mday)
("mon", t.tm_mon)
("year", t.tm_year)
("wday", t.tm_wday)
("yday", t.tm_yday)
("isdst", t.tm_isdst);
}
}
//------------------------------------------------------------------------------
std::tm getTime()
{
auto t = std::time(nullptr);
return *std::localtime(&t);
}
//------------------------------------------------------------------------------
boost::asio::awaitable<void> service(wamp::Session::Ptr session)
{
using namespace wamp;
using boost::asio::use_awaitable;
(co_await session->connect(use_awaitable)).value();
(co_await session->join(Realm(realm), use_awaitable)).value();
(co_await session->enroll(Procedure("get_time"),
simpleRpc<std::tm>(&getTime),
use_awaitable)).value();
boost::asio::steady_timer timer(co_await boost::asio::this_coro::executor);
auto deadline = std::chrono::steady_clock::now();
while (true)
{
deadline += std::chrono::seconds(1);
timer.expires_at(deadline);
co_await timer.async_wait(use_awaitable);
auto t = std::time(nullptr);
const std::tm* local = std::localtime(&t);
(co_await session->publish(Pub("time_tick").withArgs(*local),
use_awaitable)).value();
std::cout << "Tick: " << std::asctime(local) << "\n";
}
}
//------------------------------------------------------------------------------
int main()
{
using namespace wamp;
AsioContext ioctx;
auto tcp = connector<Json>(ioctx, TcpHost(address, port));
auto session = Session::create(ioctx, tcp);
boost::asio::co_spawn(ioctx, service(session), boost::asio::detached);
ioctx.run();
return 0;
}
wamp::Session::create
static Ptr create(AnyIoExecutor exec, const Connector::Ptr &connector)
Creates a new Session instance.
Definition: session.ipp:22
session.hpp
Contains the asynchronous session API used by a client peer in WAMP applications.
wamp::Realm
Realm URI and other options contained within WAMP HELLO messages.
Definition: peerdata.hpp:59
wamp::Session::Ptr
std::shared_ptr< Session > Ptr
Shared pointer to a Session.
Definition: session.hpp:117
wamp::Procedure
Contains the procedure URI and other options contained within WAMP REGISTER messages.
Definition: peerdata.hpp:492
wamp::TcpHost
Contains TCP host address information, as well as other socket options.
Definition: tcphost.hpp:103
wamp
Definition: anyhandler.hpp:36
unpacker.hpp
Contains utilities for unpacking positional arguments passed to event slots and call slots.
json.hpp
Contains the JSON codec.
tcp.hpp
Contains facilities for creating TCP transport connectors.
wamp::Pub
Provides the topic URI, options, and payload contained within WAMP PUBLISH messages.
Definition: peerdata.hpp:363
variant.hpp
Contains the declaration of Variant and other closely related types/functions.
wamp::convert
void convert(TConverter &c, TValue &val)
General function for converting custom types to/from Variant.
Definition: variant.hpp:709
wamp::AsioContext
boost::asio::io_context AsioContext
Queues and runs I/O completion handlers.
Definition: asiodefs.hpp:34