Open Lighting Architecture  0.9.2
 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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/base/Macro.h>
28 #include <ola/network/Socket.h>
30 #include <ola/slp/Base.h>
31 #include <ola/slp/URLEntry.h>
32 
33 #include <memory>
34 #include <string>
35 #include <vector>
36 
37 namespace ola {
38 namespace slp {
39 
40 
41 // Information about the server
42 class ServerInfo {
43  public:
44  bool da_enabled;
45  uint16_t port;
46  std::vector<std::string> scopes;
47 
48  ServerInfo() : da_enabled(false), port(0) {}
49 };
50 
51 // Used to communicate with the local SLP server.
52 class SLPClient {
53  public:
54  explicit SLPClient(ola::io::ConnectedDescriptor *descriptor);
55  ~SLPClient();
56 
57  bool Setup();
58  bool Stop();
59 
60  void SetCloseHandler(ola::SingleUseCallback0<void> *callback);
61 
65  bool RegisterService(
66  const std::vector<std::string> &scopes,
67  const std::string &service,
68  uint16_t lifetime,
70 
75  const std::vector<std::string> &scopes,
76  const std::string &service,
77  uint16_t lifetime,
79 
83  bool DeRegisterService(
84  const std::vector<std::string> &scopes,
85  const std::string &service,
87 
91  bool FindService(
92  const std::vector<std::string> &scopes,
93  const std::string &service,
94  SingleUseCallback2<void, const std::string&,
95  const std::vector<URLEntry> &> *callback);
96 
100  bool GetServerInfo(
101  SingleUseCallback2<void,
102  const std::string&,
103  const ServerInfo&> *callback);
104 
105  private:
106  std::auto_ptr<class SLPClientCore> m_core;
107 
108  DISALLOW_COPY_AND_ASSIGN(SLPClient);
109 };
110 
111 
113  public:
114  SLPClientWrapper() : BaseClientWrapper() {}
115 
116  SLPClient *GetClient() const { return m_client.get(); }
117 
118  private:
119  std::auto_ptr<SLPClient> m_client;
120 
121  void CreateClient() {
122  if (!m_client.get()) {
123  m_client.reset(new SLPClient(m_socket.get()));
124  }
125  }
126 
127  bool StartupClient() {
128  bool ok = m_client->Setup();
129  m_client->SetCloseHandler(
130  ola::NewSingleCallback(static_cast<BaseClientWrapper*>(this),
131  &BaseClientWrapper::SocketClosed));
132  return ok;
133  }
134 
135  void InitSocket() {
136  m_socket.reset(ola::network::TCPSocket::Connect(
139  OLA_SLP_DEFAULT_PORT)));
140  }
141 
142  DISALLOW_COPY_AND_ASSIGN(SLPClientWrapper);
143 };
144 } // namespace slp
145 } // namespace ola
146 #endif // INCLUDE_OLA_SLP_SLPCLIENT_H_