(ixcobra) change cobra event callback to use a struct instead of several objects, which is more flexible/extensible

This commit is contained in:
Benjamin Sergeant
2020-04-15 17:38:21 -07:00
parent 71a421eefc
commit 64754df66c
13 changed files with 224 additions and 178 deletions

View File

@ -96,7 +96,12 @@ namespace ix
std::lock_guard<std::mutex> lock(_eventCallbackMutex);
if (_eventCallback)
{
_eventCallback(eventType, errorMsg, headers, subscriptionId, msgId);
_eventCallback(
std::make_unique<CobraEvent>(eventType,
errorMsg,
headers,
subscriptionId,
msgId));
}
}

View File

@ -9,6 +9,7 @@
#include <ixwebsocket/IXWebSocketHttpHeaders.h>
#include <ixwebsocket/IXWebSocketPerMessageDeflateOptions.h>
#include "IXCobraEventType.h"
#include "IXCobraEvent.h"
#include <json/json.h>
#include <memory>
#include <mutex>
@ -36,11 +37,7 @@ namespace ix
};
using SubscriptionCallback = std::function<void(const Json::Value&, const std::string&)>;
using EventCallback = std::function<void(CobraEventType,
const std::string&,
const WebSocketHttpHeaders&,
const std::string&,
uint64_t msgId)>;
using EventCallback = std::function<void(const CobraEventPtr&)>;
using TrafficTrackerCallback = std::function<void(size_t size, bool incoming)>;
using PublishTrackerCallback = std::function<void(bool sent, bool acked)>;

View File

@ -0,0 +1,41 @@
/*
* IXCobraEvent.h
* Author: Benjamin Sergeant
* Copyright (c) 2020 Machine Zone, Inc. All rights reserved.
*/
#pragma once
#include "IXCobraEventType.h"
#include <ixwebsocket/IXWebSocketHttpHeaders.h>
#include <memory>
#include <string>
#include <cstdint>
namespace ix
{
struct CobraEvent
{
ix::CobraEventType type;
const std::string& errMsg;
const ix::WebSocketHttpHeaders& headers;
const std::string& subscriptionId;
uint64_t msgId; // CobraConnection::MsgId
CobraEvent(ix::CobraEventType t,
const std::string& e,
const ix::WebSocketHttpHeaders& h,
const std::string& s,
uint64_t m)
: type(t)
, errMsg(e)
, headers(h)
, subscriptionId(s)
, msgId(m)
{
;
}
};
using CobraEventPtr = std::unique_ptr<CobraEvent>;
}

View File

@ -22,53 +22,59 @@ namespace ix
CobraMetricsThreadedPublisher::CobraMetricsThreadedPublisher() :
_stop(false)
{
_cobra_connection.setEventCallback(
[]
(ix::CobraEventType eventType,
const std::string& errMsg,
const ix::WebSocketHttpHeaders& headers,
const std::string& subscriptionId,
CobraConnection::MsgId msgId)
_cobra_connection.setEventCallback([](const CobraEventPtr& event)
{
std::stringstream ss;
if (eventType == ix::CobraEventType::Open)
if (event->type == ix::CobraEventType::Open)
{
ss << "Handshake headers" << std::endl;
for (auto it : headers)
for (auto&& it : event->headers)
{
ss << it.first << ": " << it.second << std::endl;
}
}
else if (eventType == ix::CobraEventType::Authenticated)
else if (event->type == ix::CobraEventType::Authenticated)
{
ss << "Authenticated";
}
else if (eventType == ix::CobraEventType::Error)
else if (event->type == ix::CobraEventType::Error)
{
ss << "Error: " << errMsg;
ss << "Error: " << event->errMsg;
}
else if (eventType == ix::CobraEventType::Closed)
else if (event->type == ix::CobraEventType::Closed)
{
ss << "Connection closed: " << errMsg;
ss << "Connection closed: " << event->errMsg;
}
else if (eventType == ix::CobraEventType::Subscribed)
else if (event->type == ix::CobraEventType::Subscribed)
{
ss << "Subscribed through subscription id: " << subscriptionId;
ss << "Subscribed through subscription id: " << event->subscriptionId;
}
else if (eventType == ix::CobraEventType::UnSubscribed)
else if (event->type == ix::CobraEventType::UnSubscribed)
{
ss << "Unsubscribed through subscription id: " << subscriptionId;
ss << "Unsubscribed through subscription id: " << event->subscriptionId;
}
else if (eventType == ix::CobraEventType::Published)
else if (event->type == ix::CobraEventType::Published)
{
ss << "Published message " << msgId << " acked";
ss << "Published message " << event->msgId << " acked";
}
else if (eventType == ix::CobraEventType::Pong)
else if (event->type == ix::CobraEventType::Pong)
{
ss << "Received websocket pong";
}
else if (event->type == ix::CobraEventType::HandshakeError)
{
ss << "Handshake error: " << event->errMsg;
}
else if (event->type == ix::CobraEventType::AuthenticationError)
{
ss << "Authentication error: " << event->errMsg;
}
else if (event->type == ix::CobraEventType::SubscriptionError)
{
ss << "Subscription error: " << event->errMsg;
}
ix::IXCoreLogger::Log(ss.str().c_str());
});