implement object registry
This commit is contained in:
+15
-2
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <ipc/IpcConfig.h>
|
||||
#include <rpc/RpcRegistry.h>
|
||||
#include <rpc/RpcValue.h>
|
||||
|
||||
#include <string>
|
||||
@@ -10,11 +11,18 @@
|
||||
// (RpcValue/RpcArgs).
|
||||
namespace IpcCodec {
|
||||
|
||||
// Запрос: имя метода + вектор аргументов.
|
||||
inline IpcMessage encodeRequest(const std::string& method,
|
||||
// Используем ObjectId из специализированного RPC-реестра.
|
||||
using ObjectId = RpcRegistry::ObjectId;
|
||||
|
||||
// Запрос: ObjectId + имя метода + вектор аргументов.
|
||||
inline IpcMessage encodeRequest(ObjectId objectId,
|
||||
const std::string& method,
|
||||
const RpcArgs& args) {
|
||||
IpcMessage msg;
|
||||
|
||||
// ObjectId (PoC: приводим к int)
|
||||
msg.add(static_cast<int>(objectId));
|
||||
|
||||
// имя метода
|
||||
msg.add(method);
|
||||
|
||||
@@ -27,10 +35,15 @@ inline IpcMessage encodeRequest(const std::string& method,
|
||||
}
|
||||
|
||||
inline void decodeRequest(const IpcMessage& msg,
|
||||
ObjectId& objectId,
|
||||
std::string& method,
|
||||
RpcArgs& args) {
|
||||
IpcMessage copy = msg;
|
||||
|
||||
// ObjectId (PoC: читаем как int и приводим к ObjectId)
|
||||
int rawId = copy.get<int>();
|
||||
objectId = static_cast<ObjectId>(rawId);
|
||||
|
||||
// имя метода
|
||||
method = copy.get<std::string>();
|
||||
|
||||
|
||||
@@ -24,11 +24,12 @@ public:
|
||||
return false;
|
||||
}
|
||||
|
||||
IpcCodec::ObjectId objectId;
|
||||
std::string method;
|
||||
RpcArgs args;
|
||||
IpcCodec::decodeRequest(req, method, args);
|
||||
IpcCodec::decodeRequest(req, objectId, method, args);
|
||||
|
||||
RpcValue result = invoker_.dispatch(method, args);
|
||||
RpcValue result = invoker_.dispatch(objectId, method, args);
|
||||
IpcMessage resp = IpcCodec::encodeResponse(result);
|
||||
channel_.send(resp);
|
||||
return true;
|
||||
|
||||
@@ -9,13 +9,14 @@
|
||||
// и IpcCodec, но снаружи предъявляет только callTyped<T>(...).
|
||||
class IpcMarshaller {
|
||||
public:
|
||||
explicit IpcMarshaller(IpcChannel& ch)
|
||||
: channel(ch) {}
|
||||
explicit IpcMarshaller(IpcChannel& ch, IpcCodec::ObjectId objectId = 0)
|
||||
: channel(ch)
|
||||
, objectId_(objectId) {}
|
||||
|
||||
// Базовый type-erased вызов: принимает вектор RpcValue и возвращает RpcValue.
|
||||
RpcValue call(const std::string& method, const RpcArgs& args) {
|
||||
// упаковать запрос в IpcMessage
|
||||
IpcMessage msg = IpcCodec::encodeRequest(method, args);
|
||||
IpcMessage msg = IpcCodec::encodeRequest(objectId_, method, args);
|
||||
|
||||
// отправить
|
||||
channel.send(msg);
|
||||
@@ -39,6 +40,7 @@ public:
|
||||
|
||||
private:
|
||||
IpcChannel& channel;
|
||||
IpcCodec::ObjectId objectId_;
|
||||
};
|
||||
|
||||
|
||||
|
||||
+13
-36
@@ -1,56 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
#include <rpc/RpcRegistry.h>
|
||||
#include <rpc/RpcValue.h>
|
||||
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <unordered_map>
|
||||
|
||||
// Инвокер — тонкий фасад над RpcRegistry:
|
||||
// по ObjectId находит объект и делегирует вызов его IRpcObject::invoke().
|
||||
class RpcInvoker {
|
||||
public:
|
||||
template<typename Obj, typename Ret, typename... Args>
|
||||
void registerMethod(Obj* instance,
|
||||
const std::string& name,
|
||||
Ret (Obj::*method)(Args...)) {
|
||||
handlers[name] =
|
||||
[instance, method](const RpcArgs& args) -> RpcValue {
|
||||
auto tuple = readArgs<Args...>(args);
|
||||
Ret result = std::apply(
|
||||
method, std::tuple_cat(std::make_tuple(instance), tuple));
|
||||
using ObjectId = RpcRegistry::ObjectId; // согласован с IpcCodec::ObjectId
|
||||
|
||||
// PoC: считаем, что Ret == int.
|
||||
return RpcValue::fromInt(result);
|
||||
};
|
||||
}
|
||||
explicit RpcInvoker(RpcRegistry& registry)
|
||||
: registry_(registry) {}
|
||||
|
||||
RpcValue dispatch(const std::string& method, const RpcArgs& args) const {
|
||||
auto it = handlers.find(method);
|
||||
if (it == handlers.end()) {
|
||||
RpcValue dispatch(ObjectId objectId,
|
||||
const std::string& method,
|
||||
const RpcArgs& args) const {
|
||||
IRpcObject* obj = registry_.get(objectId);
|
||||
if (!obj) {
|
||||
// PoC: в случае ошибки возвращаем 0.
|
||||
return RpcValue::fromInt(0);
|
||||
}
|
||||
|
||||
return it->second(args);
|
||||
return obj->invoke(method, args);
|
||||
}
|
||||
|
||||
private:
|
||||
template<typename... Args, std::size_t... I>
|
||||
static std::tuple<Args...>
|
||||
readArgsImpl(const RpcArgs& args, std::index_sequence<I...>) {
|
||||
return std::tuple<Args...>{
|
||||
static_cast<Args>(args[I].asInt())...}; // PoC: только int
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
static std::tuple<Args...> readArgs(const RpcArgs& args) {
|
||||
return readArgsImpl<Args...>(
|
||||
args, std::make_index_sequence<sizeof...(Args)>{});
|
||||
}
|
||||
|
||||
std::unordered_map<std::string,
|
||||
std::function<RpcValue(const RpcArgs&)>>
|
||||
handlers;
|
||||
RpcRegistry& registry_;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
|
||||
#include <rpc/RpcValue.h>
|
||||
|
||||
// Базовый интерфейс для всех RPC-объектов, живущих в реестре.
|
||||
// Каждая реализация (обычно *Skeleton) знает, как вызвать реальные методы.
|
||||
struct IRpcObject {
|
||||
virtual ~IRpcObject() = default;
|
||||
|
||||
virtual RpcValue invoke(const std::string& method,
|
||||
const RpcArgs& args) = 0;
|
||||
};
|
||||
|
||||
// Специализированный реестр объектов именно для RPC-уровня.
|
||||
// Владеет скелетонами (IRpcObject) через unique_ptr. Сами доменные объекты
|
||||
// (MyService и т.п.) живут снаружи и могут быть обёрнуты в shared_ptr.
|
||||
class RpcRegistry {
|
||||
public:
|
||||
// PoC: ObjectId храним как int-совместимый тип, чтобы его можно было
|
||||
// передавать через BaseIpcMessage, который поддерживает только int/string.
|
||||
using ObjectId = int;
|
||||
|
||||
RpcRegistry() = default;
|
||||
|
||||
// Зарегистрировать объект-обёртку (обычно Skeleton) и вернуть его
|
||||
// идентификатор. По сути, это registerSkeleton<T>(...).
|
||||
template<typename T, typename... Args>
|
||||
ObjectId registerObject(Args&&... args) {
|
||||
static_assert(std::is_base_of_v<IRpcObject, T>,
|
||||
"T must inherit IRpcObject");
|
||||
|
||||
const ObjectId id = nextId_++;
|
||||
objects_.emplace(
|
||||
id, std::make_unique<T>(std::forward<Args>(args)...));
|
||||
return id;
|
||||
}
|
||||
|
||||
// Уничтожить объект по идентификатору.
|
||||
void destroyObject(ObjectId id) {
|
||||
objects_.erase(id);
|
||||
}
|
||||
|
||||
// Найти объект по идентификатору.
|
||||
IRpcObject* get(ObjectId id) const {
|
||||
auto it = objects_.find(id);
|
||||
return it == objects_.end() ? nullptr : it->second.get();
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<ObjectId, std::unique_ptr<IRpcObject>> objects_;
|
||||
ObjectId nextId_ = 0; // первый объект будет иметь id = 0 (PoC)
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user