IXWebSocket/ws/ws_cobra_publish.cpp

109 lines
3.6 KiB
C++
Raw Normal View History

2019-04-21 20:16:33 +02:00
/*
* ws_cobra_publish.cpp
* Author: Benjamin Sergeant
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
*/
#include <atomic>
2019-09-23 19:25:23 +02:00
#include <chrono>
#include <fstream>
#include <ixcobra/IXCobraMetricsPublisher.h>
2019-09-23 19:25:23 +02:00
#include <jsoncpp/json/json.h>
#include <mutex>
#include <spdlog/spdlog.h>
2019-09-23 19:25:23 +02:00
#include <sstream>
#include <thread>
2019-04-21 20:16:33 +02:00
namespace ix
{
int ws_cobra_publish_main(const std::string& appkey,
const std::string& endpoint,
const std::string& rolename,
const std::string& rolesecret,
const std::string& channel,
const std::string& path,
const ix::SocketTLSOptions& tlsOptions)
2019-04-21 20:16:33 +02:00
{
std::ifstream f(path);
2019-09-23 19:25:23 +02:00
std::string str((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
2019-04-21 20:16:33 +02:00
Json::Value data;
Json::Reader reader;
if (!reader.parse(str, data))
{
spdlog::info("Input file is not a JSON file");
return 1;
}
ix::CobraConnection conn;
conn.configure(appkey,
endpoint,
rolename,
rolesecret,
ix::WebSocketPerMessageDeflateOptions(true),
tlsOptions);
// Display incoming messages
std::atomic<bool> authenticated(false);
std::atomic<bool> messageAcked(false);
2019-10-14 20:15:14 +02:00
conn.setEventCallback([&conn, &channel, &data, &authenticated, &messageAcked](
2019-09-23 19:25:23 +02:00
ix::CobraConnectionEventType eventType,
const std::string& errMsg,
const ix::WebSocketHttpHeaders& headers,
const std::string& subscriptionId,
CobraConnection::MsgId msgId) {
if (eventType == ix::CobraConnection_EventType_Open)
{
2019-09-23 19:25:23 +02:00
spdlog::info("Publisher connected");
2019-09-23 19:25:23 +02:00
for (auto it : headers)
{
2019-09-23 19:25:23 +02:00
spdlog::info("{}: {}", it.first, it.second);
}
}
else if (eventType == ix::CobraConnection_EventType_Authenticated)
{
spdlog::info("Publisher authenticated");
authenticated = true;
2019-09-23 19:25:23 +02:00
Json::Value channels;
channels[0] = channel;
auto msgId = conn.publish(channels, data);
2019-09-23 19:25:23 +02:00
spdlog::info("Published msg {}", msgId);
}
else if (eventType == ix::CobraConnection_EventType_Subscribed)
{
spdlog::info("Publisher: subscribed to channel {}", subscriptionId);
}
else if (eventType == ix::CobraConnection_EventType_UnSubscribed)
{
spdlog::info("Publisher: unsubscribed from channel {}", subscriptionId);
}
else if (eventType == ix::CobraConnection_EventType_Error)
{
spdlog::error("Publisher: error {}", errMsg);
}
else if (eventType == ix::CobraConnection_EventType_Published)
{
spdlog::info("Published message id {} acked", msgId);
messageAcked = true;
}
else if (eventType == ix::CobraConnection_EventType_Pong)
{
spdlog::info("Received websocket pong");
}
2019-09-23 19:25:23 +02:00
});
2019-04-21 20:16:33 +02:00
conn.connect();
2019-09-23 19:25:23 +02:00
while (!authenticated)
;
while (!messageAcked)
;
2019-04-21 20:16:33 +02:00
return 0;
}
2019-09-23 19:25:23 +02:00
} // namespace ix