#include "ribbonmanager.h"
#include "ribbonserver.h"
-
int main(int argc, char** argv)
{
QApplication* app = new QApplication(argc, 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();
}
// 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 =
#include <QtNetwork/QTcpServer>
#include <QtNetwork/QTcpSocket>
#include <QJsonDocument>
+#include <QJsonObject>
#include <QDebug>
RibbonServer::RibbonServer(QObject* parent) :
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);
+ }
+}
{
}
-const QTcpSocket* RibbonSocket::s()
+QTcpSocket* RibbonSocket::s()
{
return socket;
}
// 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;
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;