Open Lighting Architecture
 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/OlaClient.h>
30 #include <ola/OlaCallbackClient.h>
31 #include <ola/io/SelectServer.h>
32 #include <ola/network/SocketAddress.h>
33 #include <ola/network/TCPSocket.h>
34 #include <memory>
35 
36 namespace ola {
37 
40 using std::auto_ptr;
41 
42 /*
43  * The base class, not used directly.
44  */
46  public:
48  virtual ~BaseClientWrapper();
49 
50  SelectServer *GetSelectServer() { return &m_ss; }
51 
52  bool Setup();
53  bool Cleanup();
54  void SocketClosed();
55 
56  protected:
57  auto_ptr<TCPSocket> m_socket;
58 
59  private:
60  SelectServer m_ss;
61 
62  virtual void CreateClient() = 0;
63  virtual bool StartupClient() = 0;
64  virtual void InitSocket() = 0;
65 };
66 
67 
68 /*
69  * This wrapper uses the OlaClient class.
70  */
71 template <typename ClientClass>
73  public:
74  explicit GenericClientWrapper(bool auto_start = true):
76  m_auto_start(auto_start) {
77  }
79 
80  ClientClass *GetClient() const { return m_client.get(); }
81 
82  private:
83  auto_ptr<ClientClass> m_client;
84  bool m_auto_start;
85 
86  void CreateClient() {
87  if (!m_client.get()) {
88  m_client.reset(new ClientClass(m_socket.get()));
89  }
90  }
91 
92  bool StartupClient() {
93  bool ok = m_client->Setup();
94  m_client->SetCloseHandler(
95  ola::NewSingleCallback(static_cast<BaseClientWrapper*>(this),
96  &BaseClientWrapper::SocketClosed));
97  return ok;
98  }
99 
100  void InitSocket() {
101  if (m_auto_start) {
102  m_socket.reset(ola::client::ConnectToServer(OLA_DEFAULT_PORT));
103  } else {
104  m_socket.reset(TCPSocket::Connect(
106  ola::network::IPV4Address::Loopback(),
107  OLA_DEFAULT_PORT)));
108  }
109  m_socket->SetNoDelay();
110  }
111 };
112 
114 // for historical reasons we typedef SimpleClient to OlaClientWrapper
116 
117 
119 } // namespace ola
120 #endif // OLA_OLACLIENTWRAPPER_H_