acl/lib_acl_cpp/samples/redis/redis_pubsub/redis_pubsub.cpp

218 lines
4.5 KiB
C++
Raw Normal View History

#include "stdafx.h"
static acl::string __channel_prefix("test_channel");
2023-05-14 22:59:42 +08:00
static int __count = 0;
2023-05-15 13:45:41 +08:00
static bool __show = false;
2023-05-14 22:59:42 +08:00
static bool get_msg(acl::redis_pubsub& redis, int cnt)
{
2023-05-14 22:59:42 +08:00
acl::string channel, msg;
2023-05-14 22:59:42 +08:00
for (int i = 0; i < cnt; i++) {
channel.clear();
msg.clear();
redis.clear();
2023-05-14 22:59:42 +08:00
if ((redis.get_message(channel, msg)) == false) {
printf("get_message error(%s)\r\n", redis.result_error());
return false;
}
2023-05-14 22:59:42 +08:00
2023-05-15 13:45:41 +08:00
if (++__count < 10 || __show) {
2023-05-14 22:59:42 +08:00
printf("get message from channel %s, msg=%s\r\n",
channel.c_str(), msg.c_str());
}
}
2023-05-14 22:59:42 +08:00
return true;
}
2023-05-14 22:59:42 +08:00
static bool test_subscribe(acl::redis_pubsub& redis, int n, int cnt)
{
acl::string channel;
2023-05-14 22:59:42 +08:00
for (int i = 0; i < n; i++) {
2023-05-15 13:45:41 +08:00
channel.format("%s_%d", __channel_prefix.c_str(), i);
2023-05-14 22:59:42 +08:00
redis.clear();
int ret = redis.subscribe(channel, NULL);
if (ret <= 0) {
printf("subscribe %serror(%s), ret: %d\r\n",
channel.c_str(), redis.result_error(), ret);
return false;
2023-05-14 22:59:42 +08:00
} else if (i < 10) {
printf("subscribe %s ok\r\n", channel.c_str());
}
2023-05-14 22:59:42 +08:00
}
2023-05-14 22:59:42 +08:00
printf(">>>subscribe channels total: %d\r\n", n);
2023-05-15 13:45:41 +08:00
acl::redis_client* client = redis.get_client();
if (client) {
acl::socket_stream* conn = client->get_stream(false);
const char* peer_addr;
if (conn) {
peer_addr = conn->get_peer(true);
} else {
peer_addr = "";
}
printf("subscribe client on addr=%s, peer=%s\r\n",
client->get_addr(), peer_addr);
}
2023-05-14 22:59:42 +08:00
for (int i = 0; i < n; i++) {
if (get_msg(redis, cnt) == false) {
printf("get_msg error\r\n");
return false;
}
}
2023-05-14 22:59:42 +08:00
printf(">>>message total: %d\r\n", n * cnt);
return true;
}
2023-05-14 22:59:42 +08:00
static bool pub_msg(acl::redis_pubsub& redis, const char* channel, int n)
{
2023-05-14 22:59:42 +08:00
acl::string msg;
2023-05-14 22:59:42 +08:00
for (int i = 0; i < n; i++) {
redis.clear();
2023-05-15 13:45:41 +08:00
msg.format("msg_%s_%d", channel, i);
2023-05-14 22:59:42 +08:00
int ret = redis.publish(channel, msg.c_str(), msg.length());
2023-05-15 13:45:41 +08:00
if (ret < 0) {
printf("publish to %s %s error(%s), ret: %d\r\n",
2023-05-14 22:59:42 +08:00
channel, msg.c_str(),
redis.result_error(), ret);
return false;
}
2023-05-15 13:45:41 +08:00
if (++__count < 10 || __show) {
2023-05-14 22:59:42 +08:00
printf("publish ok, channel=%s, msg=%s\r\n",
channel, msg.c_str());
}
}
2023-05-14 22:59:42 +08:00
return true;
}
static bool test_publish(acl::redis_pubsub& redis, int n, int cnt)
{
acl::string channel;
for (int i = 0; i < n; i++) {
2023-05-15 13:45:41 +08:00
channel.format("%s_%d", __channel_prefix.c_str(), i);
2023-05-14 22:59:42 +08:00
if (!pub_msg(redis, channel, cnt)) {
return false;
}
}
2023-05-14 22:59:42 +08:00
printf(">>>publish total: %d\r\n", n * cnt);
return true;
}
static void usage(const char* procname)
{
printf("usage: %s -h[help]\r\n"
"-s redis_addr[127.0.0.1:6379]\r\n"
2023-05-13 22:59:07 +08:00
"-p passwd\r\n"
"-n count\r\n"
2023-05-14 22:59:42 +08:00
"-m msg_count\r\n"
2023-05-14 23:08:17 +08:00
"-c connect_timeout[default: 10]\r\n"
"-I rw_timeout[default: 0]\r\n"
2023-05-14 23:08:17 +08:00
"-C [use cluster mode]\r\n"
2023-05-15 13:45:41 +08:00
"-S [if show messages]\r\n"
"-a cmd[subscribe|publish]\r\n",
procname);
}
int main(int argc, char* argv[])
{
2023-05-14 22:59:42 +08:00
int ch, n = 1, cnt = 10, conn_timeout = 10, rw_timeout = 0;
2023-05-13 22:59:07 +08:00
acl::string addr("127.0.0.1:6379"), cmd, passwd;
2015-08-26 20:45:40 +08:00
bool cluster_mode = false;
2023-05-15 13:45:41 +08:00
while ((ch = getopt(argc, argv, "hs:n:m:c:I:a:Cp:S")) > 0) {
2023-05-14 22:59:42 +08:00
switch (ch) {
case 'h':
usage(argv[0]);
return 0;
case 's':
addr = optarg;
break;
case 'n':
n = atoi(optarg);
break;
2023-05-14 22:59:42 +08:00
case 'm':
cnt = atoi(optarg);
break;
2023-05-14 23:08:17 +08:00
case 'c':
conn_timeout = atoi(optarg);
break;
case 'I':
rw_timeout = atoi(optarg);
break;
case 'a':
cmd = optarg;
break;
2023-05-14 23:08:17 +08:00
case 'C':
2015-08-26 20:45:40 +08:00
cluster_mode = true;
break;
2023-05-13 22:59:07 +08:00
case 'p':
passwd = optarg;
break;
2023-05-15 13:45:41 +08:00
case 'S':
__show = true;
break;
default:
break;
}
}
acl::acl_cpp_init();
acl::log::stdout_open(true);
2015-08-26 20:45:40 +08:00
2023-05-13 22:59:07 +08:00
acl::redis_client client(addr.c_str(), conn_timeout, rw_timeout);
acl::redis_client_cluster cluster;
cluster.set(addr.c_str(), 100, conn_timeout, rw_timeout);
2015-08-26 20:45:40 +08:00
2023-05-14 22:59:42 +08:00
if (!passwd.empty()) {
2023-05-13 22:59:07 +08:00
client.set_password(passwd);
cluster.set_password("default", passwd);
}
2015-08-26 20:45:40 +08:00
acl::redis_pubsub redis;
2023-05-14 22:59:42 +08:00
if (cluster_mode) {
2023-05-14 23:08:17 +08:00
printf("use cluster mode\r\n");
redis.set_cluster(&cluster);
2023-05-14 22:59:42 +08:00
} else {
2023-05-14 23:08:17 +08:00
printf("use single mode\r\n");
2015-08-26 20:45:40 +08:00
redis.set_client(&client);
2023-05-14 22:59:42 +08:00
}
2015-02-02 16:50:06 +08:00
bool ret;
2023-05-14 22:59:42 +08:00
if (cmd == "subscribe") {
ret = test_subscribe(redis, n, cnt);
} else if (cmd == "publish") {
ret = test_publish(redis, n, cnt);
} else {
2015-02-02 16:50:06 +08:00
ret = false;
printf("unknown cmd: %s\r\n", cmd.c_str());
2015-02-02 16:50:06 +08:00
}
2023-05-14 22:59:42 +08:00
if (ret == true) {
2015-02-02 16:50:06 +08:00
printf("test OK!\r\n");
2023-05-14 22:59:42 +08:00
} else {
2015-02-02 16:50:06 +08:00
printf("test failed!\r\n");
2023-05-14 22:59:42 +08:00
}
#ifdef WIN32
printf("enter any key to exit\r\n");
getchar();
#endif
return 0;
}