Stellarium 0.15.2
SyncServer.hpp
1 /*
2  * Stellarium Remote Sync plugin
3  * Copyright (C) 2015 Florian Schaukowitsch
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (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 Free Software
17  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18  */
19 
20 #ifndef SYNCSERVER_HPP_
21 #define SYNCSERVER_HPP_
22 
23 #include "SyncProtocol.hpp"
24 #include <QObject>
25 #include <QAbstractSocket>
26 #include <QDateTime>
27 #include <QUuid>
28 
29 class QTcpServer;
31 
33 class SyncServer : public QObject
34 {
35  Q_OBJECT
36 
37 public:
38  SyncServer(QObject* parent = 0);
39  virtual ~SyncServer();
40 
42  void update();
43 
45  void broadcastMessage(const SyncMessage& msg);
46 public slots:
49  bool start(int port);
51  void stop();
53  QString errorString() const;
54 
55 protected:
56  void timerEvent(QTimerEvent* evt) Q_DECL_OVERRIDE;
57 private slots:
58  void handleNewConnection();
59  void connectionError(QAbstractSocket::SocketError err);
60 
62  void clientDataReceived();
63  void clientError(QAbstractSocket::SocketError);
64  void clientAuthenticated(SyncRemotePeer& peer);
65  void clientDisconnected();
66 
67 private:
68  void addSender(SyncServerEventSender* snd);
69  void checkTimeouts();
70  void clientLog(QAbstractSocket* cl, const QString& msg);
71  //use composition instead of inheritance, cleaner interfaace this way
72  //for now, we use TCP, but will test multicast UDP later if the basic setup is working
73  QTcpServer* qserver;
74  QVector<SyncMessageHandler*> handlerList;
75  QVector<SyncServerEventSender*> senderList;
76 
77  // a map to associate sockets with the client data
78  typedef QMap<QAbstractSocket*,SyncRemotePeer> tClientMap;
79  tClientMap clients;
80 
81  QByteArray broadcastBuffer;
82  int timeoutTimerId;
83  friend class ServerAuthHandler;
84 };
85 
86 #endif
QString errorString() const
Returns a string of the last server error.
Server-side auth handler.
Handling the connection to a remote peer (i.e. all clients on the server, and the server on the clien...
Subclasses of this class notify clients of state changes.
void stop()
Disconnects all clients, and stops listening.
Base interface for the messages themselves, allowing to serialize/deserialize them.
void broadcastMessage(const SyncMessage &msg)
Broadcasts this message to all connected and authenticated clients.
void update()
This should be called in the StelModule::update function.
bool start(int port)
Starts the SyncServer on the specified port.
Implements a server to which SyncClients can connect and receive state changes.
Definition: SyncServer.hpp:33