lichao
2021-04-23 02ba913dc7bb5d711471b27f2ea23a897d0f2e28
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
/*
 * =====================================================================================
 *
 *       Filename:  failed_msg.h
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2021年04月12日 11时21分30秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Li Chao (), lichao@aiotlink.com
 *   Organization:  
 *
 * =====================================================================================
 */
#ifndef FAILED_MSG_9YOI86AS
#define FAILED_MSG_9YOI86AS
 
#include "msg.h"
#include "socket.h"
#include "timed_queue.h"
#include <string>
 
//TODO change storage to map<Remote,list<TimedData<Msg>>>, to avoid retry failed remotes messages.
class FailedMsgQ
{
    typedef std::function<bool(void *)> Func;
    typedef TimedQueue<Func> TimedFuncQ;
 
public:
    typedef bhome_msg::MsgI Msg;
 
    void Push(const std::string &remote, Msg const &msg, TimedFuncQ::TimePoint const &exr) { queue_.Push(PrepareSender(remote, msg), exr); }
    void Push(const std::string &remote, Msg const &msg, TimedFuncQ::Duration const &exr) { queue_.Push(PrepareSender(remote, msg), exr); }
    void TrySend(ShmSocket &socket)
    {
        queue_.CheckAll([&](Func &f) { return f(&socket); });
    }
 
private:
    Func PrepareSender(const std::string &remote, Msg msg);
 
    TimedFuncQ queue_;
};
 
#endif // end of include guard: FAILED_MSG_9YOI86AS