Fix protocol errors and connection to UI. Make sure protocol handles nulls

This commit is contained in:
Mikko Ahlroth 2014-07-07 15:16:28 +03:00
parent 56d7c58ac0
commit aed3fca0bf
7 changed files with 57 additions and 31 deletions

View file

@ -23,6 +23,7 @@ ApplicationWindow
connectionHandler.connected.connect(C.onConnected);
connectionHandler.disconnected.connect(C.onDisconnected);
connectionHandler.displayDebugData.connect(C.onDisplayDebugData);
connectionHandler.newEvent.connect(C.newEvent);
connectionHandler.sslError.connect(C.onSslError);
}
@ -30,5 +31,3 @@ ApplicationWindow
id: connectionHandler
}
}

View file

@ -34,6 +34,11 @@ function init(connectionHandler, pageStack) {
// Signal handlers for incoming data from connection
function newEvent(id, data) {
debugViewPage.display("Event received: " + id);
debugViewPage.display(JSON.stringify(data));
}
function onDisplayDebugData(str) {
debugViewPage.display(str);
}

View file

@ -184,4 +184,5 @@ void ConnectionHandler::connectSignals()
QObject::connect(handler, &ProtocolHandler::debugData, this, &ConnectionHandler::handleDebugData);
QObject::connect(handler, &ProtocolHandler::sendData, connection, &RelayConnection::write);
QObject::connect(handler, SIGNAL(newEvent(QString,QVariant)), this, SIGNAL(newEvent(QString,QVariant)));
}

View file

@ -48,6 +48,7 @@ signals:
void disconnected();
void displayDebugData(QString data);
void newEvent(QString id, QVariant data);
void sslError(QString errorStrings, QString issuerInfo,
QDateTime startDate, QDateTime expiryDate,
QString digest);

View file

@ -26,6 +26,7 @@ public:
signals:
void debugData(QString hexString);
void newEvent(QString id, QVariant data);
// ProtocolHandler wants to send data to the associated network stream
void sendData(QByteArray data);

View file

@ -103,7 +103,7 @@ qint64 WeeChatProtocolHandler::handleLong(QDataStream* data)
if (i == 0)
{
if (current == 45) // ASCII '-'
if (current == '-')
{
magnitude = -1;
}
@ -121,28 +121,26 @@ qint64 WeeChatProtocolHandler::handleLong(QDataStream* data)
QString WeeChatProtocolHandler::handleString(QDataStream* data)
{
// Strings are stored as length (4 bytes) + data (no \0)
QString ret;
quint32 length;
*data >> length;
// Strings are just buffers interpreted as string data
QByteArray bytes = handleBuffer(data);
quint8 current;
for (quint32 i = 0; i < length; ++i)
{
*data >> current;
ret.append(static_cast<char>(current));
}
return ret;
// Lovely WeeChat sends everything our way in UTF-8 <3
return QString::fromUtf8(bytes);
}
QByteArray WeeChatProtocolHandler::handleBuffer(QDataStream* data)
{
// Buffer is the same as string, but just bytes
// Buffers consist of a length (4 bytes) + data
QByteArray ret;
quint32 length;
*data >> length;
// If length is all 1s, the content is empty
if (length == 0xffffffff)
{
return QByteArray();
}
quint8 current;
for (quint32 i = 0; i < length; ++i)
{
@ -160,13 +158,24 @@ QString WeeChatProtocolHandler::handlePointer(QDataStream* data)
quint8 length;
*data >> length;
qDebug() << "Pointer length: " << length;
quint8 current;
for (quint8 i = 0; i < length; ++i)
{
*data >> current;
// Convert 0 to '0' to handle null pointer
if (current == 0)
{
current = '0';
}
ret.append(static_cast<char>(current));
}
qDebug() << "Pointer data: " << ret;
return ret.prepend("0x");
}
@ -217,13 +226,16 @@ QVariantHash WeeChatProtocolHandler::handleHdata(QDataStream* data)
* of parts in H-path
* * List of objects, with the same keys and types as defined in Keys
*/
QVariantHash hdata;
QVariantList objectSets;
QVariantHash keys;
QString hPath = WeeChatProtocolHandler::handleString(data);
QString hPath = handleString(data);
qDebug() << "H-path: " << hPath;
QString keyString = WeeChatProtocolHandler::handleString(data);
QString keyString = handleString(data);
qDebug() << "Keystr: " << keyString;
QStringList keyList = keyString.split(",");
QStringList orderedKeys;
@ -236,7 +248,7 @@ QVariantHash WeeChatProtocolHandler::handleHdata(QDataStream* data)
orderedKeys.append(keyName);
}
qint32 objectSetCount = WeeChatProtocolHandler::handleInt(data);
qint32 objectSetCount = handleInt(data);
for (qint32 i = 0; i < objectSetCount; ++i)
{
@ -246,9 +258,10 @@ QVariantHash WeeChatProtocolHandler::handleHdata(QDataStream* data)
// separated by /
QVariantList pointers;
int pointerCount = hPath.split("/").size();
qDebug() << "PointerCount: " << pointerCount;
for (int r = 0; r < pointerCount; ++r)
{
pointers.append(WeeChatProtocolHandler::handlePointer(data));
pointers.append(handlePointer(data));
}
objectSet.insert(QString("__path"), pointers);
@ -256,7 +269,8 @@ QVariantHash WeeChatProtocolHandler::handleHdata(QDataStream* data)
for (QString keyName : orderedKeys)
{
Type type = stringToType(keys.value(keyName).toString());
objectSet.insert(keyName, WeeChatProtocolHandler::handleDynamicType(type, data));
qDebug() << "Handling key " << keyName;
objectSet.insert(keyName, handleDynamicType(type, data));
}
objectSets.append(objectSet);
@ -413,6 +427,10 @@ void WeeChatProtocolHandler::handleBody(QDataStream* data)
qDebug() << "ID: " << id;
Type type = readType(data);
qDebug() << "Type: " << type;
// IDs for all event types
if (id == "_pong")
@ -427,10 +445,12 @@ void WeeChatProtocolHandler::handleBody(QDataStream* data)
{
handleUpgradeEnded(id, data);
}
else
{
else if (type == HDATA) {
handleDefaultEvent(id, data);
}
else {
qDebug() << "Unknown type!";
}
delete[] dataArr;
@ -442,27 +462,26 @@ void WeeChatProtocolHandler::handleBody(QDataStream* data)
void WeeChatProtocolHandler::handleDefaultEvent(QString id, QDataStream* data)
{
// Type for default events is hda, so skip it
readType(data);
qDebug() << "Making hdata!";
QVariantHash hdata = handleHdata(data);
emit event(id, hdata);
qDebug() << "Got hdata made!";
emit newEvent(id, hdata);
}
void WeeChatProtocolHandler::handlePong(QString id, QDataStream* data)
{
QString response = handleString(data);
emit event(id, response);
emit newEvent(id, response);
}
void WeeChatProtocolHandler::handleUpgrade(QString id, QDataStream* data)
{
// There is no data to read for this message
emit event(id, QVariant());
emit newEvent(id, QVariant());
}
void WeeChatProtocolHandler::handleUpgradeEnded(QString id, QDataStream* data)
{
// There is no data to read for this message
emit event(id, QVariant());
emit newEvent(id, QVariant());
}

View file

@ -63,7 +63,7 @@ public:
static Type stringToType(QString type);
signals:
void event(QString id, QVariant data);
void newEvent(QString id, QVariant data);
public slots:
void handleNewData(RelayConnection* connection);