CppWAMP
C++11 client library for the WAMP protocol
examples/timeclient/main.cpp
/*------------------------------------------------------------------------------
Copyright Butterfly Energy Systems 2014-2015, 2022.
Distributed under the Boost Software License, Version 1.0.
http://www.boost.org/LICENSE_1_0.txt
------------------------------------------------------------------------------*/
//******************************************************************************
// Example WAMP service consumer app using stackful coroutines.
//******************************************************************************
#include <ctime>
#include <iostream>
#include <boost/asio/spawn.hpp>
#include <cppwamp/json.hpp>
#include <cppwamp/tcp.hpp>
const std::string realm = "cppwamp.demo.time";
const std::string address = "localhost";
const short port = 12345u;
//------------------------------------------------------------------------------
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);
}
}
//------------------------------------------------------------------------------
void onTimeTick(std::tm time)
{
std::cout << "The current time is: " << std::asctime(&time) << "\n";
}
//------------------------------------------------------------------------------
int main()
{
using namespace wamp;
AsioContext ioctx;
auto tcp = connector<Json>(ioctx, TcpHost(address, port));
auto session = Session::create(ioctx, tcp);
boost::asio::spawn(ioctx, [&session](boost::asio::yield_context yield)
{
session->connect(yield).value();
session->join(Realm(realm), yield).value();
auto result = session->call(Rpc("get_time"), yield).value();
auto time = result[0].to<std::tm>();
std::cout << "The current time is: " << std::asctime(&time) << "\n";
session->subscribe(Topic("time_tick"),
wamp::simpleEvent<std::tm>(&onTimeTick),
yield).value();
});
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::Topic
Provides the topic URI and other options contained within WAMP ‘SUBSCRIBE’ messages.
Definition: peerdata.hpp:331
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.
wamp::Rpc
Contains the procedure URI, options, and payload contained within WAMP CALL messages.
Definition: peerdata.hpp:535
tcp.hpp
Contains facilities for creating TCP transport connectors.
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