forked from flexibity-team/boost-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchannelMgr.hpp
More file actions
78 lines (62 loc) · 1.85 KB
/
channelMgr.hpp
File metadata and controls
78 lines (62 loc) · 1.85 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* serialPortMgr.hpp
*
* Created on: Aug 13, 2015
* Author: Roman Savrulin <romeo.deepmind@gmail.com>
*/
#ifndef INCLUDE_FLEXIBITY_CHANNELMGR_HPP_
#define INCLUDE_FLEXIBITY_CHANNELMGR_HPP_
#include <jsoncpp/json/json.h>
#include <flexibity/channel.hpp>
#include <flexibity/genericMgr.hpp>
#include <flexibity/jsonrpc/jsonRpcTransport.hpp>
namespace Flexibity{
class channelMgr:
public genericMgr<serialChannel::sPtr>{
public:
typedef shared_ptr<channelMgr> sPtr;
static constexpr const char* threadPoolOption = "threadPool";
channelMgr(const Json::Value& cfg, serialPortMgr::sPtr pm):
_channel_io_service(),
_work(_channel_io_service),
_pm(pm),
_threadPool() {
ILOG_INITSev(INFO);
populateItems(cfg, [&](const Json::Value& iCfg){
return std::make_shared<Flexibity::serialChannel>(_channel_io_service, pm, iCfg);
});
size_t _size = 1;
size_t tp = cfg[threadPoolOption].asUInt();
if(tp != 0)
_size = tp;
for ( size_t i = 0; i < _size; ++i){
_threadPool.create_thread(boost::bind(&boost::asio::io_service::run, &_channel_io_service));
}
IINFO(_threadPool.size() << " channel worker thread(s) created");
}
void loop() {
IDEBUG("enter");
while (!_channel_io_service.stopped()) {
size_t ev = _channel_io_service.run(); // processes the tasks
IDEBUG("processed " << ev << " event(s)");
}
IDEBUG("exit");
}
void clearChannels(jsonRpcTransport::sPtr _rpc){
for(auto& channel:*this){
channel.second->removeMonitors(_rpc);
}
}
virtual ~channelMgr(){
IINFO("waiting for " << _threadPool.size() << " thread(s) to exit");
_channel_io_service.stop();
_threadPool.join_all();
IINFO("Done");
}
boost::asio::io_service _channel_io_service;
boost::asio::io_service::work _work;
Flexibity::serialPortMgr::sPtr _pm;
boost::thread_group _threadPool;
};
}
#endif /* INCLUDE_FLEXIBITY_SERIALPORTMGR_HPP_ */