diff options
Diffstat (limited to 'src/server/serverapp/serverapp.cpp')
-rw-r--r-- | src/server/serverapp/serverapp.cpp | 95 |
1 files changed, 94 insertions, 1 deletions
diff --git a/src/server/serverapp/serverapp.cpp b/src/server/serverapp/serverapp.cpp index c645b39..3df996c 100644 --- a/src/server/serverapp/serverapp.cpp +++ b/src/server/serverapp/serverapp.cpp @@ -1,8 +1,10 @@ #include <QTranslator> - +#include <QNetworkInterface> #include "serverapp.h" +static QSize minimalGridSize(const QMap<QString, QPoint>& clientPositions, QSize& clientSize); + ServerApp::ServerApp(int& argc, char** argv) : QApplication(argc, argv), _mainWindow(NULL), @@ -15,6 +17,15 @@ ServerApp::ServerApp(int& argc, char** argv) _arguments = parseParameters(); + loadRooms(); + + // If started in manager-only mode, and there is no current room + // after reading the config, exit right away + if (_managerOnly && _currentRoom == "") { + ::exit(0); + return; + } + // System strings QTranslator *qtTranslator = new QTranslator(this); qtTranslator->load("qt_" + QLocale::system().name(), @@ -56,6 +67,68 @@ QStringList ServerApp::arguments() return _arguments; } +void ServerApp::loadRooms() +{ + QSharedPointer<QSettings> conf = getSettings(); + + if (!conf->contains("rooms")) { qDebug() << "Invalid config file (no rooms are set)!"; return; } + QStringList rooms = conf->value("rooms").toStringList(); + + for (QString roomId : rooms) { + conf->beginGroup(roomId); + QString roomName = conf->value("name").toString(); + + /* fallback to the old format where the room id was actually just the name */ + if (roomName == "") { + roomName = roomId; + } + if (!conf->contains("mgrIP")) { + qDebug() << "Warning: Incomplete config file (room " << roomName << " needs a mgrIP)!"; + } + QMap<QString, QPoint> clientPositions; + // First store all room configurations in _rooms. + int size = conf->beginReadArray("client"); + for (int j = 0; j < size; j++) { + conf->setArrayIndex(j); + clientPositions.insert(conf->value("ip").toString(), conf->value("pos").toPoint()); + } + conf->endArray(); + + /* read backgroundImage */ + QString image = conf->contains("backgroundImage") ? conf->value("backgroundImage").toString() : ""; + QString mgrIP = conf->value("mgrIP").toString(); + QString tutorIP = conf->value("tutorIP").toString(); + + QSize gridSize; + QSize clientSize(1, 1); + /* read some other properties of the room */ + if (conf->contains("gridSize")) { + gridSize = conf->value("gridSize").toSize(); + } + if (conf->contains("clientSize")) { + clientSize = conf->value("clientSize").toSize(); + } + + foreach (const QHostAddress & address, QNetworkInterface::allAddresses()) { + if (address != QHostAddress(QHostAddress::LocalHost) && mgrIP == address.toString()) { + qDebug("Found this ip in config."); + _currentRoom = roomName; + } + } + conf->endGroup(); + + if (!gridSize.isValid()) { + /* ok, let's choose the minimum gridSize to fit all clients */ + gridSize = minimalGridSize(clientPositions, clientSize); + qDebug() << "had to use minimalGridSize(): = " << gridSize; + + } + Room* r = new Room(clientPositions, gridSize, clientSize, image, tutorIP); + qDebug() << "read new room: " << roomName << ": " << gridSize << ", " << clientSize; + _rooms.insert(roomName, r); + } +} + const Room* ServerApp::getCurrentRoom() { if (_rooms.contains(_currentRoom)) { @@ -96,3 +169,23 @@ QSharedPointer<QSettings> ServerApp::getSettings() return set; } +/** + * returns the minimal grid size such that all clients fit on the grid + **/ +static QSize minimalGridSize(const QMap<QString, QPoint>& clientPositions, QSize& clientSize) +{ + /* collect the maximum coordinates */ + int x = 0; + int y = 0; + + for (auto it = clientPositions.begin(); it != clientPositions.end(); ++it) { + QPoint pos = it.value(); + if (pos.x() > x) { x = pos.x(); } + if (pos.y() > y) { y = pos.y(); } + + } + /* need a little extra space */ + QSize size(x + clientSize.width(), y + clientSize.height()); + return size; +} + |