// 消息总线
// 负责消息的发布和订阅
#pragma once
 
#include <softbus_bus/BusMessage.h>

#include <cstdint>
#include <functional>
#include <mutex>
#include <string>
#include <vector>

namespace softbus::bus {

using BusHandler = std::function<void(const BusMessage&)>;

class Bus {
public:
    using SubscriptionId = std::uint64_t;

    static Bus& instance();

    SubscriptionId subscribe(const std::string& topicPattern, BusHandler handler);
    void unsubscribe(SubscriptionId id);
    void publish(const std::string& topic, const BusMessage& message);

    static bool topicMatches(const std::string& pattern, const std::string& topic);

private:
    struct Subscription {
        SubscriptionId id{0};
        std::string pattern;
        BusHandler handler;
    };

    std::mutex mutex_;
    std::vector<Subscription> subscriptions_;
    SubscriptionId nextId_{1};
};

} // namespace softbus::bus
