Api: centralize formatting stream topic+qualifier

This commit is contained in:
Eric Harding committed 2018-12-05 11:33:13 -05:00
1 parent a0c572ef2e
commit 0bdd2dfca0
19 files changed
+41 -55

No files matched your search

+2 -2
View File
@@ -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")]
-3
View File
@@ -19,9 +19,6 @@ namespace bts.api.core
{
public abstract void dispatch(RequestHeader header, Stream request, Action<byte[]> 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<T>(int request_id, Task<T> task, Action<byte[]> on_response) where T : IMessage, IImmutableMessage {
var header = new ResponseHeader() { RequestId = request_id };
if (task.IsCanceled || task.IsFaulted) {
-4
View File
@@ -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<THeader, TBody> {
public THeader header;
@@ -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<TParse, TOut>(string method_and_qualifier, MessageParser<TParse> parser, Func<TParse, TOut> converter, IObserver<RawUpdate<TOut>> observer) where TParse : IMessage<TParse> {
private unsafe IDisposable SubscribeNative<TParse, TOut>(string method, string qualifier, MessageParser<TParse> parser, Func<TParse, TOut> converter, IObserver<RawUpdate<TOut>> observer) where TParse : IMessage<TParse> {
bool first_update = true;
// Memory optimization. There is only one update per subscription.
RawUpdate<TOut> the_update = new RawUpdate<TOut>();
@@ -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;
+2 -2
View File
@@ -34,8 +34,8 @@ namespace bts.api.core {
}
public Action<RequestHeader, Stream, Action<byte[]>> 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() {
+9 -4
View File
@@ -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) {
+1 -1
View File
@@ -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
+3 -3
View File
@@ -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);
}
+1 -1
View File
@@ -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
+3 -3
View File
@@ -63,7 +63,7 @@ public:
template <typename T>
rxcpp::observable<RawUpdate<std::shared_ptr<const T>>> subscribe(const std::string& method_and_qualifier) {
rxcpp::observable<RawUpdate<std::shared_ptr<const T>>> subscribe(const char* method, const char* qualifier) {
return rxcpp::observable<>::create<RawUpdate<std::shared_ptr<const T>>>([=](rxcpp::subscriber<RawUpdate<std::shared_ptr<const T>>> 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<void()> Unsubscribe;
typedef std::function<void(EventType,const StreamUpdate*,int)> 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;
+2 -2
View File
@@ -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<OnSubscriptionCB*>(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;
+1 -1
View File
@@ -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;
};
+2 -2
View File
@@ -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);
+1 -1
View File
@@ -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:
@@ -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) {
@@ -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<std::shared_ptr<const {{output_type_cpp}}>,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<std::shared_ptr<const {{output_type_cpp}}>,const {{key_type.cpptype}}, {{../name}}>());
}
@@ -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}}
@@ -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);
}
@@ -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<bts::api::StreamAccessResponse>& reply);