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 20:46:16 +02:00
|
|
|
#include <ixsnake/IXRedisClient.h>
|
2019-09-23 19:25:23 +02:00
|
|
|
#include <atomic>
|
|
|
|
#include <chrono>
|
2019-03-20 22:29:02 +01:00
|
|
|
#include <iostream>
|
|
|
|
#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))
|
|
|
|
{
|
|
|
|
std::cerr << "Cannot connect to redis host" << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-03-26 17:33:22 +01:00
|
|
|
if (!password.empty())
|
|
|
|
{
|
|
|
|
std::string authResponse;
|
|
|
|
if (!redisClient.auth(password, authResponse))
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
std::cerr << "Cannot authenticated to redis" << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
std::cout << "Auth response: " << authResponse << ":" << port << std::endl;
|
|
|
|
}
|
|
|
|
|
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-03-29 17:35:19 +01:00
|
|
|
std::cout << "received: " << message << std::endl;
|
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-03-27 21:41:46 +01:00
|
|
|
std::cout << "Redis subscribe response: " << redisResponse << std::endl;
|
|
|
|
};
|
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-03-20 22:29:02 +01:00
|
|
|
std::cout << "#messages " << msgCount << " "
|
2019-09-23 19:25:23 +02:00
|
|
|
<< "msg/s " << msgPerSeconds << std::endl;
|
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-03-20 22:29:02 +01:00
|
|
|
std::cerr << "Subscribing to " << channel << "..." << std::endl;
|
2019-03-26 17:33:22 +01:00
|
|
|
if (!redisClient.subscribe(channel, responseCallback, callback))
|
2019-03-20 22:29:02 +01:00
|
|
|
{
|
|
|
|
std::cerr << "Error subscribing to channel " << channel << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2019-09-23 19:25:23 +02:00
|
|
|
} // namespace ix
|