properly handling disconnects - this might even fix an antique bug with duplicate...
authorMarcus Eggenberger <egs@quassel-irc.org>
Tue, 21 Oct 2008 13:49:15 +0000 (15:49 +0200)
committerMarcus Eggenberger <egs@quassel-irc.org>
Tue, 21 Oct 2008 16:52:53 +0000 (18:52 +0200)
src/client/client.cpp
src/client/client.h
src/client/clientsyncer.cpp
src/core/coresession.cpp
src/qtui/monoapplication.cpp
src/qtui/monoapplication.h

index 9611f71..b17fa28 100644 (file)
@@ -65,7 +65,6 @@ void Client::init(AbstractUi *ui) {
 
 Client::Client(QObject *parent)
   : QObject(parent),
 
 Client::Client(QObject *parent)
   : QObject(parent),
-    socket(0),
     _signalProxy(new SignalProxy(SignalProxy::Client, this)),
     mainUi(0),
     _networkModel(0),
     _signalProxy(new SignalProxy(SignalProxy::Client, this)),
     mainUi(0),
     _networkModel(0),
@@ -116,7 +115,7 @@ void Client::init() {
   p->attachSlot(SIGNAL(networkCreated(NetworkId)), this, SLOT(coreNetworkCreated(NetworkId)));
   p->attachSlot(SIGNAL(networkRemoved(NetworkId)), this, SLOT(coreNetworkRemoved(NetworkId)));
 
   p->attachSlot(SIGNAL(networkCreated(NetworkId)), this, SLOT(coreNetworkCreated(NetworkId)));
   p->attachSlot(SIGNAL(networkRemoved(NetworkId)), this, SLOT(coreNetworkRemoved(NetworkId)));
 
-  connect(p, SIGNAL(disconnected()), this, SLOT(disconnectFromCore()));
+  connect(p, SIGNAL(disconnected()), this, SLOT(disconnectedFromCore()));
 
   //connect(mainUi, SIGNAL(connectToCore(const QVariantMap &)), this, SLOT(connectToCore(const QVariantMap &)));
   connect(mainUi, SIGNAL(disconnectFromCore()), this, SLOT(disconnectFromCore()));
 
   //connect(mainUi, SIGNAL(connectToCore(const QVariantMap &)), this, SLOT(connectToCore(const QVariantMap &)));
   connect(mainUi, SIGNAL(disconnectFromCore()), this, SLOT(disconnectFromCore()));
@@ -252,13 +251,20 @@ void Client::userInput(BufferInfo bufferInfo, QString message) {
 
 /*** core connection stuff ***/
 
 
 /*** core connection stuff ***/
 
-void Client::setConnectedToCore(QIODevice *sock, AccountId id) {
-  socket = sock;
+void Client::setConnectedToCore(QIODevice *socket, AccountId id) {
+  // if the socket is an orphan, the signalProxy adopts it.
+  // -> we don't need to care about it anymore
+  socket->setParent(0);
   signalProxy()->addPeer(socket);
   _connectedToCore = true;
   setCurrentCoreAccount(id);
 }
 
   signalProxy()->addPeer(socket);
   _connectedToCore = true;
   setCurrentCoreAccount(id);
 }
 
+void Client::setConnectedToInternalCore() {
+  _connectedToCore = true;
+  setCurrentCoreAccount(AccountId());
+}
+
 void Client::setSyncedToCore() {
   // create buffersyncer
   Q_ASSERT(!_bufferSyncer);
 void Client::setSyncedToCore() {
   // create buffersyncer
   Q_ASSERT(!_bufferSyncer);
@@ -286,12 +292,12 @@ void Client::setSecuredConnection() {
 void Client::disconnectFromCore() {
   if(!isConnected())
     return;
 void Client::disconnectFromCore() {
   if(!isConnected())
     return;
-  _connectedToCore = false;
 
 
-  if(socket) {
-    socket->close();
-    socket->deleteLater();
-  }
+  signalProxy()->removeAllPeers();
+}
+
+void Client::disconnectedFromCore() {
+  _connectedToCore = false;
   _syncedToCore = false;
   emit disconnected();
   emit coreConnectionStateChanged(false);
   _syncedToCore = false;
   emit disconnected();
   emit coreConnectionStateChanged(false);
@@ -336,10 +342,6 @@ void Client::disconnectFromCore() {
 
 }
 
 
 }
 
-void Client::setCoreConfiguration(const QVariantMap &settings) {
-  SignalProxy::writeDataToDevice(socket, settings);
-}
-
 /*** ***/
 
 void Client::networkDestroyed() {
 /*** ***/
 
 void Client::networkDestroyed() {
index 2d073c5..b8faf9f 100644 (file)
@@ -154,16 +154,11 @@ public slots:
 
   void disconnectFromCore();
 
 
   void disconnectFromCore();
 
-  void setCoreConfiguration(const QVariantMap &settings);
-
   void bufferRemoved(BufferId bufferId);
   void bufferRenamed(BufferId bufferId, const QString &newName);
 
 private slots:
   void bufferRemoved(BufferId bufferId);
   void bufferRenamed(BufferId bufferId, const QString &newName);
 
 private slots:
-  //void coreSocketError(QAbstractSocket::SocketError);
-
-  //void networkConnected(NetworkId);
-  //void networkDisconnected(NetworkId);
+  void disconnectedFromCore();
 
   void recvMessage(const Message &message);
   void recvStatusMsg(QString network, QString message);
 
   void recvMessage(const Message &message);
   void recvStatusMsg(QString network, QString message);
@@ -175,6 +170,7 @@ private slots:
   void coreNetworkRemoved(NetworkId);
 
   void setConnectedToCore(QIODevice *socket, AccountId id);
   void coreNetworkRemoved(NetworkId);
 
   void setConnectedToCore(QIODevice *socket, AccountId id);
+  void setConnectedToInternalCore();
   void setSyncedToCore();
   void setSecuredConnection();
 
   void setSyncedToCore();
   void setSecuredConnection();
 
@@ -190,8 +186,6 @@ private:
 
   static QPointer<Client> instanceptr;
 
 
   static QPointer<Client> instanceptr;
 
-  QPointer<QIODevice> socket;
-
   SignalProxy * _signalProxy;
   AbstractUi * mainUi;
   NetworkModel * _networkModel;
   SignalProxy * _signalProxy;
   AbstractUi * mainUi;
   NetworkModel * _networkModel;
index a9d5972..ed59b99 100644 (file)
@@ -248,7 +248,7 @@ void ClientSyncer::loginToCore(const QString &user, const QString &passwd) {
 void ClientSyncer::internalSessionStateReceived(const QVariant &packedState) {
   QVariantMap state = packedState.toMap();
   emit sessionProgress(1, 1);
 void ClientSyncer::internalSessionStateReceived(const QVariant &packedState) {
   QVariantMap state = packedState.toMap();
   emit sessionProgress(1, 1);
-  // Client::instance()->setConnectedToCore(socket, AccountId());
+  Client::instance()->setConnectedToInternalCore();
   syncToCore(state);
 }
 
   syncToCore(state);
 }
 
@@ -256,13 +256,11 @@ void ClientSyncer::sessionStateReceived(const QVariantMap &state) {
   emit sessionProgress(1, 1);
   disconnect(this, SIGNAL(recvPartialItem(quint32, quint32)), this, SIGNAL(sessionProgress(quint32, quint32)));
   disconnect(socket, 0, this, 0);  // rest of communication happens through SignalProxy
   emit sessionProgress(1, 1);
   disconnect(this, SIGNAL(recvPartialItem(quint32, quint32)), this, SIGNAL(sessionProgress(quint32, quint32)));
   disconnect(socket, 0, this, 0);  // rest of communication happens through SignalProxy
-  //Client::signalProxy()->addPeer(socket);
   Client::instance()->setConnectedToCore(socket, coreConnectionInfo["AccountId"].value<AccountId>());
   syncToCore(state);
 }
 
 void ClientSyncer::syncToCore(const QVariantMap &sessionState) {
   Client::instance()->setConnectedToCore(socket, coreConnectionInfo["AccountId"].value<AccountId>());
   syncToCore(state);
 }
 
 void ClientSyncer::syncToCore(const QVariantMap &sessionState) {
-
   // create identities
   foreach(QVariant vid, sessionState["Identities"].toList()) {
     Client::instance()->coreIdentityCreated(vid.value<Identity>());
   // create identities
   foreach(QVariant vid, sessionState["Identities"].toList()) {
     Client::instance()->coreIdentityCreated(vid.value<Identity>());
index f287ac2..020950f 100644 (file)
@@ -233,6 +233,9 @@ void CoreSession::addClient(QIODevice *device) {
   if(!device) {
     quError() << "Invoking CoreSession::addClient with a QObject that is not a QIODevice!";
   } else {
   if(!device) {
     quError() << "Invoking CoreSession::addClient with a QObject that is not a QIODevice!";
   } else {
+    // if the socket is an orphan, the signalProxy adopts it.
+    // -> we don't need to care about it anymore
+    device->setParent(0);
     signalProxy()->addPeer(device);
     QVariantMap reply;
     reply["MsgType"] = "SessionInit";
     signalProxy()->addPeer(device);
     QVariantMap reply;
     reply["MsgType"] = "SessionInit";
@@ -250,7 +253,6 @@ void CoreSession::removeClient(QIODevice *iodev) {
   QTcpSocket *socket = qobject_cast<QTcpSocket *>(iodev);
   if(socket)
     quInfo() << qPrintable(tr("Client")) << qPrintable(socket->peerAddress().toString()) << qPrintable(tr("disconnected (UserId: %1).").arg(user().toInt()));
   QTcpSocket *socket = qobject_cast<QTcpSocket *>(iodev);
   if(socket)
     quInfo() << qPrintable(tr("Client")) << qPrintable(socket->peerAddress().toString()) << qPrintable(tr("disconnected (UserId: %1).").arg(user().toInt()));
-  iodev->deleteLater();
 }
 
 SignalProxy *CoreSession::signalProxy() const {
 }
 
 SignalProxy *CoreSession::signalProxy() const {
index 7b05c73..b628079 100644 (file)
@@ -26,7 +26,8 @@
 #include "qtui.h"
 
 MonolithicApplication::MonolithicApplication(int &argc, char **argv)
 #include "qtui.h"
 
 MonolithicApplication::MonolithicApplication(int &argc, char **argv)
-  : QtUiApplication(argc, argv)
+  : QtUiApplication(argc, argv),
+    _internalInitDone(false)
 {
   _internal = new CoreApplicationInternal(); // needed for parser options
   setRunMode(Monolithic);
 {
   _internal = new CoreApplicationInternal(); // needed for parser options
   setRunMode(Monolithic);
@@ -34,10 +35,7 @@ MonolithicApplication::MonolithicApplication(int &argc, char **argv)
 
 bool MonolithicApplication::init() {
   connect(Client::instance(), SIGNAL(newClientSyncer(ClientSyncer *)), this, SLOT(newClientSyncer(ClientSyncer *)));
 
 bool MonolithicApplication::init() {
   connect(Client::instance(), SIGNAL(newClientSyncer(ClientSyncer *)), this, SLOT(newClientSyncer(ClientSyncer *)));
-  if(QtUiApplication::init()) {
-    return true;
-  }
-  return false;
+  return QtUiApplication::init();
 }
 
 MonolithicApplication::~MonolithicApplication() {
 }
 
 MonolithicApplication::~MonolithicApplication() {
@@ -51,7 +49,9 @@ void MonolithicApplication::newClientSyncer(ClientSyncer *syncer) {
 }
 
 void MonolithicApplication::startInternalCore() {
 }
 
 void MonolithicApplication::startInternalCore() {
-  _internal->init();
+  if(!_internalInitDone) {
+    _internal->init();
+  }
   Core *core = Core::instance();
   ClientSyncer *syncer = static_cast<ClientSyncer *>(sender());
   connect(syncer, SIGNAL(connectToInternalCore(SignalProxy *)), core, SLOT(setupInternalClientSession(SignalProxy *)));
   Core *core = Core::instance();
   ClientSyncer *syncer = static_cast<ClientSyncer *>(sender());
   connect(syncer, SIGNAL(connectToInternalCore(SignalProxy *)), core, SLOT(setupInternalClientSession(SignalProxy *)));
index ff35ddd..9b49403 100644 (file)
@@ -40,6 +40,7 @@ private slots:
 
 private:
   CoreApplicationInternal *_internal;
 
 private:
   CoreApplicationInternal *_internal;
+  bool _internalInitDone;
 };
 
 #endif
 };
 
 #endif