2019-03-20 22:29:02 +01:00
|
|
|
/*
|
|
|
|
* ws_redis_subscribe.cpp
|
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2019-09-23 19:25:23 +02:00
|
|
|
#include <atomic>
|
|
|
|
#include <chrono>
|
2020-06-11 07:30:55 +02:00
|
|
|
#include <ixredis/IXRedisClient.h>
|
2019-12-25 06:55:34 +01:00
|
|
|
#include <spdlog/spdlog.h>
|
2019-03-20 22:29:02 +01:00
|
|
|
#include <sstream>
|
2019-03-27 21:41:46 +01:00
|
|
|
#include <thread>
|
2019-03-20 22:29:02 +01:00
|
|
|
|
|
|
|
namespace ix
|
|
|
|
{
|
|
|
|
int ws_redis_subscribe_main(const std::string& hostname,
|
|
|
|
int port,
|
2019-03-26 17:33:22 +01:00
|
|
|
const std::string& password,
|
2019-03-20 22:29:02 +01:00
|
|
|
const std::string& channel,
|
|
|
|
bool verbose)
|
|
|
|
{
|
|
|
|
RedisClient redisClient;
|
|
|
|
if (!redisClient.connect(hostname, port))
|
|
|
|
{
|
2019-12-25 06:55:34 +01:00
|
|
|
spdlog::info("Cannot connect to redis host");
|
2019-03-20 22:29:02 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-03-26 17:33:22 +01:00
|
|
|
if (!password.empty())
|
|
|
|
{
|
|
|
|
std::string authResponse;
|
|
|
|
if (!redisClient.auth(password, authResponse))
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
2019-12-25 06:55:34 +01:00
|
|
|
spdlog::info("Cannot authenticated to redis");
|
2019-03-26 17:33:22 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2019-12-25 06:55:34 +01:00
|
|
|
spdlog::info("Auth response: {}", authResponse);
|
2019-03-26 17:33:22 +01:00
|
|
|
}
|
|
|
|
|
2019-03-27 21:41:46 +01:00
|
|
|
std::atomic<int> msgPerSeconds(0);
|
|
|
|
std::atomic<int> msgCount(0);
|
2019-03-20 22:29:02 +01:00
|
|
|
|
2019-09-23 19:25:23 +02:00
|
|
|
auto callback = [&msgPerSeconds, &msgCount, verbose](const std::string& message) {
|
2019-03-20 22:29:02 +01:00
|
|
|
if (verbose)
|
|
|
|
{
|
2019-12-25 06:55:34 +01:00
|
|
|
spdlog::info("recived: {}", message);
|
2019-03-20 22:29:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
msgPerSeconds++;
|
2019-03-27 21:41:46 +01:00
|
|
|
msgCount++;
|
|
|
|
};
|
2019-03-20 22:29:02 +01:00
|
|
|
|
2019-09-23 19:25:23 +02:00
|
|
|
auto responseCallback = [](const std::string& redisResponse) {
|
2019-12-25 06:55:34 +01:00
|
|
|
spdlog::info("Redis subscribe response: {}", redisResponse);
|
2019-03-27 21:41:46 +01:00
|
|
|
};
|
2019-03-20 22:29:02 +01:00
|
|
|
|
2019-09-23 19:25:23 +02:00
|
|
|
auto timer = [&msgPerSeconds, &msgCount] {
|
2019-03-27 21:41:46 +01:00
|
|
|
while (true)
|
|
|
|
{
|
2019-12-25 06:55:34 +01:00
|
|
|
spdlog::info("#messages {} msg/s {}", msgCount, msgPerSeconds);
|
2019-03-20 22:29:02 +01:00
|
|
|
|
|
|
|
msgPerSeconds = 0;
|
2019-03-27 21:41:46 +01:00
|
|
|
auto duration = std::chrono::seconds(1);
|
|
|
|
std::this_thread::sleep_for(duration);
|
2019-03-20 22:29:02 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-03-27 21:41:46 +01:00
|
|
|
std::thread t(timer);
|
2019-03-26 17:33:22 +01:00
|
|
|
|
2019-12-25 06:55:34 +01:00
|
|
|
spdlog::info("Subscribing to {} ...", channel);
|
2019-03-26 17:33:22 +01:00
|
|
|
if (!redisClient.subscribe(channel, responseCallback, callback))
|
2019-03-20 22:29:02 +01:00
|
|
|
{
|
2019-12-25 06:55:34 +01:00
|
|
|
spdlog::info("Error subscribing to channel {}", channel);
|
2019-03-20 22:29:02 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2019-09-23 19:25:23 +02:00
|
|
|
} // namespace ix
|