| | |
| | | #include "defs.h" |
| | | #include "msg.h" |
| | | #include "timed_queue.h" |
| | | #include <boost/variant2/variant.hpp> |
| | | #include <deque> |
| | | #include <functional> |
| | | #include <list> |
| | |
| | | typedef MQId Remote; |
| | | typedef bhome_msg::MsgI MsgI; |
| | | typedef std::string Content; |
| | | typedef boost::variant2::variant<MsgI, Content> Data; |
| | | typedef int64_t Data; |
| | | typedef std::function<void(const Data &)> OnMsgEvent; |
| | | struct MsgInfo { |
| | | MQInfo mq_; |
| | | Data data_; |
| | | OnMsgEvent on_expire_; |
| | | }; |
| | |
| | | typedef TimedMsg::TimePoint TimePoint; |
| | | typedef TimedMsg::Duration Duration; |
| | | |
| | | // template <class... Rest> |
| | | // void Append(const MQId &id, Rest &&...rest) |
| | | // { |
| | | // Append(std::string((const char *) &id, sizeof(id)), std::forward<decltype(rest)>(rest)...); |
| | | // } |
| | | |
| | | void Append(const Remote addr, const MsgI msg, OnMsgEvent onExpire = OnMsgEvent()) |
| | | bool Append(const MQInfo &mq, MsgI msg) |
| | | { |
| | | msg.AddRef(); |
| | | AppendData(addr, Data(msg), DefaultExpire(), onExpire); |
| | | auto onMsgExpire = [](const Data &d) { MsgI(d).Release(); }; |
| | | try { |
| | | AppendData(mq, msg.Offset(), DefaultExpire(), onMsgExpire); |
| | | return true; |
| | | } catch (...) { |
| | | msg.Release(); |
| | | return false; |
| | | } |
| | | } |
| | | void Append(const Remote addr, Content &&content, OnMsgEvent onExpire = OnMsgEvent()) |
| | | |
| | | bool Append(const MQInfo &mq, MsgI msg, OnMsgEvent onExpire) |
| | | { |
| | | AppendData(addr, Data(std::move(content)), DefaultExpire(), onExpire); |
| | | msg.AddRef(); |
| | | auto onMsgExpire = [onExpire](const Data &d) { |
| | | onExpire(d); |
| | | MsgI(d).Release(); |
| | | }; |
| | | try { |
| | | AppendData(mq, msg.Offset(), DefaultExpire(), onMsgExpire); |
| | | return true; |
| | | } catch (...) { |
| | | msg.Release(); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | bool Append(const MQInfo &mq, const Data command, OnMsgEvent onExpire = OnMsgEvent()) |
| | | { |
| | | try { |
| | | AppendData(mq, command, DefaultExpire(), onExpire); |
| | | return true; |
| | | } catch (...) { |
| | | return false; |
| | | } |
| | | } |
| | | bool TrySend(ShmMsgQueue &mq); |
| | | // bool empty() const { return store_.empty(); } |
| | | |
| | | private: |
| | | static TimePoint Now() { return TimedMsg::Clock::now(); } |
| | | static TimePoint DefaultExpire() { return Now() + std::chrono::seconds(60); } |
| | | void AppendData(const Remote addr, Data &&data, const TimePoint &expire, OnMsgEvent onExpire) |
| | | { |
| | | //TODO simple queue, organize later ? |
| | | static TimePoint DefaultExpire() { return Now() + std::chrono::seconds(NodeTimeoutSec()); } |
| | | void AppendData(const MQInfo &mq, const Data data, const TimePoint &expire, OnMsgEvent onExpire); |
| | | |
| | | TimedMsg tmp(expire, MsgInfo{std::move(data), std::move(onExpire)}); |
| | | std::unique_lock<std::mutex> lock(mutex_in_); |
| | | auto &al = in_[addr]; |
| | | if (!al.empty()) { |
| | | al.front().emplace_back(std::move(tmp)); |
| | | } else { |
| | | al.insert(al.begin(), Array())->emplace_back(std::move(tmp)); |
| | | } |
| | | } |
| | | typedef std::deque<TimedMsg> Array; |
| | | typedef std::list<Array> ArrayList; |
| | | typedef std::unordered_map<Remote, ArrayList> Store; |
| | |
| | | int DoSend1Remote(ShmMsgQueue &mq, const Remote remote, Array &arr); |
| | | int DoSend1Remote(ShmMsgQueue &mq, const Remote remote, ArrayList &arr); |
| | | |
| | | bool TooFast(); |
| | | |
| | | std::mutex mutex_in_; |
| | | std::mutex mutex_out_; |
| | | Store in_; |
| | | Store out_; |
| | | |
| | | int64_t count_ = 0; |
| | | int64_t last_time_ = 0; |
| | | }; |
| | | |
| | | #endif // end of include guard: SENDQ_IWKMSK7M |