First way to publish
This commit is contained in:
parent
0be7fe9bff
commit
647b8f3072
17 changed files with 240 additions and 21 deletions
|
@ -7,7 +7,8 @@ Connection::Connection(const std::shared_ptr<Core>& core):
|
|||
Shared::Loggable(core->logger, {"Connection"}),
|
||||
state(initial),
|
||||
core(core),
|
||||
gloox()
|
||||
gloox(),
|
||||
pubsub()
|
||||
{}
|
||||
|
||||
Connection::~Connection() noexcept {
|
||||
|
@ -33,6 +34,8 @@ void Connection::initialize() {
|
|||
gloox->setSASLMechanisms(gloox::SaslMechAll);
|
||||
gloox->setStreamManagement(true, true);
|
||||
|
||||
pubsub = std::make_unique<gloox::PubSub::Manager>(gloox.get());
|
||||
|
||||
state = disconnected;
|
||||
}
|
||||
|
||||
|
@ -65,6 +68,42 @@ void Connection::send(const std::string& jid, const std::string& body) {
|
|||
gloox->send(gloox::Message(gloox::Message::Chat, jid, body));
|
||||
}
|
||||
|
||||
void Connection::publish(const std::string& service, const std::string& node, const std::string& title, const std::string& body) {
|
||||
debug("publishing an article \"" + title + "\" to " + node + "@" + service);
|
||||
|
||||
gloox::Tag* entry = new gloox::Tag("entry");
|
||||
entry->setXmlns("http://www.w3.org/2005/Atom");
|
||||
|
||||
entry->addChild(new gloox::Tag("id", "urn:uuid:" + Shared::getUUID()));
|
||||
entry->addChild(new gloox::Tag("title", title));
|
||||
entry->addChild(new gloox::Tag("summary", body));
|
||||
entry->addChild(new gloox::Tag("updated", Shared::getISOTimestamp()));
|
||||
entry->addChild(new gloox::Tag("published", Shared::getISOTimestamp()));
|
||||
|
||||
gloox::PubSub::Item* item = new gloox::PubSub::Item();
|
||||
item->setPayload(entry);
|
||||
gloox::PubSub::ItemList list({item});
|
||||
|
||||
pubsub->publishItem(service, node, list, nullptr, this);
|
||||
}
|
||||
|
||||
std::string Connection::errorTypeToString(gloox::StanzaErrorType err) {
|
||||
switch (err) {
|
||||
case gloox::StanzaErrorTypeAuth:
|
||||
return "Authentication";
|
||||
case gloox::StanzaErrorTypeCancel:
|
||||
return "Cancel";
|
||||
case gloox::StanzaErrorTypeContinue:
|
||||
return "Continue";
|
||||
case gloox::StanzaErrorTypeModify:
|
||||
return "Modify";
|
||||
case gloox::StanzaErrorTypeWait:
|
||||
return "Wait";
|
||||
case gloox::StanzaErrorTypeUndefined:
|
||||
return "Undefined";
|
||||
}
|
||||
}
|
||||
|
||||
void Connection::handleMessage(const gloox::Message& message, gloox::MessageSession* session) {
|
||||
if (message.subtype() != gloox::Message::Chat)
|
||||
return;
|
||||
|
@ -78,9 +117,21 @@ void Connection::handleMessage(const gloox::Message& message, gloox::MessageSess
|
|||
core->router.routeMessage(jid, body);
|
||||
}
|
||||
|
||||
void Connection::handleItemPublication(const std::string& id, const gloox::JID& service, const std::string& node, const gloox::PubSub::ItemList& itemList, const gloox::Error* err) {
|
||||
std::string srv(node + "@" + service.full());
|
||||
|
||||
if (err) {
|
||||
error("Publish failed to " + srv + ", Error: [" + errorTypeToString(err->type()) + "]");
|
||||
return;
|
||||
}
|
||||
|
||||
info("Publish successful to " + srv + ", ID: " + id);
|
||||
}
|
||||
|
||||
void Connection::onConnect() {
|
||||
info("connection established");
|
||||
}
|
||||
|
||||
void Connection::onDisconnect(gloox::ConnectionError e) {
|
||||
std::string error;
|
||||
|
||||
|
@ -148,6 +199,7 @@ void Connection::onDisconnect(gloox::ConnectionError e) {
|
|||
else
|
||||
Loggable::error("disconnected: " + error);
|
||||
}
|
||||
|
||||
bool Connection::onTLSConnect(const gloox::CertInfo&) {
|
||||
info("TLS established");
|
||||
return true;
|
||||
|
|
|
@ -10,14 +10,19 @@
|
|||
#include <gloox/disco.h>
|
||||
#include <gloox/connectionlistener.h>
|
||||
#include <gloox/messagehandler.h>
|
||||
#include <gloox/pubsubmanager.h>
|
||||
#include <gloox/pubsubitem.h>
|
||||
#include <gloox/pubsubresulthandler.h>
|
||||
|
||||
#include "shared/loggable.h"
|
||||
#include "shared/utils.h"
|
||||
#include "component/core.h"
|
||||
|
||||
class Connection:
|
||||
private Shared::Loggable,
|
||||
public gloox::ConnectionListener,
|
||||
public gloox::MessageHandler
|
||||
public gloox::MessageHandler,
|
||||
public gloox::PubSub::ResultHandler
|
||||
{
|
||||
public:
|
||||
enum State {
|
||||
|
@ -34,6 +39,9 @@ public:
|
|||
void deinitialize();
|
||||
void connect();
|
||||
void send(const std::string& jid, const std::string& body);
|
||||
void publish(const std::string& service, const std::string& node, const std::string& title, const std::string& body);
|
||||
|
||||
static std::string errorTypeToString(gloox::StanzaErrorType err);
|
||||
|
||||
public:
|
||||
void onConnect() override;
|
||||
|
@ -41,9 +49,33 @@ public:
|
|||
bool onTLSConnect(const gloox::CertInfo&) override;
|
||||
void handleMessage(const gloox::Message& message, gloox::MessageSession* session = 0) override;
|
||||
|
||||
void handleItemPublication(const std::string& id, const gloox::JID& service, const std::string& node, const gloox::PubSub::ItemList& itemList, const gloox::Error* error = 0) override;
|
||||
|
||||
// All other methods are not needed; make them no-op
|
||||
void handleItem(const gloox::JID&, const std::string&, const gloox::Tag*) override {}
|
||||
void handleItems(const std::string&, const gloox::JID&, const std::string&, const gloox::PubSub::ItemList&, const gloox::Error* = 0) override {}
|
||||
void handleItemDeletion(const std::string&, const gloox::JID&, const std::string&, const gloox::PubSub::ItemList&, const gloox::Error* = 0) override {}
|
||||
void handleSubscriptionResult(const std::string&, const gloox::JID&, const std::string&, const std::string&, const gloox::JID&, const gloox::PubSub::SubscriptionType, const gloox::Error* = 0) override {}
|
||||
void handleUnsubscriptionResult(const std::string&, const gloox::JID&, const gloox::Error* = 0) override {}
|
||||
void handleSubscriptionOptions(const std::string&, const gloox::JID&, const gloox::JID&, const std::string&, const gloox::DataForm*, const std::string& = gloox::EmptyString, const gloox::Error* = 0) override {}
|
||||
void handleSubscriptionOptionsResult(const std::string&, const gloox::JID&, const gloox::JID&, const std::string&, const std::string& = gloox::EmptyString, const gloox::Error* = 0) override {}
|
||||
void handleSubscribers(const std::string&, const gloox::JID&, const std::string&, const gloox::PubSub::SubscriptionList&, const gloox::Error* = 0) override {}
|
||||
void handleSubscribersResult(const std::string&, const gloox::JID&, const std::string&, const gloox::PubSub::SubscriberList*, const gloox::Error* = 0) override {}
|
||||
void handleAffiliates(const std::string&, const gloox::JID&, const std::string&, const gloox::PubSub::AffiliateList*, const gloox::Error* = 0) override {}
|
||||
void handleAffiliatesResult(const std::string&, const gloox::JID&, const std::string&, const gloox::PubSub::AffiliateList*, const gloox::Error* = 0) override {}
|
||||
void handleNodeConfig(const std::string&, const gloox::JID&, const std::string&, const gloox::DataForm*, const gloox::Error* = 0) override {}
|
||||
void handleNodeConfigResult(const std::string&, const gloox::JID&, const std::string&, const gloox::Error* = 0) override {}
|
||||
void handleNodeCreation(const std::string&, const gloox::JID&, const std::string&, const gloox::Error* = 0) override {}
|
||||
void handleNodeDeletion(const std::string&, const gloox::JID&, const std::string&, const gloox::Error* = 0) override {}
|
||||
void handleNodePurge(const std::string&, const gloox::JID&, const std::string&, const gloox::Error* = 0) override {}
|
||||
void handleSubscriptions(const std::string&, const gloox::JID&, const gloox::PubSub::SubscriptionMap&, const gloox::Error* = 0) override {}
|
||||
void handleAffiliations(const std::string&, const gloox::JID&, const gloox::PubSub::AffiliationMap&, const gloox::Error* = 0) override {}
|
||||
void handleDefaultNodeConfig(const std::string&, const gloox::JID&, const gloox::DataForm*, const gloox::Error* = 0) override {}
|
||||
|
||||
private:
|
||||
State state;
|
||||
std::shared_ptr<Core> core;
|
||||
std::unique_ptr<gloox::Client> gloox;
|
||||
std::unique_ptr<gloox::PubSub::Manager> pubsub;
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue