Open Lighting Architecture  0.9.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OlaClientWrapper.h
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library 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 GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15  *
16  * OlaClientWrapper.h
17  * This provides a helpful wrapper for the OlaClient & OlaCallbackClient
18  * classes.
19  * Copyright (C) 2005 Simon Newton
20  *
21  * The OlaClientWrapper classes takes care of setting up the socket, select
22  * server and client for you.
23  */
24 
25 #ifndef OLA_OLACLIENTWRAPPER_H_
26 #define OLA_OLACLIENTWRAPPER_H_
27 
28 #include <ola/AutoStart.h>
29 #include <ola/Callback.h>
30 #include <ola/OlaCallbackClient.h>
31 #include <ola/client/OlaClient.h>
32 #include <ola/io/SelectServer.h>
34 #include <ola/network/TCPSocket.h>
35 
36 #include <memory>
37 
38 namespace ola {
39 namespace client {
40 
47  public:
49 
51  virtual ~BaseClientWrapper();
52 
61  void SetCloseCallback(CloseCallback *callback);
62 
68 
73  bool Setup();
74 
79  bool Cleanup();
80 
85  void SocketClosed();
86 
87  protected:
88  std::auto_ptr<ola::network::TCPSocket> m_socket;
89 
90  private:
92  std::auto_ptr<CloseCallback> m_close_callback;
93 
94  virtual void CreateClient() = 0;
95  virtual bool StartupClient() = 0;
96  virtual void InitSocket() = 0;
97 };
98 
99 
103 template <typename ClientClass>
105  public:
106  explicit GenericClientWrapper(bool auto_start = true):
108  m_auto_start(auto_start) {
109  }
111 
115  ClientClass *GetClient() const { return m_client.get(); }
116 
117  private:
118  std::auto_ptr<ClientClass> m_client;
119  bool m_auto_start;
120 
121  void CreateClient() {
122  if (!m_client.get()) {
123  m_client.reset(new ClientClass(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),
132  return ok;
133  }
134 
135  void InitSocket() {
136  if (m_auto_start) {
137  m_socket.reset(ola::client::ConnectToServer(OLA_DEFAULT_PORT));
138  } else {
139  m_socket.reset(ola::network::TCPSocket::Connect(
142  OLA_DEFAULT_PORT)));
143  }
144  m_socket->SetNoDelay();
145  }
146 };
147 
152 } // namespace client
153 
160 
161 } // namespace ola
162 #endif // OLA_OLACLIENTWRAPPER_H_