diff --git a/.gitignore b/.gitignore index 7c976336d..fc8b9f309 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,5 @@ trace_data/ /src/lib/orderman/perf_analysis/last_pretty_log.csv /src/lib/orderman/perf_analysis/log.csv /doc/user/public/* +.ionide/symbolCache.db +src/web/.ionide/symbolCache.db diff --git a/src/lib/api/apigateway.proto b/src/lib/api/apigateway.proto index 47b44c838..ab22af4cb 100644 --- a/src/lib/api/apigateway.proto +++ b/src/lib/api/apigateway.proto @@ -13,6 +13,7 @@ message GatewayLoginResult { api.AuthToken token = 3; bool can_create = 4; } + message GatewayCallHeader { int32 request_id = 1; int32 method = 2; @@ -21,8 +22,33 @@ message GatewayCallHeader { string service_qualifier = 5; } -message GatewaySubscribe { } -message GatewayUnsubscribe {} +message GatewaySubscribe { + int32 request_id = 1; + int32 method = 2; + api.AuthToken token = 3; + string service_type = 4; + string service_qualifier = 5; + string stream_qualifier = 6; +} +message GatewayUnsubscribe { + uint32 subscription_id = 1; +} + +message StreamUpdate { + enum UpdateType { + REMOVE = 0; + REPLACE = 1; + CREATE= 2; + } + UpdateType update_type = 1; + int32 key = 2; + bytes buffer = 3; +} +message SubscriptionUpdate { + int32 id = 1; + repeated StreamUpdate updates = 2; +} + message GatewayRequest { oneof request { GatewayLogin login = 1; @@ -40,6 +66,7 @@ message GatewayResponse { oneof response { GatewayLoginResult logon = 1; bytes call_result = 2; + SubscriptionUpdate tick = 3; string error = 10; } } \ No newline at end of file diff --git a/src/lib/api/service_compiler/templates/fs/fable.fs b/src/lib/api/service_compiler/templates/fs/fable.fs index 8cd5c3385..e9a7a06c8 100644 --- a/src/lib/api/service_compiler/templates/fs/fable.fs +++ b/src/lib/api/service_compiler/templates/fs/fable.fs @@ -68,7 +68,8 @@ module {{name}} = type I{{name}} = {{#each method}} {{#if server_streaming}} - // todo: {{name}} + // {{comment}} + abstract member subscribe_{{name}} : qualifier:string -> callback:(DynamicService.Update<{{output_type_full}}>->unit) -> unit {{else}} // {{comment}} abstract member {{name}}: {{input_type_full}} -> Fable.Import.JS.Promise<{{output_type_full}}> @@ -78,6 +79,7 @@ type I{{name}} = module {{name}} = let Create (context:ServiceContext.ServiceContext) (qualifier:string) : I{{name}} = Imports.DynamicService.DynamicService.Create(context, "{{name}}", qualifier, + // methods unbox [| {{#each method}} {{#if server_streaming}} @@ -85,7 +87,16 @@ module {{name}} = "{{name}}", {{@index}}., "{{input_type_full}}", "{{output_type_full}}" {{/if}} {{/each}} - |]) + |], + // streams + unbox [| + {{#each method}} + {{#if server_streaming}} + "{{name}}", {{@index}}., "{{output_type_full}}" + {{/if}} + {{/each}} + |] + ) |> unbox {{/each}} \ No newline at end of file diff --git a/src/server/sparkd/http/ApiGateway.cpp b/src/server/sparkd/http/ApiGateway.cpp index a242fe3c1..d31a7579d 100644 --- a/src/server/sparkd/http/ApiGateway.cpp +++ b/src/server/sparkd/http/ApiGateway.cpp @@ -66,23 +66,27 @@ struct ApiGatewayImpl { r(response); } - void handle_call(const GatewayCallHeader& header, const std::string& body, const Responder& r) { - std::lock_guard lock(_m); - if (header.service_type().empty()) { - error(r, "Invalid service name"); - return; - } - auto key = header.service_type() + header.service_qualifier(); - // create the api_client if needed + // Create or return the client proxy + api::ApiClient* get_api_client(const std::string& service_type, const std::string& qualifier) { + auto key = service_type + qualifier; auto iter = _apiclient.find(key); - api::ApiClient* pclient = nullptr; if (iter == _apiclient.end()) { - pclient = api::api_client_create(&_sc.bus(), header.service_type().c_str(), header.service_qualifier().c_str()); + auto* pclient = api::api_client_create(&_sc.bus(), service_type.c_str(), qualifier.c_str()); _apiclient.insert(std::make_pair(key, pclient)); + return pclient; } else { - pclient = iter->second; + return iter->second; } + } + + void handle_call(const GatewayCallHeader& header, const std::string& body, const Responder& reply) { + std::lock_guard lock(_m); + if (header.service_type().empty()) { + error(reply, "Invalid service name"); + return; + } + api::ApiClient* pclient = get_api_client(header.service_type(), header.service_qualifier()); // Form the api request buffer api::RequestHeader api_header; api_header.set_method(header.method()); @@ -96,7 +100,7 @@ struct ApiGatewayImpl { api_header.AppendToString(&buffer); buffer += body; // call send - Responder* presponder = new Responder(move(r)); + Responder* presponder = new Responder(move(reply)); api_client_send(pclient, buffer.c_str(), buffer.size(), [](const char* buffer, size_t length, void* extra) { // buffer = [size][ResponseHeader][Payload]. GatewayResponse response; @@ -111,8 +115,25 @@ struct ApiGatewayImpl { }, presponder); } - // void handle_subscribe() - // void handle_unsubscribe() + void handle_subscribe(const GatewaySubscribe& request, const Responder& reply) { + std::lock_guard lock(_m); + api::ApiClient* pclient = get_api_client(request.service_type(), request.service_qualifier()); + if (pclient == nullptr) { + error(reply, "Invalid service"); + return; + } + // call api_client_subscribe() store the TableSubscription and the id and the reply channel + // When a tableupate fires attach the id to the reply and send it over the channel + Responder* presponder = new Responder(move(reply)); + (void)presponder; + + // reply with empty response + reply({}); + } + void handle_unsubscribe(const GatewayUnsubscribe&, const Responder& reply) { + // empty response makes quickbus happy + reply({}); + } }; @@ -125,7 +146,6 @@ ApiGatewaySession::~ApiGatewaySession(){} void ApiGatewaySession::handle(const api::gateway::GatewayRequest& request, const Responder& reply) { - GatewayResponse response; LOG(Api,Diag, "Gateway Request: {}", api::toJSON(request)); switch(request.request_case()) { case GatewayRequest::kLogin: @@ -135,8 +155,14 @@ void ApiGatewaySession::handle(const api::gateway::GatewayRequest& request, cons _me->handle_call(request.call(), request.addendum(), reply); break; case GatewayRequest::kSubscribe: + _me->handle_subscribe(request.subscribe(), reply); + break; case GatewayRequest::kUnsubscribe: - case GatewayRequest::REQUEST_NOT_SET: + _me->handle_unsubscribe(request.unsubscribe(), reply); + break; + case GatewayRequest::REQUEST_NOT_SET: { + _me->error(reply, "Request type not set"); + } break; } } diff --git a/src/web/api/package-lock.json b/src/web/api/package-lock.json index f923270ae..29e3dec15 100644 --- a/src/web/api/package-lock.json +++ b/src/web/api/package-lock.json @@ -132,6 +132,7 @@ "resolved": "https://registry.npmjs.org/align-text/-/align-text-0.1.4.tgz", "integrity": "sha1-DNkKVhCT810KmSVsIrcGlDP60Rc=", "dev": true, + "optional": true, "requires": { "kind-of": "^3.0.2", "longest": "^1.0.1", @@ -1832,7 +1833,8 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "aproba": { "version": "1.1.1", @@ -1883,7 +1885,8 @@ "balanced-match": { "version": "0.4.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "bcrypt-pbkdf": { "version": "1.0.1", @@ -1898,6 +1901,7 @@ "version": "0.0.9", "bundled": true, "dev": true, + "optional": true, "requires": { "inherits": "~2.0.0" } @@ -1906,6 +1910,7 @@ "version": "2.10.1", "bundled": true, "dev": true, + "optional": true, "requires": { "hoek": "2.x.x" } @@ -1914,6 +1919,7 @@ "version": "1.1.7", "bundled": true, "dev": true, + "optional": true, "requires": { "balanced-match": "^0.4.1", "concat-map": "0.0.1" @@ -1922,7 +1928,8 @@ "buffer-shims": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "caseless": { "version": "0.12.0", @@ -1939,12 +1946,14 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "combined-stream": { "version": "1.0.5", "bundled": true, "dev": true, + "optional": true, "requires": { "delayed-stream": "~1.0.0" } @@ -1952,22 +1961,26 @@ "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "core-util-is": { "version": "1.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "cryptiles": { "version": "2.0.5", "bundled": true, "dev": true, + "optional": true, "requires": { "boom": "2.x.x" } @@ -2007,7 +2020,8 @@ "delayed-stream": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "delegates": { "version": "1.0.0", @@ -2039,7 +2053,8 @@ "extsprintf": { "version": "1.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "forever-agent": { "version": "0.6.1", @@ -2061,12 +2076,14 @@ "fs.realpath": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "fstream": { "version": "1.0.11", "bundled": true, "dev": true, + "optional": true, "requires": { "graceful-fs": "^4.1.2", "inherits": "~2.0.0", @@ -2122,6 +2139,7 @@ "version": "7.1.2", "bundled": true, "dev": true, + "optional": true, "requires": { "fs.realpath": "^1.0.0", "inflight": "^1.0.4", @@ -2134,7 +2152,8 @@ "graceful-fs": { "version": "4.1.11", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "har-schema": { "version": "1.0.5", @@ -2162,6 +2181,7 @@ "version": "3.1.3", "bundled": true, "dev": true, + "optional": true, "requires": { "boom": "2.x.x", "cryptiles": "2.x.x", @@ -2172,7 +2192,8 @@ "hoek": { "version": "2.16.3", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "http-signature": { "version": "1.1.1", @@ -2189,6 +2210,7 @@ "version": "1.0.6", "bundled": true, "dev": true, + "optional": true, "requires": { "once": "^1.3.0", "wrappy": "1" @@ -2197,7 +2219,8 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "ini": { "version": "1.3.4", @@ -2209,6 +2232,7 @@ "version": "1.0.0", "bundled": true, "dev": true, + "optional": true, "requires": { "number-is-nan": "1.0.1" } @@ -2222,7 +2246,8 @@ "isarray": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "isstream": { "version": "0.1.2", @@ -2295,12 +2320,14 @@ "mime-db": { "version": "1.27.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "mime-types": { "version": "2.1.15", "bundled": true, "dev": true, + "optional": true, "requires": { "mime-db": "1.27.0" } @@ -2309,6 +2336,7 @@ "version": "3.0.4", "bundled": true, "dev": true, + "optional": true, "requires": { "brace-expansion": "1.1.7" } @@ -2316,12 +2344,14 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "mkdirp": { "version": "0.5.1", "bundled": true, "dev": true, + "optional": true, "requires": { "minimist": "0.0.8" } @@ -2376,7 +2406,8 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "oauth-sign": { "version": "0.8.2", @@ -2394,6 +2425,7 @@ "version": "1.4.0", "bundled": true, "dev": true, + "optional": true, "requires": { "wrappy": "1" } @@ -2423,7 +2455,8 @@ "path-is-absolute": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "performance-now": { "version": "0.2.0", @@ -2434,7 +2467,8 @@ "process-nextick-args": { "version": "1.0.7", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "punycode": { "version": "1.4.1", @@ -2472,6 +2506,7 @@ "version": "2.2.9", "bundled": true, "dev": true, + "optional": true, "requires": { "buffer-shims": "~1.0.0", "core-util-is": "~1.0.0", @@ -2516,6 +2551,7 @@ "version": "2.6.1", "bundled": true, "dev": true, + "optional": true, "requires": { "glob": "^7.0.5" } @@ -2523,7 +2559,8 @@ "safe-buffer": { "version": "5.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "semver": { "version": "5.3.0", @@ -2547,6 +2584,7 @@ "version": "1.0.9", "bundled": true, "dev": true, + "optional": true, "requires": { "hoek": "2.x.x" } @@ -2580,6 +2618,7 @@ "version": "1.0.2", "bundled": true, "dev": true, + "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -2590,6 +2629,7 @@ "version": "1.0.1", "bundled": true, "dev": true, + "optional": true, "requires": { "safe-buffer": "^5.0.1" } @@ -2604,6 +2644,7 @@ "version": "3.0.1", "bundled": true, "dev": true, + "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -2618,6 +2659,7 @@ "version": "2.2.1", "bundled": true, "dev": true, + "optional": true, "requires": { "block-stream": "*", "fstream": "^1.0.2", @@ -2673,7 +2715,8 @@ "util-deprecate": { "version": "1.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "uuid": { "version": "3.0.1", @@ -2702,7 +2745,8 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true } } }, @@ -4011,7 +4055,8 @@ "version": "1.0.1", "resolved": "https://registry.npmjs.org/longest/-/longest-1.0.1.tgz", "integrity": "sha1-MKCy2jj3N3DoKUoNIuZiXtd9AJc=", - "dev": true + "dev": true, + "optional": true }, "loose-envify": { "version": "1.3.1", diff --git a/src/web/api/src/DynamicService.ts b/src/web/api/src/DynamicService.ts index fa2745e3a..78cc7d8e9 100644 --- a/src/web/api/src/DynamicService.ts +++ b/src/web/api/src/DynamicService.ts @@ -1,8 +1,18 @@ import {root} from 'proto' -import { ServiceContext } from 'ServiceContext' +import { ServiceContext, RawUpdate, RawUpdateType } from 'ServiceContext' import { assertValid } from 'bts_assert'; -export interface IService { +export interface Change { + old : T + new : T +} +export interface Update { + all : { [key:number]: T } + added : { [key:number]: T } + changed : { [key:number]: Change } + removed : { [key:number]: T } +} +interface IService { context : ServiceContext name: string qualifier : string @@ -24,15 +34,56 @@ function gen_method(service:IService, method_id : number, request_type : string, } } +function gen_subscribe(service:IService, method_id : number, output_type : string) { + const resultProto = root.lookupType(output_type) + let state = {}; + return (qualifier, callback) => { + return service.context.subscribe(service.name, service.qualifier, method_id, qualifier, (updates) => { + // iterate and decode each update + // create an Update + let result : Update = { all: {}, added: {}, changed: {}, removed: {} }; + updates.forEach((value:RawUpdate) => { + const key = value.key; + switch(value.updateType) { + case RawUpdateType.CREATE: { + var obj = resultProto.decode(value.buffer) + state[key] = obj + result.added[key] = obj + } + break; + case RawUpdateType.REMOVE: { + var obj = state[key] + delete state[key] + result.removed[key] = obj + } + break; + case RawUpdateType.REPLACE: { + var oldItem = state[key] + var newItem = resultProto.decode(value.buffer); + result.changed[key] = { new : newItem, old : oldItem }; + } + } + }); + result.all = { ...state }; + callback(result); + }); + } +} + export class DynamicService { - constructor(context : ServiceContext, name : string, qualifier:string, methods : Array<[string,number,string,string]>) { + constructor( + context : ServiceContext, + name : string, qualifier:string, + methods : Array<[string,number,string,string]>, + streams : Array<[string,number,string]>) { + const svc = { context, name, qualifier } - for (var m of methods) { - // m[0] = method_name - // m[1] = method id - // m[2] = input type name - // m[3] = output type name - this[m[0]] = gen_method(svc, m[1], m[2], m[3]) + for (var [methodName, id, inType, outType] of methods) { + this[methodName] = gen_method(svc, id, inType, outType) + } + for (var [streamName,id,typeName] of streams) { + const fnName = "subscribe_" + streamName; + this[fnName] = gen_subscribe(svc, id, typeName) } } } diff --git a/src/web/api/src/ServiceContext.ts b/src/web/api/src/ServiceContext.ts index c0efa340d..e7bf105f4 100644 --- a/src/web/api/src/ServiceContext.ts +++ b/src/web/api/src/ServiceContext.ts @@ -1,5 +1,5 @@ import {root} from 'proto' -import { assertValid } from 'bts_assert' +import { assert, assertValid } from 'bts_assert' function default_ws_uri() { if (typeof window == 'undefined') return 'ws://sparkvm:8000/' @@ -21,6 +21,8 @@ const GatewayLogin = root.lookupType("bts.api.gateway.GatewayLogin") const GatewayRequest = root.lookupType("bts.api.gateway.GatewayRequest") const GatewayResponse = root.lookupType("bts.api.gateway.GatewayResponse") const GatewayCallHeader = root.lookupType("bts.api.gateway.GatewayCallHeader") +const GatewaySubscribe = root.lookupType("bts.api.gateway.GatewaySubscribe") +const GatewayUnsubscribe = root.lookupType("bts.api.gateway.GatewaySubscribe") const ResponseHeader = root.lookupType("bts.api.ResponseHeader") class PrivateContext { @@ -114,12 +116,29 @@ class PrivateContext { // console.log(`onmessage: [${call_buffer}] h:[${sub}] b:[${body}]`) this.sc.on_msg(header, body) } + else if (response && response.tick) { + this.sc.on_tick(response.tick); + } else if (response && response.error) { console.log(response.error); } } } +export enum RawUpdateType { + REMOVE, REPLACE, CREATE +} +export interface RawUpdate { + updateType : RawUpdateType + key : number + buffer : Uint8Array +} + +interface RawSubscription { + id: number + callback : (u:RawUpdate[]) => void +} + export class ServiceContext { next_id : number send: (msg) => void @@ -127,11 +146,13 @@ export class ServiceContext { token: string can_create: boolean outstanding : { [request:number]:{ resolve : (buffer)=>void, reject: (msg)=>void } } + private subscriptions : { [id:number]:RawSubscription } constructor(send : (msg)=>void) { this.next_id = 1000 this.send = send this.outstanding = {} + this.subscriptions = {} } call(service_type : string, qualifier : string, method_id : number, body : Uint8Array) : Promise { @@ -154,7 +175,39 @@ export class ServiceContext { this.send(message) }); } - subscribe() {/*not impl*/} + + subscribe(serivce_type: string, serviceQualifier:string, methodId: number, streamQualifier : string, callback: (x:RawUpdate[])=>void) : number { + // Send a subscription request to the server + // Record the id of this subscription in the subscriptions table + // When a subscription message arrives call the callback + const id = ++this.next_id + let subReq = GatewaySubscribe.create({ + requestId: id, + method: methodId, + token: this.token, + serviceType : serivce_type, + serviceQualifier: serviceQualifier, + streamQualifier: streamQualifier + }); + let message = GatewayRequest.create({ + subscribe : subReq + }); + assertValid(GatewayRequest, message) + this.subscriptions[id] = { + id, + callback + }; + this.send(message); + return id; + } + + unsubscribe(subscriptionId : number) { + let req = GatewayUnsubscribe.crate({ subscriptionId}); + let message = GatewayRequest.create({ + unsubscribe: req + }); + this.send(message); + } on_msg(header: {requestId:number, code: number}, body : Uint8Array) { // console.log("message received: " + JSON.stringify(header) + " => " + (header.requestId in this.outstanding)); @@ -172,6 +225,20 @@ export class ServiceContext { console.log("unexpected response") } } + + + on_tick(update : {id:number, updates : [any]}) { + // updates type is defined in apigateway.proto + // I could make an interface here but nothing would keep it in sync with the proto + // so we may as well do it live + if (update.id && this.subscriptions) { + const s = this.subscriptions[update.id]; + if (s) { + assert(update.id == s.id); + s.callback(update.updates); + } + } + } }