CppWAMP
C++11 client library for the WAMP protocol
examples/asynctimeservice/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 callback handler functions.
//******************************************************************************
#include <chrono>
#include <ctime>
#include <iostream>
#include <boost/asio/steady_timer.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);
}
}
//------------------------------------------------------------------------------
class TimeService : public std::enable_shared_from_this<TimeService>
{
public:
static std::shared_ptr<TimeService> create(wamp::AsioContext& ioctx,
{
return std::shared_ptr<TimeService>(new TimeService(ioctx, session));
}
void start()
{
auto self = shared_from_this();
session_->connect([this, self](wamp::ErrorOr<size_t> index)
{
index.value(); // Throws if connect failed
join();
});
}
private:
explicit TimeService(wamp::AsioContext& ioctx, wamp::Session::Ptr session)
: session_(session),
timer_(ioctx)
{}
static std::tm getTime()
{
auto t = std::time(nullptr);
return *std::localtime(&t);
}
void join()
{
auto self = shared_from_this();
session_->join(
wamp::Realm(realm),
{
info.value(); // Throws if join failed
enroll();
});
}
void enroll()
{
auto self = shared_from_this();
session_->enroll(
wamp::Procedure("get_time"),
wamp::simpleRpc<std::tm>(&getTime),
{
reg.value(); // Throws if enroll failed
deadline_ = std::chrono::steady_clock::now();
kickTimer();
});
}
void kickTimer()
{
deadline_ += std::chrono::seconds(1);
timer_.expires_at(deadline_);
auto self = shared_from_this();
timer_.async_wait([this, self](boost::system::error_code ec)
{
if (ec)
throw boost::system::system_error(ec);
publish();
kickTimer();
});
}
void publish()
{
auto t = std::time(nullptr);
const std::tm* local = std::localtime(&t);
session_->publish(wamp::Pub("time_tick").withArgs(*local));
std::cout << "Tick: " << std::asctime(local) << "\n";
}
boost::asio::steady_timer timer_;
std::chrono::steady_clock::time_point deadline_;
};
//------------------------------------------------------------------------------
int main()
{
using namespace wamp;
AsioContext ioctx;
auto tcp = connector<Json>(ioctx, TcpHost(address, port));
auto session = Session::create(ioctx, tcp);
auto service = TimeService::create(ioctx, session);
service->start();
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::ErrorOr::value
value_type & value() &
Checked access of the stored value.
Definition: erroror.hpp:257
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
wamp::ErrorOr
Minimalistic implementation of std::expected<T, std::error_code>
Definition: erroror.hpp:128
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