Skip to content

Commit

Permalink
Use account service v2 in gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaosvex committed Oct 4, 2024
1 parent 6769980 commit 94f2350
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 217 deletions.
3 changes: 2 additions & 1 deletion schemas/spark/v2/services/Accountv2.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ enum Status : ubyte {
RPC_ERROR,
ILLFORMED_MESSAGE,
ALREADY_LOGGED_IN,
SESSION_NOT_FOUND
SESSION_NOT_FOUND,
ACCOUNT_NOT_FOUND
}

table LookupID {
Expand Down
8 changes: 7 additions & 1 deletion src/account/AccountService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ AccountService::handle_account_i_d_fetch(
const spark::v2::Link& link,
const spark::v2::Token& token) {
LOG_TRACE(logger_) << log_func << LOG_ASYNC;
return std::nullopt;

messaging::Accountv2::AccountFetchResponseT response {
.status = messaging::Accountv2::Status::OK,
.account_id = 1 // todo, temp
};

return response;
}

std::optional<messaging::Accountv2::DisconnectSessionResponseT>
Expand Down
92 changes: 92 additions & 0 deletions src/gateway/AccountClient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright (c) 2024 Ember
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#include "AccountClient.h"
#include <vector>

namespace ember {

AccountClient::AccountClient(spark::v2::Server& spark, log::Logger& logger)
: services::Accountv2Client(spark),
logger_(logger) {
connect("127.0.0.1", 8000); // temp
}

void AccountClient::on_link_up(const spark::v2::Link& link) {
LOG_TRACE(logger_) << log_func << LOG_ASYNC;
link_ = link;
}

void AccountClient::on_link_down(const spark::v2::Link& link) {
LOG_TRACE(logger_) << log_func << LOG_ASYNC;
}

void AccountClient::locate_session(const std::uint32_t account_id, LocateCB cb) const {
LOG_TRACE(logger_) << log_func << LOG_ASYNC;

messaging::Accountv2::SessionLookupT msg {
.account_id = account_id
};

send<messaging::Accountv2::SessionResponse>(msg, link_,
[this, cb = std::move(cb)](auto link, auto message) {
handle_locate_response(message, cb);
}
);
}

void AccountClient::locate_account_id(const std::string& username, AccountCB cb) const {
LOG_TRACE(logger_) << log_func << LOG_ASYNC;

messaging::Accountv2::LookupIDT msg {
.account_name = username
};

send<messaging::Accountv2::AccountFetchResponse>(msg, link_,
[this, cb = std::move(cb)](auto link, auto message) {
handle_lookup_response(message, cb);
}
);
}

void AccountClient::handle_lookup_response(
std::expected<const messaging::Accountv2::AccountFetchResponse*, spark::v2::Result> resp,
const AccountCB& cb) const {
LOG_TRACE(logger_) << log_func << LOG_ASYNC;

if(!resp) {
cb(messaging::Accountv2::Status::RPC_ERROR, {});
return;
}

const auto msg = *resp;
cb(msg->status(), msg->account_id());
}

void AccountClient::handle_locate_response(
std::expected<const messaging::Accountv2::SessionResponse*, spark::v2::Result> resp,
const LocateCB& cb) const {
LOG_TRACE(logger_) << log_func << LOG_ASYNC;

if(!resp) {
cb(messaging::Accountv2::Status::RPC_ERROR, {});
return;
}

const auto msg = *resp;

if(!msg->key()) {
cb(msg->status(), {});
return;
}

auto key = Botan::BigInt::decode(msg->key()->data(), msg->key()->size());
cb(msg->status(), std::move(key));
}

} // ember
49 changes: 49 additions & 0 deletions src/gateway/AccountClient.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2024 Ember
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#pragma once

#include <Accountv2ClientStub.h>
#include <logger/Logger.h>
#include <spark/v2/Server.h>
#include <botan/bigint.h>
#include <functional>
#include <cstdint>

namespace ember {

class AccountClient final : public services::Accountv2Client {
public:
using LocateCB = std::function<void(messaging::Accountv2::Status, Botan::BigInt)>;
using AccountCB = std::function<void(messaging::Accountv2::Status, std::uint32_t)>;

private:
log::Logger& logger_;
spark::v2::Link link_;

void on_link_up(const spark::v2::Link& link) override;
void on_link_down(const spark::v2::Link& link) override;

void handle_locate_response(
std::expected<const messaging::Accountv2::SessionResponse*, spark::v2::Result> resp,
const LocateCB& cb
) const;

void handle_lookup_response(
std::expected<const messaging::Accountv2::AccountFetchResponse*, spark::v2::Result> resp,
const AccountCB& cb
) const;

public:
AccountClient(spark::v2::Server& spark, log::Logger& logger);

void locate_session(std::uint32_t account_id, LocateCB cb) const;
void locate_account_id(const std::string& username, AccountCB cb) const;
};

} // ember
134 changes: 0 additions & 134 deletions src/gateway/AccountService.cpp

This file was deleted.

60 changes: 0 additions & 60 deletions src/gateway/AccountService.h

This file was deleted.

4 changes: 2 additions & 2 deletions src/gateway/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ set(LIBRARY_HDR
NetworkListener.h
ClientConnection.h
ClientConnection.inl
AccountService.h
AccountClient.h
RealmQueue.h
ClientHandler.h
ClientHandler.inl
Expand Down Expand Up @@ -62,7 +62,7 @@ set(LIBRARY_SRC
ClientConnection.cpp
RealmService.cpp
RealmServicev2.cpp
AccountService.cpp
AccountClient.cpp
RealmQueue.cpp
ClientHandler.cpp
QoS.cpp
Expand Down
Loading

0 comments on commit 94f2350

Please sign in to comment.