Vidalia  0.2.21
UPNPControl.h
Go to the documentation of this file.
1 /*
2 ** This file is part of Vidalia, and is subject to the license terms in the
3 ** LICENSE file, found in the top level directory of this distribution. If
4 ** you did not receive the LICENSE file with this file, you may obtain it
5 ** from the Vidalia source package distributed by the Vidalia Project at
6 ** http://www.torproject.org/projects/vidalia.html. No part of Vidalia,
7 ** including this file, may be copied, modified, propagated, or distributed
8 ** except according to the terms described in the LICENSE file.
9 */
10 
11 /*
12 ** \file UPNPControl.h
13 ** \brief Singleton object for interacting with UPNP device
14 */
15 
16 #ifndef _UPNPCONTROL_H
17 #define _UPNPCONTROL_H
18 
19 #include <QObject>
20 #include <QMutex>
21 
22 /* Forward declaration to make it build */
23 class UPNPControlThread;
24 
25 
26 class UPNPControl : public QObject
27 {
28  Q_OBJECT
29 
30 public:
31  /** UPnP-related error values. */
32  enum UPNPError {
41  };
42  /** UPnP port forwarding state. */
43  enum UPNPState {
50  };
51 
52  /** Returns a pointer to this object's singleton instance. */
53  static UPNPControl* instance();
54  /** Terminates the UPnP control thread and frees memory allocated to this
55  * object's singleton instance. */
56  static void cleanup();
57  /** Sets <b>desiredDirPort</b> and <b>desiredOrPort</b> to the currently
58  * forwarded DirPort and ORPort values. */
59  void getDesiredState(quint16 *desiredDirPort, quint16 *desiredOrPort);
60  /** Sets the desired DirPort and ORPort port mappings to
61  * <b>desiredDirPort</b> and <b>desiredOrPort</b>, respectively. */
62  void setDesiredState(quint16 desiredDirPort, quint16 desiredOrPort);
63 
64  /** Returns the type of error that occurred last. */
65  UPNPError error() const;
66  /** Returns a QString describing the type of error that occurred last. */
67  QString errorString() const;
68 
69  /** Returns the number of milliseconds to wait for devices to respond
70  * when attempting to discover UPnP-enabled IGDs. */
71  int discoverTimeout() const;
72 
73 signals:
74  /** Emitted when the UPnP control thread status changes. */
76 
77  /** Emitted when a UPnP error occurs. */
79 
80 protected:
81  /** Constructor. Initializes and starts a thread in which all blocking UPnP
82  * operations will be performed. */
83  UPNPControl();
84  /** Destructor. cleanup() should be called before the object is destroyed. */
85  ~UPNPControl();
86 
87  /** Sets the most recent UPnP-related error to <b>error</b> and emits the
88  * error() signal.
89  * \sa error
90  */
91  void setError(UPNPError error);
92 
93  /** Sets the current UPnP state to <b>state</b> and emits the stateChanged()
94  * signal.
95  * \sa stateChanged
96  */
97  void setState(UPNPState state);
98 
99 private:
100  static UPNPControl* _instance; /**< UPNPControl singleton instance. */
101 
102  quint16 _forwardedORPort; /**< Currently forwarded ORPort. */
103  quint16 _forwardedDirPort; /**< Currently forwarded DirPort. */
104  QMutex* _mutex; /**< Mutex around variables shared with UPNPControlThread. */
105  UPNPError _error; /**< Most recent UPNP error. */
106  UPNPState _state; /**< Current UPNP status. */
107 
108  friend class UPNPControlThread;
109  UPNPControlThread* _controlThread; /**< Thread used for UPnP operations. */
110 };
111 
112 #endif
113