From 3dc8dd7acb9b4c4ac4d9501fd3cb66c1ffb95feb Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Wed, 25 Apr 2018 12:29:39 -0400 Subject: [PATCH] SPARK-958 APIGateway.cpp RPC handler --- src/client/sparkcmd/Program.cs | 13 ++++ src/lib/api/apicore.proto | 2 - src/lib/api/apigateway.proto | 14 +++- src/lib/api/public/ServiceContext.h | 2 + src/server/sparkd/http/ApiGateway.cpp | 82 ++++++++++++++++---- src/server/sparkd/http/ApiGateway.h | 4 +- src/server/sparkd/http/WebServer.cpp | 13 ++-- src/web/api/__tests__/ServiceContext.test.ts | 6 +- src/web/api/src/HelloService.ts | 6 +- src/web/api/src/ServiceContext.ts | 39 ++++++---- src/web/api/tsconfig.json | 1 - 11 files changed, 136 insertions(+), 46 deletions(-) diff --git a/src/client/sparkcmd/Program.cs b/src/client/sparkcmd/Program.cs index 170a954e5..2e3fd0c2b 100644 --- a/src/client/sparkcmd/Program.cs +++ b/src/client/sparkcmd/Program.cs @@ -1,6 +1,7 @@ using bts.api; using bts.api.core; using bts.api.synthetic; +using bts.api.test; using System; using System.Linq; using System.Threading.Tasks; @@ -22,6 +23,9 @@ namespace sparkcmd else if (args.FirstOrDefault() == "synthetic") { synthetic_test(); } + else if (args.FirstOrDefault() == "hello") { + hello_test(); + } else if (args.FirstOrDefault() == "scriptserver") { ScriptClient.runserver(); } @@ -40,6 +44,15 @@ namespace sparkcmd } + private static void hello_test() { + using (var ctx = new ServiceContext()) { + var hello = ctx.locate(); + var a = hello.say_hello(new HelloRequest { Name = "Bob" }); + Console.WriteLine(a.Message); + + } + } + private static void synthetic_test() { using (var ctx = new ServiceContext()) { var security = new Security { diff --git a/src/lib/api/apicore.proto b/src/lib/api/apicore.proto index 2d609c2b6..8c156ab18 100644 --- a/src/lib/api/apicore.proto +++ b/src/lib/api/apicore.proto @@ -15,8 +15,6 @@ message PingPong { message RequestHeader { int32 request_id = 1; int32 method = 2; - string token = 3; - string service_id = 4; } enum ApiError { diff --git a/src/lib/api/apigateway.proto b/src/lib/api/apigateway.proto index 62f97cbbc..aa2f10466 100644 --- a/src/lib/api/apigateway.proto +++ b/src/lib/api/apigateway.proto @@ -1,6 +1,5 @@ syntax="proto3"; package bts.api.gateway; -import "lib/api/apicore.proto"; option csharp_namespace="bts.api.gateway"; message GatewayLogin { @@ -12,6 +11,13 @@ message GatewayLoginResult { string reason = 2; string token = 3; } +message GatewayCallHeader { + int32 request_id = 1; + int32 method = 2; + string token = 3; + string service_type = 4; + string service_qualifier = 5; +} message GatewaySubscribe { } message GatewayUnsubscribe {} @@ -22,7 +28,7 @@ message GatewayRequest { // proxy is not generated until first call // client can determine if a service "should" exist. // If the types are there server will return OK regardless - api.RequestHeader call = 2; + GatewayCallHeader call = 2; GatewaySubscribe subscribe = 3; GatewayUnsubscribe unsubscribe = 4; } @@ -31,7 +37,7 @@ message GatewayRequest { message GatewayResponse { oneof response { GatewayLoginResult logon = 1; - api.ResponseHeader result = 2; + bytes call_result = 2; + string error = 10; } - bytes addendum = 100; } \ No newline at end of file diff --git a/src/lib/api/public/ServiceContext.h b/src/lib/api/public/ServiceContext.h index 4816b7319..c42515ab8 100644 --- a/src/lib/api/public/ServiceContext.h +++ b/src/lib/api/public/ServiceContext.h @@ -33,6 +33,8 @@ public: typedef std::function Action; void post(const Action&, uint64_t delayms=0); + Quickbus& bus() const { return _qbus; } + private: friend class ApiServer; std::unique_ptr create_channel(const char*, const char*) const; diff --git a/src/server/sparkd/http/ApiGateway.cpp b/src/server/sparkd/http/ApiGateway.cpp index 27f465aa9..648bdb231 100644 --- a/src/server/sparkd/http/ApiGateway.cpp +++ b/src/server/sparkd/http/ApiGateway.cpp @@ -2,6 +2,8 @@ #include "lib/logging/logging.h" #include "lib/api/public/ApiUtils.h" +#include "lib/api/core/ApiClientInterop.h" +#include "lib/utils/bts_assert.h" #include #include #include @@ -14,33 +16,87 @@ namespace http { struct ApiGatewayImpl { std::mutex _m; - const api::ServiceContext& _ctx; + const api::ServiceContext& _sc; std::unordered_map _apiclient; std::vector _subscriptions; + ApiGatewayImpl(const api::ServiceContext& ctx) : _m(), - _ctx(ctx), + _sc(ctx), _apiclient(), _subscriptions() { } - void handle_login(const GatewayLogin& login, GatewayLoginResult* presult) { + void handle_login(const GatewayLogin& login, const Responder& r) { LOG(Api,Info, "Logon request for {}", login.username()); + GatewayResponse result; + //todo: UserService authenticate_login_async if (login.username() == "bts") { - presult->set_is_valid(true); - presult->set_token("SuperSecretToken"); + result.mutable_logon()->set_is_valid(true); + result.mutable_logon()->set_token("SuperSecretToken"); + r(result); } else { - presult->set_is_valid(false); - presult->set_reason("Your mother is a hamster and your father smelt of elderberries."); + result.mutable_logon()->set_is_valid(false); + result.mutable_logon()->set_reason("Your mother is a hamster and your father smelt of elderberries."); + r(result); } } - // void handle_call() + void error(const Responder& r, const std::string msg) { + GatewayResponse response; + response.set_error(msg); + 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 + 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()); + _apiclient.insert(std::make_pair(key, pclient)); + } + else { + pclient = iter->second; + } + // Form the api request buffer + api::RequestHeader api_header; + api_header.set_method(header.method()); + api_header.set_request_id(header.request_id()); + //todo: token + int header_size = 1 + api_header.ByteSize(); + bts_assert(header_size < 128); + std::string buffer; + buffer.reserve(header_size + body.size()); + buffer += (char)header_size; + api_header.AppendToString(&buffer); + buffer += body; + // call send + Responder* presponder = new Responder(move(r)); + api_client_send(pclient, buffer.c_str(), buffer.size(), [](const char* buffer, size_t length, void* extra) { + // buffer = [size][ResponseHeader][Payload]. + GatewayResponse response; + response.set_call_result(buffer, length); + LOG(Api,Info, "Gateway Result: {}", api::toJSON(response)); + // in response send to client + const Responder* r = static_cast(extra); + if (r != nullptr) { + (*r)(response); + delete r; + } + }, presponder); + } + // void handle_subscribe() // void handle_unsubscribe() - }; @@ -52,21 +108,21 @@ ApiGatewaySession::~ApiGatewaySession(){} -const GatewayResponse ApiGatewaySession::handle(const api::gateway::GatewayRequest& request) { +void ApiGatewaySession::handle(const api::gateway::GatewayRequest& request, const Responder& reply) { GatewayResponse response; LOG(Api,Info, "Gateway Request: {}", api::toJSON(request)); switch(request.request_case()) { case GatewayRequest::kLogin: - _me->handle_login(request.login(), response.mutable_logon()); + _me->handle_login(request.login(), reply); break; case GatewayRequest::kCall: + _me->handle_call(request.call(), request.addendum(), reply); + break; case GatewayRequest::kSubscribe: case GatewayRequest::kUnsubscribe: case GatewayRequest::REQUEST_NOT_SET: break; } - - return response; } }} \ No newline at end of file diff --git a/src/server/sparkd/http/ApiGateway.h b/src/server/sparkd/http/ApiGateway.h index 8d2c6514f..f710e00eb 100644 --- a/src/server/sparkd/http/ApiGateway.h +++ b/src/server/sparkd/http/ApiGateway.h @@ -8,6 +8,8 @@ namespace bts { namespace http { +typedef std::function Responder; + struct ApiGatewayImpl; class ApiGatewaySession { std::shared_ptr _me; @@ -17,7 +19,7 @@ class ApiGatewaySession { ApiGatewaySession(const api::ServiceContext& ctx); ~ApiGatewaySession(); - const api::gateway::GatewayResponse handle(const api::gateway::GatewayRequest& request); + void handle(const api::gateway::GatewayRequest& request, const Responder&); }; }} \ No newline at end of file diff --git a/src/server/sparkd/http/WebServer.cpp b/src/server/sparkd/http/WebServer.cpp index 69b6f64c4..655b27e24 100644 --- a/src/server/sparkd/http/WebServer.cpp +++ b/src/server/sparkd/http/WebServer.cpp @@ -47,7 +47,7 @@ namespace http { std::string _root; const api::ServiceContext& _ctx; - ServerState(const api::ServiceContext ctx, int port, const char* web_root) : + ServerState(const api::ServiceContext& ctx, int port, const char* web_root) : _m(), _dispatcher(utils::Dispatcher::get_named("Webserver")), _session(), @@ -98,8 +98,9 @@ namespace http { if (iter != _session.end()) { GatewayRequest request; if(request.ParseFromString(msg->get_payload())) { - GatewayResponse response = iter->second.handle(request); - _server.send(hdl, response.SerializeAsString(), websocketpp::frame::opcode::binary); + iter->second.handle(request, [this,hdl](const GatewayResponse& response) { + _server.send(hdl, response.SerializeAsString(), websocketpp::frame::opcode::binary); + }); } else { LOG(Api,Info, "Could not parse request as GatewayRequest: {}", msg->get_payload()); @@ -126,9 +127,9 @@ namespace http { // Concatanate std::string body; for (const auto& p : files) { - body += "//////////////////////////\n"; - body += "// " + p.filename().string(); - body += "\n//////////////////////////\n"; + body += "##########################\n"; + body += "## " + p.filename().string(); + body += "\n##########################\n"; body += read_file(p.string()); } diff --git a/src/web/api/__tests__/ServiceContext.test.ts b/src/web/api/__tests__/ServiceContext.test.ts index c86270929..bc4ff55d7 100644 --- a/src/web/api/__tests__/ServiceContext.test.ts +++ b/src/web/api/__tests__/ServiceContext.test.ts @@ -7,8 +7,10 @@ test('log in and say hi', async () => { let ctx = await connect("bts", "password"); expect(ctx instanceof ServiceContext).toBe(true) const hello = new HelloService(ctx) - const reply = await hello.say_hello({ name: "name" }) -}); + const reply = await hello.say_hello({ name: "Bob" }) + expect(reply.message).toBe("hello Bob") + console.log(reply.message) +}, 15000); test('api_errors_updated', () => { diff --git a/src/web/api/src/HelloService.ts b/src/web/api/src/HelloService.ts index be9425af6..c369c0b6e 100644 --- a/src/web/api/src/HelloService.ts +++ b/src/web/api/src/HelloService.ts @@ -15,11 +15,11 @@ export interface HelloResponse { export class HelloService { private context : ServiceContext - private service_id : string + private qualifier : string constructor(sc : ServiceContext, qualifier?: string) { this.context = sc - this.service_id = (qualifier||"").replace(/[+_]/g,""); + this.qualifier = (qualifier||""); } async say_hello(request : HelloRequest) : Promise { @@ -27,7 +27,7 @@ export class HelloService { assertValid(HelloRequestProto, r) const buffer = HelloRequestProto.encode(r).finish() // result = Uint8Array - const result = await this.context.call(this.service_id, 0, buffer) + const result = await this.context.call("HelloService", this.qualifier, 0, buffer) // decode HelloResponse type const v = HelloResponseProto.decode(result); diff --git a/src/web/api/src/ServiceContext.ts b/src/web/api/src/ServiceContext.ts index 3405f72f2..0329628bd 100644 --- a/src/web/api/src/ServiceContext.ts +++ b/src/web/api/src/ServiceContext.ts @@ -19,7 +19,8 @@ function default_ws_uri() { 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 RequestHeader = root.lookupType("bts.api.RequestHeader") +const GatewayCallHeader = root.lookupType("bts.api.gateway.GatewayCallHeader") +const ResponseHeader = root.lookupType("bts.api.ResponseHeader") class PrivateContext { url : string @@ -87,10 +88,19 @@ class PrivateContext { },3000); } onmessage(e){ - const buffer = new Uint8Array(e.data) - const response = GatewayResponse.decode(buffer) - if (response) { - this.sc.on_msg(response.result, response.addendum) + const raw_buffer = new Uint8Array(e.data) + const response = GatewayResponse.decode(raw_buffer) + if (response && response.callResult) { + const call_buffer = response.callResult + const header_size = call_buffer[0] + const sub = call_buffer.subarray(1, header_size) + const header = ResponseHeader.decode(sub) + const body = call_buffer.subarray(header_size) + // console.log(`onmessage: [${call_buffer}] h:[${sub}] b:[${body}]`) + this.sc.on_msg(header, body) + } + else if (response && response.error) { + console.log(response.error); } } } @@ -107,13 +117,14 @@ export class ServiceContext { this.outstanding = {} } - call(service_id : string, method_id : number, body : Uint8Array) : Promise { + call(service_type : string, qualifier : string, method_id : number, body : Uint8Array) : Promise { const id = ++this.next_id - let header = RequestHeader.create({ - request_id: id, + let header = GatewayCallHeader.create({ + requestId: id, method: method_id, token: this.token, - service_id: service_id + serviceType: service_type, + serviceQualifier: qualifier }) let message = GatewayRequest.create({ call: header, @@ -128,11 +139,11 @@ export class ServiceContext { } subscribe() {/*not impl*/} - on_msg(header: {request_id:number, code: number}, body : Uint8Array) { - console.log("message received: " + JSON.stringify(header)); - if (header.request_id in this.outstanding) { - const v = this.outstanding[header.request_id] - delete this.outstanding[header.request_id] + on_msg(header: {requestId:number, code: number}, body : Uint8Array) { + console.log("message received: " + JSON.stringify(header) + " => " + (header.requestId in this.outstanding)); + if (header.requestId in this.outstanding) { + const v = this.outstanding[header.requestId] + delete this.outstanding[header.requestId] if (header.code == 0) { v.resolve(body) } diff --git a/src/web/api/tsconfig.json b/src/web/api/tsconfig.json index fcf7812fb..851a63d54 100644 --- a/src/web/api/tsconfig.json +++ b/src/web/api/tsconfig.json @@ -3,7 +3,6 @@ "target": "ES5", "module": "commonjs", "lib": [ - "es2015", "dom", "es2016" ],