SPARK-958 APIGateway.cpp RPC handler

This commit is contained in:
Eric Harding committed 2018-04-25 18:01:05 -04:00
1 parent f3a2525b8d
commit 3dc8dd7acb
11 files changed
+136 -46

No files matched your search

+13
View File
@@ -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<HelloService>();
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 {
-2
View File
@@ -15,8 +15,6 @@ message PingPong {
message RequestHeader {
int32 request_id = 1;
int32 method = 2;
string token = 3;
string service_id = 4;
}
enum ApiError {
+10 -4
View File
@@ -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;
}
+2
View File
@@ -33,6 +33,8 @@ public:
typedef std::function<void()> Action;
void post(const Action&, uint64_t delayms=0);
Quickbus& bus() const { return _qbus; }
private:
friend class ApiServer;
std::unique_ptr<ClientChannel> create_channel(const char*, const char*) const;
+69 -13
View File
@@ -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 <vector>
#include <unordered_map>
#include <mutex>
@@ -14,33 +16,87 @@ namespace http {
struct ApiGatewayImpl {
std::mutex _m;
const api::ServiceContext& _ctx;
const api::ServiceContext& _sc;
std::unordered_map<std::string, api::ApiClient*> _apiclient;
std::vector<api::TableSubscription> _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<std::mutex> 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<const Responder*>(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;
}
}}
+3 -1
View File
@@ -8,6 +8,8 @@
namespace bts {
namespace http {
typedef std::function<void(const api::gateway::GatewayResponse&)> Responder;
struct ApiGatewayImpl;
class ApiGatewaySession {
std::shared_ptr<ApiGatewayImpl> _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&);
};
}}
+7 -6
View File
@@ -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());
}
+4 -2
View File
@@ -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(<ServiceContext>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', () => {
+3 -3
View File
@@ -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<HelloResponse> {
@@ -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);
+25 -14
View File
@@ -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<Uint8Array> {
call(service_type : string, qualifier : string, method_id : number, body : Uint8Array) : Promise<Uint8Array> {
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)
}
-1
View File
@@ -3,7 +3,6 @@
"target": "ES5",
"module": "commonjs",
"lib": [
"es2015",
"dom",
"es2016"
],