Open Lighting Architecture  0.10.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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  if (m_socket.get()) {
149  m_socket->SetNoDelay();
150  }
151  }
152 };
153 
158 
159 } // namespace client
160 } // namespace ola
161 #endif // INCLUDE_OLA_CLIENT_CLIENTWRAPPER_H_
ola::io::SelectServer * GetSelectServer()
Get the SelectServer used by this client.
Definition: ClientWrapper.h:71
bool Cleanup()
Reset the connection to the server.
Definition: OlaClientWrapper.cpp:62
Represents Socket Addresses.
SingleUseCallback0< ReturnType > * NewSingleCallback(ReturnType(*callback)())
A helper function to create a new SingleUseCallback with 0 create-time arguments and 0 execution time...
Definition: Callback.h:194
GenericClientWrapper< OlaClient > OlaClientWrapper
A ClientWrapper that uses the OlaClient.
Definition: ClientWrapper.h:157
bool Setup()
Setup the client.
Definition: OlaClientWrapper.cpp:43
static const int OLA_DEFAULT_PORT
The default port which olad listens on for incoming RPC connections.
Definition: Constants.h:68
The base Client Wrapper class.
Definition: ClientWrapper.h:50
A single threaded I/O event management system.
Definition: SelectServer.h:63
void SetCloseCallback(CloseCallback *callback)
Set the callback to be run when the client socket is closed.
Definition: OlaClientWrapper.cpp:39
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
ClientClass * GetClient() const
Return the underlying client object.
Definition: ClientWrapper.h:119
void SocketClosed()
Called internally when the client socket is closed.
Definition: OlaClientWrapper.cpp:70
static IPV4Address Loopback()
Returns the loopback address (127.0.0.1).
Definition: IPV4Address.cpp:165
An IPv4 SocketAddress.
Definition: SocketAddress.h:77
A templatized ClientWrapper.
Definition: ClientWrapper.h:108