forked from ChunelFeng/CGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathE03-ThirdFlow.cpp
More file actions
124 lines (102 loc) · 3.82 KB
/
E03-ThirdFlow.cpp
File metadata and controls
124 lines (102 loc) · 3.82 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
/***************************
@Author: Chunel
@Contact: chunel@foxmail.com
@File: E03-ThirdFlow.cpp
@Time: 2023/9/22 20:35
@Desc: 本example主要展示,常见的三流模型,使用CGraph如何表示
***************************/
#include <cmath>
#include <memory>
#include "CGraph.h"
using namespace CGraph;
static const char* INPUT_TOPIC_NAME = "example/input/topic";
static const char* RESULT_TOPIC_NAME = "example/result/topic";
int randomSleep(int start, int range) {
// 随机休息一下,模拟处理的耗时
int ms = std::abs((int)std::random_device{}()) % range + start;
CGRAPH_SLEEP_MILLISECOND(ms);
return ms;
}
struct InputMParam : public GMessageParam {
int num_ = 0;
};
struct ResultMParam : public GMessageParam {
int negative_num_ = 0;
int process_ts_ = 0;
std::string eng_info_;
};
class InputGNode : public GNode {
public:
CStatus run() override {
for (int i = 0; i < 30; i++) {
std::unique_ptr<InputMParam> input(new InputMParam());
randomSleep(1, 5); // 间隔1~6ms,发送一次
input->num_ = std::abs((int)std::random_device{}()) % 5 + 1;
CGRAPH_SEND_MPARAM(InputMParam, INPUT_TOPIC_NAME, input, GMessagePushStrategy::WAIT);
}
CGRAPH_ECHO(" ----> InputGNode run finished");
return CStatus();
}
};
class ProcessGNode : public GNode {
public:
CStatus run() override {
while (true) {
std::unique_ptr<InputMParam> input = nullptr;
auto status = CGRAPH_RECV_MPARAM_WITH_TIMEOUT(InputMParam, INPUT_TOPIC_NAME, input, 1000);
if (status.isErr()) {
break; // 一阵子收不到消息了,就自动停止好了
}
int ms = randomSleep(1, 100); // 模拟处理流程,随机休息不超过 100ms
std::unique_ptr<ResultMParam> result(new ResultMParam);
switch (input->num_) {
case 1: result->eng_info_ = "one"; break;
case 2: result->eng_info_ = "two"; break;
case 3: result->eng_info_ = "three"; break;
default: result->eng_info_ = "other";
}
result->negative_num_ = -1 * input->num_;
result->process_ts_ = ms;
CGRAPH_SEND_MPARAM(ResultMParam, RESULT_TOPIC_NAME, result, GMessagePushStrategy::WAIT);
}
CGRAPH_ECHO(" ----> ProcessGNode run finished");
return CStatus();
}
};
class ResultGNode : public GNode {
public:
CStatus run() override {
while (true) {
std::unique_ptr<ResultMParam> result = nullptr;
auto status = CGRAPH_RECV_MPARAM_WITH_TIMEOUT(ResultMParam, RESULT_TOPIC_NAME, result, 1000);
if (status.isErr()) {
break;
}
randomSleep(1, 5);
CGRAPH_ECHO("negative num is [%d], english info is [%s], process time is [%d]ms",
result->negative_num_, result->eng_info_.c_str(), result->process_ts_);
}
CGRAPH_ECHO(" ----> ResultGNode run finished");
return CStatus();
}
};
void example_third_flow() {
CGRAPH_CREATE_MESSAGE_TOPIC(InputMParam, INPUT_TOPIC_NAME, 16);
CGRAPH_CREATE_MESSAGE_TOPIC(ResultMParam, RESULT_TOPIC_NAME, 16);
auto pipeline = GPipelineFactory::create();
GElementPtr input, process, result = nullptr;
pipeline->registerGElement<InputGNode>(&input);
pipeline->registerGElement<ProcessGNode>(&process);
pipeline->registerGElement<ResultGNode>(&result);
UThreadPoolConfig config;
config.default_thread_size_ = 3;
config.secondary_thread_size_ = 0;
pipeline->setUniqueThreadPoolConfig(config); // 设置3个线程执行
pipeline->process();
CGRAPH_CLEAR_MESSAGES()
GPipelineFactory::clear();
}
int main() {
example_third_flow();
return 0;
}