From 0bdd2dfca02e765780c70f8cacf7d1920ed16906 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Wed, 5 Dec 2018 11:33:13 -0500 Subject: [PATCH] Api: centralize formatting stream topic+qualifier --- src/client/spark_api/api/ApiNativeMethods.cs | 4 ++-- src/client/spark_api/api/ApiServer.cs | 3 --- src/client/spark_api/api/ApiUtils.cs | 4 ---- .../api/Channels/QuickBusClientChannel.cs | 8 ++++---- src/client/spark_api/api/ServerChannel.cs | 4 ++-- src/lib/api/core/ApiClientInterop.cpp | 13 +++++++++---- src/lib/api/core/ApiClientInterop.h | 2 +- src/lib/api/core/ApiServerInterop.cpp | 6 +++--- src/lib/api/core/ApiServerInterop.h | 2 +- src/lib/api/public/ClientChannel.h | 6 +++--- src/lib/api/public/QuickbusClientChannel.cpp | 4 ++-- src/lib/api/public/QuickbusClientChannel.h | 2 +- src/lib/api/public/ServerChannel.cpp | 4 ++-- src/lib/api/public/ServerChannel.h | 2 +- .../api/service_compiler/templates/c#/server.pb.cs | 2 +- .../service_compiler/templates/cpp/client.pb.cc | 4 ++-- .../api/service_compiler/templates/cpp/client.pb.h | 4 +--- .../service_compiler/templates/cpp/server.pb.cc | 14 +++----------- .../api/service_compiler/templates/cpp/server.pb.h | 8 +++----- 19 files changed, 41 insertions(+), 55 deletions(-) diff --git a/src/client/spark_api/api/ApiNativeMethods.cs b/src/client/spark_api/api/ApiNativeMethods.cs index 7db7c0d20..fdc18ed0c 100644 --- a/src/client/spark_api/api/ApiNativeMethods.cs +++ b/src/client/spark_api/api/ApiNativeMethods.cs @@ -64,7 +64,7 @@ namespace bts.api.core { internal static extern void api_client_send(IntPtr nchannel, byte[] buffer, int length, raw_pb_callback p, IntPtr extra); [DllImport("libspark")] - internal static extern IntPtr api_client_subscribe(IntPtr client, string method_and_qualifier, raw_subscription_callback cb, IntPtr extra); + internal static extern IntPtr api_client_subscribe(IntPtr client, string method, string qualifier, raw_subscription_callback cb, IntPtr extra); [DllImport("libspark")] internal static extern void api_client_unsubscribe(IntPtr client, IntPtr subscription); @@ -96,7 +96,7 @@ namespace bts.api.core { } [DllImport("libspark")] - internal static extern IntPtr api_server_create_table(IntPtr server, string method_and_qualifier); + internal static extern IntPtr api_server_create_table(IntPtr server, string method, string qualifier); [DllImport("libspark")] internal static extern void api_server_update_table(IntPtr table, [In] StreamUpdate[] changes, long change_count); [DllImport("libspark")] diff --git a/src/client/spark_api/api/ApiServer.cs b/src/client/spark_api/api/ApiServer.cs index 6bf47ca34..6e7f08c95 100644 --- a/src/client/spark_api/api/ApiServer.cs +++ b/src/client/spark_api/api/ApiServer.cs @@ -19,9 +19,6 @@ namespace bts.api.core { public abstract void dispatch(RequestHeader header, Stream request, Action on_response); - // Indirection to allow ApiServer in different assembly - protected string format_qualifier(string method, string qualifier) => ApiUtils.format_qualifier(method, qualifier); - protected void respond(int request_id, Task task, Action on_response) where T : IMessage, IImmutableMessage { var header = new ResponseHeader() { RequestId = request_id }; if (task.IsCanceled || task.IsFaulted) { diff --git a/src/client/spark_api/api/ApiUtils.cs b/src/client/spark_api/api/ApiUtils.cs index ef1cd61ba..73f7c7545 100644 --- a/src/client/spark_api/api/ApiUtils.cs +++ b/src/client/spark_api/api/ApiUtils.cs @@ -23,10 +23,6 @@ namespace bts.api { return buffer; } - public static string format_qualifier(string method, string qualifier) { - if (string.IsNullOrEmpty(qualifier)) return method; - return $"{method}/{qualifier}"; - } public struct DeserializeResult { public THeader header; diff --git a/src/client/spark_api/api/Channels/QuickBusClientChannel.cs b/src/client/spark_api/api/Channels/QuickBusClientChannel.cs index 15870c2f1..570794013 100644 --- a/src/client/spark_api/api/Channels/QuickBusClientChannel.cs +++ b/src/client/spark_api/api/Channels/QuickBusClientChannel.cs @@ -69,12 +69,12 @@ namespace bts.api.core { observer.OnError(new UnauthorizedAccessException()); return Disposable.Empty; } - return SubscribeNative(ApiUtils.format_qualifier(method, qualifier), parser, converter, observer); + return SubscribeNative(method, qualifier, parser, converter, observer); }); return result; } - private unsafe IDisposable SubscribeNative(string method_and_qualifier, MessageParser parser, Func converter, IObserver> observer) where TParse : IMessage { + private unsafe IDisposable SubscribeNative(string method, string qualifier, MessageParser parser, Func converter, IObserver> observer) where TParse : IMessage { bool first_update = true; // Memory optimization. There is only one update per subscription. RawUpdate the_update = new RawUpdate(); @@ -107,13 +107,13 @@ namespace bts.api.core { } } catch (Exception e) { - ApiNativeMethods.api_client_log_error($"Exception in subscription callback for sub: '{method_and_qualifier}' \n--\n '{e}'"); + ApiNativeMethods.api_client_log_error($"Exception in subscription callback for sub: '{method}/{qualifier}' \n--\n '{e}'"); // Sleep to let the log message get asyncronously written to disk Thread.Sleep(100); Environment.Exit(-1); } }; - var subscription = ApiNativeMethods.api_client_subscribe(_client, method_and_qualifier, cb, IntPtr.Zero); + var subscription = ApiNativeMethods.api_client_subscribe(_client, method, qualifier, cb, IntPtr.Zero); return FinalizeableAnonymousDisposable.Create((disposing) => { ApiNativeMethods.api_client_unsubscribe(_client, subscription); subscription = IntPtr.Zero; diff --git a/src/client/spark_api/api/ServerChannel.cs b/src/client/spark_api/api/ServerChannel.cs index ca1b2e1a8..a16c3d4b4 100644 --- a/src/client/spark_api/api/ServerChannel.cs +++ b/src/client/spark_api/api/ServerChannel.cs @@ -34,8 +34,8 @@ namespace bts.api.core { } public Action> on_request; - public IntPtr create_table(string method_and_qualifier) { - return ApiNativeMethods.api_server_create_table(_server, method_and_qualifier); + public IntPtr create_table(string method, string qualifier) { + return ApiNativeMethods.api_server_create_table(_server, method, qualifier); } public void Dispose() { diff --git a/src/lib/api/core/ApiClientInterop.cpp b/src/lib/api/core/ApiClientInterop.cpp index e905114a2..3927bbc3e 100644 --- a/src/lib/api/core/ApiClientInterop.cpp +++ b/src/lib/api/core/ApiClientInterop.cpp @@ -181,11 +181,16 @@ void safe_call_subscription_callback(raw_subscription_callback cb, EventType typ } -bts::qbus::Topic stream_topic(const std::string& base_topic, const char* method_and_qualifier) { - return qbus::Topic(base_topic + "-" + method_and_qualifier); +bts::qbus::Topic stream_topic(const std::string& base_topic, const char* method, const char* qualifier) { + if (qualifier == nullptr || strlen(qualifier) == 0) { + return qbus::Topic(base_topic + "-" + method); + } + else { + return qbus::Topic(base_topic + "-" + method + "/" + qualifier); + } } -CAPI TableSubscription api_client_subscribe(ApiClient* pclient, const char* method_and_qualifier, raw_subscription_callback cb, void* extra) { +CAPI TableSubscription api_client_subscribe(ApiClient* pclient, const char* method, const char* qualifier, raw_subscription_callback cb, void* extra) { bts_assert(pclient); static_assert((int)qbus::TableEvent::EventType::TABLE_CREATED == EventType::TABLE_CREATED, "Keep these Enums in sync"); static_assert((int)qbus::TableEvent::EventType::TABLE_DESTROYED == EventType::TABLE_DESTROYED, ""); @@ -194,7 +199,7 @@ CAPI TableSubscription api_client_subscribe(ApiClient* pclient, const char* meth static_assert((int)qbus::Change::Type::REPLACE == StreamUpdate::REPLACE, ""); static_assert((int)qbus::Change::Type::CREATE == StreamUpdate::CREATE, ""); - auto topic = stream_topic(pclient->service_name, method_and_qualifier); + auto topic = stream_topic(pclient->service_name, method, qualifier); LOG(Api,Diag,"api subscribe: {}" ,topic.value()); auto result = new qbus::TableSubscriber(pclient->qbus, equal_topic(topic), [cb, extra](const qbus::TableEvent& ev) { diff --git a/src/lib/api/core/ApiClientInterop.h b/src/lib/api/core/ApiClientInterop.h index 863672f8b..b150e5463 100644 --- a/src/lib/api/core/ApiClientInterop.h +++ b/src/lib/api/core/ApiClientInterop.h @@ -21,7 +21,7 @@ enum EventType { typedef void* TableSubscription; typedef void(*raw_subscription_callback)(EventType event_type, const StreamUpdate* changes, int count, void* extra); -CAPI TableSubscription api_client_subscribe(ApiClient* pclient, const char* method_and_qualifier, raw_subscription_callback cb, void* extra); +CAPI TableSubscription api_client_subscribe(ApiClient* pclient, const char* method, const char* qualifier, raw_subscription_callback cb, void* extra); CAPI void api_client_unsubscribe(ApiClient* pclient, TableSubscription ptable); }}//namespace diff --git a/src/lib/api/core/ApiServerInterop.cpp b/src/lib/api/core/ApiServerInterop.cpp index fa00bdef9..8b57c72dd 100644 --- a/src/lib/api/core/ApiServerInterop.cpp +++ b/src/lib/api/core/ApiServerInterop.cpp @@ -9,7 +9,7 @@ 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); +bts::qbus::Topic stream_topic(const std::string& base_topic, const char* method, const char* qualifier); struct ApiServerCore { ApiServerCore(Quickbus& qbus, const std::string topic, const qbus::OnRPCRequest& handler) : @@ -51,9 +51,9 @@ CAPI void api_server_call_callback(ReplyHandler** ppvhandler, const uint8_t* buf *pphandler = nullptr; } -CAPI TableHandle api_server_create_table(ApiServerCore* pserver, const char* method_and_qualifier) { +CAPI TableHandle api_server_create_table(ApiServerCore* pserver, const char* method, const char* qualifier) { bts_assert(pserver); - auto topic = stream_topic(pserver->topic, method_and_qualifier); + auto topic = stream_topic(pserver->topic, method, qualifier); LOG(Api,Info,"Create table: {}",topic.value()); return new qbus::TablePublisher(pserver->qbus, topic); } diff --git a/src/lib/api/core/ApiServerInterop.h b/src/lib/api/core/ApiServerInterop.h index 3e03d2be3..e25b637b2 100644 --- a/src/lib/api/core/ApiServerInterop.h +++ b/src/lib/api/core/ApiServerInterop.h @@ -12,7 +12,7 @@ CAPI ApiServerCore* api_server_create(Quickbus* pbus, const char* type_name, con CAPI void api_server_call_callback(ReplyHandler** pphandler, const uint8_t* buffer, int length); typedef void* TableHandle; -CAPI TableHandle api_server_create_table(ApiServerCore* pserver, const char* method_and_qualifier); +CAPI TableHandle api_server_create_table(ApiServerCore* pserver, const char* method, const char* qualifier); CAPI void api_server_update_table(TableHandle htable, StreamUpdate* raw_changes, int change_count); }}//namespace diff --git a/src/lib/api/public/ClientChannel.h b/src/lib/api/public/ClientChannel.h index 17782f46d..c194a1b5a 100644 --- a/src/lib/api/public/ClientChannel.h +++ b/src/lib/api/public/ClientChannel.h @@ -63,7 +63,7 @@ public: template - rxcpp::observable>> subscribe(const std::string& method_and_qualifier) { + rxcpp::observable>> subscribe(const char* method, const char* qualifier) { return rxcpp::observable<>::create>>([=](rxcpp::subscriber>> observer) { bool first_update = true; auto cb = [observer, first_update](EventType event_type, const StreamUpdate* changes, int change_count) mutable { @@ -98,7 +98,7 @@ public: } }; observer.add( - subscribe_core(method_and_qualifier, cb)); + subscribe_core(method, qualifier, cb)); }); } @@ -109,7 +109,7 @@ protected: typedef std::function Unsubscribe; typedef std::function OnSubscriptionCB; virtual void send_core(const std::string& request_buffer, OnReply on_response) = 0; - virtual Unsubscribe subscribe_core(const std::string& method_and_qualifier, OnSubscriptionCB cb) = 0; + virtual Unsubscribe subscribe_core(const char* method, const char* qualifier, OnSubscriptionCB cb) = 0; private: ClientChannel(const ClientChannel& other);// = delete; diff --git a/src/lib/api/public/QuickbusClientChannel.cpp b/src/lib/api/public/QuickbusClientChannel.cpp index 3713bcbf1..157e00c40 100644 --- a/src/lib/api/public/QuickbusClientChannel.cpp +++ b/src/lib/api/public/QuickbusClientChannel.cpp @@ -28,14 +28,14 @@ void QuickbusClientChannel::send_core(const std::string& request_envalope, OnRep api_client_send(_pclient, request_envalope.c_str(), request_envalope.size(), trampoline, heap_on_response); } -ClientChannel::Unsubscribe QuickbusClientChannel::subscribe_core(const std::string & method_and_qualifier, OnSubscriptionCB cb) { +ClientChannel::Unsubscribe QuickbusClientChannel::subscribe_core(const char* method, const char* qualifier, OnSubscriptionCB cb) { raw_subscription_callback trampoline = [](EventType event_type, const StreamUpdate* changes, int count, void* extra) { OnSubscriptionCB* heap_cb = static_cast(extra); assert(heap_cb != nullptr); (*heap_cb)(event_type, changes, count); }; OnSubscriptionCB* heap_cb = new OnSubscriptionCB(cb); - auto subscription = api_client_subscribe(_pclient, method_and_qualifier.c_str(), trampoline, heap_cb); + auto subscription = api_client_subscribe(_pclient, method, qualifier, trampoline, heap_cb); return [this,subscription,heap_cb]() { api_client_unsubscribe(_pclient, subscription); delete heap_cb; diff --git a/src/lib/api/public/QuickbusClientChannel.h b/src/lib/api/public/QuickbusClientChannel.h index ab973a9ee..bfda19f4f 100644 --- a/src/lib/api/public/QuickbusClientChannel.h +++ b/src/lib/api/public/QuickbusClientChannel.h @@ -19,7 +19,7 @@ private: BTS_NO_COPY(QuickbusClientChannel); virtual void send_core(const std::string& request_envalope, OnReply on_response) override; - virtual Unsubscribe subscribe_core(const std::string& method_and_qualifier, OnSubscriptionCB cb) override; + virtual Unsubscribe subscribe_core(const char* method, const char* qualifier, OnSubscriptionCB cb) override; virtual bool is_sync_call_ok() override; }; diff --git a/src/lib/api/public/ServerChannel.cpp b/src/lib/api/public/ServerChannel.cpp index a614806e8..6a791638e 100644 --- a/src/lib/api/public/ServerChannel.cpp +++ b/src/lib/api/public/ServerChannel.cpp @@ -51,8 +51,8 @@ ServerChannel::ServerChannel(ServerChannel&& other) : _me(std::move(other._me)) ServerChannel::~ServerChannel() { } -TableHandle ServerChannel::create_table(const std::string& method_and_qualifier) const { - return api_server_create_table(_me->_core, method_and_qualifier.c_str()); +TableHandle ServerChannel::create_table(const char* method, const char* qualifier) const { + return api_server_create_table(_me->_core, method, qualifier); } void ServerChannel::publish(TableHandle table, StreamUpdate* changes, int change_count) const { api_server_update_table(table, changes, change_count); diff --git a/src/lib/api/public/ServerChannel.h b/src/lib/api/public/ServerChannel.h index 102db0bf3..bce1c6661 100644 --- a/src/lib/api/public/ServerChannel.h +++ b/src/lib/api/public/ServerChannel.h @@ -15,7 +15,7 @@ public: ServerChannel(ServerChannel&& other);// : _me(std::move(other._me)) {} ~ServerChannel(); - TableHandle create_table(const std::string& method_and_qualifier) const; + TableHandle create_table(const char* method, const char* qualifier) const; void publish(TableHandle table, StreamUpdate* changes, int change_count) const; private: diff --git a/src/lib/api/service_compiler/templates/c#/server.pb.cs b/src/lib/api/service_compiler/templates/c#/server.pb.cs index 59a03518b..8e4fa0076 100644 --- a/src/lib/api/service_compiler/templates/c#/server.pb.cs +++ b/src/lib/api/service_compiler/templates/c#/server.pb.cs @@ -123,7 +123,7 @@ internal abstract class Authenticated{{name}}Base : ApiServer, IDisposable { //{{comment}} protected {{cap name}}Publisher make_{{name}}_publisher(string qualifier=null) { - IntPtr table = _channel.create_table(format_qualifier("{{name}}",qualifier)); + IntPtr table = _channel.create_table("{{name}}",qualifier); return new {{cap name}}Publisher(table); } protected static {{key_type.cstype}} {{name}}_key({{output_type_full}} row) { diff --git a/src/lib/api/service_compiler/templates/cpp/client.pb.cc b/src/lib/api/service_compiler/templates/cpp/client.pb.cc index 1bdc3dc9e..a14427d06 100644 --- a/src/lib/api/service_compiler/templates/cpp/client.pb.cc +++ b/src/lib/api/service_compiler/templates/cpp/client.pb.cc @@ -18,12 +18,12 @@ using namespace rxcpp::operators; {{#if server_streaming}} rxcpp::observable<{{cap name}}Update> {{../name}}::subscribe_{{name}}(std::string const&qualifier) { return - _pchannel->subscribe<{{output_type_cpp}}>(std::string("{{name}}/") + qualifier) + _pchannel->subscribe<{{output_type_cpp}}>("{{name}}/", qualifier.c_str()) | (bts::api::reconstitute,const {{key_type.cpptype}}, {{../name}}>()); } rxcpp::observable<{{cap name}}Update> {{../name}}::subscribe_{{name}}() { return - _pchannel->subscribe<{{output_type_cpp}}>("{{name}}") + _pchannel->subscribe<{{output_type_cpp}}>("{{name}}", nullptr) | (bts::api::reconstitute,const {{key_type.cpptype}}, {{../name}}>()); } diff --git a/src/lib/api/service_compiler/templates/cpp/client.pb.h b/src/lib/api/service_compiler/templates/cpp/client.pb.h index 836e8db84..338462bd1 100644 --- a/src/lib/api/service_compiler/templates/cpp/client.pb.h +++ b/src/lib/api/service_compiler/templates/cpp/client.pb.h @@ -47,9 +47,7 @@ class BTS_EXPORT {{name}} { {{#each method}} {{#if server_streaming}} // {{comment}} - // rxcpp::observable<{{cap name}}Update> subscribe_{{name}}(const {{input_type_cpp}}& qualifier); - // {{comment}} - rxcpp::observable<{{cap name}}Update> subscribe_{{name}}(std::string const&); + rxcpp::observable<{{cap name}}Update> subscribe_{{name}}(std::string const& qualifier); // {{comment}} rxcpp::observable<{{cap name}}Update> subscribe_{{name}}(); {{else}} diff --git a/src/lib/api/service_compiler/templates/cpp/server.pb.cc b/src/lib/api/service_compiler/templates/cpp/server.pb.cc index 77b1592fa..9c83b25c4 100644 --- a/src/lib/api/service_compiler/templates/cpp/server.pb.cc +++ b/src/lib/api/service_compiler/templates/cpp/server.pb.cc @@ -72,12 +72,8 @@ void Authenticated{{name}}Base::dispatch(const bts::api::RequestHeader& header, {{#each method}}{{#if server_streaming}} -{{../name}}Base::{{cap name}}Publisher Authenticated{{../name}}Base::make_{{name}}_publisher(std::string const&qualifier) const { - bts::api::TableHandle handle = _channel.create_table(std::string("{{name}}/") + qualifier); - return make_{{name}}_publisher(handle); -} -{{../name}}Base::{{cap name}}Publisher Authenticated{{../name}}Base::make_{{name}}_publisher() const { - bts::api::TableHandle handle = _channel.create_table("{{name}}"); +{{../name}}Base::{{cap name}}Publisher Authenticated{{../name}}Base::make_{{name}}_publisher(const char* qualifier) const { + bts::api::TableHandle handle = _channel.create_table("{{name}}", qualifier); return make_{{name}}_publisher(handle); } {{../name}}Base::{{cap name}}Publisher Authenticated{{../name}}Base::make_{{name}}_publisher(bts::api::TableHandle handle) const { @@ -175,11 +171,7 @@ void Authenticated{{name}}Base::dispatch(const bts::api::RequestHeader& header, }; } -{{../name}}Base::{{cap name}}StatefulPublisher Authenticated{{../name}}Base::make_{{name}}_stateful_publisher() const { - return bts::api::StatefulPublisher<{{output_type_cpp}},{{key_type.cpptype}}>(make_{{name}}_publisher(), {{name}}_key); -} - -{{../name}}Base::{{cap name}}StatefulPublisher Authenticated{{../name}}Base::make_{{name}}_stateful_publisher(const std::string& qualifier) const { +{{../name}}Base::{{cap name}}StatefulPublisher Authenticated{{../name}}Base::make_{{name}}_stateful_publisher(const char* qualifier) const { return bts::api::StatefulPublisher<{{output_type_cpp}},{{key_type.cpptype}}>(make_{{name}}_publisher(qualifier), {{name}}_key); } diff --git a/src/lib/api/service_compiler/templates/cpp/server.pb.h b/src/lib/api/service_compiler/templates/cpp/server.pb.h index ac6cc21fc..90aec631c 100644 --- a/src/lib/api/service_compiler/templates/cpp/server.pb.h +++ b/src/lib/api/service_compiler/templates/cpp/server.pb.h @@ -37,11 +37,9 @@ protected: {{#each method}} {{#if server_streaming}} - {{cap name}}Publisher make_{{name}}_publisher() const; - {{cap name}}Publisher make_{{name}}_publisher(const std::string &) const; - - {{cap name}}StatefulPublisher make_{{name}}_stateful_publisher() const; - {{cap name}}StatefulPublisher make_{{name}}_stateful_publisher(const std::string& qualifier) const; + {{cap name}}Publisher make_{{name}}_publisher(const std::string& qualifier) const { return make_{{name}}_publisher(qualifier.c_str()); } + {{cap name}}Publisher make_{{name}}_publisher(const char* qualifier=nullptr) const; + {{cap name}}StatefulPublisher make_{{name}}_stateful_publisher(const char* qualifier=nullptr) const; virtual void allow_{{name}}(const bts::api::AuthToken* token, const std::string& qualifier, const api::Response& reply);