lichao
2021-04-02 c28cdf2fbf1565709b359c9cca6c5e29d9592dce
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_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>
 
// publish/subcribe manager.
class PubSubCenter
{
    class SocketBus : public ShmSocket
    {
    public:
        SocketBus(ShmSocket::Shm &shm) :
            ShmSocket(shm, &kBHTopicBus, 1000) {}
        using ShmSocket::shm;
    };
    SocketBus socket_;
    ShmSocket::Shm &shm() { return socket_.shm(); }
 
    std::mutex mutex_;
    typedef std::set<MQId> Clients;
    std::unordered_map<Topic, Clients> records_;
    bool Find1(const Topic &topic);
 
public:
    PubSubCenter(ShmSocket::Shm &shm) :
        socket_(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