Open Lighting Architecture
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SLPClient.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  * SLPClient.h
17  * The API to talk to the SLP Server.
18  * Copyright (C) 2012 Simon Newton
19  */
20 
21 #ifndef INCLUDE_OLA_SLP_SLPCLIENT_H_
22 #define INCLUDE_OLA_SLP_SLPCLIENT_H_
23 
24 #include <ola/Callback.h>
25 #include <ola/OlaClientWrapper.h>
26 #include <ola/network/IPV4Address.h>
27 #include <ola/network/Socket.h>
28 #include <ola/network/SocketAddress.h>
29 #include <ola/slp/Base.h>
30 #include <ola/slp/URLEntry.h>
31 
32 #include <memory>
33 #include <string>
34 #include <vector>
35 
36 namespace ola {
37 namespace slp {
38 
39 using std::auto_ptr;
40 using std::string;
43 
44 // Information about the server
45 class ServerInfo {
46  public:
47  bool da_enabled;
48  uint16_t port;
49  vector<string> scopes;
50 
51  ServerInfo() : da_enabled(false), port(0) {}
52 };
53 
54 // Used to communicate with the local SLP server.
55 class SLPClient {
56  public:
57  explicit SLPClient(ola::io::ConnectedDescriptor *descriptor);
58  ~SLPClient();
59 
60  bool Setup();
61  bool Stop();
62 
63  void SetCloseHandler(ola::SingleUseCallback0<void> *callback);
64 
68  bool RegisterService(
69  const vector<string> &scopes,
70  const string &service,
71  uint16_t lifetime,
73 
78  const vector<string> &scopes,
79  const string &service,
80  uint16_t lifetime,
82 
86  bool DeRegisterService(
87  const vector<string> &scopes,
88  const string &service,
90 
94  bool FindService(
95  const vector<string> &scopes,
96  const string &service,
97  SingleUseCallback2<void, const string&,
98  const vector<URLEntry> &> *callback);
99 
103  bool GetServerInfo(
105 
106  private:
107  SLPClient(const SLPClient&);
108  SLPClient operator=(const SLPClient&);
109 
110  auto_ptr<class SLPClientCore> m_core;
111 };
112 
113 
115  public:
117 
118  SLPClient *GetClient() const { return m_client.get(); }
119 
120  private:
121  auto_ptr<SLPClient> m_client;
122 
123  void CreateClient() {
124  if (!m_client.get()) {
125  m_client.reset(new SLPClient(m_socket.get()));
126  }
127  }
128 
129  bool StartupClient() {
130  bool ok = m_client->Setup();
131  m_client->SetCloseHandler(
132  ola::NewSingleCallback(static_cast<BaseClientWrapper*>(this),
133  &BaseClientWrapper::SocketClosed));
134  return ok;
135  }
136 
137  void InitSocket() {
138  m_socket.reset(TCPSocket::Connect(
139  IPV4SocketAddress(IPV4Address::Loopback(), OLA_SLP_DEFAULT_PORT)));
140  }
141 };
142 } // namespace slp
143 } // namespace ola
144 #endif // INCLUDE_OLA_SLP_SLPCLIENT_H_