2019-03-20 22:29:02 +01:00
|
|
|
/*
|
|
|
|
* ws_redis_publish.cpp
|
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
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>
|
|
|
|
|
|
|
|
namespace ix
|
|
|
|
{
|
|
|
|
int ws_redis_publish_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,
|
2019-03-27 21:41:46 +01:00
|
|
|
const std::string& message,
|
|
|
|
int count)
|
2019-03-20 22:29:02 +01:00
|
|
|
{
|
|
|
|
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-04-23 02:24:01 +02:00
|
|
|
std::string errMsg;
|
2019-03-27 21:41:46 +01:00
|
|
|
for (int i = 0; i < count; i++)
|
2019-03-20 22:29:02 +01:00
|
|
|
{
|
2019-04-23 02:24:01 +02:00
|
|
|
if (!redisClient.publish(channel, message, errMsg))
|
2019-03-27 21:41:46 +01:00
|
|
|
{
|
2019-12-25 06:55:34 +01:00
|
|
|
spdlog::error("Error publishing to channel {} error {}", channel, errMsg);
|
2019-03-27 21:41:46 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2019-03-20 22:29:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2019-09-23 19:25:23 +02:00
|
|
|
} // namespace ix
|