2019-03-20 14:29:02 -07:00
|
|
|
/*
|
|
|
|
* ws_redis_publish.cpp
|
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2020-06-10 22:30:55 -07:00
|
|
|
#include <ixredis/IXRedisClient.h>
|
2019-12-24 21:55:34 -08:00
|
|
|
#include <spdlog/spdlog.h>
|
2019-03-20 14:29:02 -07:00
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
namespace ix
|
|
|
|
{
|
|
|
|
int ws_redis_publish_main(const std::string& hostname,
|
|
|
|
int port,
|
2019-03-26 09:33:22 -07:00
|
|
|
const std::string& password,
|
2019-03-20 14:29:02 -07:00
|
|
|
const std::string& channel,
|
2019-03-27 13:41:46 -07:00
|
|
|
const std::string& message,
|
|
|
|
int count)
|
2019-03-20 14:29:02 -07:00
|
|
|
{
|
|
|
|
RedisClient redisClient;
|
|
|
|
if (!redisClient.connect(hostname, port))
|
|
|
|
{
|
2019-12-24 21:55:34 -08:00
|
|
|
spdlog::info("Cannot connect to redis host");
|
2019-03-20 14:29:02 -07:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-03-26 09:33:22 -07:00
|
|
|
if (!password.empty())
|
|
|
|
{
|
|
|
|
std::string authResponse;
|
|
|
|
if (!redisClient.auth(password, authResponse))
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
2019-12-24 21:55:34 -08:00
|
|
|
spdlog::info("Cannot authenticated to redis");
|
2019-03-26 09:33:22 -07:00
|
|
|
return 1;
|
|
|
|
}
|
2019-12-24 21:55:34 -08:00
|
|
|
spdlog::info("Auth response: {}", authResponse);
|
2019-03-26 09:33:22 -07:00
|
|
|
}
|
|
|
|
|
2019-04-22 17:24:01 -07:00
|
|
|
std::string errMsg;
|
2019-03-27 13:41:46 -07:00
|
|
|
for (int i = 0; i < count; i++)
|
2019-03-20 14:29:02 -07:00
|
|
|
{
|
2019-04-22 17:24:01 -07:00
|
|
|
if (!redisClient.publish(channel, message, errMsg))
|
2019-03-27 13:41:46 -07:00
|
|
|
{
|
2019-12-24 21:55:34 -08:00
|
|
|
spdlog::error("Error publishing to channel {} error {}", channel, errMsg);
|
2019-03-27 13:41:46 -07:00
|
|
|
return 1;
|
|
|
|
}
|
2019-03-20 14:29:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2019-09-23 10:25:23 -07:00
|
|
|
} // namespace ix
|