lichao
2021-03-31 6eefba812ede29549af3633c490f2e85a4805524
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
/*
 * =====================================================================================
 *
 *       Filename:  pubsub.h
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2021年03月24日 18时44分36秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Li Chao (), 
 *   Organization:  
 *
 * =====================================================================================
 */
#ifndef PUBSUB_4KGRA997
#define PUBSUB_4KGRA997
 
#include "shm_queue.h"
#include <atomic>
#include <mutex>
#include <set>
#include <thread>
#include <unordered_map>
#include <vector>
 
namespace bhome_shm
{
 
// publish/subcribe manager.
class BusManager
{
    SharedMemory &shm_;
    ShmMsgQueue busq_;
    std::atomic<bool> run_;
    std::vector<std::thread> workers_;
    std::mutex mutex_;
    typedef std::set<MQId> Clients;
    std::unordered_map<std::string, Clients> records_;
 
    bool StopNoLock();
    void OnMsg(MsgI &msg);
 
public:
    BusManager(SharedMemory &shm);
    ~BusManager();
    bool Start(const int nworker = 2);
    bool Stop();
};
 
} // namespace bhome_shm
 
#endif // end of include guard: PUBSUB_4KGRA997