273 lines
9.6 KiB
C++
273 lines
9.6 KiB
C++
#include "ApiGateway.h"
|
|
|
|
#include "lib/api/core/ApiClientInterop.h"
|
|
#include "lib/api/public/ApiUtils.h"
|
|
#include "lib/api/services/AdminService.client.pb.h"
|
|
#include "lib/api/services/AuthService.client.pb.h"
|
|
#include "lib/logging/logging.h"
|
|
#include "lib/utils/bts_assert.h"
|
|
#include <mutex>
|
|
#include <unordered_map>
|
|
#include <vector>
|
|
|
|
using namespace bts::api::gateway;
|
|
|
|
namespace bts {
|
|
namespace http {
|
|
|
|
struct SubscriptionContext {
|
|
int id;
|
|
Responder send;
|
|
api::TableSubscription table;
|
|
};
|
|
|
|
struct ApiGatewayImpl {
|
|
std::mutex _m;
|
|
const api::ServiceContext &_sc;
|
|
api::user::AuthService _logon;
|
|
|
|
std::unordered_map<std::string, api::ApiClient *> _apiclient;
|
|
std::map<int32_t, SubscriptionContext *> _subscriptions;
|
|
|
|
ApiGatewayImpl(const api::ServiceContext &ctx)
|
|
: _m(), _sc(ctx), _logon(ctx.locate<api::user::AuthService>()),
|
|
_apiclient(), _subscriptions() {}
|
|
|
|
void handle_login(const GatewayLogin &login, const Responder &respond) {
|
|
LOG(Api, Info, "Logon request for {}", login.username());
|
|
|
|
api::user::AuthenticateLoginRequest alr;
|
|
alr.set_login(login.username());
|
|
alr.set_password(login.password());
|
|
_logon.authenticate_login(
|
|
alr, [respond](const api::Result<api::user::AuthenticateLoginResponse>
|
|
&logon_response) {
|
|
GatewayResponse resp;
|
|
resp.mutable_logon()->set_is_valid(false);
|
|
if (logon_response) {
|
|
if (logon_response.value()->is_valid()) {
|
|
resp.mutable_logon()->set_is_valid(true);
|
|
resp.mutable_logon()->mutable_token()->CopyFrom(
|
|
logon_response.value()->token());
|
|
resp.mutable_logon()->set_can_create(
|
|
logon_response.value()->can_create());
|
|
// Do NOT log in _ctx as it is shared across all web clients
|
|
} else {
|
|
resp.mutable_logon()->set_reason(
|
|
logon_response.value()->reason());
|
|
}
|
|
} else {
|
|
resp.mutable_logon()->set_reason(
|
|
logon_response.error_message());
|
|
}
|
|
respond(resp);
|
|
});
|
|
}
|
|
|
|
void error(const Responder &r, const std::string msg) {
|
|
GatewayResponse response;
|
|
response.set_error(msg);
|
|
r(response);
|
|
}
|
|
|
|
// Create or return the client proxy
|
|
api::ApiClient *get_api_client(
|
|
const std::string &service_type,
|
|
const std::string &qualifier,
|
|
bool create = true) {
|
|
auto key = service_type + qualifier;
|
|
auto iter = _apiclient.find(key);
|
|
if (iter == _apiclient.end()) {
|
|
if (create) {
|
|
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
|
|
return nullptr;
|
|
} else {
|
|
return iter->second;
|
|
}
|
|
}
|
|
|
|
void handle_call(
|
|
const GatewayCallHeader &header,
|
|
const std::string &body,
|
|
const Responder &reply) {
|
|
std::lock_guard<std::mutex> 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());
|
|
api_header.set_request_id(header.request_id());
|
|
api_header.mutable_token()->CopyFrom(header.token());
|
|
int header_size = 1 + api_header.ByteSizeLong();
|
|
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(reply));
|
|
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, Diag, "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(const GatewaySubscribe &request, const Responder &reply) {
|
|
std::lock_guard<std::mutex> 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* preply = new
|
|
// Responder(move(reply));
|
|
auto id = request.request_id();
|
|
SubscriptionContext *pcontext =
|
|
new SubscriptionContext{id, move(reply), nullptr};
|
|
|
|
auto table = api_client_subscribe(
|
|
pclient, request.method_name().c_str(),
|
|
request.stream_qualifier().c_str(),
|
|
[](api::EventType type, const api::StreamUpdate *changes, int count,
|
|
void *extra) {
|
|
SubscriptionContext *pcontext = (SubscriptionContext *)extra;
|
|
GatewayResponse update;
|
|
update.mutable_tick()->set_id(pcontext->id);
|
|
if (type == api::EventType::TABLE_UPDATED) {
|
|
for (int x = 0; x < count; ++x) {
|
|
const api::StreamUpdate ¤t = changes[x];
|
|
auto *row = update.mutable_tick()->add_updates();
|
|
row->set_update_type(
|
|
(StreamGWUpdate::UpdateType)current.type);
|
|
row->set_key(current.key);
|
|
if (current.type == api::StreamUpdate::CREATE ||
|
|
current.type == api::StreamUpdate::REPLACE) {
|
|
row->set_buffer(current.buffer, current.buffer_len);
|
|
}
|
|
}
|
|
} else {
|
|
update.mutable_tick()->add_updates()->set_update_type(
|
|
StreamGWUpdate::RESET);
|
|
}
|
|
pcontext->send(update);
|
|
},
|
|
pcontext);
|
|
pcontext->table = table;
|
|
|
|
_subscriptions[id] = pcontext;
|
|
// reply with empty response so we don't time out
|
|
reply({});
|
|
}
|
|
void handle_unsubscribe(
|
|
const GatewayUnsubscribe &request, const Responder &reply) {
|
|
auto id = request.subscription_id();
|
|
api::ApiClient *pclient = get_api_client(
|
|
request.service_type(), request.service_qualifier(), false);
|
|
bts_assert_d(pclient);
|
|
auto iter = _subscriptions.find(id);
|
|
if (iter != _subscriptions.end()) {
|
|
SubscriptionContext *pcontext = iter->second;
|
|
_subscriptions.erase(iter);
|
|
if (pcontext && pclient) {
|
|
api_client_unsubscribe(pclient, pcontext->table);
|
|
}
|
|
// Note: not safe to delete context yet
|
|
api::api_post(
|
|
_sc.bus(), [pcontext]() { delete pcontext; }, 5000);
|
|
}
|
|
// empty response makes quickbus happy
|
|
reply({});
|
|
}
|
|
};
|
|
|
|
ApiGatewaySession::ApiGatewaySession() : _me() {}
|
|
ApiGatewaySession::ApiGatewaySession(const api::ServiceContext &ctx)
|
|
: _me(std::make_unique<ApiGatewayImpl>(ctx)) {}
|
|
ApiGatewaySession::~ApiGatewaySession() {}
|
|
|
|
void ApiGatewaySession::handle(
|
|
const api::gateway::GatewayRequest &request, const Responder &reply) {
|
|
LOG(Api, Diag, "Gateway Request: {}", api::toJSON(request));
|
|
switch (request.request_case()) {
|
|
case GatewayRequest::kLogin:
|
|
_me->handle_login(request.login(), reply);
|
|
break;
|
|
case GatewayRequest::kCall:
|
|
_me->handle_call(request.call(), request.addendum(), reply);
|
|
break;
|
|
case GatewayRequest::kSubscribe:
|
|
_me->handle_subscribe(request.subscribe(), reply);
|
|
break;
|
|
case GatewayRequest::kUnsubscribe:
|
|
_me->handle_unsubscribe(request.unsubscribe(), reply);
|
|
break;
|
|
case GatewayRequest::REQUEST_NOT_SET: {
|
|
_me->error(reply, "Request type not set");
|
|
} break;
|
|
}
|
|
}
|
|
|
|
std::string get_server_info(const api::ServiceContext &ctx) {
|
|
using namespace api::admin;
|
|
using std::endl;
|
|
api::VoidMessage vod;
|
|
AdminService admin(ctx.locate<api::admin::AdminService>());
|
|
auto response = admin.get_admin_data(vod, 100);
|
|
|
|
std::stringstream ss;
|
|
ss << "{" << endl;
|
|
|
|
if (response.success()) {
|
|
ss << " \"users\": [" << endl;
|
|
bool first = true;
|
|
for (const auto &user : response.value()->users()) {
|
|
if (user.is_present()) {
|
|
ss << " ";
|
|
if (first) {
|
|
first = false;
|
|
} else {
|
|
ss << ",";
|
|
}
|
|
ss << "{ ";
|
|
ss << "\"name\": \"" << user.name() << "\", ";
|
|
ss << "\"operator_id\": \""
|
|
<< user.user_data().exchange_user_data().cme_data().operator_id()
|
|
<< "\"";
|
|
ss << "}" << endl;
|
|
}
|
|
}
|
|
ss << " ]" << endl;
|
|
}
|
|
|
|
ss << "}" << endl;
|
|
|
|
return ss.str();
|
|
}
|
|
|
|
} // namespace http
|
|
} // namespace bts
|