After quite a while, we have another big SVN update now.
[quassel.git] / core / coreproxy.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005/06 by The Quassel Team                             *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include <QDebug>
22
23 #include "coreproxy.h"
24 #include "global.h"
25 #include "util.h"
26 #include "core.h"
27
28 CoreProxy::CoreProxy() {
29   if(coreProxy) qFatal("Trying to instantiate more than one CoreProxy object!");
30   coreProxy = this;
31   core = new Core();
32
33   connect(global, SIGNAL(dataPutLocally(QString)), this, SLOT(updateGlobalData(QString)));
34   connect(&server, SIGNAL(newConnection()), this, SLOT(incomingConnection()));
35   if(!server.listen(QHostAddress::Any, 4242)) {
36     qFatal(QString(QString("Could not open GUI client port %1: %2").arg(4242).arg(server.errorString())).toAscii());
37   }
38   qDebug() << "Listening for GUI clients on port" << server.serverPort() << ".";
39 }
40
41 void CoreProxy::incomingConnection() {
42   QTcpSocket *socket = server.nextPendingConnection();
43   connect(socket, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
44   connect(socket, SIGNAL(readyRead()), this, SLOT(clientHasData()));
45   clients.append(socket);
46   blockSizes.insert(socket, (quint32)0);
47   qDebug() << "Client connected from " << socket->peerAddress().toString();
48 }
49
50 void CoreProxy::clientHasData() {
51   QTcpSocket *socket = dynamic_cast<QTcpSocket*>(sender());
52   Q_ASSERT(socket && blockSizes.contains(socket));
53   quint32 bsize = blockSizes.value(socket);
54   QVariant item;
55   while(readDataFromDevice(socket, bsize, item)) {
56     QList<QVariant> sigdata = item.toList();
57     Q_ASSERT(sigdata.size() == 4);
58     switch((GUISignal)sigdata[0].toInt()) {
59       case GS_CLIENT_INIT:  processClientInit(socket, sigdata[1]); break;
60       case GS_UPDATE_GLOBAL_DATA: processClientUpdate(socket, sigdata[1].toString(), sigdata[2]); break;
61       //case GS_CLIENT_READY: processClientReady(sigdata[1], sigdata[2], sigdata[3]); break;
62       default: recv((GUISignal)sigdata[0].toInt(), sigdata[1], sigdata[2], sigdata[3]); break;
63     }
64     blockSizes[socket] = bsize = 0;
65   }
66   blockSizes[socket] = bsize;
67 }
68
69 void CoreProxy::clientDisconnected() {
70   QTcpSocket *socket = dynamic_cast<QTcpSocket*>(sender());
71   blockSizes.remove(socket);
72   clients.removeAll(socket);
73   qDebug() << "Client disconnected.";
74 }
75
76 void CoreProxy::processClientInit(QTcpSocket *socket, const QVariant &v) {
77   VarMap msg = v.toMap();
78   if(msg["GUIProtocol"].toUInt() != GUI_PROTOCOL) {
79     qDebug() << "Client version mismatch. Disconnecting.";
80     socket->close();
81     return;
82   }
83   VarMap reply;
84   VarMap coreData;
85   QStringList dataKeys = global->getKeys();
86   QString key;
87   foreach(key, dataKeys) {
88     coreData[key] = global->getData(key);
89   }
90   reply["CoreData"] = coreData;
91   VarMap bl;
92   QHash<QString, QList<Message> > log = core->getBackLog();
93   foreach(QString net, log.keys()) {
94     QByteArray buf;
95     QDataStream out(&buf, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_2);
96     foreach(Message msg, log[net]) { out << msg; }
97     bl[net] = buf;
98   }
99   reply["CoreBackLog"] = bl;
100   QList<QVariant> sigdata;
101   sigdata.append(CS_CORE_STATE); sigdata.append(QVariant(reply)); sigdata.append(QVariant()); sigdata.append(QVariant());
102   writeDataToDevice(socket, QVariant(sigdata));
103   emit requestServerStates();
104 }
105
106 void CoreProxy::processClientUpdate(QTcpSocket *socket, QString key, QVariant data) {
107   global->updateData(key, data);
108   QList<QVariant> sigdata;
109   sigdata.append(CS_UPDATE_GLOBAL_DATA); sigdata.append(key); sigdata.append(data); sigdata.append(QVariant());
110   QTcpSocket *s;
111   foreach(s, clients) {
112     if(s != socket) writeDataToDevice(s, QVariant(sigdata));
113   }
114 }
115
116 void CoreProxy::updateGlobalData(QString key) {
117   QVariant data = global->getData(key);
118   emit csUpdateGlobalData(key, data);
119 }
120
121 void CoreProxy::send(CoreSignal sig, QVariant arg1, QVariant arg2, QVariant arg3) {
122   sendToGUI(sig, arg1, arg2, arg3);
123   QList<QVariant> sigdata;
124   sigdata.append(sig); sigdata.append(arg1); sigdata.append(arg2); sigdata.append(arg3);
125   //qDebug() << "Sending signal: " << sigdata;
126   QTcpSocket *socket;
127   foreach(socket, clients) {
128     writeDataToDevice(socket, QVariant(sigdata));
129   }
130 }
131
132 void CoreProxy::recv(GUISignal sig, QVariant arg1, QVariant arg2, QVariant arg3) {
133   //qDebug() << "[CORE] Received signal" << sig << ":" << arg1<<arg2<<arg3;
134   switch(sig) {
135     case GS_UPDATE_GLOBAL_DATA: emit gsPutGlobalData(arg1.toString(), arg2); break;
136     case GS_USER_INPUT: emit gsUserInput(arg1.toString(), arg2.toString(), arg3.toString()); break;
137     case GS_REQUEST_CONNECT: emit gsRequestConnect(arg1.toStringList()); break;
138     default: qWarning() << "Unknown signal in CoreProxy::recv: " << sig;
139   }
140 }
141
142 CoreProxy *coreProxy;