From: Drew Fisher Date: Thu, 24 Apr 2014 05:34:43 +0000 (-0700) Subject: ribbon: dispatch network event to ribbon manager X-Git-Url: http://git.zarvox.org/static/%7Bwebsite%7D?a=commitdiff_plain;h=b74ee0cf7895c318798501dc64438fe2f56dd92b;p=imoo.git ribbon: dispatch network event to ribbon manager --- diff --git a/ribbon/main.cpp b/ribbon/main.cpp index 1e183d8..3103aff 100644 --- a/ribbon/main.cpp +++ b/ribbon/main.cpp @@ -32,12 +32,14 @@ int main(int argc, char** argv) QObject::connect(b, SIGNAL(clicked()), purple, SLOT(sign_in_user())); + RibbonServer* server = new RibbonServer(); server->listen(QHostAddress::Any, 8888); QObject::connect(purple, SIGNAL(event(QString,QString,QVariantMap)), server, SLOT(sendEventToClient(QString,QString,QVariantMap))); - + QObject::connect(server, SIGNAL(messageReceived(QString,QString,QVariantMap)), + purple, SLOT(handleExternalEvent(QString,QString,QVariantMap))); return app->exec(); } diff --git a/ribbon/ribbonmanager.cpp b/ribbon/ribbonmanager.cpp index 1f37384..373d3e7 100644 --- a/ribbon/ribbonmanager.cpp +++ b/ribbon/ribbonmanager.cpp @@ -290,6 +290,12 @@ void RibbonManager::received_im_msg(PurpleAccount *account, char *sender, char * qDebug() << QDateTime::currentDateTime().toString("hh:mm:ss") << sender << purple_conversation_get_name(conv) << message; } +void RibbonManager::handleExternalEvent(QString destination, QString method, QVariantMap context) +{ + // TODO: handle external event + qDebug() << "TODO: do something with" << destination << "." << method << "(" << context << ")"; +} + void RibbonManager::sign_in_user(void) { QFile file("account.txt"); diff --git a/ribbon/ribbonmanager.h b/ribbon/ribbonmanager.h index 0773bfd..7d9de96 100644 --- a/ribbon/ribbonmanager.h +++ b/ribbon/ribbonmanager.h @@ -23,6 +23,7 @@ public: void received_im_msg(PurpleAccount* account, char* sender, char* message, PurpleConversation* conv, PurpleMessageFlags flags); public slots: + void handleExternalEvent(QString destination, QString method, QVariantMap context); void sign_in_user(void); signals: diff --git a/ribbon/ribbonserver.cpp b/ribbon/ribbonserver.cpp index d14ac3d..e3bbc8a 100644 --- a/ribbon/ribbonserver.cpp +++ b/ribbon/ribbonserver.cpp @@ -7,6 +7,17 @@ #include #include +// Helper functions for sanitizing external events +static bool hasStringMember(QVariantMap m, QString memberName) +{ + return m.contains(memberName) && m[memberName].type() == QVariant::String; +} + +static bool hasMapMember(QVariantMap m, QString memberName) +{ + return m.contains(memberName) && m[memberName].type() == QVariant::Map; +} + RibbonServer::RibbonServer(QObject* parent) : QObject(parent) { @@ -63,8 +74,27 @@ void RibbonServer::onMessageReceived(RibbonSocket* r, QByteArray buf) qWarning() << buf.constData(); qWarning() << "hexencoded:" << buf.toHex().constData(); r->abort(); + return; + } + qDebug() << "json document:" << json; + QJsonObject json_object = json.object(); + if (json_object.isEmpty()) { + qWarning() << "Json document was not an object"; + r->abort(); + return; + } + QVariantMap m = json_object.toVariantMap(); + // verify that m contains: + // - a string `destination`, + // - a string `method`, and + // - a map `params` + // If so, emit a signal with those items. + if (hasStringMember(m, "destination") + && hasStringMember(m, "method") + && hasMapMember(m, "params")) { + emit messageReceived(m["destination"].toString(), m["method"].toString(), m["params"].toMap()); } else { - qDebug() << "json document:" << json; + qWarning() << "json did not validate, not emitting signal"; } } diff --git a/ribbon/ribbonserver.h b/ribbon/ribbonserver.h index f1b32bf..17143c4 100644 --- a/ribbon/ribbonserver.h +++ b/ribbon/ribbonserver.h @@ -3,6 +3,7 @@ #include #include +#include #include // forward declarations @@ -21,6 +22,7 @@ public slots: void sendEventToClient(QString sender, QString method, QVariantMap ctx); signals: void newConnection(); + void messageReceived(QString destination, QString method, QVariantMap ctx); private: QTcpServer* server; QHostAddress _addr;