Event subscription infrastructure and ledger events. (#319)

- Introduced event subscription infrastructure for users.
- Added ability to subscribe to ledger events.
- Updated client lib for event subscription support.
This commit is contained in:
Ravin Perera
2021-06-07 10:24:58 +05:30
committed by GitHub
parent a7b7f6c9de
commit cc11ebd7b3
16 changed files with 599 additions and 152 deletions

View File

@@ -246,6 +246,16 @@ namespace usr
user.session.send(resp);
return 0;
}
else if (msg_type == msg::usrmsg::MSGTYPE_SUBSCRIPTION)
{
NOTIFICATION_CHANNEL channel;
bool enabled;
if (parser.extract_subscription_request(channel, enabled) == -1)
return -1;
user.subscriptions[channel] = enabled;
return 0;
}
else if (msg_type == msg::usrmsg::MSGTYPE_LEDGER_QUERY)
{
ledger::query::query_request req;
@@ -547,25 +557,50 @@ namespace usr
// Array to hold constructed message cache from each protocol.
std::vector<uint8_t> protocol_msgs[2];
if (ev.index() == 0) // UNL change event. Broadcast for all users.
if (ev.index() == 0) // UNL change event. Broadcast for subscribed users.
{
const status::unl_change_event &unl_ev = std::get<status::unl_change_event>(ev);
std::scoped_lock<std::mutex> lock(ctx.users_mutex);
for (auto &[sid, user] : ctx.users)
{
std::vector<uint8_t> &msg = protocol_msgs[user.protocol];
if (msg.empty()) // Construct the message with relevant protocol if not done so already.
if (user.subscriptions[NOTIFICATION_CHANNEL::UNL_CHANGE])
{
msg::usrmsg::usrmsg_parser parser(user.protocol);
parser.create_unl_list_container(msg, unl_ev.unl);
std::vector<uint8_t> &msg = protocol_msgs[user.protocol];
if (msg.empty()) // Construct the message with relevant protocol if not done so already.
{
msg::usrmsg::usrmsg_parser parser(user.protocol);
const status::unl_change_event &unl_ev = std::get<status::unl_change_event>(ev);
parser.create_unl_notification(msg, unl_ev.unl);
}
user.session.send(msg);
}
user.session.send(msg);
}
}
else if (ev.index() == 1 || ev.index() == 2) // Ledger events. Broadcast for subscribed users.
{
std::scoped_lock<std::mutex> lock(ctx.users_mutex);
for (auto &[sid, user] : ctx.users)
{
if (user.subscriptions[NOTIFICATION_CHANNEL::LEDGER_EVENT])
{
std::vector<uint8_t> &msg = protocol_msgs[user.protocol];
if (msg.empty()) // Construct the message with relevant protocol if not done so already.
{
msg::usrmsg::usrmsg_parser parser(user.protocol);
// Clear the caches for the next event.
protocol_msgs[util::PROTOCOL::JSON].clear();
protocol_msgs[util::PROTOCOL::BSON].clear();
if (ev.index() == 1) // Ledger created event.
{
const status::ledger_created_event &ledger_ev = std::get<status::ledger_created_event>(ev);
parser.create_ledger_created_notification(msg, ledger_ev.ledger);
}
else if (ev.index() == 2) // Sync status chnge event.
{
const status::sync_status_change_event &sync_ev = std::get<status::sync_status_change_event>(ev);
parser.create_sync_status_notification(msg, sync_ev.in_sync);
}
}
user.session.send(msg);
}
}
}
}
}