-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathtutorial-18-redis_subscriber.cc
150 lines (123 loc) · 2.77 KB
/
tutorial-18-redis_subscriber.cc
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
#include <cerrno>
#include <cctype>
#include <cstring>
#include <iostream>
#include <string>
#include "workflow/WFRedisSubscriber.h"
#include "workflow/WFFacilities.h"
#include "workflow/StringUtil.h"
void extract(WFRedisSubscribeTask *task)
{
auto *resp = task->get_resp();
protocol::RedisValue value;
resp->get_result(value);
if (value.is_array())
{
for (size_t i = 0; i < value.arr_size(); i++)
{
if (value[i].is_string())
std::cout << value[i].string_value();
else if (value[i].is_int())
std::cout << value[i].int_value();
else if (value[i].is_nil())
std::cout << "nil";
else
std::cout << "Unexpected value in array!";
std::cout << "\n";
}
}
else
std::cout << "Unexpected value!\n";
}
int main(int argc, char *argv[])
{
if (argc < 3)
{
std::cerr << argv[0] << " <URL> <Channel> [<Channel>]..." << std::endl;
exit(1);
}
std::string url = argv[1];
if (strncasecmp(argv[1], "redis://", 8) != 0 &&
strncasecmp(argv[1], "rediss://", 9) != 0)
{
url = "redis://" + url;
}
WFRedisSubscriber suber;
if (suber.init(url) != 0)
{
std::cerr << "Subscriber init failed " << strerror(errno) << std::endl;
exit(1);
}
std::vector<std::string> channels;
for (int i = 2; i < argc; i++)
channels.push_back(argv[i]);
WFFacilities::WaitGroup wg(1);
bool finished = false;
auto callback = [&](WFRedisSubscribeTask *task)
{
std::cout << "state = " << task->get_state()
<< ", error = " << task->get_error() << std::endl;
finished = true;
wg.done();
};
WFRedisSubscribeTask *task;
task = suber.create_subscribe_task(channels, extract, callback);
task->set_watch_timeout(1000000);
task->start();
std::string line;
while (!finished)
{
std::string cmd;
std::vector<std::string> params;
if (std::getline(std::cin, line))
{
if (line.empty())
continue;
params = StringUtil::split_filter_empty(line, ' ');
}
if (finished)
break;
if (params.empty())
{
task->unsubscribe();
task->punsubscribe();
break;
}
cmd = params[0];
params.erase(params.begin());
for (char &c : cmd)
c = std::toupper(c);
int ret;
if (cmd == "SUBSCRIBE")
ret = task->subscribe(params);
else if (cmd == "UNSUBSCRIBE")
ret = task->unsubscribe(params);
else if (cmd == "PSUBSCRIBE")
ret = task->psubscribe(params);
else if (cmd == "PUNSUBSCRIBE")
ret = task->punsubscribe(params);
else if (cmd == "PING")
{
if (params.empty())
ret = task->ping();
else
ret = task->ping(params[0]);
}
else if (cmd == "QUIT")
ret = task->quit();
else
{
std::cerr << "Invalid command " << cmd << std::endl;
ret = 0;
}
if (ret < 0)
{
std::cerr << "Send command failed " << strerror(errno) << std::endl;
break;
}
}
task->release();
wg.wait();
suber.deinit();
return 0;
}