server accepts connections
authorDavid Solbach <d@vidsolbach.de>
Sun, 14 Nov 2010 07:05:16 +0000 (08:05 +0100)
committerDavid Solbach <d@vidsolbach.de>
Sun, 14 Nov 2010 07:05:16 +0000 (08:05 +0100)
buliscores-server/buliscores-server.pro
buliscores-server/buliscoressrv.cpp
buliscores-server/buliscoressrv.h
buliscores-server/clientconn.cpp [new file with mode: 0644]
buliscores-server/clientconn.h [new file with mode: 0644]
buliscores-server/main.cpp

index 611fd92..179909d 100644 (file)
@@ -17,7 +17,9 @@ TEMPLATE = app
 
 
 SOURCES += main.cpp \
-    buliscoressrv.cpp
+    buliscoressrv.cpp \
+    clientconn.cpp
 
 HEADERS += \
-    buliscoressrv.h
+    buliscoressrv.h \
+    clientconn.h
index 05e26d6..2b36d84 100644 (file)
  */
 
 #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();
+}
+
index 0f8aa82..4b06aa3 100644 (file)
 
 #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:
 
 };
diff --git a/buliscores-server/clientconn.cpp b/buliscores-server/clientconn.cpp
new file mode 100644 (file)
index 0000000..ee9db62
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * 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();
+    }
+}
diff --git a/buliscores-server/clientconn.h b/buliscores-server/clientconn.h
new file mode 100644 (file)
index 0000000..3fa9318
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * 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
index 0ae31c6..366d3bc 100644 (file)
@@ -3,11 +3,13 @@
 #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;
 
@@ -53,7 +55,16 @@ void messageHandler(QtMsgType type, const char *msg)
 
 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();
 }