Open Lighting Architecture  0.9.1
 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15  *
16  * OlaServer.h
17  * Interface for the ola server class
18  * Copyright (C) 2005 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/base/Macro.h"
35 #include "ola/io/SelectServer.h"
36 #include "ola/network/InterfacePicker.h"
37 #include "ola/network/Socket.h"
38 #include "ola/network/TCPSocketFactory.h"
39 #include "ola/plugin_id.h"
40 #include "ola/rdm/PidStore.h"
41 #include "ola/rdm/UID.h"
42 
43 namespace ola {
44 
45 
46 #ifdef HAVE_LIBMICROHTTPD
47 typedef class OladHTTPServer OladHTTPServer_t;
48 #else
49 typedef int OladHTTPServer_t;
50 #endif
51 
52 /*
53  * The main OlaServer class
54  */
55 class OlaServer {
56  public:
57  struct Options {
58  bool http_enable; // run the http server
59  bool http_localhost_only; // restrict access to localhost only
60  bool http_enable_quit; // enable /quit
61  unsigned int http_port; // port to run the http server on
62  std::string http_data_dir; // directory that contains the static content
63  std::string network_interface;
64  std::string pid_data_dir; // directory with the pid definitions.
65  };
66 
67  OlaServer(class OlaClientServiceFactory *factory,
68  const std::vector<class PluginLoader*> &plugin_loaders,
69  class PreferencesFactory *preferences_factory,
71  const Options &ola_options,
72  ola::network::TCPAcceptingSocket *socket = NULL,
73  ExportMap *export_map = NULL);
74  ~OlaServer();
75 
76  bool Init();
77 
78  // Thread safe.
79  void ReloadPlugins();
80  void ReloadPidStore();
81 
82  void StopServer() { m_ss->Terminate(); }
83  void NewConnection(ola::io::ConnectedDescriptor *descriptor);
84  void NewTCPConnection(ola::network::TCPSocket *socket);
85  void ChannelClosed(ola::io::DescriptorHandle read_descriptor);
86  bool RunHousekeeping();
87 
88  static const unsigned int DEFAULT_HTTP_PORT = 9090;
89 
90  private :
91  struct ClientEntry {
92  ola::io::ConnectedDescriptor *client_descriptor;
93  class OlaClientService *client_service;
94  };
95 
96  typedef std::map<ola::io::DescriptorHandle, ClientEntry> ClientMap;
97 
98  class OlaClientServiceFactory *m_service_factory;
99  std::vector<class PluginLoader*> m_plugin_loaders;
100  ola::io::SelectServer *m_ss;
101  ola::network::TCPSocketFactory m_tcp_socket_factory;
102  ola::network::TCPAcceptingSocket *m_accepting_socket;
103 
104  std::auto_ptr<class ExportMap> m_our_export_map;
105  class ExportMap *m_export_map;
106  class PreferencesFactory *m_preferences_factory;
107  class Preferences *m_universe_preferences;
108 
109  std::auto_ptr<class DeviceManager> m_device_manager;
110  std::auto_ptr<class PluginManager> m_plugin_manager;
111  std::auto_ptr<class PluginAdaptor> m_plugin_adaptor;
112  std::auto_ptr<class UniverseStore> m_universe_store;
113  std::auto_ptr<class PortManager> m_port_manager;
114  std::auto_ptr<class OlaServerServiceImpl> m_service_impl;
115  std::auto_ptr<class ClientBroker> m_broker;
116  std::auto_ptr<class PortBroker> m_port_broker;
117  std::auto_ptr<const ola::rdm::RootPidStore> m_pid_store;
118 
119  ola::thread::timeout_id m_housekeeping_timeout;
120  ClientMap m_sd_to_service;
121  std::auto_ptr<OladHTTPServer_t> m_httpd;
122  std::auto_ptr<class DiscoveryAgentInterface> m_discovery_agent;
123  const Options m_options;
124  ola::rdm::UID m_default_uid;
125 
126 #ifdef HAVE_LIBMICROHTTPD
127  bool StartHttpServer(const ola::network::Interface &iface);
128 #endif
129  void StopPlugins();
130  void InternalNewConnection(ola::io::ConnectedDescriptor *descriptor);
131  void CleanupConnection(ClientEntry client);
132  void ReloadPluginsInternal();
133  void UpdatePidStore(const ola::rdm::RootPidStore *pid_store);
134 
135  static const char UNIVERSE_PREFERENCES[];
136  static const char K_CLIENT_VAR[];
137  static const char K_UID_VAR[];
138  static const char K_DISCOVERY_SERVICE_TYPE[];
139  static const unsigned int K_HOUSEKEEPING_TIMEOUT_MS;
140 
141  DISALLOW_COPY_AND_ASSIGN(OlaServer);
142 };
143 } // namespace ola
144 #endif // OLAD_OLASERVER_H_