Init
This commit is contained in:
commit
950e7b7e2d
350 files changed
+38599
No files matched your search
@@ -0,0 +1,82 @@
|
||||
#include "ApiServerInterop.h"
|
||||
#include "lib/quickbus/quickbus.h"
|
||||
#include "lib/logging/logging.h"
|
||||
|
||||
namespace bts {
|
||||
namespace api {
|
||||
|
||||
// defined in ApiClientInterop.h
|
||||
std::string base_topic(const char* type_name, const char* service_qualifier);
|
||||
bts::qbus::RPCServiceName rpc_topic(const std::string& base_topic);
|
||||
bts::qbus::Topic stream_topic(const std::string& base_topic, const char* method_and_qualifier);
|
||||
|
||||
struct ApiServerCore {
|
||||
ApiServerCore(Quickbus& qbus, const std::string topic, const qbus::OnRPCRequest& handler) :
|
||||
qbus(qbus),
|
||||
topic(topic),
|
||||
rpc(qbus, rpc_topic(topic), handler)
|
||||
{}
|
||||
|
||||
Quickbus& qbus;
|
||||
std::string topic;
|
||||
qbus::RPCServer rpc;
|
||||
};
|
||||
|
||||
BTS_EXPORT ApiServerCore* api_server_create(Quickbus* pbus, const char* type_name, const char* service_qualifier, raw_rpc_request_callback cb, void* extra) {
|
||||
bts_assert(pbus);
|
||||
// Invalid characters in qualifier: +
|
||||
for (auto ch : { '+', '_' }) {
|
||||
bts_assert(strchr(service_qualifier, ch) == nullptr);
|
||||
}
|
||||
auto service_name = base_topic(type_name, service_qualifier);
|
||||
LOG(Api,Info,"create server: {}",service_name);
|
||||
return new ApiServerCore(*pbus, service_name, [=](qbus::RPCRequestPtr const& req, qbus::ReplyHandler const& reply) {
|
||||
// Extend the lifetime of the ReplyHandler by moving it to the heap
|
||||
qbus::ReplyHandler* phandler = new qbus::ReplyHandler(std::move(reply));
|
||||
LOG(Api,Info,"Received rpc request: {}",reinterpret_cast<intptr_t>(phandler));
|
||||
cb((uint8_t*)(req->value().c_str()), req->value().size(), reinterpret_cast<ReplyHandler*>(phandler), extra);
|
||||
});
|
||||
}
|
||||
|
||||
BTS_EXPORT void api_server_call_callback(ReplyHandler** ppvhandler, const uint8_t* buffer, int length) {
|
||||
bts_assert(ppvhandler);
|
||||
bts_assert(*ppvhandler);
|
||||
LOG(Api,Info,"Send rpc reply: {}",reinterpret_cast<intptr_t>(*ppvhandler));
|
||||
qbus::ReplyHandler** pphandler = reinterpret_cast<qbus::ReplyHandler**>(ppvhandler);
|
||||
std::string reply_buffer((const char*)buffer, length);
|
||||
qbus::RPCReplyPtr replyptr = std::make_shared<qbus::RPCReply>(move(reply_buffer));
|
||||
(**pphandler)(replyptr);
|
||||
delete (*pphandler);
|
||||
*pphandler = nullptr;
|
||||
}
|
||||
|
||||
BTS_EXPORT TableHandle api_server_create_table(ApiServerCore* pserver, const char* method_and_qualifier) {
|
||||
bts_assert(pserver);
|
||||
auto topic = stream_topic(pserver->topic, method_and_qualifier);
|
||||
LOG(Api,Info,"Create table: {}",topic.value());
|
||||
return new qbus::TablePublisher(pserver->qbus, topic);
|
||||
}
|
||||
|
||||
BTS_EXPORT void api_server_update_table(TableHandle htable, StreamUpdate* raw_changes, int change_count) {
|
||||
if (change_count > 0) {
|
||||
bts_assert(htable);
|
||||
qbus::TablePublisher* ptable = reinterpret_cast<qbus::TablePublisher*>(htable);
|
||||
std::vector<qbus::Change> changes(change_count);
|
||||
for (int x = 0; x < change_count; ++x) {
|
||||
StreamUpdate& ch = raw_changes[x];
|
||||
std::string buffer(ch.buffer, ch.buffer_len);
|
||||
qbus::RecordPtr record = std::make_shared<qbus::Record>(std::move(buffer));
|
||||
changes[x] = qbus::Change((qbus::Change::Type)ch.type, qbus::RecordKey(ch.key), record);
|
||||
}
|
||||
ptable->publish(std::move(changes));
|
||||
}
|
||||
}
|
||||
|
||||
BTS_EXPORT void api_server_destroy_table(qbus::TablePublisher* ptable) {
|
||||
if (ptable) {
|
||||
ptable->disconnect();
|
||||
delete ptable;
|
||||
}
|
||||
}
|
||||
|
||||
} }//namespace
|
||||
Reference in new issue
Block a user