]> git.zarvox.org Git - imoo.git/commitdiff
Dispatch some basic events to the network socket
authorDrew Fisher <drew.m.fisher@gmail.com>
Thu, 17 Apr 2014 09:42:46 +0000 (02:42 -0700)
committerDrew Fisher <drew.m.fisher@gmail.com>
Thu, 17 Apr 2014 09:42:46 +0000 (02:42 -0700)
ribbon/main.cpp
ribbon/ribbonmanager.cpp
ribbon/ribbonmanager.h
ribbon/ribbonserver.cpp
ribbon/ribbonserver.h
ribbon/ribbonsocket.cpp
ribbon/ribbonsocket.h

index 3f1f7acf16264c16ec2a587018908df8162c7a4c..1e183d8f00a92c984e946e50c94285e3e0323db2 100644 (file)
@@ -10,7 +10,6 @@
 #include "ribbonmanager.h"
 #include "ribbonserver.h"
 
-
 int main(int argc, char** argv)
 {
        QApplication* app = new QApplication(argc, argv);
@@ -33,10 +32,13 @@ 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)));
+
+
        return app->exec();
 }
 
index fff8d56877a8feabd33a7669df8e32e3663b23b3..1f373848304ed7ff1773e313914766be2977fd50 100644 (file)
@@ -100,38 +100,66 @@ static PurpleEventLoopUiOps glib_eventloops =
 //     NULL
 //};
 
+static QVariantMap ctx_from_account(PurpleAccount* account)
+{
+       QVariantMap ctx;
+       ctx["protocol"] = QVariant(purple_account_get_protocol_id(account));
+       ctx["username"] = QVariant(purple_account_get_username(account));
+       return ctx;
+}
+
 static void connect_progress(PurpleConnection *gc, const char* text, size_t step, size_t step_count)
 {
-       Q_UNUSED(gc);
-       qDebug() << "Connection progress:" << text << step << "of" << step_count;
+       PurpleAccount* account = purple_connection_get_account(gc);
+       RibbonManager* r = static_cast<RibbonManager*>(account->ui_data);
+       qDebug() << QString("Connection progress (%1:%2):").arg(account->protocol_id, account->username).toUtf8().constData() << text << step << "of" << step_count;
+       // TODO: add enough unique information from account to ctx to handle the message
+       QVariantMap ctx = ctx_from_account(account);
+       ctx["step"] = QVariant((qulonglong)step);
+       ctx["step_count"] = QVariant((qulonglong)step_count);
+       ctx["text"] = QVariant(QString::fromUtf8(text));
+       r->event(QString("connection"), QString("connect_progress"), ctx);
 }
 
 static void connection_notice(PurpleConnection *gc, const char* text)
 {
-       Q_UNUSED(gc);
        qDebug() << "Connection notice:" << text;
+       PurpleAccount* account = purple_connection_get_account(gc);
+       RibbonManager* r = static_cast<RibbonManager*>(account->ui_data);
+       QVariantMap ctx = ctx_from_account(account);
+       ctx["message"] = QVariant(QString::fromUtf8(text));
+       r->event(QString("connection"), QString("connect_notice"), ctx);
 }
 
 static void connection_disconnected(PurpleConnection *gc, const char* text)
 {
-       Q_UNUSED(gc);
        qDebug() << "Connection disconnected:" << text;
+       PurpleAccount* account = purple_connection_get_account(gc);
+       RibbonManager* r = static_cast<RibbonManager*>(account->ui_data);
+       QVariantMap ctx = ctx_from_account(account);
+       ctx["message"] = QVariant(QString::fromUtf8(text));
+       r->event(QString("connection"), QString("connection_disconnected"), ctx);
 }
 
 static void network_connected(void)
 {
-       printf("Network connection established\n");
+       qWarning() << "Network connection established";
 }
 
 static void network_disconnected(void)
 {
-       printf("This machine has been disconnected from the internet\n");
+       qWarning() << "This machine has been disconnected from the internet";
 }
 
 static void report_disconnect_reason(PurpleConnection *gc, PurpleConnectionError reason, const char *text)
 {
        PurpleAccount *account = purple_connection_get_account(gc);
-       printf("Connection disconnected: \"%s\" (%s)\n  >Error: %d\n  >Reason: %s\n", purple_account_get_username(account), purple_account_get_protocol_id(account), reason, text);
+       //qDebug() << QString("Connection disconnected: \'%1\' (%2)\n  >Error: %3\n  >Reason: %4\n").arg(purple_account_get_username(account), purple_account_get_protocol_id(account), qint32(reason), text);
+       RibbonManager* r = static_cast<RibbonManager*>(account->ui_data);
+       QVariantMap ctx = ctx_from_account(account);
+       ctx["reason_code"] = QVariant(qint32(reason));
+       ctx["reason_text"] = QVariant(QString::fromUtf8(text));
+       r->event(QString("connection"), QString("disconnect_reason"), ctx);
 }
 
 static PurpleConnectionUiOps connection_uiops =
index 0e808e6da2e0ca96a40663099ac550292cae4b5c..0773bfd25eca78f553d6cc1738def9a5c0e0075d 100644 (file)
@@ -2,6 +2,7 @@
 #define RIBBONMANAGER_H
 
 #include <QObject>
+#include <QVariantMap>
 #include <libpurple/purple.h>
 
 #define UI_ID "ribbon"
@@ -24,6 +25,9 @@ public:
 public slots:
        void sign_in_user(void);
 
+signals:
+       void event(QString source, QString signal, QVariantMap context);
+
 private:
        // TODO: add a map of currently-open conversations?
        Q_DISABLE_COPY(RibbonManager)
index 1c8e3a80cbc295bcefda4088991f0589b527b194..d14ac3d6ad19f7854dff42b9a5f85586d4dcb3c7 100644 (file)
@@ -4,6 +4,7 @@
 #include <QtNetwork/QTcpServer>
 #include <QtNetwork/QTcpSocket>
 #include <QJsonDocument>
+#include <QJsonObject>
 #include <QDebug>
 
 RibbonServer::RibbonServer(QObject* parent) :
@@ -66,3 +67,22 @@ void RibbonServer::onMessageReceived(RibbonSocket* r, QByteArray buf)
                qDebug() << "json document:" << json;
        }
 }
+
+void RibbonServer::sendEventToClient(QString sender, QString method, QVariantMap ctx)
+{
+       QVariantMap msg;
+       msg["sender"] = sender;
+       msg["method"] = method;
+       msg["params"] = ctx;
+       QJsonObject obj = QJsonObject::fromVariantMap(msg);
+       QJsonDocument doc(obj);
+       QByteArray msg_bytes = doc.toJson();
+       QByteArray packet;
+       QDataStream stream(&packet, QIODevice::WriteOnly);
+       stream << msg_bytes.constData();
+       //qDebug() << "Would send" << packet.toHex();
+       // No queueing, no blocking, no reliable delivery, no frills.
+       foreach (RibbonSocket* s , sockets) {
+               s->s()->write(packet);
+       }
+}
index cadb6238d09a26558ee576d07739e33d93a69aa6..f1b32bf453749b358ff376b40979d906eb674708 100644 (file)
@@ -18,6 +18,7 @@ public:
        bool listen(const QHostAddress& address = QHostAddress::Any, quint16 port = 0);
 public slots:
        //void dispatch(QByteArray ba);
+       void sendEventToClient(QString sender, QString method, QVariantMap ctx);
 signals:
        void newConnection();
 private:
index c9c5dc94d66924041c899cf4664a3e37fb74ec91..c2611c4a792b742139e1551f4e751222d3096bc3 100644 (file)
@@ -19,7 +19,7 @@ RibbonSocket::~RibbonSocket()
 {
 }
 
-const QTcpSocket* RibbonSocket::s()
+QTcpSocket* RibbonSocket::s()
 {
        return socket;
 }
@@ -52,7 +52,7 @@ bool RibbonSocket::tryProcess()
                                // Optimization: preallocate the right number of bytes
                                // TODO: decide on a maximum acceptable packet length, to avoid
                                // arbitrarily large packet buffer
-                               buf.reserve(bytes_wanted);
+                               //buf.reserve(bytes_wanted);
                                qDebug() << "socket: got length:" << bytes_wanted;
                                state = WANT_DATA;
                                return true;
@@ -63,7 +63,7 @@ bool RibbonSocket::tryProcess()
                                QByteArray data = buf.left(bytes_wanted);
                                buf = buf.mid(bytes_wanted);
                                // Memory optimization: release the rest of the buffer
-                               buf.squeeze();
+                               //buf.squeeze();
                                emit messageReceived(this, data);
                                state = WANT_LENGTH;
                                return true;
index 672c4b82bf7013540b9a5f52f61c5a12606f4cd2..b1f02d715748278a1485db21cd0ead298a5c5c6b 100644 (file)
@@ -14,7 +14,7 @@ class RibbonSocket : public QObject {
 public:
        RibbonSocket(QTcpSocket* socket, QObject* parent=0);
        // Consider s to be final.  Don't modify it.
-       const QTcpSocket* s();
+       QTcpSocket* s();
        void abort();
 signals:
        void disconnected(RibbonSocket* s);