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>
|
2019-04-21 20:20:17 +02:00
|
|
|
#include <ixcobra/IXCobraMetricsPublisher.h>
|
2019-09-23 19:25:23 +02:00
|
|
|
#include <jsoncpp/json/json.h>
|
|
|
|
#include <mutex>
|
2019-09-01 01:46:44 +02:00
|
|
|
#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,
|
2019-12-20 05:49:28 +01:00
|
|
|
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;
|
2019-09-19 21:51:11 +02:00
|
|
|
if (!reader.parse(str, data))
|
2019-04-23 02:24:01 +02:00
|
|
|
{
|
2019-09-19 21:51:11 +02:00
|
|
|
spdlog::info("Input file is not a JSON file");
|
|
|
|
return 1;
|
2019-04-23 02:24:01 +02:00
|
|
|
}
|
2019-09-19 21:51:11 +02:00
|
|
|
|
|
|
|
ix::CobraConnection conn;
|
2019-12-20 05:49:28 +01:00
|
|
|
conn.configure(appkey,
|
|
|
|
endpoint,
|
|
|
|
rolename,
|
|
|
|
rolesecret,
|
|
|
|
ix::WebSocketPerMessageDeflateOptions(true),
|
|
|
|
tlsOptions);
|
2019-09-19 21:51:11 +02:00
|
|
|
|
|
|
|
// 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-04-23 02:24:01 +02:00
|
|
|
{
|
2019-09-23 19:25:23 +02:00
|
|
|
spdlog::info("Publisher connected");
|
2019-09-19 21:51:11 +02:00
|
|
|
|
2019-09-23 19:25:23 +02:00
|
|
|
for (auto it : headers)
|
2019-09-19 21:51:11 +02:00
|
|
|
{
|
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-04-23 02:24:01 +02:00
|
|
|
|
2019-09-23 19:25:23 +02:00
|
|
|
Json::Value channels;
|
|
|
|
channels[0] = channel;
|
|
|
|
auto msgId = conn.publish(channels, data);
|
2019-09-21 18:23:58 +02:00
|
|
|
|
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;
|
2019-04-23 02:24:01 +02:00
|
|
|
}
|
2019-12-25 02:16:41 +01:00
|
|
|
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
|
|
|
|
2019-12-23 05:29:37 +01:00
|
|
|
conn.connect();
|
|
|
|
|
2019-09-23 19:25:23 +02:00
|
|
|
while (!authenticated)
|
|
|
|
;
|
|
|
|
while (!messageAcked)
|
|
|
|
;
|
2019-09-01 01:46:44 +02:00
|
|
|
|
2019-04-21 20:16:33 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2019-09-23 19:25:23 +02:00
|
|
|
} // namespace ix
|