Open Lighting Architecture  0.9.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OLASLPThread.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  * OLASLPThread.h
17  * Copyright (C) 2011 Simon Newton
18  *
19  * An implementation of BaseSLPThread that uses OLA's SLP server.
20  */
21 
22 #ifndef INCLUDE_OLA_E133_OLASLPTHREAD_H_
23 #define INCLUDE_OLA_E133_OLASLPTHREAD_H_
24 
25 #include <ola/Callback.h>
26 #include <ola/base/Macro.h>
27 #include <ola/e133/SLPThread.h>
28 #include <ola/network/Socket.h>
29 #include <ola/network/TCPSocket.h>
30 #include <ola/slp/SLPClient.h>
31 #include <ola/thread/ExecutorInterface.h>
32 #include <ola/thread/Thread.h>
33 #include <ola/util/Backoff.h>
34 
35 #include <memory>
36 #include <string>
37 
38 namespace ola {
39 namespace e133 {
40 
41 using std::string;
42 using std::auto_ptr;
43 
44 class OLASLPThread: public BaseSLPThread {
45  public:
46  // Ownership of the discovery_callback is transferred.
49  unsigned int discovery_interval = DEFAULT_DISCOVERY_INTERVAL_SECONDS);
50  ~OLASLPThread();
51 
52  bool Init();
53  void Cleanup();
54 
55  protected:
56  void RunDiscovery(InternalDiscoveryCallback *callback,
57  const string &service);
58  void RegisterSLPService(RegistrationCallback *callback,
59  const string& url,
60  uint16_t lifetime);
61  void DeRegisterSLPService(RegistrationCallback *callback,
62  const string& url);
63  void SLPServerInfo(ServerInfoCallback *callback);
64 
65  void ThreadStopping();
66 
67  private:
68  bool m_init_ok;
69  ola::BackoffGenerator m_backoff_generator;
70  auto_ptr<ola::network::TCPSocket> m_slp_socket;
71  auto_ptr<ola::slp::SLPClient> m_slp_client;
72  ola::thread::timeout_id m_reconnect_timeout;
73 
74  void HandleDiscovery(InternalDiscoveryCallback *callback,
75  const string &status,
76  const URLEntries &urls);
77  void HandleRegistration(RegistrationCallback *callback,
78  const string &status, uint16_t error_code);
79  void HandleDeRegistration(RegistrationCallback *callback,
80  const string &status, uint16_t error_code);
81  void HandleServerInfo(ServerInfoCallback *callback,
82  const string &status,
83  const ola::slp::ServerInfo &server_info);
84 
85  void SocketClosed();
86  void ShutdownClient();
87  bool ConnectAndSetupClient();
88  void AttemptSLPConnection();
89 
90  DISALLOW_COPY_AND_ASSIGN(OLASLPThread);
91 };
92 } // namespace e133
93 } // namespace ola
94 #endif // INCLUDE_OLA_E133_OLASLPTHREAD_H_