lichao
2021-04-01 b55ffe89f4b237be5f79232cfddfe22bfdb87c64
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
/*
 * =====================================================================================
 *
 *       Filename:  pubsub_center.h
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2021年04月01日 09时29分39秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Li Chao (), 
 *   Organization:  
 *
 * =====================================================================================
 */
#ifndef PUBSUB_CENTER_MFSUZJU7
#define PUBSUB_CENTER_MFSUZJU7
 
#include "defs.h"
#include "socket.h"
#include <mutex>
#include <set>
#include <unordered_map>
using namespace bhome_shm;
 
// publish/subcribe manager.
class PubSubCenter
{
    class SocketBus : public ShmSocket
    {
    public:
        SocketBus(SharedMemory &shm) :
            ShmSocket(shm, &kBHBusQueueId, 1000) {}
        using ShmSocket::shm;
    };
    SocketBus socket_;
    std::mutex mutex_;
    typedef std::set<MQId> Clients;
    std::unordered_map<std::string, Clients> records_;
    ShmSocket::Shm &shm() { return socket_.shm(); }
 
public:
    PubSubCenter(SharedMemory &shm);
    PubSubCenter() :
        PubSubCenter(BHomeShm()) {}
    ~PubSubCenter() { Stop(); }
    bool Start(const int nworker = 2);
    bool Stop() { return socket_.Stop(); }
};
 
#endif // end of include guard: PUBSUB_CENTER_MFSUZJU7