Say hello to compression!
authorMarcus Eggenberger <egs@quassel-irc.org>
Wed, 4 Jun 2008 17:22:18 +0000 (19:22 +0200)
committerMarcus Eggenberger <egs@quassel-irc.org>
Wed, 4 Jun 2008 17:22:18 +0000 (19:22 +0200)
This feature doesn't break protocol and is automagically enabled if both client and core support compression.
Distclean is needed!

src/client/clientsyncer.cpp
src/common/signalproxy.cpp
src/common/signalproxy.h
src/core/core.cpp

index b7b2088..e7f68b0 100644 (file)
@@ -160,6 +160,12 @@ void ClientSyncer::coreSocketConnected() {
   clientInit["ClientDate"] = Global::quasselDate;
   clientInit["ClientBuild"] = Global::quasselBuild; // this is a minimum, since we probably won't update for every commit
   clientInit["UseSsl"] = coreConnectionInfo["useSsl"];
   clientInit["ClientDate"] = Global::quasselDate;
   clientInit["ClientBuild"] = Global::quasselBuild; // this is a minimum, since we probably won't update for every commit
   clientInit["UseSsl"] = coreConnectionInfo["useSsl"];
+#ifndef QT_NO_COMPRESS
+  clientInit["UseCompression"] = true;
+#else
+  clientInit["UseCompression"] = false;
+#endif
+
   
   SignalProxy::writeDataToDevice(socket, clientInit);
 }
   
   SignalProxy::writeDataToDevice(socket, clientInit);
 }
@@ -206,6 +212,12 @@ void ClientSyncer::clientInitAck(const QVariantMap &msg) {
   }
 #endif
 
   }
 #endif
 
+#ifndef QT_NO_COMPRESS
+  if(msg["SupportsCompression"].toBool()) {
+    socket->setProperty("UseCompression", true);
+  }
+#endif
+  
   if(!msg["Configured"].toBool()) {
     // start wizard
     emit startCoreSetup(msg["StorageBackends"].toList());
   if(!msg["Configured"].toBool()) {
     // start wizard
     emit startCoreSetup(msg["StorageBackends"].toList());
index 22e71f1..f467b1b 100644 (file)
@@ -179,7 +179,7 @@ void SignalRelay::attachSignal(int methodId, const QByteArray &func) {
   sigNames.insert(methodId, fn);
 }
 // ====================
   sigNames.insert(methodId, fn);
 }
 // ====================
-// END SIGNALRELAY
+//  /SIGNALRELAY
 // ====================
 
 
 // ====================
 
 
@@ -211,14 +211,14 @@ SignalProxy::~SignalProxy() {
     detachObject(sender);
 
   // close peer connections
     detachObject(sender);
 
   // close peer connections
-  foreach(QIODevice *device, _peerByteCount.keys()) {
+  foreach(QIODevice *device, _peers.keys()) {
     device->close();
     delete device;
   }
 }
 
 void SignalProxy::setProxyMode(ProxyMode mode) {
     device->close();
     delete device;
   }
 }
 
 void SignalProxy::setProxyMode(ProxyMode mode) {
-  foreach(QIODevice* peer, _peerByteCount.keys()) {
+  foreach(QIODevice* peer, _peers.keys()) {
     if(peer->isOpen()) {
       qWarning() << "SignalProxy: Cannot change proxy mode while connected";
       return;
     if(peer->isOpen()) {
       qWarning() << "SignalProxy: Cannot change proxy mode while connected";
       return;
@@ -251,10 +251,10 @@ bool SignalProxy::addPeer(QIODevice* iodev) {
   if(!iodev)
     return false;
   
   if(!iodev)
     return false;
   
-  if(_peerByteCount.contains(iodev))
+  if(_peers.contains(iodev))
     return true;
 
     return true;
 
-  if(proxyMode() == Client && !_peerByteCount.isEmpty()) {
+  if(proxyMode() == Client && !_peers.isEmpty()) {
     qWarning("SignalProxy: only one peer allowed in client mode!");
     return false;
   }
     qWarning("SignalProxy: only one peer allowed in client mode!");
     return false;
   }
@@ -270,43 +270,45 @@ bool SignalProxy::addPeer(QIODevice* iodev) {
     connect(sock, SIGNAL(disconnected()), this, SLOT(removePeerBySender()));
   }
 
     connect(sock, SIGNAL(disconnected()), this, SLOT(removePeerBySender()));
   }
 
-  _peerByteCount[iodev] = 0;
+  _peers[iodev] = peerInfo();
+  if(iodev->property("UseCompression").toBool())
+    _peers[iodev].usesCompression = true;
 
 
-  if(_peerByteCount.count() == 1)
+  if(_peers.count() == 1)
     emit connected();
 
   return true;
 }
 
 void SignalProxy::removePeer(QIODevice* iodev) {
     emit connected();
 
   return true;
 }
 
 void SignalProxy::removePeer(QIODevice* iodev) {
-  if(_peerByteCount.isEmpty()) {
+  if(_peers.isEmpty()) {
     qWarning() << "SignalProxy::removePeer(): No peers in use!";
     return;
   }
 
   if(proxyMode() == Server && !iodev) {
     // disconnect all
     qWarning() << "SignalProxy::removePeer(): No peers in use!";
     return;
   }
 
   if(proxyMode() == Server && !iodev) {
     // disconnect all
-    QList<QIODevice *> peers = _peerByteCount.keys();
+    QList<QIODevice *> peers = _peers.keys();
     foreach(QIODevice *peer, peers)
       removePeer(peer);
   }
 
   if(proxyMode() != Server && !iodev)
     foreach(QIODevice *peer, peers)
       removePeer(peer);
   }
 
   if(proxyMode() != Server && !iodev)
-    iodev = _peerByteCount.keys().first();
+    iodev = _peers.keys().first();
 
   Q_ASSERT(iodev);
 
 
   Q_ASSERT(iodev);
 
-  if(!_peerByteCount.contains(iodev)) {
+  if(!_peers.contains(iodev)) {
     qWarning() << "SignalProxy: unknown QIODevice" << iodev;
     return;
   }
 
     qWarning() << "SignalProxy: unknown QIODevice" << iodev;
     return;
   }
 
-  _peerByteCount.remove(iodev);
+  _peers.remove(iodev);
 
   disconnect(iodev, 0, this, 0);
   emit peerRemoved(iodev);
 
 
   disconnect(iodev, 0, this, 0);
   emit peerRemoved(iodev);
 
-  if(_peerByteCount.isEmpty())
+  if(_peers.isEmpty())
     emit disconnected();
 }
 
     emit disconnected();
 }
 
@@ -672,10 +674,11 @@ void SignalProxy::stopSync(SyncableObject* obj) {
 }
 
 void SignalProxy::dispatchSignal(QIODevice *receiver, const RequestType &requestType, const QVariantList &params) {
 }
 
 void SignalProxy::dispatchSignal(QIODevice *receiver, const RequestType &requestType, const QVariantList &params) {
+  Q_ASSERT(_peers.contains(receiver));
   QVariantList packedFunc;
   packedFunc << (qint16)requestType;
   packedFunc << params;
   QVariantList packedFunc;
   packedFunc << (qint16)requestType;
   packedFunc << params;
-  writeDataToDevice(receiver, QVariant(packedFunc));
+  writeDataToDevice(receiver, QVariant(packedFunc), _peers[receiver].usesCompression);
 }
 
 void SignalProxy::dispatchSignal(const RequestType &requestType, const QVariantList &params) {
 }
 
 void SignalProxy::dispatchSignal(const RequestType &requestType, const QVariantList &params) {
@@ -683,8 +686,10 @@ void SignalProxy::dispatchSignal(const RequestType &requestType, const QVariantL
   QVariantList packedFunc;
   packedFunc << (qint16)requestType;
   packedFunc << params;
   QVariantList packedFunc;
   packedFunc << (qint16)requestType;
   packedFunc << params;
-  foreach(QIODevice* dev, _peerByteCount.keys())
-    writeDataToDevice(dev, QVariant(packedFunc));
+  foreach(QIODevice* dev, _peers.keys()) {
+    Q_ASSERT(_peers.contains(dev));
+    writeDataToDevice(dev, QVariant(packedFunc), _peers[dev].usesCompression);
+  }
 }
 
 void SignalProxy::receivePeerSignal(QIODevice *sender, const QVariant &packedFunc) {
 }
 
 void SignalProxy::receivePeerSignal(QIODevice *sender, const QVariant &packedFunc) {
@@ -893,27 +898,43 @@ bool SignalProxy::invokeSlot(QObject *receiver, int methodId, const QVariantList
 void SignalProxy::dataAvailable() {
   // yet again. it's a private slot. no need for checks.
   QIODevice* ioDev = qobject_cast<QIODevice* >(sender());
 void SignalProxy::dataAvailable() {
   // yet again. it's a private slot. no need for checks.
   QIODevice* ioDev = qobject_cast<QIODevice* >(sender());
+  Q_ASSERT(_peers.contains(ioDev));
   QVariant var;
   QVariant var;
-  while(readDataFromDevice(ioDev, _peerByteCount[ioDev], var))
+  while(readDataFromDevice(ioDev, _peers[ioDev].byteCount, var, _peers[ioDev].usesCompression))
     receivePeerSignal(ioDev, var);
 }
 
     receivePeerSignal(ioDev, var);
 }
 
-void SignalProxy::writeDataToDevice(QIODevice *dev, const QVariant &item) {
+void SignalProxy::writeDataToDevice(QIODevice *dev, const QVariant &item, bool compressed) {
   QAbstractSocket* sock  = qobject_cast<QAbstractSocket*>(dev);
   if(!dev->isOpen() || (sock && sock->state()!=QAbstractSocket::ConnectedState)) {
     qWarning("SignalProxy: Can't call on a closed device");
     return;
   }
   QAbstractSocket* sock  = qobject_cast<QAbstractSocket*>(dev);
   if(!dev->isOpen() || (sock && sock->state()!=QAbstractSocket::ConnectedState)) {
     qWarning("SignalProxy: Can't call on a closed device");
     return;
   }
+
   QByteArray block;
   QDataStream out(&block, QIODevice::WriteOnly);
   out.setVersion(QDataStream::Qt_4_2);
   QByteArray block;
   QDataStream out(&block, QIODevice::WriteOnly);
   out.setVersion(QDataStream::Qt_4_2);
-  out << (quint32)0 << item;
-  out.device()->seek(0);
-  out << (quint32)(block.size() - sizeof(quint32));
+
+  if(compressed) {
+    QByteArray rawItem;
+    QDataStream itemStream(&rawItem, QIODevice::WriteOnly);
+
+    itemStream.setVersion(QDataStream::Qt_4_2);
+    itemStream << item;
+
+    rawItem = qCompress(rawItem);
+
+    out << (quint32)rawItem.size() << rawItem;
+  } else {
+    out << (quint32)0 << item;
+    out.device()->seek(0);
+    out << (quint32)(block.size() - sizeof(quint32));
+  }
+
   dev->write(block);
 }
 
   dev->write(block);
 }
 
-bool SignalProxy::readDataFromDevice(QIODevice *dev, quint32 &blockSize, QVariant &item) {
+bool SignalProxy::readDataFromDevice(QIODevice *dev, quint32 &blockSize, QVariant &item, bool compressed) {
   QDataStream in(dev);
   in.setVersion(QDataStream::Qt_4_2);
 
   QDataStream in(dev);
   in.setVersion(QDataStream::Qt_4_2);
 
@@ -924,8 +945,21 @@ bool SignalProxy::readDataFromDevice(QIODevice *dev, quint32 &blockSize, QVarian
 
   if(dev->bytesAvailable() < blockSize)
     return false;
 
   if(dev->bytesAvailable() < blockSize)
     return false;
-  in >> item;
+
+  if(compressed) {
+    QByteArray rawItem;
+    in >> rawItem;
+    rawItem = qUncompress(rawItem);
+
+    QDataStream itemStream(&rawItem, QIODevice::ReadOnly);
+    itemStream.setVersion(QDataStream::Qt_4_2);
+    itemStream >> item;
+  } else {
+    in >> item;
+  }
+
   blockSize = 0;
   blockSize = 0;
+
   return true;
 }
 
   return true;
 }
 
index 1090547..7ffb73b 100644 (file)
@@ -81,13 +81,13 @@ public:
    *  so the corresponding function readDataFromDevice() can check if enough data is available
    *  at the device to reread the item.
    */
    *  so the corresponding function readDataFromDevice() can check if enough data is available
    *  at the device to reread the item.
    */
-  static void writeDataToDevice(QIODevice *dev, const QVariant &item);
+  static void writeDataToDevice(QIODevice *dev, const QVariant &item, bool compressed = false);
 
   //! Reads a data item from a device that has been written by writeDataToDevice().
   /** If not enough data bytes are available, the function returns false and the QVariant reference
    *  remains untouched.
    */
 
   //! Reads a data item from a device that has been written by writeDataToDevice().
   /** If not enough data bytes are available, the function returns false and the QVariant reference
    *  remains untouched.
    */
-  static bool readDataFromDevice(QIODevice *dev, quint32 &blockSize, QVariant &item);
+  static bool readDataFromDevice(QIODevice *dev, quint32 &blockSize, QVariant &item, bool compressed = false);
 
   static QString methodBaseName(const QMetaMethod &method);
 
 
   static QString methodBaseName(const QMetaMethod &method);
 
@@ -163,7 +163,13 @@ public:
   
 private:
   // Hash of used QIODevices
   
 private:
   // Hash of used QIODevices
-  QHash<QIODevice*, quint32> _peerByteCount;
+  struct peerInfo {
+    quint32 byteCount;
+    bool usesCompression;
+    peerInfo() : byteCount(0), usesCompression(false) {};
+  };
+  //QHash<QIODevice*, peerInfo> _peerByteCount;
+  QHash<QIODevice*, peerInfo> _peers;
 
   // containg a list of argtypes for fast access
   QHash<const QMetaObject *, ClassInfo*> _classInfo;
 
   // containg a list of argtypes for fast access
   QHash<const QMetaObject *, ClassInfo*> _classInfo;
index 1af8926..f413bbf 100644 (file)
@@ -401,9 +401,16 @@ void Core::processClientMessage(QTcpSocket *socket, const QVariantMap &msg) {
 #else
     bool supportSsl = false;
 #endif
 #else
     bool supportSsl = false;
 #endif
+
+#ifndef QT_NO_COMPRESS
+    bool supportsCompression = true;
+#else
+    bool supportsCompression = false;
+#endif
     
     reply["SupportSsl"] = supportSsl;
     
     reply["SupportSsl"] = supportSsl;
-    // switch to ssl after client has been informed about our capabilities (see below)
+    reply["SupportsCompression"] = supportsCompression;
+    // switch to ssl/compression after client has been informed about our capabilities (see below)
 
     reply["LoginEnabled"] = true;
 
 
     reply["LoginEnabled"] = true;
 
@@ -439,12 +446,19 @@ void Core::processClientMessage(QTcpSocket *socket, const QVariantMap &msg) {
 #ifndef QT_NO_OPENSSL
     // after we told the client that we are ssl capable we switch to ssl mode
     if(supportSsl && msg["UseSsl"].toBool()) {
 #ifndef QT_NO_OPENSSL
     // after we told the client that we are ssl capable we switch to ssl mode
     if(supportSsl && msg["UseSsl"].toBool()) {
-      qDebug() << "Starting TLS for Client:"  << qPrintable(socket->peerAddress().toString());
+      qDebug() << "Starting TLS for Client:" << qPrintable(socket->peerAddress().toString());
       connect(sslSocket, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(sslErrors(const QList<QSslError> &)));
       sslSocket->startServerEncryption();
     }
 #endif
 
       connect(sslSocket, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(sslErrors(const QList<QSslError> &)));
       sslSocket->startServerEncryption();
     }
 #endif
 
+#ifndef QT_NO_COMPRESS
+    if(supportsCompression && msg["UseCompression"].toBool()) {
+      socket->setProperty("UseCompression", true);
+      qDebug() << "Using compression for Client:" << qPrintable(socket->peerAddress().toString());
+    }
+#endif
+    
   } else {
     // for the rest, we need an initialized connection
     if(!clientInfo.contains(socket)) {
   } else {
     // for the rest, we need an initialized connection
     if(!clientInfo.contains(socket)) {