lichao
2021-04-15 af86015d724e5edf001aa024fe7d8581c45cffd9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * =====================================================================================
 *
 *       Filename:  sendq.h
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2021年04月14日 09时22分59秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Li Chao (), lichao@aiotlink.com
 *   Organization:  
 *
 * =====================================================================================
 */
#ifndef SENDQ_IWKMSK7M
#define SENDQ_IWKMSK7M
 
#include "defs.h"
#include "msg.h"
#include "timed_queue.h"
#include <deque>
#include <functional>
#include <list>
#include <mutex>
#include <string>
#include <unordered_map>
 
namespace bhome_shm
{
class ShmMsgQueue;
} // namespace bhome_shm
 
class SendQ
{
public:
    typedef std::string Remote;
    typedef bhome_msg::MsgI MsgI;
    typedef std::function<void(const MsgI &msg)> OnMsgEvent;
    struct MsgInfo {
        MsgI msg_;
        OnMsgEvent on_expire_;
        // OnMsgEvent on_send_;
    };
    typedef TimedData<MsgInfo> TimedMsg;
    typedef TimedMsg::TimePoint TimePoint;
    typedef TimedMsg::Duration Duration;
 
    void Append(const MQId &id, const MsgI &msg, OnMsgEvent onExpire = OnMsgEvent())
    {
        Append(std::string((const char *) &id, sizeof(id)), msg, onExpire);
    }
    void Append(const Remote &addr, const MsgI &msg, OnMsgEvent onExpire = OnMsgEvent())
    {
        using namespace std::chrono_literals;
        Append(addr, msg, Now() + 60s, onExpire);
    }
    bool TrySend(bhome_shm::ShmMsgQueue &mq);
    // bool empty() const { return store_.empty(); }
 
private:
    static TimePoint Now() { return TimedMsg::Clock::now(); }
    void Append(const Remote &addr, const MsgI &msg, const TimePoint &expire, OnMsgEvent onExpire)
    {
        //TODO simple queue, organize later ?
 
        msg.AddRef();
        TimedMsg tmp(expire, MsgInfo{msg, 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(bhome_shm::ShmMsgQueue &mq, const Remote &remote, Array &arr);
    int DoSend1Remote(bhome_shm::ShmMsgQueue &mq, const Remote &remote, ArrayList &arr);
 
    class MsgIter
    {
        Array::iterator iter_;
 
    public:
        MsgIter(Array::iterator iter) :
            iter_(iter) {}
        MsgIter &operator++() { return ++iter_, *this; }
        bool operator==(const MsgIter &a) { return iter_ == a.iter_; }
        MsgI &operator*() { return iter_->data().msg_; }
    };
 
    std::mutex mutex_in_;
    std::mutex mutex_out_;
    Store in_;
    Store out_;
};
 
#endif // end of include guard: SENDQ_IWKMSK7M