Torc  0.1
torcnetwork.h
Go to the documentation of this file.
1 #ifndef TORCNETWORK_H
2 #define TORCNETWORK_H
3 
4 // Qt
5 #include <QObject>
6 #include <QtNetwork>
7 #include <QAtomicInt>
8 
9 // Torc
10 #include "torcsetting.h"
11 #include "torctimer.h"
12 #include "torcnetworkrequest.h"
13 
14 #define DEFAULT_MAC_ADDRESS QString("00:00:00:00:00:00")
15 #define DEFAULT_STREAMED_BUFFER_SIZE (1024 * 1024 * 10)
16 #define DEFAULT_STREAMED_READ_SIZE (32768)
17 #define DEFAULT_MAX_REDIRECTIONS 3
18 #define DEFAULT_USER_AGENT QByteArray("Wget/1.12 (linux-gnu))")
19 
20 class TorcNetworkRequest;
21 
22 class TorcNetwork : public QNetworkAccessManager
23 {
24  friend class TorcNetworkObject;
25 
26  Q_OBJECT
27 
28  public:
29  // Public API
30  static bool IsAvailable (void);
31  static bool IsOwnAddress (const QHostAddress &Address);
32  static bool Get (TorcNetworkRequest* Request);
33  static void Cancel (TorcNetworkRequest* Request);
34  static void Poke (TorcNetworkRequest* Request);
35  static bool GetAsynchronous (TorcNetworkRequest* Request, QObject *Parent);
36  static void AddHostName (const QString &Host);
37  static void RemoveHostName (const QString &Host);
38  static QStringList GetHostNames (void);
39 
40  static QString IPAddressToLiteral (const QHostAddress& Address, int Port, bool UseLocalhost = true);
41  static bool IsExternal (const QHostAddress &Address);
42  static bool IsLinkLocal (const QHostAddress &Address);
43  static bool IsLocal (const QHostAddress &Address);
44  static bool IsGlobal (const QHostAddress &Address);
45  static QList<QSslError> AllowableSslErrors (const QList<QSslError> &Errors);
46 
47  signals:
48  void NewRequest (TorcNetworkRequest* Request);
49  void CancelRequest (TorcNetworkRequest* Request);
50  void PokeRequest (TorcNetworkRequest* Request);
51  void NewAsyncRequest (TorcNetworkRequest* Request, QObject *Parent);
52 
53  protected slots:
54  // QNetworkConfigurationManager
55  void ConfigurationAdded (const QNetworkConfiguration &Config);
56  void ConfigurationChanged (const QNetworkConfiguration &Config);
57  void ConfigurationRemoved (const QNetworkConfiguration &Config);
58  void OnlineStateChanged (bool Online);
59  void UpdateCompleted (void);
60 
61  // QNetworkReply
62  void ReadyRead (void);
63  void Finished (void);
64  void Error (QNetworkReply::NetworkError Code);
65  void SSLErrors (const QList<QSslError> &Errors);
66  void DownloadProgress (qint64 Received, qint64 Total);
67 
68  // QNetworkAccessManager
69  void Authenticate (QNetworkReply* Reply, QAuthenticator* Authenticator);
70 
71  // QHostInfo
72  void NewHostName (const QHostInfo &Info);
73 
74  private slots:
75  // Torc
76  void GetSafe (TorcNetworkRequest* Request);
77  void CancelSafe (TorcNetworkRequest* Request);
78  void PokeSafe (TorcNetworkRequest* Request);
79  void GetAsynchronousSafe (TorcNetworkRequest* Request, QObject *Parent);
80 
81  public:
82  virtual ~TorcNetwork();
83 
84  protected:
85  static void Setup (bool Create);
86 
87  protected:
88  TorcNetwork();
89  bool IsOnline (void);
90  bool IsOwnAddressPriv (const QHostAddress &Address);
91  bool CheckHeaders (TorcNetworkRequest* Request, QNetworkReply *Reply);
92  bool Redirected (TorcNetworkRequest* Request, QNetworkReply *Reply);
93 
94  void CloseConnections (void);
95  void UpdateConfiguration (bool Creating = false);
96 
97  private:
98  bool m_online;
99  QNetworkConfigurationManager m_manager;
100  QNetworkConfiguration m_configuration;
101  QStringList m_hostNames;
102 
103  QMap<QNetworkReply*,TorcNetworkRequest*> m_requests;
104  QMap<TorcNetworkRequest*,QNetworkReply*> m_reverseRequests;
105  QMap<TorcNetworkRequest*,QObject*> m_asynchronousRequests;
106 };
107 
108 extern TorcNetwork *gNetwork;
109 extern QMutex *gNetworkLock;
110 
111 #endif // TORCNETWORK_H
void ConfigurationChanged(const QNetworkConfiguration &Config)
void NewAsyncRequest(TorcNetworkRequest *Request, QObject *Parent)
void CancelRequest(TorcNetworkRequest *Request)
static void Setup(bool Create)
void UpdateConfiguration(bool Creating=false)
A wrapper around QNetworkRequest.
static bool GetAsynchronous(TorcNetworkRequest *Request, QObject *Parent)
Queue an asynchronous HTTP request.
Definition: torcnetwork.cpp:94
void ConfigurationRemoved(const QNetworkConfiguration &Config)
QMutex * gNetworkLock
Definition: torcnetwork.cpp:32
static QStringList GetHostNames(void)
Retrieve the list of currently identified host names.
static bool IsGlobal(const QHostAddress &Address)
static QString IPAddressToLiteral(const QHostAddress &Address, int Port, bool UseLocalhost=true)
Convert an IP address to a string literal.
void UpdateCompleted(void)
static bool IsAvailable(void)
Definition: torcnetwork.cpp:46
bool IsOwnAddressPriv(const QHostAddress &Address)
bool IsOnline(void)
void ConfigurationAdded(const QNetworkConfiguration &Config)
static QList< QSslError > AllowableSslErrors(const QList< QSslError > &Errors)
static void AddHostName(const QString &Host)
Register a known host name for this application.
void PokeRequest(TorcNetworkRequest *Request)
bool Redirected(TorcNetworkRequest *Request, QNetworkReply *Reply)
static bool IsLinkLocal(const QHostAddress &Address)
void Authenticate(QNetworkReply *Reply, QAuthenticator *Authenticator)
void DownloadProgress(qint64 Received, qint64 Total)
void ReadyRead(void)
virtual ~TorcNetwork()
static bool IsExternal(const QHostAddress &Address)
Returns true if the address is accessible from other devices.
void NewRequest(TorcNetworkRequest *Request)
void SSLErrors(const QList< QSslError > &Errors)
bool CheckHeaders(TorcNetworkRequest *Request, QNetworkReply *Reply)
static void Poke(TorcNetworkRequest *Request)
Definition: torcnetwork.cpp:80
void Error(QNetworkReply::NetworkError Code)
Subclass of QNetworkAccessManager for sending network requests and monitoring the network state...
Definition: torcnetwork.h:22
static void Cancel(TorcNetworkRequest *Request)
Definition: torcnetwork.cpp:73
void Finished(void)
static bool Get(TorcNetworkRequest *Request)
Definition: torcnetwork.cpp:60
static bool IsOwnAddress(const QHostAddress &Address)
Definition: torcnetwork.cpp:53
void CloseConnections(void)
Cancel all current network requests.
static void RemoveHostName(const QString &Host)
Remove a host name from the known list of host names.
TorcNetwork * gNetwork
Definition: torcnetwork.cpp:31
void OnlineStateChanged(bool Online)
A static class used to create the TorcNetwork singleton in the admin thread.
void NewHostName(const QHostInfo &Info)
Receives host name updates from QHostInfo.
static bool IsLocal(const QHostAddress &Address)