-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathflowmgr.cpp
More file actions
336 lines (293 loc) · 10.6 KB
/
flowmgr.cpp
File metadata and controls
336 lines (293 loc) · 10.6 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include <stdexcept>
#include <iostream>
#include <iomanip>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include "flowmgr.h"
#include "auxil.h"
dsp::FlowMgr::Registrar::~Registrar(){}
dsp::FlowMgr::FlowMgr(){
_regis.reserve(5);
_regis.push_back(new dsp::FlowMgr::DPE);
}
dsp::FlowMgr::~FlowMgr(){
for (auto const& id: _flowlist)
if (id.first) delete id.first;
for (auto const& ptr:_regis) delete ptr;
}
size_t
dsp::FlowMgr::EmergencyStop()const{
std::cout << "[FlorMgr] Emergency Stop. All flow stopping." << std::endl;
size_t actual = 0;
for (auto& pair: _flowlist){
if (!pair.first) continue;
if (pair.first->Stop() == 0) actual ++;
} return actual;
}
dsp::FlowMgr::FlowMgr(const FlowMgr& rhs):_alias(rhs._alias){
_flowlist.reserve(rhs._flowlist.size());
for (auto const& pair: rhs._flowlist)
createFlow(pair.second);
}//hopefully will never need this.
int
dsp::FlowMgr::createFlow(const std::string& type, const std::string& alias){
FlowIdent fid (nullptr,"");
for(auto const& ptr:_regis){
if (!ptr->match(type)) continue;
fid.first = ptr->createFlow();
fid.second = ptr->get_name();
}
if (!fid.first){
std::cerr << "[FlowMgr] Flow type \"" << type << "\" does not exist." << std::endl;
return -1;
}
_flowlist.push_back(fid);
const size_t flowN = _flowlist.size() - 1;
if (!alias.empty()) addAlias(alias,flowN);
std::clog << "[FlowMgr] Flow #"<< flowN << " (" << fid.second << ") created." << std::endl;
return 0;
}
int
dsp::FlowMgr::destroyFlow(const size_t& idx){
if (idx >= _flowlist.size())
throw std::out_of_range("[FlowMgr] destroyFlow: idx exceeded max range.");
if (!_flowlist[idx].first){
std::cerr<< "[FlowMgr] Flow #" << idx << " previously destroyed." << std::endl;
return -1;
}
//_flowlist[idx].first -> Stop();
delete _flowlist[idx].first;
_flowlist[idx].first = nullptr;
std::clog << "[FlowMgr] Flow #" << idx << " (" << _flowlist[idx].second <<") succesfully deleted." << std::endl;
return 0;
}
int
dsp::FlowMgr::startFlow(const size_t& idx)const{
try{
int status = -1;
if ((status = getFlowPtr(idx)->Start())!=0) return status;
std::clog << "[FlowMgr] Flow #" << idx << " started." << std::endl;
} catch (const std::out_of_range& e) {
std::cerr << e.what() << std::endl;
} return 0;
}
int
dsp::FlowMgr::stopFlow(const size_t& idx)const{
try{
int status = -1;
if ((status = getFlowPtr(idx)->Stop()) != 0) return status;
std::clog << "[FlowMgr] Flow #" << idx << " being stopped." << std::endl;
} catch (const std::out_of_range& e) {
std::cerr << e.what() << std::endl;
} return 0;
}
int
dsp::FlowMgr::loadFlow(const std::string& key,const char* fname)const{
size_t idx = getFlowIdx (key);
return (idx == NPOS)?-1:loadFlow(idx,fname);
}
int
dsp::FlowMgr::loadFlow(const size_t& idx,const char* fname)const{
int status = -1;
try {
status = getFlowPtr(idx)->LoadFlow(fname);
std::clog << "[FlowMgr] Flow #" << idx << " being loaded." << std::endl;
} catch (const std::out_of_range& e) {
std::cerr << e.what() << std::endl;
} return status;
}
size_t
dsp::FlowMgr::getFlowIdx(const std::string& key)const{
size_t idx = NPOS;
bool allnum = true;
for (auto const& c: key){
if (!isalnum(c)){
std::cerr << "[FlowMgr] Alias \"" << key << "\" contains invalid character."<<std::endl;
return NPOS;
} allnum = (allnum && isdigit(c));
}
if (!key.empty() && allnum){
size_t idx = size_t(atoi(key.c_str()));
if (idx >= _flowlist.size()) return NPOS;
return idx;
}
try {
idx = _alias.at (key);
} catch (const std::out_of_range& e){
std::cerr <<"[FlowMgr] Alias \"" << key <<"\" does not exist." <<std::endl;
return NPOS;
} return idx;
}
dsp::Flow*
dsp::FlowMgr::getFlowPtr(const size_t& idx)const{
if (idx >= _flowlist.size())
throw std::out_of_range("[FlowMgr] getFlow: idx exceeded max range.");
dsp::Flow* ptr = _flowlist[idx].first;
if (!ptr) throw std::logic_error("[FlowMgr] Flow constructor not properly invoked.");
return _flowlist[idx].first;
}
void
dsp::FlowMgr::addAlias (const std::string& key,const size_t& value){
if (value >= _flowlist.size()){
std::cerr << "[FlowMgr] addAlias: idx exceeded max range." << std::endl;
return;
} else if (key.size() > 80 || key.empty()){
std::cerr << "[FlowMgr] Maximum length for alias is 80 characters." << std::endl;
return;
}
for (auto const& c:key)
if (!isalnum(c)) {
std::cerr << "[FlowMgr] Alias \"" << key <<"\" invalid; must be alphanumerals." << std::endl;
return;
}
if (!isalpha(key[0]))
std::cerr << "[FlowMgr] Alias \"" << key <<"\" invalid; first char must be alphabet." << std::endl;
else if (_alias.insert(std::pair<std::string,size_t>(key,value)).second)
std::clog << "[FlowMgr] Alias \"" << key <<"\" created for Flow #" << value << std::endl;
else
std::cerr << "[FlowMgr] Alias \"" << key <<"\" already existed for Flow #" << _alias[key] << '.' << std::endl;
}
void
dsp::FlowMgr::listAlias()const{
if (_alias.empty()){
std::cout << "No active alias." << std::endl;
return;
}
for (auto const & entry: _alias){
const size_t i = entry.second;
if (!_flowlist[i].first) continue;
std::cout << std::setw (15) <<std::left << ("\"" + entry.first + "\" -> ");
std::cout << std::setw(3) << i << " : ("
<< std::setw(15) << std::left << _flowlist[i].second
<< ") " << _flowlist[i].first << std::endl;
}
}
void
dsp::FlowMgr::listFlow()const{
if (_flowlist.empty()){
std::cout << "No active flow." << std::endl;
return;
}
for(size_t i = 0 ; i < _flowlist.size() ;i++){
if (!_flowlist[i].first) continue;
std::cout << std::setw(3) << i << " : ("
<< std::setw(15) << std::left << _flowlist[i].second
<< ") " << _flowlist[i].first << std::endl;
}
}
int
dsp::FlowMgr::setParam (const std::string& key, const std::string& modName, const std::string& paramName, const std::string& expr)const{
size_t idx = getFlowIdx (key);
return (idx == NPOS)?-1:setParam(idx,modName,paramName,expr);
}
int
dsp::FlowMgr::setParam (const size_t& idx, const std::string& modName, const std::string& paramName, const std::string& expr)const{
std::clog << "[FlowMgr] Param type = " ;
if (/*expr.size() == 3 && expr[0] == '\'' && expr[1] != '\'' && expr[2] == '\''*/ expr[0] == '\\' && expr[1] == 'x'){
std::clog << "char" << std::endl;
char val = std::stoi(expr.substr(2),nullptr,16);
return getFlowPtr(idx) -> SetModParam (modName,paramName,val);
} else if (expr[0] == '"' && expr[expr.size() - 1] == '"'){
const std::string val = expr.substr(1,expr.size() -2);
if (val.find('"') == std::string::npos) {
std::clog << "char*" << std::endl;
return getFlowPtr(idx) -> SetModParam(modName,paramName,val.c_str());
} std::clog << "UNDEFINED" << std::endl; return -1;
} else if (expr == "true") {
std::clog << "bool" << std::endl;
return getFlowPtr(idx) -> SetModParam(modName,paramName,true);
} else if (expr == "false") {
std::clog << "bool" << std::endl;
return getFlowPtr(idx) -> SetModParam(modName,paramName,false);
} else if (expr.find_first_not_of("0123456789.+-ef") != std::string::npos){
std::clog << "bool" << std::endl;
return -1;
}
const size_t sign = expr.find_first_of ("+-");
if (sign != 0 && sign != std::string::npos) {
std::clog << "UNDEFINED" << std::endl;
return -1;
}
std::istringstream iss (expr);
size_t decimal = expr.find_first_of(".ef");
if (decimal != std::string::npos){
if (decimal == (expr.size() - 1) || expr.find('.',decimal + 1) == std::string::npos){ // float
float val;
iss >> val;
std::clog << "float" << std::endl;
return getFlowPtr(idx) -> SetModParam(modName,paramName,val);
} std::clog << "UNDEFINED" << std::endl; return -1;
}
int val;
iss >> val;
std::clog << "int" << std::endl;
return getFlowPtr(idx) -> SetModParam(modName,paramName,val);
}
// "list" and "monitor"
int
dsp::FlowMgr::listOutput (const std::string& key, const std::string& modName,const std::string& portName)const{
size_t idx = getFlowIdx (key);
return (idx == NPOS)?-1:listOutput(idx,modName,portName);
}
int
dsp::FlowMgr::listOutput(const size_t& idx, const std::string& modName,const std::string& portName)const{
dsp::Port* ptr = nullptr;
try {
if (getFlowPtr(idx) -> GetOutput(modName,portName,&ptr) != 0 || !ptr)
throw std::out_of_range("[FlowMgr] Cannot retrieve designated port.");
} catch (const std::out_of_range& e) {
std::cerr << e.what() << std::endl;
return -1;
}
size_t size = ptr->VectorLength;
if (!size){
std::cout << "Empty." << std::endl;
return 0;
}
switch(ptr->Datatype){
/*case FLOAT_t:
size *= sizeof(float);
std::cout << "Float: [";
break;
case DOUBLE_t:
size *= sizeof(double);
std::cout <<" Double [";
break;
case INT_t:
size *= sizeof(int);
std::cout << "Int [";
break;*/
case CHAR_t:
size *= sizeof(char);
std::cout <<" Char [";
break;
case BOOL_t:
size *= sizeof(bool);
std::cout << "Bool [";
break;
default:
std::cerr << "[FlowMgr] Data type unhandled." <<std::endl;
return -1;
}
void* data = malloc(size);
memcpy(data,ptr->Data,size);
if (ptr->Datatype == BOOL_t){
bool* bdata = (bool*) data;
for(size_t i = 0 ; i < ptr->VectorLength ; i++){
if (bdata[i]) std::cout << i << ',';
} std::cout << "\b]" << std::endl;
} else if (ptr->Datatype == CHAR_t){
char* cdata = (char*) data;
for(size_t i = 0 ; i < ptr->VectorLength ; i++)
std::cout << (int)cdata[i] << ',';
std::cout << "\b]" <<std::endl;
} free (data);
return 0;
}
void
dsp::FlowMgr::flowType()const{
for(auto const& ptr:_regis)
std::cout << std::setw(14) << std::left << ptr->get_name() << ' '<< ptr->get_desc() << std::endl;
}