From: Drew Fisher Date: Thu, 17 Apr 2014 09:42:46 +0000 (-0700) Subject: Dispatch some basic events to the network socket X-Git-Url: http://git.zarvox.org/static/widgets.js?a=commitdiff_plain;h=3785b49414d894149b9ce1c61a5e42a69051a70c;p=imoo.git Dispatch some basic events to the network socket --- diff --git a/ribbon/main.cpp b/ribbon/main.cpp index 3f1f7ac..1e183d8 100644 --- a/ribbon/main.cpp +++ b/ribbon/main.cpp @@ -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(); } diff --git a/ribbon/ribbonmanager.cpp b/ribbon/ribbonmanager.cpp index fff8d56..1f37384 100644 --- a/ribbon/ribbonmanager.cpp +++ b/ribbon/ribbonmanager.cpp @@ -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(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(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(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(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 = diff --git a/ribbon/ribbonmanager.h b/ribbon/ribbonmanager.h index 0e808e6..0773bfd 100644 --- a/ribbon/ribbonmanager.h +++ b/ribbon/ribbonmanager.h @@ -2,6 +2,7 @@ #define RIBBONMANAGER_H #include +#include #include #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) diff --git a/ribbon/ribbonserver.cpp b/ribbon/ribbonserver.cpp index 1c8e3a8..d14ac3d 100644 --- a/ribbon/ribbonserver.cpp +++ b/ribbon/ribbonserver.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include 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); + } +} diff --git a/ribbon/ribbonserver.h b/ribbon/ribbonserver.h index cadb623..f1b32bf 100644 --- a/ribbon/ribbonserver.h +++ b/ribbon/ribbonserver.h @@ -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: diff --git a/ribbon/ribbonsocket.cpp b/ribbon/ribbonsocket.cpp index c9c5dc9..c2611c4 100644 --- a/ribbon/ribbonsocket.cpp +++ b/ribbon/ribbonsocket.cpp @@ -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; diff --git a/ribbon/ribbonsocket.h b/ribbon/ribbonsocket.h index 672c4b8..b1f02d7 100644 --- a/ribbon/ribbonsocket.h +++ b/ribbon/ribbonsocket.h @@ -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);