Open Lighting Architecture  Latest Git
 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 // On MinGW, SocketAddress.h pulls in WinSock2.h, which needs to be after
34 // WinSock2.h, hence this order
36 #include <ola/network/TCPSocket.h>
37 
38 #include <ola/AutoStart.h>
39 #include <ola/Callback.h>
40 #include <ola/client/OlaClient.h>
41 #include <ola/io/SelectServer.h>
42 
43 #include <memory>
44 
45 namespace ola {
46 namespace client {
47 
54  public:
56 
58  virtual ~BaseClientWrapper();
59 
68  void SetCloseCallback(CloseCallback *callback);
69 
75 
80  bool Setup();
81 
86  bool Cleanup();
87 
92  void SocketClosed();
93 
94  protected:
95  std::auto_ptr<ola::network::TCPSocket> m_socket;
96 
97  private:
99  std::auto_ptr<CloseCallback> m_close_callback;
100 
101  virtual void CreateClient() = 0;
102  virtual bool StartupClient() = 0;
103  virtual void InitSocket() = 0;
104 };
105 
106 
110 template <typename ClientClass>
112  public:
113  explicit GenericClientWrapper(bool auto_start = true):
115  m_auto_start(auto_start) {
116  }
118 
122  ClientClass *GetClient() const { return m_client.get(); }
123 
124  private:
125  std::auto_ptr<ClientClass> m_client;
126  bool m_auto_start;
127 
128  void CreateClient() {
129  if (!m_client.get()) {
130  m_client.reset(new ClientClass(m_socket.get()));
131  }
132  }
133 
134  bool StartupClient() {
135  bool ok = m_client->Setup();
136  m_client->SetCloseHandler(
137  ola::NewSingleCallback(static_cast<BaseClientWrapper*>(this),
139  return ok;
140  }
141 
142  void InitSocket() {
143  if (m_auto_start) {
144  m_socket.reset(ola::client::ConnectToServer(OLA_DEFAULT_PORT));
145  } else {
146  m_socket.reset(ola::network::TCPSocket::Connect(
149  OLA_DEFAULT_PORT)));
150  }
151  if (m_socket.get()) {
152  m_socket->SetNoDelay();
153  }
154  }
155 };
156 
161 
162 } // namespace client
163 } // namespace ola
164 #endif // INCLUDE_OLA_CLIENT_CLIENTWRAPPER_H_
ola::io::SelectServer * GetSelectServer()
Get the SelectServer used by this client.
Definition: ClientWrapper.h:74
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:160
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:53
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:122
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:78
A templatized ClientWrapper.
Definition: ClientWrapper.h:111