forked from flexibity-team/boost-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinject.hpp
More file actions
90 lines (72 loc) · 2.28 KB
/
inject.hpp
File metadata and controls
90 lines (72 loc) · 2.28 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
79
80
81
82
83
84
85
86
87
88
89
90
/*
* inject.hpp
*
* Created on: Sep 14, 2015
* Author: romeo
*/
#ifndef INCLUDE_FLEXIBITY_INJECT_HPP_
#define INCLUDE_FLEXIBITY_INJECT_HPP_
#include "flexibity/log.h"
#include "jsoncpp/json/json.h"
#include "flexibity/channelMgr.hpp"
#include <flexibity/base64.hpp>
namespace Flexibity {
using namespace std;
class inject:
public logInstanceName
{
public:
typedef shared_ptr<inject> sPtr;
static constexpr const char* portOption = "port";
static constexpr const char* channelOption = "channel";
static constexpr const char* frontOption = "front";
static constexpr const char* dataOption = "data";
static constexpr const char* textOption = "text";
static constexpr const char* idOption = "id";
public:
INLINE_CLASS_EXCEPTION();
inject(const Json::Value& cfg, channelMgr::sPtr cm){
ILOG_INITSev(INFO);
setInstanceName(cfg.get(idOption, "").asString());
if(cfg.isMember(channelOption)){
if(cfg.get(frontOption, false).asBool()){
_injectReceiver = cm->getItem(cfg.get(channelOption, "").asString()); //injecting to front
}else{
_injectReceiver = cm->getItem(cfg.get(channelOption, "").asString())->getOut(); //injecting to port (rear end)
}
}else
_injectReceiver = cm->_pm->getItem(cfg.get(portOption, "").asString());
if(!_injectReceiver)
throw exception("Inject receiver not set for inject \"" + instanceName() + "\"");
if(cfg.isMember(dataOption)){
string enc = cfg[dataOption].asString();
IDEBUG(log::dump(enc));
_data = base64::decode(enc);
}else{
_data = cfg[textOption].asString();
}
IINFO("Constructed inject \"" << instanceName() << "\" to \"" << _injectReceiver->instanceName() << "\" with data" << log::dump(_data) );
}
void operator ()(){
IINFO("Inject to " << _injectReceiver->instanceName() << log::dump(_data));
_injectReceiver->feed(_data);
}
protected:
ioSerialMsgReceiver::sPtr _injectReceiver;
string _data;
};
class staticInjectMgr:
public genericMgr<inject::sPtr>{
public:
typedef shared_ptr<staticInjectMgr> sPtr;
staticInjectMgr(const Json::Value& cfg, channelMgr::sPtr cm){
ILOG_INIT( );
//TODO: rethrow nested???
populateItems(cfg, [&](const Json::Value& iCfg){
auto item = new Flexibity::inject(iCfg, cm);
return inject::sPtr(item);
});
}
};
}
#endif /* INCLUDE_FLEXIBITY_INJECT_HPP_ */