Message-Queues beta 1.1
A Message-Queues based Cpp
 
载入中...
搜索中...
未找到
host.hpp
浏览该文件的文档.
1
12#pragma once
13#include "exchange.hpp"
14#include "queue.hpp"
15#include "binding.hpp"
16#include "message.hpp"
17
18namespace XuMQ
19{
23 {
24 public:
25 using ptr = std::shared_ptr<VirtualHost>;
30 VirtualHost(const std::string hname, const std::string &basedir, const std::string &dbfile)
31 : _emp(std::make_shared<ExchangeManager>(dbfile)),
32 _mqmp(std::make_shared<MsgQueueManager>(dbfile)),
33 _bmp(std::make_shared<BindingManager>(dbfile)),
34 _mmp(std::make_shared<MessageManager>(basedir))
35
36 {
37 // 获取所有队列信息 通过队列信息恢复历史消息
38 QueueMap qm = _mqmp->allQueue();
39 for (auto &q : qm)
40 _mmp->initQueueMessage(q.first);
41 }
42
50 bool declareExchange(const std::string &name,
51 ExchangeType type,
52 bool durable,
53 bool auto_delete,
54 const google::protobuf::Map<std::string, std::string> &args)
55 {
56 return _emp->declareExchange(name, type, durable, auto_delete, args);
57 }
60 void deleteExchange(const std::string &name)
61 {
62 // 先删除绑定信息
63 _bmp->removeExchangeBindings(name);
64 return _emp->deleteExchange(name);
65 }
66
74 bool declareQueue(const std::string &qname,
75 bool qdurable,
76 bool qexclusive,
77 bool qauto_delete,
78 const google::protobuf::Map<std::string, std::string> &qargs)
79 {
80 // 初始化队列消息句柄
81 // 消息对立创建
82 _mmp->initQueueMessage(qname);
83 return _mqmp->declareQueue(qname, qdurable, qexclusive, qauto_delete, qargs);
84 }
87 void deleteQueue(const std::string &name)
88 {
89 // 删除队列消息
90 _mmp->destroyQueueMessage(name);
91 // 删除队列绑定信息
92 _bmp->removeMsgQueueBindings(name);
93 return _mqmp->deleteQueue(name);
94 }
95
102 bool bind(const std::string &ename, const std::string &qname, const std::string &key)
103 {
104 Exchange::ptr ep = _emp->selectExchange(ename);
105 if (ep.get() == nullptr)
106 {
107 error(logger, "队列绑定失败, 交换机 %s 不存在", ename.c_str());
108 return false;
109 }
110 MsgQueue::ptr mqp = _mqmp->selectQueue(qname);
111 if (mqp.get() == nullptr)
112 {
113 error(logger, "队列绑定失败, 队列 %s 不存在", qname.c_str());
114 return false;
115 }
116 return _bmp->bind(ename, qname, key, ep->durable & mqp->durable);
117 }
121 void unbind(const std::string &ename, const std::string &qname)
122 {
123 return _bmp->unbind(ename, qname);
124 }
128 MsgQueueBindingMap exchangeBindings(const std::string &ename)
129 {
130 return _bmp->getExchangeBindings(ename);
131 }
136 {
137 return _mqmp->allQueue();
138 }
139
145 bool basicPublish(const std::string &qname, BasicProperties *bp, const std::string &body)
146 {
147 MsgQueue::ptr mqp = _mqmp->selectQueue(qname);
148 if (mqp.get() == nullptr)
149 {
150 error(logger, "队列绑定失败, 队列 %s 不存在", qname.c_str());
151 return false;
152 }
153 return _mmp->insert(qname, bp, body, mqp->durable);
154 }
158 MessagePtr basicConsume(const std::string &qname)
159 {
160 return _mmp->front(qname);
161 }
165 void basicAck(const std::string &qname, const std::string &msg_id)
166 {
167 _mmp->ack(qname, msg_id);
168 }
169
173 Exchange::ptr selectExchange(const std::string &ename)
174 {
175 return _emp->selectExchange(ename);
176 }
177
179 void clear()
180 {
181 _emp->clear();
182 _mqmp->clear();
183 _bmp->clear();
184 _mmp->clear();
185 }
186
190 bool existsExchange(const std::string &name)
191 {
192 return _emp->exists(name);
193 }
194
198 bool existsQueue(const std::string &name)
199 {
200 return _mqmp->exists(name);
201 }
202
207 bool existsBinding(const std::string &ename, const std::string &qname)
208 {
209 return _bmp->exists(ename, qname);
210 }
211
212 private:
213 std::string _host_name;
218 };
219
222 {
223 public:
224 using ptr = std::shared_ptr<VirtualHostManager>;
226
232 bool declareVirtualHost(const std::string &hname,
233 const std::string &basedir,
234 const std::string &dbfile)
235 {
236 std::unique_lock<std::mutex> lock(_mutex);
237 auto it = _vhosts.find(hname);
238 if(it!=_vhosts.end())
239 return true;
240 auto host = std::make_shared<VirtualHost>(hname, basedir, dbfile);
241 _vhosts.insert(std::make_pair(hname, host));
242 return true;
243 }
244
247 void deleteVirtualHost(const std::string &hname)
248 {
249 std::unique_lock<std::mutex> lock(_mutex);
250 auto it = _vhosts.find(hname);
251 if(it == _vhosts.end())
252 return;
253 _vhosts.erase(hname);
254 }
255
259 VirtualHost::ptr selectVirtualHost(const std::string &hname)
260 {
261 std::unique_lock<std::mutex> lock(_mutex);
262 auto it = _vhosts.find(hname);
263 if(it == _vhosts.end())
264 return VirtualHost::ptr();
265 return it->second;
266 }
267
271 bool exists(const std::string &hname)
272 {
273 std::unique_lock<std::mutex> lock(_mutex);
274 auto it = _vhosts.find(hname);
275 if(it == _vhosts.end())
276 return false;
277 return true;
278 }
279
281 void clear()
282 {
283 std::unique_lock<std::mutex> lock(_mutex);
284 _vhosts.clear();
285 }
286
289 size_t size()
290 {
291 std::unique_lock<std::mutex> lock(_mutex);
292 return _vhosts.size();
293 }
294 private:
295 std::mutex _mutex;
296 std::unordered_map<std::string, VirtualHost::ptr> _vhosts;
297 };
298}
该文件包含消息队列绑定信息的管理类和结构体定义。
Definition msg.pb.h:122
绑定信息内存管理类
Definition binding.hpp:198
std::shared_ptr< BindingManager > ptr
绑定信息内存管理类指针
Definition binding.hpp:200
交换机数据内存管理类
Definition exchange.hpp:193
std::shared_ptr< ExchangeManager > ptr
交换机数据内存管理指针
Definition exchange.hpp:195
消息管理类
Definition message.hpp:422
std::shared_ptr< MessageManager > ptr
消息管理类指针
Definition message.hpp:424
消息队列数据内存管理类
Definition queue.hpp:194
std::shared_ptr< MsgQueueManager > ptr
消息队列数据内存管理指针
Definition queue.hpp:196
虚拟机模块
Definition host.hpp:23
BindingManager::ptr _bmp
绑定信息管理指针
Definition host.hpp:216
void basicAck(const std::string &qname, const std::string &msg_id)
应答消息
Definition host.hpp:165
bool existsExchange(const std::string &name)
判断交换机是否存在
Definition host.hpp:190
void unbind(const std::string &ename, const std::string &qname)
解除绑定信息
Definition host.hpp:121
void deleteQueue(const std::string &name)
删除消息队列
Definition host.hpp:87
MessageManager::ptr _mmp
消息管理指针
Definition host.hpp:217
std::string _host_name
Definition host.hpp:213
MsgQueueManager::ptr _mqmp
消息队列管理指针
Definition host.hpp:215
std::shared_ptr< VirtualHost > ptr
Definition host.hpp:25
bool bind(const std::string &ename, const std::string &qname, const std::string &key)
添加绑定信息
Definition host.hpp:102
void clear()
清理
Definition host.hpp:179
VirtualHost(const std::string hname, const std::string &basedir, const std::string &dbfile)
虚拟机构造函数 恢复历史消息
Definition host.hpp:30
bool existsBinding(const std::string &ename, const std::string &qname)
判断绑定信息是否存在
Definition host.hpp:207
bool declareExchange(const std::string &name, ExchangeType type, bool durable, bool auto_delete, const google::protobuf::Map< std::string, std::string > &args)
声明交换机
Definition host.hpp:50
bool declareQueue(const std::string &qname, bool qdurable, bool qexclusive, bool qauto_delete, const google::protobuf::Map< std::string, std::string > &qargs)
声明消息队列
Definition host.hpp:74
ExchangeManager::ptr _emp
交换机管理指针
Definition host.hpp:214
MessagePtr basicConsume(const std::string &qname)
获取队头消息
Definition host.hpp:158
MsgQueueBindingMap exchangeBindings(const std::string &ename)
获取交换机绑定信息
Definition host.hpp:128
Exchange::ptr selectExchange(const std::string &ename)
获取指定交换机句柄
Definition host.hpp:173
bool existsQueue(const std::string &name)
判断队列是否存在
Definition host.hpp:198
QueueMap allQueues()
获取所有队列
Definition host.hpp:135
void deleteExchange(const std::string &name)
删除交换机
Definition host.hpp:60
bool basicPublish(const std::string &qname, BasicProperties *bp, const std::string &body)
向指定队列插入新消息
Definition host.hpp:145
虚拟机管理类
Definition host.hpp:222
VirtualHostManager()
Definition host.hpp:225
std::mutex _mutex
互斥锁
Definition host.hpp:295
std::unordered_map< std::string, VirtualHost::ptr > _vhosts
虚拟机名称到虚拟机管理句柄的映射表
Definition host.hpp:296
void clear()
清除所有虚拟机数据
Definition host.hpp:281
size_t size()
获取交换机个数
Definition host.hpp:289
std::shared_ptr< VirtualHostManager > ptr
虚拟机管理类指针
Definition host.hpp:224
bool exists(const std::string &hname)
判断虚拟机是否存在
Definition host.hpp:271
void deleteVirtualHost(const std::string &hname)
删除一个虚拟机
Definition host.hpp:247
bool declareVirtualHost(const std::string &hname, const std::string &basedir, const std::string &dbfile)
声明一个虚拟机
Definition host.hpp:232
VirtualHost::ptr selectVirtualHost(const std::string &hname)
获取一个虚拟机
Definition host.hpp:259
交换机管理模块,定义了交换机的结构、持久化以及内存管理功能
消息队列文件存储的实现
XuMQ::MsgQueueManager::ptr mqp
Definition mqqueuetest.cpp:4
Definition channel.hpp:22
Xulog::Logger::ptr logger
日志器的智能指针类型
Definition logger.hpp:24
std::shared_ptr< google::protobuf::Message > MessagePtr
消息句柄
Definition channel.hpp:23
ExchangeType
Definition msg.pb.h:66
std::unordered_map< std::string, Binding::ptr > MsgQueueBindingMap
消息队列绑定映射表-—消息队列->绑定信息的映射表
Definition binding.hpp:43
std::unordered_map< std::string, MsgQueue::ptr > QueueMap
消息队列映射表 消息队列名称->消息队列指针
Definition queue.hpp:82
消息队列模块的定义和实现
std::shared_ptr< Exchange > ptr
使用智能指针管理交换机对象
Definition exchange.hpp:24
std::shared_ptr< MsgQueue > ptr
消息队列指针
Definition queue.hpp:27