(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

@ -180,44 +180,41 @@ namespace
_conn.configure(_cobraConfig);
_conn.connect();
_conn.setEventCallback([this, channel](ix::CobraEventType eventType,
const std::string& errMsg,
const ix::WebSocketHttpHeaders& headers,
const std::string& subscriptionId,
CobraConnection::MsgId msgId) {
if (eventType == ix::CobraEventType::Open)
_conn.setEventCallback([this, channel](const CobraEventPtr& event)
{
if (event->type == ix::CobraEventType::Open)
{
log("Subscriber connected: " + _user);
for (auto&& it : headers)
for (auto&& it : event->headers)
{
log("Headers " + it.first + " " + it.second);
}
}
else if (eventType == ix::CobraEventType::Authenticated)
else if (event->type == ix::CobraEventType::Authenticated)
{
log("Subscriber authenticated: " + _user);
subscribe(channel);
}
else if (eventType == ix::CobraEventType::Error)
else if (event->type == ix::CobraEventType::Error)
{
log(errMsg + _user);
log(event->errMsg + _user);
}
else if (eventType == ix::CobraEventType::Closed)
else if (event->type == ix::CobraEventType::Closed)
{
log("Connection closed: " + _user);
}
else if (eventType == ix::CobraEventType::Subscribed)
else if (event->type == ix::CobraEventType::Subscribed)
{
log("Subscription ok: " + _user + " subscription_id " + subscriptionId);
log("Subscription ok: " + _user + " subscription_id " + event->subscriptionId);
_connectedAndSubscribed = true;
}
else if (eventType == ix::CobraEventType::UnSubscribed)
else if (event->type == ix::CobraEventType::UnSubscribed)
{
log("Unsubscription ok: " + _user + " subscription_id " + subscriptionId);
log("Unsubscription ok: " + _user + " subscription_id " + event->subscriptionId);
}
else if (eventType == ix::CobraEventType::Published)
else if (event->type == ix::CobraEventType::Published)
{
TLogger() << "Subscriber: published message acked: " << msgId;
TLogger() << "Subscriber: published message acked: " << event->msgId;
}
});
@ -248,11 +245,7 @@ namespace
ix::msleep(50);
_conn.disconnect();
_conn.setEventCallback([](ix::CobraEventType /*eventType*/,
const std::string& /*errMsg*/,
const ix::WebSocketHttpHeaders& /*headers*/,
const std::string& /*subscriptionId*/,
CobraConnection::MsgId /*msgId*/) { ; });
_conn.setEventCallback([](const CobraEventPtr& /*event*/) {});
}
} // namespace

View File

@ -54,24 +54,25 @@ namespace
conn.configure(config);
conn.connect();
conn.setEventCallback([&conn, &channel](ix::CobraEventType eventType,
const std::string& errMsg,
const ix::WebSocketHttpHeaders& headers,
const std::string& subscriptionId,
CobraConnection::MsgId msgId) {
if (eventType == ix::CobraEventType::Open)
conn.setEventCallback([&conn, &channel](const CobraEventPtr& event)
{
if (event->type == ix::CobraEventType::Open)
{
TLogger() << "Subscriber connected:";
for (auto&& it : headers)
for (auto&& it : event->headers)
{
log("Headers " + it.first + " " + it.second);
}
}
if (eventType == ix::CobraEventType::Error)
else if (event->type == ix::CobraEventType::Closed)
{
TLogger() << "Subscriber error:" << errMsg;
TLogger() << "Subscriber closed:" << event->errMsg;
}
else if (eventType == ix::CobraEventType::Authenticated)
else if (event->type == ix::CobraEventType::Error)
{
TLogger() << "Subscriber error:" << event->errMsg;
}
else if (event->type == ix::CobraEventType::Authenticated)
{
log("Subscriber authenticated");
std::string filter;
@ -92,29 +93,29 @@ namespace
gMessageCount++;
});
}
else if (eventType == ix::CobraEventType::Subscribed)
else if (event->type == ix::CobraEventType::Subscribed)
{
TLogger() << "Subscriber: subscribed to channel " << subscriptionId;
if (subscriptionId == channel)
TLogger() << "Subscriber: subscribed to channel " << event->subscriptionId;
if (event->subscriptionId == channel)
{
gSubscriberConnectedAndSubscribed = true;
}
else
{
TLogger() << "Subscriber: unexpected channel " << subscriptionId;
TLogger() << "Subscriber: unexpected channel " << event->subscriptionId;
}
}
else if (eventType == ix::CobraEventType::UnSubscribed)
else if (event->type == ix::CobraEventType::UnSubscribed)
{
TLogger() << "Subscriber: ununexpected from channel " << subscriptionId;
if (subscriptionId != channel)
TLogger() << "Subscriber: ununexpected from channel " << event->subscriptionId;
if (event->subscriptionId != channel)
{
TLogger() << "Subscriber: unexpected channel " << subscriptionId;
TLogger() << "Subscriber: unexpected channel " << event->subscriptionId;
}
}
else if (eventType == ix::CobraEventType::Published)
else if (event->type == ix::CobraEventType::Published)
{
TLogger() << "Subscriber: published message acked: " << msgId;
TLogger() << "Subscriber: published message acked: " << event->msgId;
}
});