Torc  0.1
torcnetworkedcontext.h
Go to the documentation of this file.
1 #ifndef TORCNETWORKEDCONTEXT_H
2 #define TORCNETWORKEDCONTEXT_H
3 
4 // Qt
5 #include <QObject>
6 #include <QReadWriteLock>
7 #include <QHostAddress>
8 
9 // Torc
10 #include "torchttpservice.h"
11 #include "torclocalcontext.h"
12 
13 class TorcRPCRequest;
14 class TorcNetworkRequest;
15 class TorcWebSocket;
17 class TorcHTTPRequest;
18 class QTcpSocket;
19 
20 class TorcNetworkService : public QObject
21 {
22  Q_OBJECT
23  Q_FLAGS(ServiceSource)
24 
25  public:
27  {
28  Spontaneous = (0 << 0),
29  Bonjour = (1 << 0),
30  UPnP = (1 << 1)
31  };
32 
33  Q_DECLARE_FLAGS(ServiceSources, ServiceSource)
34 
35  public:
36  static bool WeActAsServer (int Priority, qint64 StartTime, const QString &UUID);
37  TorcNetworkService(const QString &Name, const QString &UUID, int Port, bool Secure, const QList<QHostAddress> &Addresses);
39 
40  Q_PROPERTY (QString name READ GetName CONSTANT)
41  Q_PROPERTY (QString uuid READ GetUuid CONSTANT)
42  Q_PROPERTY (int port READ GetPort CONSTANT)
43  Q_PROPERTY (QString uiAddress READ GetAddress CONSTANT)
44  Q_PROPERTY (QString host READ GetHost CONSTANT)
45  Q_PROPERTY (qint64 startTime READ GetStartTime NOTIFY StartTimeChanged)
46  Q_PROPERTY (int priority READ GetPriority NOTIFY PriorityChanged)
47  Q_PROPERTY (QString apiVersion READ GetAPIVersion NOTIFY ApiVersionChanged)
48  Q_PROPERTY (bool connected READ GetConnected NOTIFY ConnectedChanged)
49 
50  signals:
51  void StartTimeChanged (void);
52  void PriorityChanged (void);
53  void ApiVersionChanged (void);
54  void ConnectedChanged (void);
55  void TryConnect (void);
56 
57  public slots:
58  QString GetName (void);
59  QString GetUuid (void);
60  int GetPort (void);
61  QString GetHost (void);
62  QList<QHostAddress> GetAddresses (void);
63  QString GetAddress (void);
64  qint64 GetStartTime (void);
65  int GetPriority (void);
66  QString GetAPIVersion (void);
67  bool GetConnected (void);
68 
69  void Connect (void);
70  void Connected (void);
71  void Disconnected (void);
72  void RequestReady (TorcNetworkRequest *Request);
73  void RequestReady (TorcRPCRequest *Request);
74 
75  public:
76  void SetHost (const QString &Host);
77  void SetStartTime (qint64 StartTime);
78  void SetPriority (int Priority);
79  void SetAPIVersion (const QString &Version);
80  void SetSource (ServiceSource Source);
81  ServiceSources GetSources (void);
82  void RemoveSource (ServiceSource Source);
84  void RemoteRequest (TorcRPCRequest *Request);
85  void CancelRequest (TorcRPCRequest *Request);
86  QVariant ToMap (void);
87 
88  private:
89  Q_DISABLE_COPY(TorcNetworkService)
90  void ScheduleRetry (void);
91  void QueryPeerDetails (void);
92 
93  private:
94  QString name;
95  QString uuid;
96  int port;
97  QString host;
98  bool secure;
99  QString uiAddress;
100  qint64 startTime;
101  int priority;
102  QString apiVersion;
103  bool connected;
104 
105  ServiceSources m_sources;
106  QString m_debugString;
107  QList<QHostAddress> m_addresses;
108  int m_preferredAddressIndex;
109  int m_abort;
110  TorcRPCRequest *m_getPeerDetailsRPC;
111  TorcNetworkRequest *m_getPeerDetails;
112  TorcWebSocketThread *m_webSocketThread;
113  bool m_retryScheduled;
114  int m_retryInterval;
115 };
116 
117 Q_DECLARE_METATYPE(TorcNetworkService*)
118 Q_DECLARE_OPERATORS_FOR_FLAGS(TorcNetworkService::ServiceSources)
119 
120 class TorcNetworkedContext: public QObject, public TorcHTTPService
121 {
123  friend class TorcNetworkService;
124 
125  Q_OBJECT
126  Q_CLASSINFO("Version", "1.0.0")
127  Q_CLASSINFO("GetPeers", "type=peers")
128  Q_PROPERTY(QVariantList peers READ GetPeers NOTIFY PeersChanged)
129 
130  public:
131  static void PeerConnected (TorcWebSocketThread* Thread, const QVariantMap &Data);
132  static void RemoteRequest (const QString &UUID, TorcRPCRequest *Request);
133  static void CancelRequest (const QString &UUID, TorcRPCRequest *Request, int Wait = 1000);
134 
135  // TorcHTTPService
136  QString GetUIName (void);
137 
138  signals:
139  void PeersChanged (void);
140  void PeerConnected (QString &Name, QString &UUID);
141  void PeerDisconnected (QString &Name, QString &UUID);
142  void NewPeer (TorcWebSocketThread* Socket, const QVariantMap &Data);
143  void NewRequest (const QString &UUID, TorcRPCRequest *Request);
144  void RequestCancelled (const QString &UUID, TorcRPCRequest *Request);
145 
146  public slots:
147  QVariantList GetPeers (void);
148  void SubscriberDeleted (QObject *Subscriber);
149 
150  protected slots:
151  void HandleNewPeer (TorcWebSocketThread *Thread, const QVariantMap &Data);
152  void HandleNewRequest (const QString &UUID, TorcRPCRequest *Request);
153  void HandleCancelRequest (const QString &UUID, TorcRPCRequest *Request);
154 
155  protected:
157  ~TorcNetworkedContext();
158 
159  void Connected (TorcNetworkService* Peer);
160  void Disconnected (TorcNetworkService* Peer);
161  bool event (QEvent* Event);
162 
163  private:
164  Q_DISABLE_COPY(TorcNetworkedContext)
165  void Add (TorcNetworkService* Peer);
166  void Remove (const QString &UUID, TorcNetworkService::ServiceSource Source = TorcNetworkService::Spontaneous);
167 
168  private:
169  QList<TorcNetworkService*> m_discoveredServices;
170  QList<QString> m_serviceList;
171  QVariantList peers; // dummy
172 };
173 
174 extern TorcNetworkedContext *gNetworkedContext;
175 
176 #endif // TORCNETWORKEDCONTEXT_H
void SetSource(ServiceSource Source)
A class to encapsulate an incoming HTTP request.
static bool WeActAsServer(int Priority, qint64 StartTime, const QString &UUID)
Determine whether we (the local device) should be the server for peer to peer communications.
void SetAPIVersion(const QString &Version)
void RemoteRequest(TorcRPCRequest *Request)
Overlays the Websocket protocol over a QTcpSocket.
Definition: torcwebsocket.h:23
void RemoveSource(ServiceSource Source)
void SetHost(const QString &Host)
QList< QHostAddress > GetAddresses(void)
A wrapper around QNetworkRequest.
void Connect(void)
Establish a WebSocket connection to the peer if necessary.
A class encapsulating a Remote Procedure Call.
void CancelRequest(TorcRPCRequest *Request)
Encapsulates information on a discovered Torc peer.
void PriorityChanged(void)
void SetPriority(int Priority)
void SetStartTime(qint64 StartTime)
void StartTimeChanged(void)
void ConnectedChanged(void)
void RequestReady(TorcNetworkRequest *Request)
void SetWebSocketThread(TorcWebSocketThread *Thread)
TorcNetworkedContext * gNetworkedContext
ServiceSources GetSources(void)
void ApiVersionChanged(void)
Wraps a TorcQThread around a TorcWebsocket.
void TryConnect(void)
A class to discover and connect to other Torc applications.