Open Lighting Architecture
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OlaServer.h
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  *
16  * OlaServer.h
17  * Interface for the ola server class
18  * Copyright (C) 2005-2008 Simon Newton
19  */
20 
21 #ifndef OLAD_OLASERVER_H_
22 #define OLAD_OLASERVER_H_
23 
24 #if HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27 
28 #include <map>
29 #include <memory>
30 #include <string>
31 #include <vector>
32 
33 #include "ola/ExportMap.h"
34 #include "ola/io/SelectServer.h"
35 #include "ola/network/InterfacePicker.h"
36 #include "ola/network/Socket.h"
37 #include "ola/network/TCPSocketFactory.h"
38 #include "ola/plugin_id.h"
39 #include "ola/rdm/PidStore.h"
40 #include "ola/rdm/UID.h"
41 
42 namespace ola {
43 
45 using std::auto_ptr;
46 using std::string;
47 
48 #ifdef HAVE_LIBMICROHTTPD
49 typedef class OladHTTPServer OladHTTPServer_t;
50 #else
51 typedef int OladHTTPServer_t;
52 #endif
53 
54 /*
55  * The main OlaServer class
56  */
57 class OlaServer {
58  public:
59  struct Options {
60  bool http_enable; // run the http server
61  bool http_localhost_only; // restrict access to localhost only
62  bool http_enable_quit; // enable /quit
63  unsigned int http_port; // port to run the http server on
64  string http_data_dir; // directory that contains the static content
65  string interface;
66  string pid_data_dir; // directory with the pid definitions.
67  };
68 
69 
70  OlaServer(class OlaClientServiceFactory *factory,
71  const vector<class PluginLoader*> &plugin_loaders,
72  class PreferencesFactory *preferences_factory,
74  const Options &ola_options,
75  ola::network::TCPAcceptingSocket *socket = NULL,
76  ExportMap *export_map = NULL);
77  ~OlaServer();
78 
79  bool Init();
80 
81  // Thread safe.
82  void ReloadPlugins();
83  void ReloadPidStore();
84 
85  void StopServer() { m_ss->Terminate(); }
86  void NewConnection(ola::io::ConnectedDescriptor *descriptor);
87  void NewTCPConnection(ola::network::TCPSocket *socket);
88  void ChannelClosed(int read_descriptor);
89  bool RunHousekeeping();
90 
91  static const unsigned int DEFAULT_HTTP_PORT = 9090;
92 
93  private :
94  struct ClientEntry {
95  ola::io::ConnectedDescriptor *client_descriptor;
96  class OlaClientService *client_service;
97  };
98 
99  typedef std::map<int, ClientEntry> ClientMap;
100 
101  class OlaClientServiceFactory *m_service_factory;
102  vector<class PluginLoader*> m_plugin_loaders;
103  ola::io::SelectServer *m_ss;
104  ola::network::TCPSocketFactory m_tcp_socket_factory;
105  ola::network::TCPAcceptingSocket *m_accepting_socket;
106 
107  auto_ptr<class ExportMap> m_our_export_map;
108  class ExportMap *m_export_map;
109  class PreferencesFactory *m_preferences_factory;
110  class Preferences *m_universe_preferences;
111 
112  auto_ptr<class DeviceManager> m_device_manager;
113  auto_ptr<class PluginManager> m_plugin_manager;
114  auto_ptr<class PluginAdaptor> m_plugin_adaptor;
115  auto_ptr<class UniverseStore> m_universe_store;
116  auto_ptr<class PortManager> m_port_manager;
117  auto_ptr<class OlaServerServiceImpl> m_service_impl;
118  auto_ptr<class ClientBroker> m_broker;
119  auto_ptr<class PortBroker> m_port_broker;
120  auto_ptr<const RootPidStore> m_pid_store;
121 
122  ola::thread::timeout_id m_housekeeping_timeout;
123  ClientMap m_sd_to_service;
124  auto_ptr<OladHTTPServer_t> m_httpd;
125  const Options m_options;
126  ola::rdm::UID m_default_uid;
127 
128 #ifdef HAVE_LIBMICROHTTPD
129  bool StartHttpServer(const ola::network::Interface &interface);
130 #endif
131  void StopPlugins();
132  void InternalNewConnection(ola::io::ConnectedDescriptor *descriptor);
133  void CleanupConnection(ClientEntry client);
134  void ReloadPluginsInternal();
135  void UpdatePidStore(const RootPidStore *pid_store);
136 
137  OlaServer(const OlaServer&);
138  OlaServer& operator=(const OlaServer&);
139 
140  static const char UNIVERSE_PREFERENCES[];
141  static const char K_CLIENT_VAR[];
142  static const char K_UID_VAR[];
143  static const unsigned int K_HOUSEKEEPING_TIMEOUT_MS;
144 };
145 } // namespace ola
146 #endif // OLAD_OLASERVER_H_