Open Lighting Architecture  0.9.4
 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 <ola/Constants.h>
29 #include <ola/ExportMap.h>
30 #include <ola/base/Macro.h>
31 #include <ola/io/SelectServer.h>
32 #include <ola/network/InterfacePicker.h>
33 #include <ola/network/Socket.h>
34 #include <ola/network/TCPSocketFactory.h>
35 #include <ola/plugin_id.h>
36 #include <ola/rdm/PidStore.h>
37 #include <ola/rdm/UID.h>
38 #include <ola/rpc/RpcSessionHandler.h>
39 
40 #include <map>
41 #include <memory>
42 #include <string>
43 #include <vector>
44 
45 namespace ola {
46 
47 namespace rpc {
48 class RpcSession;
49 class RpcServer;
50 }
51 
52 #ifdef HAVE_LIBMICROHTTPD
53 typedef class OladHTTPServer OladHTTPServer_t;
54 #else
55 typedef int OladHTTPServer_t;
56 #endif
57 
62  public:
66  struct Options {
67  bool http_enable;
70  unsigned int http_port;
72  std::string http_data_dir;
73  std::string network_interface;
74  std::string pid_data_dir;
75  };
76 
89  OlaServer(const std::vector<class PluginLoader*> &plugin_loaders,
90  class PreferencesFactory *preferences_factory,
92  const Options &ola_options,
93  ola::network::TCPAcceptingSocket *socket = NULL,
94  ExportMap *export_map = NULL);
95 
99  ~OlaServer();
100 
105  bool Init();
106 
112  void ReloadPlugins();
113 
119  void ReloadPidStore();
120 
126  void StopServer() { m_ss->Terminate(); }
127 
133 
140 
141  // Called by the RpcServer when clients connect or disconnect.
142  void NewClient(ola::rpc::RpcSession *session);
143  void ClientRemoved(ola::rpc::RpcSession *session);
144 
149  const std::string InstanceName() {
150  return m_instance_name;
151  }
152 
153  static const unsigned int DEFAULT_HTTP_PORT = 9090;
154 
155  static const unsigned int DEFAULT_RPC_PORT = OLA_DEFAULT_PORT;
156 
157  private :
158  struct ClientEntry {
159  ola::io::ConnectedDescriptor *client_descriptor;
160  class OlaClientService *client_service;
161  };
162 
163  typedef std::map<ola::io::DescriptorHandle, ClientEntry> ClientMap;
164 
165  // These are all passed to the constructor.
166  const Options m_options;
167  std::vector<class PluginLoader*> m_plugin_loaders;
168  class PreferencesFactory *m_preferences_factory;
169  ola::io::SelectServer *m_ss;
170  ola::network::TCPAcceptingSocket *m_accepting_socket;
171  class ExportMap *m_export_map;
172 
173  std::auto_ptr<class ExportMap> m_our_export_map;
174  ola::rdm::UID m_default_uid;
175 
176  // These are all populated in Init.
177  std::auto_ptr<class DeviceManager> m_device_manager;
178  std::auto_ptr<class PluginManager> m_plugin_manager;
179  std::auto_ptr<class PluginAdaptor> m_plugin_adaptor;
180  std::auto_ptr<class UniverseStore> m_universe_store;
181  std::auto_ptr<class PortManager> m_port_manager;
182  std::auto_ptr<class OlaServerServiceImpl> m_service_impl;
183  std::auto_ptr<class ClientBroker> m_broker;
184  std::auto_ptr<class PortBroker> m_port_broker;
185  std::auto_ptr<const ola::rdm::RootPidStore> m_pid_store;
186  std::auto_ptr<class DiscoveryAgentInterface> m_discovery_agent;
187  std::auto_ptr<ola::rpc::RpcServer> m_rpc_server;
188  class Preferences *m_server_preferences;
189  class Preferences *m_universe_preferences;
190  std::string m_instance_name;
191 
192  ola::thread::timeout_id m_housekeeping_timeout;
193  std::auto_ptr<OladHTTPServer_t> m_httpd;
194 
195  bool RunHousekeeping();
196 
197 #ifdef HAVE_LIBMICROHTTPD
198  bool StartHttpServer(ola::rpc::RpcServer *server,
199  const ola::network::Interface &iface);
200 #endif
201 
204  void StopPlugins();
205  bool InternalNewConnection(ola::rpc::RpcServer *server,
206  ola::io::ConnectedDescriptor *descriptor);
207  void ReloadPluginsInternal();
211  void UpdatePidStore(const ola::rdm::RootPidStore *pid_store);
212 
213  static const char INSTANCE_NAME_KEY[];
214  static const char K_INSTANCE_NAME_VAR[];
215  static const char K_DISCOVERY_SERVICE_TYPE[];
216  static const char K_UID_VAR[];
217  static const char SERVER_PREFERENCES[];
218  static const char UNIVERSE_PREFERENCES[];
219  static const unsigned int K_HOUSEKEEPING_TIMEOUT_MS;
220 
222 };
223 } // namespace ola
224 #endif // OLAD_OLASERVER_H_