Open Lighting Architecture
 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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/e133/SLPThread.h>
27 #include <ola/network/Socket.h>
28 #include <ola/network/TCPSocket.h>
29 #include <ola/slp/SLPClient.h>
30 #include <ola/thread/ExecutorInterface.h>
31 #include <ola/thread/Thread.h>
32 #include <ola/util/Backoff.h>
33 
34 #include <memory>
35 #include <string>
36 
37 namespace ola {
38 namespace e133 {
39 
40 using std::string;
41 using std::auto_ptr;
42 
43 class OLASLPThread: public BaseSLPThread {
44  public:
45  // Ownership of the discovery_callback is transferred.
48  unsigned int discovery_interval = DEFAULT_DISCOVERY_INTERVAL_SECONDS);
49  ~OLASLPThread();
50 
51  bool Init();
52  void Cleanup();
53 
54  protected:
55  void RunDiscovery(InternalDiscoveryCallback *callback,
56  const string &service);
57  void RegisterSLPService(RegistrationCallback *callback,
58  const string& url,
59  uint16_t lifetime);
60  void DeRegisterSLPService(RegistrationCallback *callback,
61  const string& url);
62  void SLPServerInfo(ServerInfoCallback *callback);
63 
64  void ThreadStopping();
65 
66  private:
67  bool m_init_ok;
68  ola::BackoffGenerator m_backoff_generator;
69  auto_ptr<ola::network::TCPSocket> m_slp_socket;
70  auto_ptr<ola::slp::SLPClient> m_slp_client;
71  ola::thread::timeout_id m_reconnect_timeout;
72 
73  void HandleDiscovery(InternalDiscoveryCallback *callback,
74  const string &status,
75  const URLEntries &urls);
76  void HandleRegistration(RegistrationCallback *callback,
77  const string &status, uint16_t error_code);
78  void HandleDeRegistration(RegistrationCallback *callback,
79  const string &status, uint16_t error_code);
80  void HandleServerInfo(ServerInfoCallback *callback,
81  const string &status,
82  const ola::slp::ServerInfo &server_info);
83 
84  void SocketClosed();
85  void ShutdownClient();
86  bool ConnectAndSetupClient();
87  void AttemptSLPConnection();
88 };
89 } // namespace e133
90 } // namespace ola
91 #endif // INCLUDE_OLA_E133_OLASLPTHREAD_H_