Skip to content

Commit cae1671

Browse files
committed
opt(terminal,main): 为终端模块添加更多的AddFuncNode()函数,并用此简化已有代码
1 parent 53e724d commit cae1671

File tree

9 files changed

+455
-501
lines changed

9 files changed

+455
-501
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ When various program exceptions occur in the program, such as: segment fault, as
8484
| util | Accessibility module |
8585
| event | Realized IO, Timer, Signal three kinds of event-driven, which is the heart of the whole framework |
8686
| eventx | Including ThreadPool thread pool, WorkThread worker thread, TimerPool timer pool and other modules |
87-
| log | Realize efficient and reliable terminal, syslog, and log output in the form of files |
87+
| log | Realized efficient and reliable terminal, syslog, and log output in the form of files |
88+
| trace | Realized outputting traces to files in real time |
8889
| network | Realized serial port, terminal, UDP, TCP communication module |
8990
| terminal | A shell-like command terminal that enables command interaction with programs during runtime |
9091
| **main** | Realized a complete program startup process and framework, so that developers only need to care about business code |
@@ -94,6 +95,8 @@ When various program exceptions occur in the program, such as: segment fault, as
9495
| http | Implemented HTTP Server and Client modules on the basis of network |
9596
| alarm | Realized 4 commonly used alarm clocks: CRON alarm clock, single alarm clock, weekly cycle alarm clock, weekday alarm clock |
9697
| flow | Contains multi-level state machine and behavior tree to solve the problem of action flow in asynchronous mode |
98+
| crypto | Implemented the commonly used AES and MD5 encryption and decryption calculations |
99+
| dbus | Implemented the function of integrating dbus into event to process transactions in a non-blocking manner |
97100

98101
# Environment
99102

README_CN.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,18 @@
8585
| event | 事件库 | 实现了IO,Timer,Signal三种事件驱动,是整个框架的心脏 |
8686
| eventx | 事件扩展库 | 含 ThreadPool 线程池,WorkThread工作线程,TimerPool 定时器池等模块 |
8787
| log | 日志输出库 | 实现了终端、syslog、文件形式的日志输出 |
88+
| trace | 运行痕迹输出库 | 实现实时输出运行痕迹到文件的功能 |
8889
| network | 网络库 | 实现了串口、终端、UDP、TCP 通信模块 |
89-
| terminal | 终端 | 类似shell的命令终端,可实现运行时与程序进行命令交互 |
90+
| terminal | 终端库 | 类似shell的命令终端,可实现运行时与程序进行命令交互 |
9091
| **main** | 主框架 | 实现了完备的程序启动流程与框架,让开发者只需关心业务代码 |
9192
| run | 执行器 | 是个可执行程序,可加载多个由参数`-l xxx`指定的动态库,并运行其中的Module |
9293
| mqtt | MQTT客户端库 | |
9394
| coroutine | 协程库 | 众所周知,异步框架不方便处理顺序性业务,协程弥补之 |
9495
| http | HTTP库 | 在network的基础上实现了HTTP的Server与Client模块 |
95-
| alarm | 闹钟模块 | 实现了4种常用的闹钟:CRON闹钟、单次闹钟、星期循环闹钟、工作日闹钟 |
96-
| flow | 流程模块 | 含多层级状态机与行为树,解决异步模式下动行流程问题 |
96+
| alarm | 闹钟库 | 实现了4种常用的闹钟:CRON闹钟、单次闹钟、星期循环闹钟、工作日闹钟 |
97+
| flow | 流程库| 含多层级状态机与行为树,解决异步模式下动行流程问题 |
98+
| crypto | 加密工具库 | 实现了常用的AES、MD5运算 |
99+
| dbus | DBUS适配库 | 实现了将dbus集成到event进行非阻塞处理事务的功能 |
97100

98101
# 适用环境
99102

modules/main/context_imp.cpp

Lines changed: 24 additions & 245 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <tbox/util/json.h>
3333
#include <tbox/util/fs.h>
3434
#include <tbox/terminal/session.h>
35+
#include <tbox/terminal/helper.h>
3536

3637
#include "main.h"
3738

@@ -290,257 +291,35 @@ void ContextImp::initShell()
290291
{
291292
auto water_line_node = wp_nodes->createDirNode("This is water line directory");
292293
wp_nodes->mountNode(loop_node, water_line_node, "wl");
293-
{
294-
auto func_node = wp_nodes->createFuncNode(
295-
[this] (const Session &s, const Args &args) {
296-
bool print_usage = false;
297-
std::ostringstream oss;
298-
if (args.size() >= 2) {
299-
int value = 0;
300-
auto may_throw_func = [&] { value = std::stoi(args[1]); };
301-
if (CatchThrowQuietly(may_throw_func)) {
302-
oss << "must be number\r\n";
303-
print_usage = true;
304-
} else {
305-
sp_loop_->water_line().run_in_loop_queue_size = value;
306-
oss << "done\r\n";
307-
}
308-
} else {
309-
oss << sp_loop_->water_line().run_in_loop_queue_size << "\r\n";
310-
}
311-
312-
if (print_usage)
313-
oss << "Usage: " << args[0] << " <num>\r\n";
314-
315-
s.send(oss.str());
316-
}
317-
);
318-
wp_nodes->mountNode(water_line_node, func_node, "run_in_loop_queue_size");
319-
}
320-
321-
{
322-
auto func_node = wp_nodes->createFuncNode(
323-
[this] (const Session &s, const Args &args) {
324-
bool print_usage = false;
325-
std::ostringstream oss;
326-
if (args.size() >= 2) {
327-
int value = 0;
328-
auto may_throw_func = [&] { value = std::stoi(args[1]); };
329-
if (CatchThrowQuietly(may_throw_func)) {
330-
oss << "must be number\r\n";
331-
print_usage = true;
332-
} else {
333-
sp_loop_->water_line().run_next_queue_size = value;
334-
oss << "done\r\n";
335-
}
336-
} else {
337-
oss << sp_loop_->water_line().run_next_queue_size << "\r\n";
338-
}
339-
340-
if (print_usage)
341-
oss << "Usage: " << args[0] << " <num>\r\n";
342-
343-
s.send(oss.str());
344-
}
345-
);
346-
wp_nodes->mountNode(water_line_node, func_node, "run_next_queue_size");
347-
}
348294

349-
{
350-
auto func_node = wp_nodes->createFuncNode(
351-
[this] (const Session &s, const Args &args) {
352-
bool print_usage = false;
353-
std::ostringstream oss;
354-
if (args.size() >= 2) {
355-
int value = 0;
356-
auto may_throw_func = [&] { value = std::stoi(args[1]); };
357-
if (CatchThrowQuietly(may_throw_func)) {
358-
oss << "must be number\r\n";
359-
print_usage = true;
360-
} else {
361-
sp_loop_->water_line().run_in_loop_delay = std::chrono::microseconds(value);
362-
oss << "done\r\n";
363-
}
364-
} else {
365-
oss << sp_loop_->water_line().run_in_loop_delay.count()/1000 << " us\r\n";
366-
}
367-
368-
if (print_usage)
369-
oss << "Usage: " << args[0] << " <num>\r\n";
370-
371-
s.send(oss.str());
372-
}
373-
);
374-
wp_nodes->mountNode(water_line_node, func_node, "run_in_loop_delay");
295+
#define ADD_WATERLINE_SIZE_FUNC_NODE(field) \
296+
{\
297+
terminal::IntegerFuncNodeProfile profile; \
298+
profile.get_func = [this] { return sp_loop_->water_line().field; }; \
299+
profile.set_func = [this] (int value) { sp_loop_->water_line().field = value; return true; }; \
300+
profile.min_value = 0; \
301+
terminal::AddFuncNode(*wp_nodes, water_line_node, #field, profile); \
375302
}
376303

377-
{
378-
auto func_node = wp_nodes->createFuncNode(
379-
[this] (const Session &s, const Args &args) {
380-
bool print_usage = false;
381-
std::ostringstream oss;
382-
if (args.size() >= 2) {
383-
int value = 0;
384-
auto may_throw_func = [&] { value = std::stoi(args[1]); };
385-
if (CatchThrowQuietly(may_throw_func)) {
386-
oss << "must be number\r\n";
387-
print_usage = true;
388-
} else {
389-
sp_loop_->water_line().run_next_delay = std::chrono::microseconds(value);
390-
oss << "done\r\n";
391-
}
392-
} else {
393-
oss << sp_loop_->water_line().run_next_delay.count()/1000 << " us\r\n";
394-
}
395-
396-
if (print_usage)
397-
oss << "Usage: " << args[0] << " <num>\r\n";
398-
399-
s.send(oss.str());
400-
}
401-
);
402-
wp_nodes->mountNode(water_line_node, func_node, "run_next_delay");
403-
}
304+
ADD_WATERLINE_SIZE_FUNC_NODE(run_in_loop_queue_size);
305+
ADD_WATERLINE_SIZE_FUNC_NODE(run_next_queue_size);
404306

405-
{
406-
auto func_node = wp_nodes->createFuncNode(
407-
[this] (const Session &s, const Args &args) {
408-
bool print_usage = false;
409-
std::ostringstream oss;
410-
if (args.size() >= 2) {
411-
int value = 0;
412-
auto may_throw_func = [&] { value = std::stoi(args[1]); };
413-
if (CatchThrowQuietly(may_throw_func)) {
414-
oss << "must be number\r\n";
415-
print_usage = true;
416-
} else {
417-
sp_loop_->water_line().loop_cost = std::chrono::microseconds(value);
418-
oss << "done\r\n";
419-
}
420-
} else {
421-
oss << sp_loop_->water_line().loop_cost.count()/1000 << " us\r\n";
422-
}
423-
424-
if (print_usage)
425-
oss << "Usage: " << args[0] << " <num>\r\n";
426-
427-
s.send(oss.str());
428-
}
429-
);
430-
wp_nodes->mountNode(water_line_node, func_node, "loop_cost");
307+
#define ADD_WATERLINE_TIME_FUNC_NODE(field) \
308+
{\
309+
terminal::IntegerFuncNodeProfile profile; \
310+
profile.get_func = [this] { return sp_loop_->water_line().field.count()/1000; }; \
311+
profile.set_func = [this] (int value) { sp_loop_->water_line().field= std::chrono::microseconds(value); return true; }; \
312+
profile.min_value = 0; \
313+
terminal::AddFuncNode(*wp_nodes, water_line_node, #field, profile); \
431314
}
432315

433-
{
434-
auto func_node = wp_nodes->createFuncNode(
435-
[this] (const Session &s, const Args &args) {
436-
bool print_usage = false;
437-
std::ostringstream oss;
438-
if (args.size() >= 2) {
439-
int value = 0;
440-
auto may_throw_func = [&] { value = std::stoi(args[1]); };
441-
if (CatchThrowQuietly(may_throw_func)) {
442-
oss << "must be number\r\n";
443-
print_usage = true;
444-
} else {
445-
sp_loop_->water_line().event_cb_cost = std::chrono::microseconds(value);
446-
oss << "done\r\n";
447-
}
448-
} else {
449-
oss << sp_loop_->water_line().event_cb_cost.count()/1000 << " us\r\n";
450-
}
451-
452-
if (print_usage)
453-
oss << "Usage: " << args[0] << " <num>\r\n";
454-
455-
s.send(oss.str());
456-
}
457-
);
458-
wp_nodes->mountNode(water_line_node, func_node, "event_cb_cost");
459-
}
460-
461-
{
462-
auto func_node = wp_nodes->createFuncNode(
463-
[this] (const Session &s, const Args &args) {
464-
bool print_usage = false;
465-
std::ostringstream oss;
466-
if (args.size() >= 2) {
467-
int value = 0;
468-
auto may_throw_func = [&] { value = std::stoi(args[1]); };
469-
if (CatchThrowQuietly(may_throw_func)) {
470-
oss << "must be number\r\n";
471-
print_usage = true;
472-
} else {
473-
sp_loop_->water_line().run_cb_cost = std::chrono::microseconds(value);
474-
oss << "done\r\n";
475-
}
476-
} else {
477-
oss << sp_loop_->water_line().run_cb_cost.count()/1000 << " us\r\n";
478-
}
479-
480-
if (print_usage)
481-
oss << "Usage: " << args[0] << " <num>\r\n";
482-
483-
s.send(oss.str());
484-
}
485-
);
486-
wp_nodes->mountNode(water_line_node, func_node, "run_cb_cost");
487-
}
488-
489-
{
490-
auto func_node = wp_nodes->createFuncNode(
491-
[this] (const Session &s, const Args &args) {
492-
bool print_usage = false;
493-
std::ostringstream oss;
494-
if (args.size() >= 2) {
495-
int value = 0;
496-
auto may_throw_func = [&] { value = std::stoi(args[1]); };
497-
if (CatchThrowQuietly(may_throw_func)) {
498-
oss << "must be number\r\n";
499-
print_usage = true;
500-
} else {
501-
sp_loop_->water_line().wake_delay = std::chrono::microseconds(value);
502-
oss << "done\r\n";
503-
}
504-
} else {
505-
oss << sp_loop_->water_line().wake_delay.count()/1000 << " us\r\n";
506-
}
507-
508-
if (print_usage)
509-
oss << "Usage: " << args[0] << " <num>\r\n";
510-
511-
s.send(oss.str());
512-
}
513-
);
514-
wp_nodes->mountNode(water_line_node, func_node, "wake_delay");
515-
}
516-
517-
{
518-
auto func_node = wp_nodes->createFuncNode(
519-
[this] (const Session &s, const Args &args) {
520-
bool print_usage = false;
521-
std::ostringstream oss;
522-
if (args.size() >= 2) {
523-
int value = 0;
524-
auto may_throw_func = [&] { value = std::stoi(args[1]); };
525-
if (CatchThrowQuietly(may_throw_func)) {
526-
oss << "must be number\r\n";
527-
print_usage = true;
528-
} else {
529-
sp_loop_->water_line().timer_delay = std::chrono::microseconds(value);
530-
oss << "done\r\n";
531-
}
532-
} else {
533-
oss << sp_loop_->water_line().timer_delay.count()/1000 << " us\r\n";
534-
}
535-
536-
if (print_usage)
537-
oss << "Usage: " << args[0] << " <num>\r\n";
538-
539-
s.send(oss.str());
540-
}
541-
);
542-
wp_nodes->mountNode(water_line_node, func_node, "timer_delay");
543-
}
316+
ADD_WATERLINE_TIME_FUNC_NODE(run_in_loop_delay);
317+
ADD_WATERLINE_TIME_FUNC_NODE(run_next_delay);
318+
ADD_WATERLINE_TIME_FUNC_NODE(loop_cost);
319+
ADD_WATERLINE_TIME_FUNC_NODE(event_cb_cost);
320+
ADD_WATERLINE_TIME_FUNC_NODE(run_cb_cost);
321+
ADD_WATERLINE_TIME_FUNC_NODE(wake_delay);
322+
ADD_WATERLINE_TIME_FUNC_NODE(timer_delay);
544323
}
545324

546325
{

0 commit comments

Comments
 (0)