CppWAMP
C++11 client library for the WAMP protocol
examples/futuretimeservice/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 std::future.
//******************************************************************************
#include <chrono>
#include <ctime>
#include <iostream>
#include <future>
#include <boost/asio/steady_timer.hpp>
#include <boost/asio/use_future.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);
}
//------------------------------------------------------------------------------
int main()
{
using namespace wamp;
using boost::asio::use_future;
// Run the io_context off in its own thread so that it operates
// completely asynchronously with respect to the rest of the program.
boost::asio::io_context ioctx;
auto work = boost::asio::make_work_guard(ioctx);
std::thread thread([&ioctx](){ ioctx.run(); });
auto tcp = connector<Json>(ioctx, TcpHost(address, port));
auto session = Session::create(ioctx, tcp);
boost::asio::steady_timer timer(ioctx);
// get() blocks the main thread until completion.
// value() throws if there was an error.
auto index = session->connect(use_future).get().value();
std::cout << "Connected via " << index << std::endl;
auto info = session->join(Realm(realm), use_future).get().value();
std::cout << "Joined, SessionId=" << info.id() << std::endl;
auto reg = session->enroll(Procedure("get_time"),
simpleRpc<std::tm>(&getTime),
use_future).get().value();
std::cout << "Registered 'get_time', RegistrationId=" << reg.id()
<< std::endl;
auto deadline = std::chrono::steady_clock::now();
while (true)
{
deadline += std::chrono::seconds(1);
timer.expires_at(deadline);
timer.async_wait(use_future).get();
auto t = std::time(nullptr);
const std::tm* local = std::localtime(&t);
session->publish(Pub("time_tick").withArgs(*local),
use_future).get().value();
std::cout << "Tick: " << std::asctime(local) << "\n";
}
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::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