SOURCES += main.cpp \
- buliscoressrv.cpp
+ buliscoressrv.cpp \
+ clientconn.cpp
HEADERS += \
- buliscoressrv.h
+ buliscoressrv.h \
+ clientconn.h
*/
#include "buliscoressrv.h"
+#include <QtCore/QCoreApplication>
BuliScoresSrv::BuliScoresSrv(QObject *parent) :
- QObject(parent)
+ QObject(parent),
+ m_Settings(qApp->organizationName(), qApp->applicationName())
{
+ int port;
+
+ port = m_Settings.value("port", 1959).toInt();
+ this->m_tcpServer.listen(QHostAddress::Any, port);
+ qDebug() << "Server started listening on port: " << port;
+
+ connect(&m_tcpServer, SIGNAL(newConnection()), this, SLOT(onNewConnection()));
+
}
+
+void BuliScoresSrv::onNewConnection()
+{
+ QTcpSocket* sock;
+ ClientConn* conn;
+
+
+ while ((sock = m_tcpServer.nextPendingConnection()) != NULL) {
+ conn = new ClientConn(sock, this);
+
+ connect(conn, SIGNAL(disconnected()), this, SLOT(onClientDisconnected()));
+ m_Clients.append(conn);
+ qDebug() << "New Client: " << sock->peerAddress().toString()
+ << "\tNumber of clients: " << m_Clients.count();
+ }
+}
+
+void BuliScoresSrv::onClientDisconnected()
+{
+ m_Clients.removeOne((ClientConn*)QObject::sender());
+ qDebug() << "Client disconnected, number of clients: " << m_Clients.count();
+ delete QObject::sender();
+}
+
#include <QObject>
#include <QTcpServer>
+#include <QSettings>
+
+#include "clientconn.h"
class BuliScoresSrv : public QObject
{
Q_OBJECT
private:
- QTcpServer m_tcpServer;
+ QTcpServer m_tcpServer;
+ QSettings m_Settings;
+ QList<ClientConn*> m_Clients;
public:
explicit BuliScoresSrv(QObject *parent = 0);
signals:
+private slots:
+ void onNewConnection();
+ void onClientDisconnected();
+
public slots:
};
--- /dev/null
+/*
+ * Copyright (C) 2010 David Solbach <d@vidsolbach.de>
+ *
+ * This file is part of BuliScores.
+ *
+ * BuliScores is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * BiliScores is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this program; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include <QHostAddress>
+
+#include "clientconn.h"
+
+ClientConn::ClientConn(QTcpSocket* socket, QObject *parent) :
+ QObject(parent)
+{
+ m_Socket = socket;
+
+ connect(socket, SIGNAL(disconnected()), this, SLOT(onSocketDisconnected()));
+}
+
+void ClientConn::onSocketDisconnected()
+{
+ qDebug() << "Socket disconnected: " << m_Socket->peerAddress().toString();
+ emit disconnected();
+}
+
+void ClientConn::triggerUpdate()
+{
+ if (m_Socket->isWritable()) {
+ m_Socket->write("u");
+ } else {
+ qWarning() << "Update trigger msg not send, socket state: " << m_Socket->state();
+ }
+}
--- /dev/null
+/*
+ * Copyright (C) 2010 David Solbach <d@vidsolbach.de>
+ *
+ * This file is part of BuliScores.
+ *
+ * BuliScores is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * BiliScores is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this program; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef CLIENTCONN_H
+#define CLIENTCONN_H
+
+#include <QObject>
+#include <QTcpSocket>
+
+class ClientConn : public QObject
+{
+ Q_OBJECT
+private:
+ QTcpSocket* m_Socket;
+
+public:
+ explicit ClientConn(QTcpSocket* socket, QObject *parent = 0);
+
+signals:
+ void disconnected();
+
+private slots:
+ void onSocketDisconnected();
+
+public slots:
+ void triggerUpdate();
+
+};
+
+#endif // CLIENTCONN_H
#include <QFile>
#include <QTextStream>
+#include "buliscoressrv.h"
+
void messageHandler(QtMsgType type, const char *msg)
{
static QFile logfile;
static QTextStream fw;
- static const QString LOGFILE_PATH = "/tmp/buliscores.log";
+ static const QString LOGFILE_PATH = "/tmp/buliscores-srv.log";
static const QtMsgType LOGLEVEL = QtDebugMsg;
QString out;
int main(int argc, char *argv[])
{
- QCoreApplication a(argc, argv);
+ QCoreApplication app(argc, argv);
+ app.setApplicationName("Buli Scores");
+ app.setApplicationVersion("0.1");
+ app.setOrganizationName("David Solbach");
+
+ BuliScoresSrv buliscoressrv;
+
+ app.setApplicationName("Buli Scores Server");
+ app.setApplicationVersion("0.1");
+ app.setOrganizationName("David Solbach");
- return a.exec();
+ return app.exec();
}