Implement a JSON API friendly notification system

This should bit by bit substitute RsNotify which would be very difficult
  to support properly in JSON API.
The new system is much simpler to use also from the C++ side of the
  moon.
BroadcastDiscovery take advantage of the new system to notify about new
  non friend peer discovered, tested successfully also in JSON API.
This commit is contained in:
Gioacchino Mazzurco 2019-04-15 00:12:29 +02:00
parent 9c7a8d479f
commit 7dab487bde
No known key found for this signature in database
GPG key ID: A1FBCA3872E87051
9 changed files with 503 additions and 84 deletions

View file

@ -37,6 +37,7 @@
#include "retroshare/rsinit.h"
#include "util/rsurl.h"
#include "util/rstime.h"
#include "retroshare/rsevents.h"
// Generated at compile time
#include "jsonapi-includes.inl"
@ -278,6 +279,64 @@ JsonApiServer::JsonApiServer(uint16_t port, const std::string& bindAddress,
} );
}, true);
registerHandler("/rsEvents/registerEventsHandler",
[this](const std::shared_ptr<rb::Session> session)
{
const std::multimap<std::string, std::string> headers
{
{ "Connection", "keep-alive" },
{ "Content-Type", "text/event-stream" }
};
session->yield(rb::OK, headers);
size_t reqSize = session->get_request()->get_header("Content-Length", 0);
session->fetch( reqSize, [this](
const std::shared_ptr<rb::Session> session,
const rb::Bytes& body )
{
INITIALIZE_API_CALL_JSON_CONTEXT;
if( !checkRsServicePtrReady(
rsEvents, "rsEvents", cAns, session ) )
return;
const std::weak_ptr<rb::Session> weakSession(session);
RsEventsHandlerId_t hId = rsEvents->generateUniqueHandlerId();
std::function<void(const RsEvent&)> multiCallback =
[weakSession, hId](const RsEvent& event)
{
auto session = weakSession.lock();
if(!session || session->is_closed())
{
if(rsEvents) rsEvents->unregisterEventsHandler(hId);
return;
}
RsGenericSerializer::SerializeContext ctx;
RsTypeSerializer::serial_process(
RsGenericSerializer::TO_JSON, ctx,
const_cast<RsEvent&>(event), "event" );
std::stringstream message;
message << "data: " << compactJSON << ctx.mJson << "\n\n";
session->yield(message.str());
};
bool retval = rsEvents->registerEventsHandler(multiCallback, hId);
{
RsGenericSerializer::SerializeContext& ctx(cAns);
RsGenericSerializer::SerializeJob j(RsGenericSerializer::TO_JSON);
RS_SERIAL_PROCESS(retval);
}
// return them to the API caller
std::stringstream message;
message << "data: " << compactJSON << cAns.mJson << "\n\n";
session->yield(message.str());
} );
}, true);
// Generated at compile time
#include "jsonapi-wrappers.inl"
}