Stellarium  0.90.0
RemoteSync.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 REMOTESYNC_HPP_
21 #define REMOTESYNC_HPP_
22 
23 #include "StelModule.hpp"
24 #include "SyncClient.hpp"
25 #include "SyncServer.hpp"
26 
27 #include <QFont>
28 #include <QKeyEvent>
29 #include <QLoggingCategory>
30 #include <QTimer>
31 
32 class RemoteSyncDialog;
33 
34 Q_DECLARE_LOGGING_CATEGORY(remoteSync)
35 
36 class RemoteSync : public StelModule
40 {
41  Q_OBJECT
42  Q_ENUMS(SyncState ClientBehaviour)
43 
44 public:
45  enum SyncState
46  {
47  IDLE, //Plugin is disabled
48  SERVER, //Plugin is running as server
49  CLIENT, //Plugin is connected as a client to a server
50  CLIENT_CONNECTING, //Plugin is currently trying to connect to a server
51  CLIENT_CLOSING, //Plugin is disconnecting from the server
52  CLIENT_WAIT_RECONNECT //Plugin is waiting to try reconnecting again
53  };
54 
57  {
58  NONE, //do nothing
59  RECONNECT, //automatically try to reconnect
60  QUIT //quit the client
61  };
62 
63  RemoteSync();
64  virtual ~RemoteSync();
65 
67  // Methods defined in the StelModule class
68  virtual void init() Q_DECL_OVERRIDE;
69  virtual void update(double deltaTime) Q_DECL_OVERRIDE;
70 
71  virtual double getCallOrder(StelModuleActionName actionName) const Q_DECL_OVERRIDE;
72 
73  virtual bool configureGui(bool show=true) Q_DECL_OVERRIDE;
75 
76  QString getClientServerHost() const { return clientServerHost; }
77  int getClientServerPort() const { return clientServerPort; }
78  int getServerPort() const { return serverPort; }
79  SyncClient::SyncOptions getClientSyncOptions() const { return syncOptions; }
80  QStringList getStelPropFilter() const { return stelPropFilter; }
81  ClientBehavior getConnectionLostBehavior() const { return connectionLostBehavior; }
82  ClientBehavior getQuitBehavior() const { return quitBehavior; }
83 
84  SyncState getState() const { return state; }
85 
86 public slots:
87  void setClientServerHost(const QString& clientServerHost);
88  void setClientServerPort(const int port);
89  void setServerPort(const int port);
90  void setClientSyncOptions(SyncClient::SyncOptions options);
91  void setStelPropFilter(const QStringList& stelPropFilter);
92  void setConnectionLostBehavior(const ClientBehavior bh);
93  void setQuitBehavior(const ClientBehavior bh);
94 
97  void startServer();
98 
101  void stopServer();
102 
105  void connectToServer();
106 
109  void disconnectFromServer();
110 
116  void loadSettings();
117 
120  void saveSettings();
121 
126  void restoreDefaultSettings();
127 
128 signals:
129  void errorOccurred(const QString& errorString);
130  void clientServerHostChanged(const QString& clientServerHost);
131  void clientServerPortChanged(const int port);
132  void serverPortChanged(const int port);
133  void clientSyncOptionsChanged(const SyncClient::SyncOptions options);
134  void stelPropFilterChanged(const QStringList& stelPropFilter);
135  void connectionLostBehaviorChanged(const ClientBehavior bh);
136  void quitBehaviorChanged(const ClientBehavior bh);
137 
138  void stateChanged(RemoteSync::SyncState state);
139 
140 private slots:
141  void clientDisconnected(bool clean);
142  void clientConnected();
143 private:
144  void setState(RemoteSync::SyncState state);
145  void setError(const QString& errorString);
146 
147  SyncState applyClientBehavior(ClientBehavior bh);
148 
149  static QString packStringList(const QStringList props);
150  static QStringList unpackStringList(const QString packedProps);
151 
152  //The host string/IP addr to connect to
153  QString clientServerHost;
154  //The host port to connect to
155  int clientServerPort;
156  //the port used in server mode
157  int serverPort;
158  SyncClient::SyncOptions syncOptions;
159  QStringList stelPropFilter;
160  ClientBehavior connectionLostBehavior;
161  ClientBehavior quitBehavior;
162 
163  QTimer reconnectTimer;
164 
165  SyncState state;
166  SyncServer* server;
167  SyncClient* client;
168 
169  // the last error that occurred
170  QString errorString;
171 
172  QSettings* conf;
173 
174  // GUI
175  RemoteSyncDialog* configDialog;
176 };
177 
178 Q_DECLARE_METATYPE(RemoteSync::SyncState)
179 
180 QDebug operator<<(QDebug, RemoteSync::SyncState);
182 
183 #include "StelPluginInterface.hpp"
184 
187 {
188  Q_OBJECT
189  Q_PLUGIN_METADATA(IID StelPluginInterface_iid)
190  Q_INTERFACES(StelPluginInterface)
191 public:
192  virtual StelModule* getStelModule() const;
193  virtual StelPluginInfo getPluginInfo() const;
194  virtual QObjectList getExtensionList() const { return QObjectList(); }
195 };
196 
197 #endif /*REMOTESYNC_HPP_*/
198 
A client which can connect to a SyncServer to receive state changes, and apply them.
Definition: SyncClient.hpp:33
Define the interface to implement when creating a plugin.
virtual StelPluginInfo getPluginInfo() const
Get information about the plugin.
Main class of the RemoteSync plug-in.
Definition: RemoteSync.hpp:39
This class is used by Qt to manage a plug-in interface.
Definition: RemoteSync.hpp:186
ClientBehavior
Defines behavior when client connection is lost/server quits.
Definition: RemoteSync.hpp:56
QDataStream & operator<<(QDataStream &out, const SphericalRegionP &region)
Serialize the passed SphericalRegionP into a binary blob.
StelModuleActionName
Define the possible action for which an order is defined.
Definition: StelModule.hpp:121
virtual StelModule * getStelModule() const
Get the instance of StelModule to include in the list of standard StelModule.
This is the common base class for all the main components of stellarium.
Definition: StelModule.hpp:49
Contains information about a Stellarium plugin.
Implements a server to which SyncClients can connect and receive state changes.
Definition: SyncServer.hpp:36
virtual QObjectList getExtensionList() const
A mechanism to provide abitrary QObjects to the StelModuleMgr.
Definition: RemoteSync.hpp:194