jay/component/router.h
2025-03-28 23:12:34 +02:00

54 lines
1.5 KiB
C++

// SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <string>
#include <map>
#include <vector>
#include <memory>
#include <random>
#include "shared/result.h"
#include "shared/loggable.h"
#include "shared/utils.h"
#include "actor.h"
namespace Module {
class Module;
};
class Connection;
class Router : private Shared::Loggable {
public:
Router(const Shared::Logger& logger);
void registerModule(const std::string& key, const std::shared_ptr<Module::Module>& module);
void registerActor(const std::string& key, const std::string& group);
void routeMessage(const std::string& sender, const std::string& body);
std::map<std::string, std::string> getActors() const;
std::string getDefaultGroup() const;
void setConnection(const std::shared_ptr<Connection>& connection);
void setResponses(Shared::Result result, const Shared::Strings& options);
std::string getGroup(const std::string& key) const;
private:
void onMessageResult(Shared::Result result, const std::string& sender);
private:
typedef std::map<std::string, std::weak_ptr<Module::Module>> Modules;
typedef std::map<std::string, std::shared_ptr<Actor>> Actors;
typedef std::vector<std::string> List;
typedef std::map<Shared::Result, List> Responses;
std::weak_ptr<Connection> connection;
Modules modules;
Actors actors;
std::string defaultGroup;
Responses responses;
std::mt19937 generator;
};