Open Lighting Architecture  0.9.0
 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * OlaClientWrapper.h
17  * This provides a helpful wrapper for the OlaClient & OlaCallbackClient
18  * classes.
19  * Copyright (C) 2005-2008 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/OlaCallbackClient.h>
30 #include <ola/client/OlaClient.h>
31 #include <ola/io/SelectServer.h>
33 #include <ola/network/TCPSocket.h>
34 
35 #include <memory>
36 
37 namespace ola {
38 namespace client {
39 
40 /*
41  * The base class, not used directly.
42  */
44  public:
46  virtual ~BaseClientWrapper();
47 
48  ola::io::SelectServer *GetSelectServer() { return &m_ss; }
49 
50  bool Setup();
51  bool Cleanup();
52  void SocketClosed();
53 
54  protected:
55  std::auto_ptr<ola::network::TCPSocket> m_socket;
56 
57  private:
59 
60  virtual void CreateClient() = 0;
61  virtual bool StartupClient() = 0;
62  virtual void InitSocket() = 0;
63 };
64 
65 
66 /*
67  * This wrapper uses the OlaClient class.
68  */
69 template <typename ClientClass>
71  public:
72  explicit GenericClientWrapper(bool auto_start = true):
74  m_auto_start(auto_start) {
75  }
77 
78  ClientClass *GetClient() const { return m_client.get(); }
79 
80  private:
81  std::auto_ptr<ClientClass> m_client;
82  bool m_auto_start;
83 
84  void CreateClient() {
85  if (!m_client.get()) {
86  m_client.reset(new ClientClass(m_socket.get()));
87  }
88  }
89 
90  bool StartupClient() {
91  bool ok = m_client->Setup();
92  m_client->SetCloseHandler(
93  ola::NewSingleCallback(static_cast<BaseClientWrapper*>(this),
94  &BaseClientWrapper::SocketClosed));
95  return ok;
96  }
97 
98  void InitSocket() {
99  if (m_auto_start) {
100  m_socket.reset(ola::client::ConnectToServer(OLA_DEFAULT_PORT));
101  } else {
102  m_socket.reset(ola::network::TCPSocket::Connect(
105  OLA_DEFAULT_PORT)));
106  }
107  m_socket->SetNoDelay();
108  }
109 };
110 
112 } // namespace client
113 
114 // Legacy
117 
118 } // namespace ola
119 #endif // OLA_OLACLIENTWRAPPER_H_