Open Lighting Architecture  0.9.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ClientWrapper.h
Go to the documentation of this file.
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  * ClientWrapper.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 
30 #ifndef INCLUDE_OLA_CLIENT_CLIENTWRAPPER_H_
31 #define INCLUDE_OLA_CLIENT_CLIENTWRAPPER_H_
32 
33 #include <ola/AutoStart.h>
34 #include <ola/Callback.h>
35 #include <ola/client/OlaClient.h>
36 #include <ola/io/SelectServer.h>
38 #include <ola/network/TCPSocket.h>
39 
40 #include <memory>
41 
42 namespace ola {
43 namespace client {
44 
51  public:
53 
55  virtual ~BaseClientWrapper();
56 
65  void SetCloseCallback(CloseCallback *callback);
66 
72 
77  bool Setup();
78 
83  bool Cleanup();
84 
89  void SocketClosed();
90 
91  protected:
92  std::auto_ptr<ola::network::TCPSocket> m_socket;
93 
94  private:
96  std::auto_ptr<CloseCallback> m_close_callback;
97 
98  virtual void CreateClient() = 0;
99  virtual bool StartupClient() = 0;
100  virtual void InitSocket() = 0;
101 };
102 
103 
107 template <typename ClientClass>
109  public:
110  explicit GenericClientWrapper(bool auto_start = true):
112  m_auto_start(auto_start) {
113  }
115 
119  ClientClass *GetClient() const { return m_client.get(); }
120 
121  private:
122  std::auto_ptr<ClientClass> m_client;
123  bool m_auto_start;
124 
125  void CreateClient() {
126  if (!m_client.get()) {
127  m_client.reset(new ClientClass(m_socket.get()));
128  }
129  }
130 
131  bool StartupClient() {
132  bool ok = m_client->Setup();
133  m_client->SetCloseHandler(
134  ola::NewSingleCallback(static_cast<BaseClientWrapper*>(this),
136  return ok;
137  }
138 
139  void InitSocket() {
140  if (m_auto_start) {
141  m_socket.reset(ola::client::ConnectToServer(OLA_DEFAULT_PORT));
142  } else {
143  m_socket.reset(ola::network::TCPSocket::Connect(
146  OLA_DEFAULT_PORT)));
147  }
148  m_socket->SetNoDelay();
149  }
150 };
151 
156 
157 } // namespace client
158 } // namespace ola
159 #endif // INCLUDE_OLA_CLIENT_CLIENTWRAPPER_H_