summaryrefslogtreecommitdiff
path: root/inc/server.h
blob: 260ac573d9d10bf7a06b68a6aa0079fc0b121305 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 * File:   server.h
 * Author: savop
 *
 * Created on 19. April 2009, 17:42
 */

#ifndef _SERVER_H
#define	_SERVER_H

#include <netinet/in.h>
#include <vdr/recording.h>
#include <vdr/thread.h>
#include <upnp/upnp.h>
#include "util.h"
#include "../common.h"
#include "webserver.h"
#include "metadata.h"
#include "upnp/connectionmanager.h"
#include "upnp/contentdirectory.h"
#include "../upnp.h"

/**
 * The UPnP Server
 *
 * This is the core of the UPnP server. This handles all the components which
 * are needed for a UPnP media server. Incoming messages are passed through it
 * and it determines what to do.
 */
class cUPnPServer {
    friend class cPluginUpnp;
public:
    /**
     * Constructor
     *
     * This will create a new server and initializes the main functionalities
     * The server has to be started manually by invoking cUPnPServer::start().
     */
    cUPnPServer();
    /**
     * Destructor
     *
     * This will destroy the server object. Open ports and connections will be
     * closed.
     */
    virtual ~cUPnPServer();
    /**
     * Enable the server
     *
     * This switch indicates if the server is startable or not
     *
     * If it is set to false, any invocation of start() will do nothing.
     *
     * @param enabled if \bc true, the server will be enabled. If \bc false it is
     * disabled.
     */
    void enable(bool enabled);
    /**
     * Start the UPnP server
     *
     * This will start the UPnP server activities as a background task.
     *
     * @return returns
     * - \bc true, when the server started successfully
     * - \bc false, otherwise
     */
    bool start(void);
    /**
     * Restart the server
     *
     * When the server is not operating properly it can be restarted.
     * It will stop the server functionalities, clear everything and
     * start it again.
     *
     * @return returns
     * - \bc true, when the server restarted successfully
     * - \bc false, otherwise
     */
    bool restart(void);
    /**
     * Stop the server
     *
     * This will stop the server. This means that open connections to
     * any clients and open ports will be closed.
     */
    void stop(void);
    /**
     * Automatically detect settings
     *
     * This will automatically detect the network settings if the autodetection
     * is turned on.
     *
     * @return returns
     * - \bc true, if autoDetection was successful
     * - \bc false, otherwise
     */
    bool autoDetectSettings(void);
    /**
     * Get the server address
     *
     * Returns a server address structure including IP address and port
     *
     * @return The server socket address
     */
    sockaddr_in* getServerAddress(void);
    /**
     * Get the interface the server listens to
     *
     * @return the network interface
     */
    const char* getInterface(void) const { return this->mInterface; }
    /**
     * Set the server port
     *
     * The port must be in the scope of user definied ports (49152 - 65535). If
     * the port is 0, it is autoassigned. You can retrieve the actual port by
     * calling getServerAddress(), which will give you a structure with the port
     * in it.
     *
     * The server must be restarted if the IP or port changes.
     *
     * Returns 1 when the port is valid, 0 otherwise
     *
     * @param port     The port of the server
     * @return returns
     * - \bc true, if the new server port is set
     * - \bc false, otherwise
     */
    bool setServerPort(unsigned short port);
    /**
     * The Interface to listen on
     * 
     * Sets the listener interface, for instance 'eth1' or 'wlan0'
     *
     * @param Interface     The interface of the server
     * @return returns
     * - \bc true, if the new server address is set
     * - \bc false, otherwise
     */
    bool setInterface(const char* Interface);
    /**
     * Set the server address
     *
     * Specifies the servers IP address. The server needs to restart
     * when the IP is changed. However, it's not possible to detect
     * changes through the system.
     *
     * This method should only be used in cases of fixed IP addresses
     * for example when no DHCP server is available.
     *
     * @param Address     The address of the server
     * @return returns
     * - \bc true, if the new server address is set
     * - \bc false, otherwise
     */
    bool setAddress(const char* Address);
    /**
     * Enables oder Disables auto detection mode
     *
     * If this is set to true, the setup will get it's information via
     * auto detection
     *
     * @param enable \bc true enables and \bc false disables the auto detection
     * @return returns
     * - \bc true, if the new server address is set
     * - \bc false, otherwise
     */
    bool setAutoDetection(bool enable);
    /**
     * Checks if the server is enabled
     *
     * This indicates if the server is currently enabled.
     *
     * @return returns
     * - \bc true, if the server is enabled
     * - \bc false, otherwise
     */
    bool isEnabled(void) const { return this->mIsEnabled; }
    /**
     * Checks if the server is running
     *
     * If the server is enabled, this indicates if it is running.
     *
     * @return returns
     * - \bc true if the server is running
     * - \bc false, otherwise
     */
    bool isRunning(void) const { return this->mIsRunning; }
    /**
     * Is auto detection enabled or not
     *
     * Returns true or false if auto detection is enabled or not
     *
     * @return returns
     * - \bc true, if autodetection is enabled
     * - \bc false, otherwise
     */
    bool isAutoDetectionEnabled() { return this->mIsAutoDetectionEnabled; }
protected:
private:
    /**
     * Inits the server
     *
     * This method initializes all member variables with default values
     */
    bool init(void);
    bool uninit(void); 
    static int upnpActionCallback(Upnp_EventType eventtype, void *event, void *cookie);
    bool mIsRunning;
    bool mIsEnabled;
    sockaddr_in* mServerAddr;
    cString mInterface;
    bool mIsAutoDetectionEnabled;
    cString mDeviceDescription;
    cUPnPWebServer* mWebServer;
    cMediaDatabase* mMediaDatabase;
    UpnpDevice_Handle mDeviceHandle;
    static cConnectionManager* mConnectionManager;
    static cContentDirectory* mContentDirectory;
};
#endif	/* _SERVER_H */