restructure project
This commit is contained in:
30
ixcobra/CMakeLists.txt
Normal file
30
ixcobra/CMakeLists.txt
Normal file
@ -0,0 +1,30 @@
|
||||
#
|
||||
# Author: Benjamin Sergeant
|
||||
# Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
||||
#
|
||||
|
||||
set (IXCOBRA_SOURCES
|
||||
ixcobra/IXCobraConnection.cpp
|
||||
ixcobra/IXCobraMetricsThreadedPublisher.cpp
|
||||
ixcobra/IXCobraMetricsPublisher.cpp
|
||||
)
|
||||
|
||||
set (IXCOBRA_HEADERS
|
||||
ixcobra/IXCobraConnection.h
|
||||
ixcobra/IXCobraMetricsThreadedPublisher.h
|
||||
ixcobra/IXCobraMetricsPublisher.h
|
||||
)
|
||||
|
||||
add_library(ixcobra STATIC
|
||||
${IXCOBRA_SOURCES}
|
||||
${IXCOBRA_HEADERS}
|
||||
)
|
||||
|
||||
set(IXCOBRA_INCLUDE_DIRS
|
||||
.
|
||||
../ixcore
|
||||
../ixcrypto
|
||||
../ixwebsocket
|
||||
../third_party)
|
||||
|
||||
target_include_directories( ixcobra PUBLIC ${IXCOBRA_INCLUDE_DIRS} )
|
604
ixcobra/ixcobra/IXCobraConnection.cpp
Normal file
604
ixcobra/ixcobra/IXCobraConnection.cpp
Normal file
@ -0,0 +1,604 @@
|
||||
/*
|
||||
* IXCobraConnection.cpp
|
||||
* Author: Benjamin Sergeant
|
||||
* Copyright (c) 2017-2018 Machine Zone. All rights reserved.
|
||||
*/
|
||||
|
||||
#include "IXCobraConnection.h"
|
||||
#include <ixcrypto/IXHMac.h>
|
||||
#include <ixwebsocket/IXWebSocket.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <stdexcept>
|
||||
#include <cmath>
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
namespace ix
|
||||
{
|
||||
TrafficTrackerCallback CobraConnection::_trafficTrackerCallback = nullptr;
|
||||
PublishTrackerCallback CobraConnection::_publishTrackerCallback = nullptr;
|
||||
constexpr size_t CobraConnection::kQueueMaxSize;
|
||||
|
||||
CobraConnection::CobraConnection() :
|
||||
_webSocket(new WebSocket()),
|
||||
_publishMode(CobraConnection_PublishMode_Immediate),
|
||||
_authenticated(false),
|
||||
_eventCallback(nullptr),
|
||||
_id(0)
|
||||
{
|
||||
_pdu["action"] = "rtm/publish";
|
||||
|
||||
initWebSocketOnMessageCallback();
|
||||
}
|
||||
|
||||
CobraConnection::~CobraConnection()
|
||||
{
|
||||
disconnect();
|
||||
setEventCallback(nullptr);
|
||||
}
|
||||
|
||||
void CobraConnection::setTrafficTrackerCallback(const TrafficTrackerCallback& callback)
|
||||
{
|
||||
_trafficTrackerCallback = callback;
|
||||
}
|
||||
|
||||
void CobraConnection::resetTrafficTrackerCallback()
|
||||
{
|
||||
setTrafficTrackerCallback(nullptr);
|
||||
}
|
||||
|
||||
void CobraConnection::invokeTrafficTrackerCallback(size_t size, bool incoming)
|
||||
{
|
||||
if (_trafficTrackerCallback)
|
||||
{
|
||||
_trafficTrackerCallback(size, incoming);
|
||||
}
|
||||
}
|
||||
|
||||
void CobraConnection::setPublishTrackerCallback(const PublishTrackerCallback& callback)
|
||||
{
|
||||
_publishTrackerCallback = callback;
|
||||
}
|
||||
|
||||
void CobraConnection::resetPublishTrackerCallback()
|
||||
{
|
||||
setPublishTrackerCallback(nullptr);
|
||||
}
|
||||
|
||||
void CobraConnection::invokePublishTrackerCallback(bool sent, bool acked)
|
||||
{
|
||||
if (_publishTrackerCallback)
|
||||
{
|
||||
_publishTrackerCallback(sent, acked);
|
||||
}
|
||||
}
|
||||
|
||||
void CobraConnection::setEventCallback(const EventCallback& eventCallback)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_eventCallbackMutex);
|
||||
_eventCallback = eventCallback;
|
||||
}
|
||||
|
||||
void CobraConnection::invokeEventCallback(ix::CobraConnectionEventType eventType,
|
||||
const std::string& errorMsg,
|
||||
const WebSocketHttpHeaders& headers,
|
||||
const std::string& subscriptionId,
|
||||
CobraConnection::MsgId msgId)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_eventCallbackMutex);
|
||||
if (_eventCallback)
|
||||
{
|
||||
_eventCallback(eventType, errorMsg, headers, subscriptionId, msgId);
|
||||
}
|
||||
}
|
||||
|
||||
void CobraConnection::invokeErrorCallback(const std::string& errorMsg,
|
||||
const std::string& serializedPdu)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << errorMsg << " : received pdu => " << serializedPdu;
|
||||
invokeEventCallback(ix::CobraConnection_EventType_Error, ss.str());
|
||||
}
|
||||
|
||||
void CobraConnection::disconnect()
|
||||
{
|
||||
_authenticated = false;
|
||||
_webSocket->stop();
|
||||
}
|
||||
|
||||
void CobraConnection::initWebSocketOnMessageCallback()
|
||||
{
|
||||
_webSocket->setOnMessageCallback(
|
||||
[this](const ix::WebSocketMessagePtr& msg)
|
||||
{
|
||||
CobraConnection::invokeTrafficTrackerCallback(msg->wireSize, true);
|
||||
|
||||
std::stringstream ss;
|
||||
if (msg->type == ix::WebSocketMessageType::Open)
|
||||
{
|
||||
invokeEventCallback(ix::CobraConnection_EventType_Open,
|
||||
std::string(),
|
||||
msg->openInfo.headers);
|
||||
sendHandshakeMessage();
|
||||
}
|
||||
else if (msg->type == ix::WebSocketMessageType::Close)
|
||||
{
|
||||
_authenticated = false;
|
||||
|
||||
std::stringstream ss;
|
||||
ss << "Close code " << msg->closeInfo.code;
|
||||
ss << " reason " << msg->closeInfo.reason;
|
||||
invokeEventCallback(ix::CobraConnection_EventType_Closed,
|
||||
ss.str());
|
||||
}
|
||||
else if (msg->type == ix::WebSocketMessageType::Message)
|
||||
{
|
||||
Json::Value data;
|
||||
Json::Reader reader;
|
||||
if (!reader.parse(msg->str, data))
|
||||
{
|
||||
invokeErrorCallback("Invalid json", msg->str);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!data.isMember("action"))
|
||||
{
|
||||
invokeErrorCallback("Missing action", msg->str);
|
||||
return;
|
||||
}
|
||||
|
||||
auto action = data["action"].asString();
|
||||
|
||||
if (action == "auth/handshake/ok")
|
||||
{
|
||||
if (!handleHandshakeResponse(data))
|
||||
{
|
||||
invokeErrorCallback("Error extracting nonce from handshake response", msg->str);
|
||||
}
|
||||
}
|
||||
else if (action == "auth/handshake/error")
|
||||
{
|
||||
invokeErrorCallback("Handshake error", msg->str);
|
||||
}
|
||||
else if (action == "auth/authenticate/ok")
|
||||
{
|
||||
_authenticated = true;
|
||||
invokeEventCallback(ix::CobraConnection_EventType_Authenticated);
|
||||
flushQueue();
|
||||
}
|
||||
else if (action == "auth/authenticate/error")
|
||||
{
|
||||
invokeErrorCallback("Authentication error", msg->str);
|
||||
}
|
||||
else if (action == "rtm/subscription/data")
|
||||
{
|
||||
handleSubscriptionData(data);
|
||||
}
|
||||
else if (action == "rtm/subscribe/ok")
|
||||
{
|
||||
if (!handleSubscriptionResponse(data))
|
||||
{
|
||||
invokeErrorCallback("Error processing subscribe response", msg->str);
|
||||
}
|
||||
}
|
||||
else if (action == "rtm/subscribe/error")
|
||||
{
|
||||
invokeErrorCallback("Subscription error", msg->str);
|
||||
}
|
||||
else if (action == "rtm/unsubscribe/ok")
|
||||
{
|
||||
if (!handleUnsubscriptionResponse(data))
|
||||
{
|
||||
invokeErrorCallback("Error processing unsubscribe response", msg->str);
|
||||
}
|
||||
}
|
||||
else if (action == "rtm/unsubscribe/error")
|
||||
{
|
||||
invokeErrorCallback("Unsubscription error", msg->str);
|
||||
}
|
||||
else if (action == "rtm/publish/ok")
|
||||
{
|
||||
if (!handlePublishResponse(data))
|
||||
{
|
||||
invokeErrorCallback("Error processing publish response", msg->str);
|
||||
}
|
||||
}
|
||||
else if (action == "rtm/publish/error")
|
||||
{
|
||||
invokeErrorCallback("Publish error", msg->str);
|
||||
}
|
||||
else
|
||||
{
|
||||
invokeErrorCallback("Un-handled message type", msg->str);
|
||||
}
|
||||
}
|
||||
else if (msg->type == ix::WebSocketMessageType::Error)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "Connection error: " << msg->errorInfo.reason << std::endl;
|
||||
ss << "#retries: " << msg->errorInfo.retries << std::endl;
|
||||
ss << "Wait time(ms): " << msg->errorInfo.wait_time << std::endl;
|
||||
ss << "HTTP Status: " << msg->errorInfo.http_status << std::endl;
|
||||
invokeErrorCallback(ss.str(), std::string());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void CobraConnection::setPublishMode(CobraConnectionPublishMode publishMode)
|
||||
{
|
||||
_publishMode = publishMode;
|
||||
}
|
||||
|
||||
void CobraConnection::configure(const std::string& appkey,
|
||||
const std::string& endpoint,
|
||||
const std::string& rolename,
|
||||
const std::string& rolesecret,
|
||||
const WebSocketPerMessageDeflateOptions& webSocketPerMessageDeflateOptions)
|
||||
{
|
||||
_roleName = rolename;
|
||||
_roleSecret = rolesecret;
|
||||
|
||||
std::stringstream ss;
|
||||
ss << endpoint;
|
||||
ss << "/v2?appkey=";
|
||||
ss << appkey;
|
||||
|
||||
std::string url = ss.str();
|
||||
_webSocket->setUrl(url);
|
||||
_webSocket->setPerMessageDeflateOptions(webSocketPerMessageDeflateOptions);
|
||||
}
|
||||
|
||||
//
|
||||
// Handshake message schema.
|
||||
//
|
||||
// handshake = {
|
||||
// "action": "auth/handshake",
|
||||
// "body": {
|
||||
// "data": {
|
||||
// "role": role
|
||||
// },
|
||||
// "method": "role_secret"
|
||||
// },
|
||||
// }
|
||||
//
|
||||
//
|
||||
bool CobraConnection::sendHandshakeMessage()
|
||||
{
|
||||
Json::Value data;
|
||||
data["role"] = _roleName;
|
||||
|
||||
Json::Value body;
|
||||
body["data"] = data;
|
||||
body["method"] = "role_secret";
|
||||
|
||||
Json::Value pdu;
|
||||
pdu["action"] = "auth/handshake";
|
||||
pdu["body"] = body;
|
||||
pdu["id"] = Json::UInt64(_id++);
|
||||
|
||||
std::string serializedJson = serializeJson(pdu);
|
||||
CobraConnection::invokeTrafficTrackerCallback(serializedJson.size(), false);
|
||||
|
||||
return _webSocket->send(serializedJson).success;
|
||||
}
|
||||
|
||||
//
|
||||
// Extract the nonce from the handshake response
|
||||
// use it to compute a hash during authentication
|
||||
//
|
||||
// {
|
||||
// "action": "auth/handshake/ok",
|
||||
// "body": {
|
||||
// "data": {
|
||||
// "nonce": "MTI0Njg4NTAyMjYxMzgxMzgzMg==",
|
||||
// "version": "0.0.24"
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
bool CobraConnection::handleHandshakeResponse(const Json::Value& pdu)
|
||||
{
|
||||
if (!pdu.isObject()) return false;
|
||||
|
||||
if (!pdu.isMember("body")) return false;
|
||||
Json::Value body = pdu["body"];
|
||||
|
||||
if (!body.isMember("data")) return false;
|
||||
Json::Value data = body["data"];
|
||||
|
||||
if (!data.isMember("nonce")) return false;
|
||||
Json::Value nonce = data["nonce"];
|
||||
|
||||
if (!nonce.isString()) return false;
|
||||
|
||||
return sendAuthMessage(nonce.asString());
|
||||
}
|
||||
|
||||
//
|
||||
// Authenticate message schema.
|
||||
//
|
||||
// challenge = {
|
||||
// "action": "auth/authenticate",
|
||||
// "body": {
|
||||
// "method": "role_secret",
|
||||
// "credentials": {
|
||||
// "hash": computeHash(secret, nonce)
|
||||
// }
|
||||
// },
|
||||
// }
|
||||
//
|
||||
bool CobraConnection::sendAuthMessage(const std::string& nonce)
|
||||
{
|
||||
Json::Value credentials;
|
||||
credentials["hash"] = hmac(nonce, _roleSecret);
|
||||
|
||||
Json::Value body;
|
||||
body["credentials"] = credentials;
|
||||
body["method"] = "role_secret";
|
||||
|
||||
Json::Value pdu;
|
||||
pdu["action"] = "auth/authenticate";
|
||||
pdu["body"] = body;
|
||||
pdu["id"] = Json::UInt64(_id++);
|
||||
|
||||
std::string serializedJson = serializeJson(pdu);
|
||||
CobraConnection::invokeTrafficTrackerCallback(serializedJson.size(), false);
|
||||
|
||||
return _webSocket->send(serializedJson).success;
|
||||
}
|
||||
|
||||
bool CobraConnection::handleSubscriptionResponse(const Json::Value& pdu)
|
||||
{
|
||||
if (!pdu.isObject()) return false;
|
||||
|
||||
if (!pdu.isMember("body")) return false;
|
||||
Json::Value body = pdu["body"];
|
||||
|
||||
if (!body.isMember("subscription_id")) return false;
|
||||
Json::Value subscriptionId = body["subscription_id"];
|
||||
|
||||
if (!subscriptionId.isString()) return false;
|
||||
|
||||
invokeEventCallback(ix::CobraConnection_EventType_Subscribed,
|
||||
std::string(), WebSocketHttpHeaders(),
|
||||
subscriptionId.asString());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CobraConnection::handleUnsubscriptionResponse(const Json::Value& pdu)
|
||||
{
|
||||
if (!pdu.isObject()) return false;
|
||||
|
||||
if (!pdu.isMember("body")) return false;
|
||||
Json::Value body = pdu["body"];
|
||||
|
||||
if (!body.isMember("subscription_id")) return false;
|
||||
Json::Value subscriptionId = body["subscription_id"];
|
||||
|
||||
if (!subscriptionId.isString()) return false;
|
||||
|
||||
invokeEventCallback(ix::CobraConnection_EventType_UnSubscribed,
|
||||
std::string(), WebSocketHttpHeaders(),
|
||||
subscriptionId.asString());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CobraConnection::handleSubscriptionData(const Json::Value& pdu)
|
||||
{
|
||||
if (!pdu.isObject()) return false;
|
||||
|
||||
if (!pdu.isMember("body")) return false;
|
||||
Json::Value body = pdu["body"];
|
||||
|
||||
// Identify subscription_id, so that we can find
|
||||
// which callback to execute
|
||||
if (!body.isMember("subscription_id")) return false;
|
||||
Json::Value subscriptionId = body["subscription_id"];
|
||||
|
||||
std::lock_guard<std::mutex> lock(_cbsMutex);
|
||||
auto cb = _cbs.find(subscriptionId.asString());
|
||||
if (cb == _cbs.end()) return false; // cannot find callback
|
||||
|
||||
// Extract messages now
|
||||
if (!body.isMember("messages")) return false;
|
||||
Json::Value messages = body["messages"];
|
||||
|
||||
for (auto&& msg : messages)
|
||||
{
|
||||
cb->second(msg);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CobraConnection::handlePublishResponse(const Json::Value& pdu)
|
||||
{
|
||||
if (!pdu.isObject()) return false;
|
||||
|
||||
if (!pdu.isMember("id")) return false;
|
||||
Json::Value id = pdu["id"];
|
||||
|
||||
if (!id.isUInt64()) return false;
|
||||
|
||||
uint64_t msgId = id.asUInt64();
|
||||
|
||||
invokeEventCallback(ix::CobraConnection_EventType_Published,
|
||||
std::string(), WebSocketHttpHeaders(),
|
||||
std::string(), msgId);
|
||||
|
||||
invokePublishTrackerCallback(false, true);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CobraConnection::connect()
|
||||
{
|
||||
_webSocket->start();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CobraConnection::isConnected() const
|
||||
{
|
||||
return _webSocket->getReadyState() == ix::ReadyState::Open;
|
||||
}
|
||||
|
||||
bool CobraConnection::isAuthenticated() const
|
||||
{
|
||||
return isConnected() && _authenticated;
|
||||
}
|
||||
|
||||
std::string CobraConnection::serializeJson(const Json::Value& value)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_jsonWriterMutex);
|
||||
return _jsonWriter.write(value);
|
||||
}
|
||||
|
||||
//
|
||||
// publish is not thread safe as we are trying to reuse some Json objects.
|
||||
//
|
||||
CobraConnection::MsgId CobraConnection::publish(const Json::Value& channels,
|
||||
const Json::Value& msg)
|
||||
{
|
||||
invokePublishTrackerCallback(true, false);
|
||||
|
||||
CobraConnection::MsgId msgId = _id;
|
||||
|
||||
_body["channels"] = channels;
|
||||
_body["message"] = msg;
|
||||
_pdu["body"] = _body;
|
||||
_pdu["id"] = Json::UInt64(_id++);
|
||||
|
||||
std::string serializedJson = serializeJson(_pdu);
|
||||
|
||||
//
|
||||
// 1. When we use batch mode, we just enqueue and will do the flush explicitely
|
||||
// 2. When we aren't authenticated yet to the cobra server, we need to enqueue
|
||||
// and retry later
|
||||
// 3. If the network connection was droped (WebSocket::send will return false),
|
||||
// it means the message won't be sent so we need to enqueue as well.
|
||||
//
|
||||
// The order of the conditionals is important.
|
||||
//
|
||||
if (_publishMode == CobraConnection_PublishMode_Batch || !_authenticated ||
|
||||
!publishMessage(serializedJson))
|
||||
{
|
||||
enqueue(serializedJson);
|
||||
}
|
||||
|
||||
return msgId;
|
||||
}
|
||||
|
||||
void CobraConnection::subscribe(const std::string& channel,
|
||||
const std::string& filter,
|
||||
SubscriptionCallback cb)
|
||||
{
|
||||
// Create and send a subscribe pdu
|
||||
Json::Value body;
|
||||
body["channel"] = channel;
|
||||
|
||||
if (!filter.empty())
|
||||
{
|
||||
body["filter"] = filter;
|
||||
}
|
||||
|
||||
Json::Value pdu;
|
||||
pdu["action"] = "rtm/subscribe";
|
||||
pdu["body"] = body;
|
||||
pdu["id"] = Json::UInt64(_id++);
|
||||
|
||||
_webSocket->send(pdu.toStyledString());
|
||||
|
||||
// Set the callback
|
||||
std::lock_guard<std::mutex> lock(_cbsMutex);
|
||||
_cbs[channel] = cb;
|
||||
}
|
||||
|
||||
void CobraConnection::unsubscribe(const std::string& channel)
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_cbsMutex);
|
||||
auto cb = _cbs.find(channel);
|
||||
if (cb == _cbs.end()) return;
|
||||
|
||||
_cbs.erase(cb);
|
||||
}
|
||||
|
||||
// Create and send an unsubscribe pdu
|
||||
Json::Value body;
|
||||
body["subscription_id"] = channel;
|
||||
|
||||
Json::Value pdu;
|
||||
pdu["action"] = "rtm/unsubscribe";
|
||||
pdu["body"] = body;
|
||||
pdu["id"] = Json::UInt64(_id++);
|
||||
|
||||
_webSocket->send(pdu.toStyledString());
|
||||
}
|
||||
|
||||
//
|
||||
// Enqueue strategy drops old messages when we are at full capacity
|
||||
//
|
||||
// If we want to keep only 3 items max in the queue:
|
||||
//
|
||||
// enqueue(A) -> [A]
|
||||
// enqueue(B) -> [B, A]
|
||||
// enqueue(C) -> [C, B, A]
|
||||
// enqueue(D) -> [D, C, B] -- now we drop A, the oldest message,
|
||||
// -- and keep the 'fresh ones'
|
||||
//
|
||||
void CobraConnection::enqueue(const std::string& msg)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_queueMutex);
|
||||
|
||||
if (_messageQueue.size() == CobraConnection::kQueueMaxSize)
|
||||
{
|
||||
_messageQueue.pop_back();
|
||||
}
|
||||
_messageQueue.push_front(msg);
|
||||
}
|
||||
|
||||
//
|
||||
// We process messages back (oldest) to front (newest) to respect ordering
|
||||
// when sending them. If we fail to send something, we put it back in the queue
|
||||
// at the end we picked it up originally (at the end).
|
||||
//
|
||||
bool CobraConnection::flushQueue()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_queueMutex);
|
||||
|
||||
while (!_messageQueue.empty())
|
||||
{
|
||||
auto&& msg = _messageQueue.back();
|
||||
if (!publishMessage(msg))
|
||||
{
|
||||
_messageQueue.push_back(msg);
|
||||
return false;
|
||||
}
|
||||
_messageQueue.pop_back();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CobraConnection::publishMessage(const std::string& serializedJson)
|
||||
{
|
||||
auto webSocketSendInfo = _webSocket->send(serializedJson);
|
||||
CobraConnection::invokeTrafficTrackerCallback(webSocketSendInfo.wireSize,
|
||||
false);
|
||||
return webSocketSendInfo.success;
|
||||
}
|
||||
|
||||
void CobraConnection::suspend()
|
||||
{
|
||||
disconnect();
|
||||
}
|
||||
|
||||
void CobraConnection::resume()
|
||||
{
|
||||
connect();
|
||||
}
|
||||
|
||||
} // namespace ix
|
198
ixcobra/ixcobra/IXCobraConnection.h
Normal file
198
ixcobra/ixcobra/IXCobraConnection.h
Normal file
@ -0,0 +1,198 @@
|
||||
/*
|
||||
* IXCobraConnection.h
|
||||
* Author: Benjamin Sergeant
|
||||
* Copyright (c) 2017-2018 Machine Zone. All rights reserved.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ixwebsocket/IXWebSocketHttpHeaders.h>
|
||||
#include <ixwebsocket/IXWebSocketPerMessageDeflateOptions.h>
|
||||
#include <jsoncpp/json/json.h>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <unordered_map>
|
||||
#include <limits>
|
||||
|
||||
namespace ix
|
||||
{
|
||||
class WebSocket;
|
||||
|
||||
enum CobraConnectionEventType
|
||||
{
|
||||
CobraConnection_EventType_Authenticated = 0,
|
||||
CobraConnection_EventType_Error = 1,
|
||||
CobraConnection_EventType_Open = 2,
|
||||
CobraConnection_EventType_Closed = 3,
|
||||
CobraConnection_EventType_Subscribed = 4,
|
||||
CobraConnection_EventType_UnSubscribed = 5,
|
||||
CobraConnection_EventType_Published = 6
|
||||
};
|
||||
|
||||
enum CobraConnectionPublishMode
|
||||
{
|
||||
CobraConnection_PublishMode_Immediate = 0,
|
||||
CobraConnection_PublishMode_Batch = 1
|
||||
};
|
||||
|
||||
using SubscriptionCallback = std::function<void(const Json::Value&)>;
|
||||
using EventCallback = std::function<void(CobraConnectionEventType,
|
||||
const std::string&,
|
||||
const WebSocketHttpHeaders&,
|
||||
const std::string&,
|
||||
uint64_t msgId)>;
|
||||
|
||||
using TrafficTrackerCallback = std::function<void(size_t size, bool incoming)>;
|
||||
using PublishTrackerCallback = std::function<void(bool sent, bool acked)>;
|
||||
|
||||
class CobraConnection
|
||||
{
|
||||
public:
|
||||
using MsgId = uint64_t;
|
||||
|
||||
CobraConnection();
|
||||
~CobraConnection();
|
||||
|
||||
/// Configuration / set keys, etc...
|
||||
/// All input data but the channel name is encrypted with rc4
|
||||
void configure(const std::string& appkey,
|
||||
const std::string& endpoint,
|
||||
const std::string& rolename,
|
||||
const std::string& rolesecret,
|
||||
const WebSocketPerMessageDeflateOptions& webSocketPerMessageDeflateOptions);
|
||||
|
||||
/// Set the traffic tracker callback
|
||||
static void setTrafficTrackerCallback(const TrafficTrackerCallback& callback);
|
||||
|
||||
/// Reset the traffic tracker callback to an no-op one.
|
||||
static void resetTrafficTrackerCallback();
|
||||
|
||||
/// Set the publish tracker callback
|
||||
static void setPublishTrackerCallback(const PublishTrackerCallback& callback);
|
||||
|
||||
/// Reset the publish tracker callback to an no-op one.
|
||||
static void resetPublishTrackerCallback();
|
||||
|
||||
/// Set the closed callback
|
||||
void setEventCallback(const EventCallback& eventCallback);
|
||||
|
||||
/// Start the worker thread, used for background publishing
|
||||
void start();
|
||||
|
||||
/// Publish a message to a channel
|
||||
///
|
||||
/// No-op if the connection is not established
|
||||
MsgId publish(const Json::Value& channels, const Json::Value& msg);
|
||||
|
||||
// Subscribe to a channel, and execute a callback when an incoming
|
||||
// message arrives.
|
||||
void subscribe(const std::string& channel,
|
||||
const std::string& filter = std::string(),
|
||||
SubscriptionCallback cb = nullptr);
|
||||
|
||||
/// Unsubscribe from a channel
|
||||
void unsubscribe(const std::string& channel);
|
||||
|
||||
/// Close the connection
|
||||
void disconnect();
|
||||
|
||||
/// Connect to Cobra and authenticate the connection
|
||||
bool connect();
|
||||
|
||||
/// Returns true only if we're connected
|
||||
bool isConnected() const;
|
||||
|
||||
/// Returns true only if we're authenticated
|
||||
bool isAuthenticated() const;
|
||||
|
||||
/// Flush the publish queue
|
||||
bool flushQueue();
|
||||
|
||||
/// Set the publish mode
|
||||
void setPublishMode(CobraConnectionPublishMode publishMode);
|
||||
|
||||
/// Lifecycle management. Free resources when backgrounding
|
||||
void suspend();
|
||||
void resume();
|
||||
|
||||
private:
|
||||
bool sendHandshakeMessage();
|
||||
bool handleHandshakeResponse(const Json::Value& data);
|
||||
bool sendAuthMessage(const std::string& nonce);
|
||||
bool handleSubscriptionData(const Json::Value& pdu);
|
||||
bool handleSubscriptionResponse(const Json::Value& pdu);
|
||||
bool handleUnsubscriptionResponse(const Json::Value& pdu);
|
||||
bool handlePublishResponse(const Json::Value& pdu);
|
||||
|
||||
void initWebSocketOnMessageCallback();
|
||||
|
||||
bool publishMessage(const std::string& serializedJson);
|
||||
void enqueue(const std::string& msg);
|
||||
std::string serializeJson(const Json::Value& pdu);
|
||||
|
||||
/// Invoke the traffic tracker callback
|
||||
static void invokeTrafficTrackerCallback(size_t size, bool incoming);
|
||||
|
||||
/// Invoke the publish tracker callback
|
||||
static void invokePublishTrackerCallback(bool sent, bool acked);
|
||||
|
||||
/// Invoke event callbacks
|
||||
void invokeEventCallback(CobraConnectionEventType eventType,
|
||||
const std::string& errorMsg = std::string(),
|
||||
const WebSocketHttpHeaders& headers = WebSocketHttpHeaders(),
|
||||
const std::string& subscriptionId = std::string(),
|
||||
uint64_t msgId = std::numeric_limits<uint64_t>::max());
|
||||
void invokeErrorCallback(const std::string& errorMsg, const std::string& serializedPdu);
|
||||
|
||||
///
|
||||
/// Member variables
|
||||
///
|
||||
std::unique_ptr<WebSocket> _webSocket;
|
||||
|
||||
/// Configuration data
|
||||
std::string _roleName;
|
||||
std::string _roleSecret;
|
||||
std::atomic<CobraConnectionPublishMode> _publishMode;
|
||||
|
||||
// Can be set on control+background thread, protecting with an atomic
|
||||
std::atomic<bool> _authenticated;
|
||||
|
||||
// Keep some objects around
|
||||
Json::Value _body;
|
||||
Json::Value _pdu;
|
||||
Json::FastWriter _jsonWriter;
|
||||
mutable std::mutex _jsonWriterMutex;
|
||||
|
||||
/// Traffic tracker callback
|
||||
static TrafficTrackerCallback _trafficTrackerCallback;
|
||||
|
||||
/// Publish tracker callback
|
||||
static PublishTrackerCallback _publishTrackerCallback;
|
||||
|
||||
/// Cobra events callbacks
|
||||
EventCallback _eventCallback;
|
||||
mutable std::mutex _eventCallbackMutex;
|
||||
|
||||
/// Subscription callbacks, only one per channel
|
||||
std::unordered_map<std::string, SubscriptionCallback> _cbs;
|
||||
mutable std::mutex _cbsMutex;
|
||||
|
||||
// Message Queue can be touched on control+background thread,
|
||||
// protecting with a mutex.
|
||||
//
|
||||
// Message queue is used when there are problems sending messages so
|
||||
// that sending can be retried later.
|
||||
std::deque<std::string> _messageQueue;
|
||||
mutable std::mutex _queueMutex;
|
||||
|
||||
// Cap the queue size (100 elems so far -> ~100k)
|
||||
static constexpr size_t kQueueMaxSize = 256;
|
||||
|
||||
// Each pdu sent should have an incremental unique id
|
||||
std::atomic<uint64_t> _id;
|
||||
};
|
||||
|
||||
} // namespace ix
|
241
ixcobra/ixcobra/IXCobraMetricsPublisher.cpp
Normal file
241
ixcobra/ixcobra/IXCobraMetricsPublisher.cpp
Normal file
@ -0,0 +1,241 @@
|
||||
/*
|
||||
* IXCobraMetricsPublisher.cpp
|
||||
* Author: Benjamin Sergeant
|
||||
* Copyright (c) 2017 Machine Zone. All rights reserved.
|
||||
*/
|
||||
|
||||
#include "IXCobraMetricsPublisher.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <stdexcept>
|
||||
|
||||
|
||||
namespace ix
|
||||
{
|
||||
const int CobraMetricsPublisher::kVersion = 1;
|
||||
const std::string CobraMetricsPublisher::kSetRateControlId = "sms_set_rate_control_id";
|
||||
const std::string CobraMetricsPublisher::kSetBlacklistId = "sms_set_blacklist_id";
|
||||
|
||||
CobraMetricsPublisher::CobraMetricsPublisher() :
|
||||
_enabled(true)
|
||||
{
|
||||
}
|
||||
|
||||
CobraMetricsPublisher::~CobraMetricsPublisher()
|
||||
{
|
||||
;
|
||||
}
|
||||
|
||||
void CobraMetricsPublisher::configure(const std::string& appkey,
|
||||
const std::string& endpoint,
|
||||
const std::string& channel,
|
||||
const std::string& rolename,
|
||||
const std::string& rolesecret,
|
||||
bool enablePerMessageDeflate)
|
||||
{
|
||||
// Configure the satori connection and start its publish background thread
|
||||
_cobra_metrics_theaded_publisher.start();
|
||||
|
||||
_cobra_metrics_theaded_publisher.configure(appkey, endpoint, channel,
|
||||
rolename, rolesecret,
|
||||
enablePerMessageDeflate);
|
||||
}
|
||||
|
||||
Json::Value& CobraMetricsPublisher::getGenericAttributes()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_device_mutex);
|
||||
return _device;
|
||||
}
|
||||
|
||||
void CobraMetricsPublisher::setGenericAttributes(const std::string& attrName,
|
||||
const Json::Value& value)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_device_mutex);
|
||||
_device[attrName] = value;
|
||||
}
|
||||
|
||||
void CobraMetricsPublisher::enable(bool enabled)
|
||||
{
|
||||
_enabled = enabled;
|
||||
}
|
||||
|
||||
void CobraMetricsPublisher::setBlacklist(const std::vector<std::string>& blacklist)
|
||||
{
|
||||
_blacklist = blacklist;
|
||||
std::sort(_blacklist.begin(), _blacklist.end());
|
||||
|
||||
// publish our blacklist
|
||||
Json::Value data;
|
||||
Json::Value metrics;
|
||||
for (auto&& metric : blacklist)
|
||||
{
|
||||
metrics.append(metric);
|
||||
}
|
||||
data["blacklist"] = metrics;
|
||||
push(kSetBlacklistId, data);
|
||||
}
|
||||
|
||||
bool CobraMetricsPublisher::isMetricBlacklisted(const std::string& id) const
|
||||
{
|
||||
return std::binary_search(_blacklist.begin(), _blacklist.end(), id);
|
||||
}
|
||||
|
||||
void CobraMetricsPublisher::setRateControl(
|
||||
const std::unordered_map<std::string, int>& rate_control)
|
||||
{
|
||||
for (auto&& it : rate_control)
|
||||
{
|
||||
if (it.second >= 0)
|
||||
{
|
||||
_rate_control[it.first] = it.second;
|
||||
}
|
||||
}
|
||||
|
||||
// publish our rate_control
|
||||
Json::Value data;
|
||||
Json::Value metrics;
|
||||
for (auto&& it : _rate_control)
|
||||
{
|
||||
metrics[it.first] = it.second;
|
||||
}
|
||||
data["rate_control"] = metrics;
|
||||
push(kSetRateControlId, data);
|
||||
}
|
||||
|
||||
bool CobraMetricsPublisher::isAboveMaxUpdateRate(const std::string& id) const
|
||||
{
|
||||
// Is this metrics rate controlled ?
|
||||
auto rate_control_it = _rate_control.find(id);
|
||||
if (rate_control_it == _rate_control.end()) return false;
|
||||
|
||||
// Was this metrics already sent ?
|
||||
std::lock_guard<std::mutex> lock(_last_update_mutex);
|
||||
auto last_update = _last_update.find(id);
|
||||
if (last_update == _last_update.end()) return false;
|
||||
|
||||
auto timeDeltaFromLastSend =
|
||||
std::chrono::steady_clock::now() - last_update->second;
|
||||
|
||||
return timeDeltaFromLastSend < std::chrono::seconds(rate_control_it->second);
|
||||
}
|
||||
|
||||
void CobraMetricsPublisher::setLastUpdate(const std::string& id)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_last_update_mutex);
|
||||
_last_update[id] = std::chrono::steady_clock::now();
|
||||
}
|
||||
|
||||
uint64_t CobraMetricsPublisher::getMillisecondsSinceEpoch() const
|
||||
{
|
||||
auto now = std::chrono::system_clock::now();
|
||||
auto ms =
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
now.time_since_epoch()).count();
|
||||
|
||||
return ms;
|
||||
}
|
||||
|
||||
void CobraMetricsPublisher::push(const std::string& id,
|
||||
const std::string& data,
|
||||
bool shouldPushTest)
|
||||
{
|
||||
if (!_enabled) return;
|
||||
|
||||
Json::Value root;
|
||||
Json::Reader reader;
|
||||
if (!reader.parse(data, root)) return;
|
||||
|
||||
push(id, root, shouldPushTest);
|
||||
}
|
||||
|
||||
void CobraMetricsPublisher::push(const std::string& id,
|
||||
const CobraMetricsPublisher::Message& data)
|
||||
{
|
||||
if (!_enabled) return;
|
||||
|
||||
Json::Value root;
|
||||
for (auto it : data)
|
||||
{
|
||||
root[it.first] = it.second;
|
||||
}
|
||||
|
||||
push(id, root);
|
||||
}
|
||||
|
||||
bool CobraMetricsPublisher::shouldPush(const std::string& id) const
|
||||
{
|
||||
if (!_enabled) return false;
|
||||
if (isMetricBlacklisted(id)) return false;
|
||||
if (isAboveMaxUpdateRate(id)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void CobraMetricsPublisher::push(const std::string& id,
|
||||
const Json::Value& data,
|
||||
bool shouldPushTest)
|
||||
{
|
||||
if (shouldPushTest && !shouldPush(id)) return;
|
||||
|
||||
setLastUpdate(id);
|
||||
|
||||
Json::Value msg;
|
||||
msg["id"] = id;
|
||||
msg["data"] = data;
|
||||
msg["session"] = _session;
|
||||
msg["version"] = kVersion;
|
||||
msg["timestamp"] = Json::UInt64(getMillisecondsSinceEpoch());
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_device_mutex);
|
||||
msg["device"] = _device;
|
||||
}
|
||||
|
||||
{
|
||||
//
|
||||
// Bump a counter for each id
|
||||
// This is used to make sure that we are not
|
||||
// dropping messages, by checking that all the ids is the list of
|
||||
// all natural numbers until the last value sent (0, 1, 2, ..., N)
|
||||
//
|
||||
std::lock_guard<std::mutex> lock(_device_mutex);
|
||||
auto it = _counters.emplace(id, 0);
|
||||
msg["per_id_counter"] = it.first->second;
|
||||
it.first->second += 1;
|
||||
}
|
||||
|
||||
// Now actually enqueue the task
|
||||
_cobra_metrics_theaded_publisher.push(msg);
|
||||
}
|
||||
|
||||
void CobraMetricsPublisher::setPublishMode(CobraConnectionPublishMode publishMode)
|
||||
{
|
||||
_cobra_metrics_theaded_publisher.setPublishMode(publishMode);
|
||||
}
|
||||
|
||||
bool CobraMetricsPublisher::flushQueue()
|
||||
{
|
||||
return _cobra_metrics_theaded_publisher.flushQueue();
|
||||
}
|
||||
|
||||
void CobraMetricsPublisher::suspend()
|
||||
{
|
||||
_cobra_metrics_theaded_publisher.suspend();
|
||||
}
|
||||
|
||||
void CobraMetricsPublisher::resume()
|
||||
{
|
||||
_cobra_metrics_theaded_publisher.resume();
|
||||
}
|
||||
|
||||
bool CobraMetricsPublisher::isConnected() const
|
||||
{
|
||||
return _cobra_metrics_theaded_publisher.isConnected();
|
||||
}
|
||||
|
||||
bool CobraMetricsPublisher::isAuthenticated() const
|
||||
{
|
||||
return _cobra_metrics_theaded_publisher.isAuthenticated();
|
||||
}
|
||||
|
||||
} // namespace ix
|
167
ixcobra/ixcobra/IXCobraMetricsPublisher.h
Normal file
167
ixcobra/ixcobra/IXCobraMetricsPublisher.h
Normal file
@ -0,0 +1,167 @@
|
||||
/*
|
||||
* IXCobraMetricsPublisher.h
|
||||
* Author: Benjamin Sergeant
|
||||
* Copyright (c) 2017 Machine Zone. All rights reserved.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "IXCobraMetricsThreadedPublisher.h"
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <jsoncpp/json/json.h>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace ix
|
||||
{
|
||||
class CobraMetricsPublisher
|
||||
{
|
||||
public:
|
||||
CobraMetricsPublisher();
|
||||
~CobraMetricsPublisher();
|
||||
|
||||
/// Thread safety notes:
|
||||
///
|
||||
/// 1. _enabled, _blacklist and _rate_control read/writes are not protected by a mutex
|
||||
/// to make shouldPush as fast as possible. _enabled default to false.
|
||||
///
|
||||
/// The code that set those is ran only once at init, and
|
||||
/// the last value to be set is _enabled, which is also the first value checked in
|
||||
/// shouldPush, so there shouldn't be any race condition.
|
||||
///
|
||||
/// 2. The queue of messages is thread safe, so multiple metrics can be safely pushed on
|
||||
/// multiple threads
|
||||
///
|
||||
/// 3. Access to _last_update is protected as it needs to be read/write.
|
||||
///
|
||||
|
||||
/// Configuration / set keys, etc...
|
||||
/// All input data but the channel name is encrypted with rc4
|
||||
void configure(const std::string& appkey,
|
||||
const std::string& endpoint,
|
||||
const std::string& channel,
|
||||
const std::string& rolename,
|
||||
const std::string& rolesecret,
|
||||
bool enablePerMessageDeflate);
|
||||
|
||||
/// Setter for the list of blacklisted metrics ids.
|
||||
/// That list is sorted internally for fast lookups
|
||||
void setBlacklist(const std::vector<std::string>& blacklist);
|
||||
|
||||
/// Set the maximum rate at which a metrics can be sent. Unit is seconds
|
||||
/// if rate_control = { 'foo_id': 60 },
|
||||
/// the foo_id metric cannot be pushed more than once every 60 seconds
|
||||
void setRateControl(const std::unordered_map<std::string, int>& rate_control);
|
||||
|
||||
/// Configuration / enable/disable
|
||||
void enable(bool enabled);
|
||||
|
||||
/// Simple interface, list of key value pairs where typeof(key) == typeof(value) == string
|
||||
typedef std::unordered_map<std::string, std::string> Message;
|
||||
void push(const std::string& id,
|
||||
const CobraMetricsPublisher::Message& data = CobraMetricsPublisher::Message());
|
||||
|
||||
/// Richer interface using json, which supports types (bool, int, float) and hierarchies of
|
||||
/// elements
|
||||
///
|
||||
/// The shouldPushTest argument should be set to false, and used in combination with the
|
||||
/// shouldPush method for places where we want to be as lightweight as possible when
|
||||
/// collecting metrics. When set to false, it is used so that we don't do double work when
|
||||
/// computing whether a metrics should be sent or not.
|
||||
void push(const std::string& id, const Json::Value& data, bool shouldPushTest = true);
|
||||
|
||||
/// Interface used by lua. msg is a json encoded string.
|
||||
void push(const std::string& id, const std::string& data, bool shouldPushTest = true);
|
||||
|
||||
/// Tells whether a metric can be pushed.
|
||||
/// A metric can be pushed if it satisfies those conditions:
|
||||
///
|
||||
/// 1. the metrics system should be enabled
|
||||
/// 2. the metrics shouldn't be black-listed
|
||||
/// 3. the metrics shouldn't have reached its rate control limit at this
|
||||
/// "sampling"/"calling" time
|
||||
bool shouldPush(const std::string& id) const;
|
||||
|
||||
/// Get generic information json object
|
||||
Json::Value& getGenericAttributes();
|
||||
|
||||
/// Set generic information values
|
||||
void setGenericAttributes(const std::string& attrName, const Json::Value& value);
|
||||
|
||||
/// Set a unique id for the session. A uuid can be used.
|
||||
void setSession(const std::string& session) { _session = session; }
|
||||
|
||||
/// Get the unique id used to identify the current session
|
||||
const std::string& getSession() const { return _session; }
|
||||
|
||||
/// Return the number of milliseconds since the epoch (~1970)
|
||||
uint64_t getMillisecondsSinceEpoch() const;
|
||||
|
||||
/// Set satori connection publish mode
|
||||
void setPublishMode(CobraConnectionPublishMode publishMode);
|
||||
|
||||
/// Flush the publish queue
|
||||
bool flushQueue();
|
||||
|
||||
/// Lifecycle management. Free resources when backgrounding
|
||||
void suspend();
|
||||
void resume();
|
||||
|
||||
/// Tells whether the socket connection is opened
|
||||
bool isConnected() const;
|
||||
|
||||
/// Returns true only if we're authenticated
|
||||
bool isAuthenticated() const;
|
||||
|
||||
private:
|
||||
/// Lookup an id in our metrics to see whether it is blacklisted
|
||||
/// Complexity is logarithmic
|
||||
bool isMetricBlacklisted(const std::string& id) const;
|
||||
|
||||
/// Tells whether we should drop a metrics or not as part of an enqueuing
|
||||
/// because it exceed the max update rate (it is sent too often)
|
||||
bool isAboveMaxUpdateRate(const std::string& id) const;
|
||||
|
||||
/// Record when a metric was last sent. Used for rate control
|
||||
void setLastUpdate(const std::string& id);
|
||||
|
||||
///
|
||||
/// Member variables
|
||||
///
|
||||
|
||||
CobraMetricsThreadedPublisher _cobra_metrics_theaded_publisher;
|
||||
|
||||
/// A boolean to enable or disable this system
|
||||
/// push becomes a no-op when _enabled is false
|
||||
std::atomic<bool> _enabled;
|
||||
|
||||
/// A uuid used to uniquely identify a session
|
||||
std::string _session;
|
||||
|
||||
/// The _device json blob is populated once when configuring this system
|
||||
/// It record generic metadata about the client, run (version, device model, etc...)
|
||||
Json::Value _device;
|
||||
mutable std::mutex _device_mutex; // protect access to _device
|
||||
|
||||
/// Metrics control (black list + rate control)
|
||||
std::vector<std::string> _blacklist;
|
||||
std::unordered_map<std::string, int> _rate_control;
|
||||
std::unordered_map<std::string, std::chrono::time_point<std::chrono::steady_clock>>
|
||||
_last_update;
|
||||
mutable std::mutex _last_update_mutex; // protect access to _last_update
|
||||
|
||||
/// Bump a counter for each metric type
|
||||
std::unordered_map<std::string, int> _counters;
|
||||
mutable std::mutex _counters_mutex; // protect access to _counters
|
||||
|
||||
// const strings for internal ids
|
||||
static const std::string kSetRateControlId;
|
||||
static const std::string kSetBlacklistId;
|
||||
|
||||
/// Our protocol version. Can be used by subscribers who would want to be backward
|
||||
/// compatible if we change the way we arrange data
|
||||
static const int kVersion;
|
||||
};
|
||||
|
||||
} // namespace ix
|
225
ixcobra/ixcobra/IXCobraMetricsThreadedPublisher.cpp
Normal file
225
ixcobra/ixcobra/IXCobraMetricsThreadedPublisher.cpp
Normal file
@ -0,0 +1,225 @@
|
||||
/*
|
||||
* IXCobraMetricsThreadedPublisher.cpp
|
||||
* Author: Benjamin Sergeant
|
||||
* Copyright (c) 2017 Machine Zone. All rights reserved.
|
||||
*/
|
||||
|
||||
#include "IXCobraMetricsThreadedPublisher.h"
|
||||
#include <ixwebsocket/IXSetThreadName.h>
|
||||
#include <ixcore/utils/IXCoreLogger.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <stdexcept>
|
||||
#include <cmath>
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
namespace ix
|
||||
{
|
||||
CobraMetricsThreadedPublisher::CobraMetricsThreadedPublisher() :
|
||||
_stop(false)
|
||||
{
|
||||
_cobra_connection.setEventCallback(
|
||||
[]
|
||||
(ix::CobraConnectionEventType eventType,
|
||||
const std::string& errMsg,
|
||||
const ix::WebSocketHttpHeaders& headers,
|
||||
const std::string& subscriptionId,
|
||||
CobraConnection::MsgId msgId)
|
||||
{
|
||||
std::stringstream ss;
|
||||
|
||||
if (eventType == ix::CobraConnection_EventType_Open)
|
||||
{
|
||||
ss << "Handshake headers" << std::endl;
|
||||
|
||||
for (auto it : headers)
|
||||
{
|
||||
ss << it.first << ": " << it.second << std::endl;
|
||||
}
|
||||
}
|
||||
else if (eventType == ix::CobraConnection_EventType_Authenticated)
|
||||
{
|
||||
ss << "Authenticated";
|
||||
}
|
||||
else if (eventType == ix::CobraConnection_EventType_Error)
|
||||
{
|
||||
ss << "Error: " << errMsg;
|
||||
}
|
||||
else if (eventType == ix::CobraConnection_EventType_Closed)
|
||||
{
|
||||
ss << "Connection closed: " << errMsg;
|
||||
}
|
||||
else if (eventType == ix::CobraConnection_EventType_Subscribed)
|
||||
{
|
||||
ss << "Subscribed through subscription id: " << subscriptionId;
|
||||
}
|
||||
else if (eventType == ix::CobraConnection_EventType_UnSubscribed)
|
||||
{
|
||||
ss << "Unsubscribed through subscription id: " << subscriptionId;
|
||||
}
|
||||
else if (eventType == ix::CobraConnection_EventType_Published)
|
||||
{
|
||||
ss << "Published message " << msgId << " acked";
|
||||
}
|
||||
|
||||
ix::IXCoreLogger::Log(ss.str().c_str());
|
||||
});
|
||||
}
|
||||
|
||||
CobraMetricsThreadedPublisher::~CobraMetricsThreadedPublisher()
|
||||
{
|
||||
// The background thread won't be joinable if it was never
|
||||
// started by calling CobraMetricsThreadedPublisher::start
|
||||
if (!_thread.joinable()) return;
|
||||
|
||||
_stop = true;
|
||||
_condition.notify_one();
|
||||
_thread.join();
|
||||
}
|
||||
|
||||
void CobraMetricsThreadedPublisher::start()
|
||||
{
|
||||
if (_thread.joinable()) return; // we've already been started
|
||||
|
||||
_thread = std::thread(&CobraMetricsThreadedPublisher::run, this);
|
||||
}
|
||||
|
||||
void CobraMetricsThreadedPublisher::configure(const std::string& appkey,
|
||||
const std::string& endpoint,
|
||||
const std::string& channel,
|
||||
const std::string& rolename,
|
||||
const std::string& rolesecret,
|
||||
bool enablePerMessageDeflate)
|
||||
{
|
||||
_channel = channel;
|
||||
|
||||
ix::WebSocketPerMessageDeflateOptions webSocketPerMessageDeflateOptions(enablePerMessageDeflate);
|
||||
_cobra_connection.configure(appkey, endpoint,
|
||||
rolename, rolesecret,
|
||||
webSocketPerMessageDeflateOptions);
|
||||
}
|
||||
|
||||
void CobraMetricsThreadedPublisher::pushMessage(MessageKind messageKind,
|
||||
const Json::Value& msg)
|
||||
{
|
||||
// Enqueue the task
|
||||
{
|
||||
// acquire lock
|
||||
std::unique_lock<std::mutex> lock(_queue_mutex);
|
||||
|
||||
// add the task
|
||||
_queue.push(std::make_pair(messageKind, msg));
|
||||
} // release lock
|
||||
|
||||
// wake up one thread
|
||||
_condition.notify_one();
|
||||
}
|
||||
|
||||
void CobraMetricsThreadedPublisher::setPublishMode(CobraConnectionPublishMode publishMode)
|
||||
{
|
||||
_cobra_connection.setPublishMode(publishMode);
|
||||
}
|
||||
|
||||
bool CobraMetricsThreadedPublisher::flushQueue()
|
||||
{
|
||||
return _cobra_connection.flushQueue();
|
||||
}
|
||||
|
||||
void CobraMetricsThreadedPublisher::run()
|
||||
{
|
||||
setThreadName("CobraMetricsPublisher");
|
||||
|
||||
Json::Value channels;
|
||||
channels.append(std::string());
|
||||
channels.append(std::string());
|
||||
const std::string messageIdKey("id");
|
||||
|
||||
_cobra_connection.connect();
|
||||
|
||||
while (true)
|
||||
{
|
||||
Json::Value msg;
|
||||
MessageKind messageKind;
|
||||
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(_queue_mutex);
|
||||
|
||||
while (!_stop && _queue.empty())
|
||||
{
|
||||
_condition.wait(lock);
|
||||
}
|
||||
if (_stop)
|
||||
{
|
||||
_cobra_connection.disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
auto item = _queue.front();
|
||||
_queue.pop();
|
||||
|
||||
messageKind = item.first;
|
||||
msg = item.second;
|
||||
}
|
||||
|
||||
switch (messageKind)
|
||||
{
|
||||
case MessageKind::Suspend:
|
||||
{
|
||||
_cobra_connection.suspend();
|
||||
continue;
|
||||
}; break;
|
||||
|
||||
case MessageKind::Resume:
|
||||
{
|
||||
_cobra_connection.resume();
|
||||
continue;
|
||||
}; break;
|
||||
|
||||
case MessageKind::Message:
|
||||
{
|
||||
;
|
||||
}; break;
|
||||
}
|
||||
|
||||
//
|
||||
// Publish to multiple channels. This let the consumer side
|
||||
// easily subscribe to all message of a certain type, without having
|
||||
// to do manipulations on the messages on the server side.
|
||||
//
|
||||
channels[0] = _channel;
|
||||
if (msg.isMember(messageIdKey))
|
||||
{
|
||||
channels[1] = msg[messageIdKey];
|
||||
}
|
||||
_cobra_connection.publish(channels, msg);
|
||||
}
|
||||
}
|
||||
|
||||
void CobraMetricsThreadedPublisher::push(const Json::Value& msg)
|
||||
{
|
||||
pushMessage(MessageKind::Message, msg);
|
||||
}
|
||||
|
||||
void CobraMetricsThreadedPublisher::suspend()
|
||||
{
|
||||
pushMessage(MessageKind::Suspend, Json::Value());
|
||||
}
|
||||
|
||||
void CobraMetricsThreadedPublisher::resume()
|
||||
{
|
||||
pushMessage(MessageKind::Resume, Json::Value());
|
||||
}
|
||||
|
||||
bool CobraMetricsThreadedPublisher::isConnected() const
|
||||
{
|
||||
return _cobra_connection.isConnected();
|
||||
}
|
||||
|
||||
bool CobraMetricsThreadedPublisher::isAuthenticated() const
|
||||
{
|
||||
return _cobra_connection.isAuthenticated();
|
||||
}
|
||||
|
||||
} // namespace ix
|
104
ixcobra/ixcobra/IXCobraMetricsThreadedPublisher.h
Normal file
104
ixcobra/ixcobra/IXCobraMetricsThreadedPublisher.h
Normal file
@ -0,0 +1,104 @@
|
||||
/*
|
||||
* IXCobraMetricsThreadedPublisher.h
|
||||
* Author: Benjamin Sergeant
|
||||
* Copyright (c) 2017 Machine Zone. All rights reserved.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "IXCobraConnection.h"
|
||||
#include <atomic>
|
||||
#include <condition_variable>
|
||||
#include <jsoncpp/json/json.h>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
namespace ix
|
||||
{
|
||||
class CobraMetricsThreadedPublisher
|
||||
{
|
||||
public:
|
||||
CobraMetricsThreadedPublisher();
|
||||
~CobraMetricsThreadedPublisher();
|
||||
|
||||
/// Configuration / set keys, etc...
|
||||
void configure(const std::string& appkey,
|
||||
const std::string& endpoint,
|
||||
const std::string& channel,
|
||||
const std::string& rolename,
|
||||
const std::string& rolesecret,
|
||||
bool enablePerMessageDeflate);
|
||||
|
||||
/// Start the worker thread, used for background publishing
|
||||
void start();
|
||||
|
||||
/// Push a msg to our queue of messages to be published to cobra on the background
|
||||
// thread. Main user right now is the Cobra Metrics System
|
||||
void push(const Json::Value& msg);
|
||||
|
||||
/// Set cobra connection publish mode
|
||||
void setPublishMode(CobraConnectionPublishMode publishMode);
|
||||
|
||||
/// Flush the publish queue
|
||||
bool flushQueue();
|
||||
|
||||
/// Lifecycle management. Free resources when backgrounding
|
||||
void suspend();
|
||||
void resume();
|
||||
|
||||
/// Tells whether the socket connection is opened
|
||||
bool isConnected() const;
|
||||
|
||||
/// Returns true only if we're authenticated
|
||||
bool isAuthenticated() const;
|
||||
|
||||
private:
|
||||
enum class MessageKind
|
||||
{
|
||||
Message = 0,
|
||||
Suspend = 1,
|
||||
Resume = 2
|
||||
};
|
||||
|
||||
/// Push a message to be processed by the background thread
|
||||
void pushMessage(MessageKind messageKind, const Json::Value& msg);
|
||||
|
||||
/// Get a wait time which is increasing exponentially based on the number of retries
|
||||
uint64_t getWaitTimeExp(int retry_count);
|
||||
|
||||
/// Debugging routine to print the connection parameters to the console
|
||||
void printInfo();
|
||||
|
||||
/// Publish a message to satory
|
||||
/// Will retry multiple times (3) if a problem occurs.
|
||||
///
|
||||
/// Right now, only called on the publish worker thread.
|
||||
void safePublish(const Json::Value& msg);
|
||||
|
||||
/// The worker thread "daemon" method. That method never returns unless _stop is set to true
|
||||
void run();
|
||||
|
||||
/// Our connection to cobra.
|
||||
CobraConnection _cobra_connection;
|
||||
|
||||
/// The channel we are publishing to
|
||||
std::string _channel;
|
||||
|
||||
/// Internal data structures used to publish to cobra
|
||||
/// Pending messages are stored into a queue, which is protected by a mutex
|
||||
/// We used a condition variable to prevent the worker thread from busy polling
|
||||
/// So we notify the condition variable when an incoming message arrives to signal
|
||||
/// that it should wake up and take care of publishing it to cobra
|
||||
/// To shutdown the worker thread one has to set the _stop boolean to true.
|
||||
/// This is done in the destructor
|
||||
std::queue<std::pair<MessageKind, Json::Value>> _queue;
|
||||
mutable std::mutex _queue_mutex;
|
||||
std::condition_variable _condition;
|
||||
std::atomic<bool> _stop;
|
||||
std::thread _thread;
|
||||
};
|
||||
|
||||
} // namespace ix
|
1
ixcobra/ixcobra/README.md
Normal file
1
ixcobra/ixcobra/README.md
Normal file
@ -0,0 +1 @@
|
||||
Client code to publish to a real time analytic system, described in [https://bsergean.github.io/redis_conf_2019/slides.html#1](link).
|
Reference in New Issue
Block a user