Open Lighting Architecture: include/ola/client/ClientWrapper.h Source File

| Open Lighting Architecture 0.10.9

Loading...
Searching...
No Matches
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
29
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
35#include <ola/network/SocketAddress.h>
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
45namespace ola {
46namespace client {
47
53 class BaseClientWrapper {
54 public:
55 typedef Callback0<void> CloseCallback;
56
57 BaseClientWrapper();
58 virtual ~BaseClientWrapper();
59
68 void SetCloseCallback(CloseCallback *callback);
69
74 ola::io::SelectServer *GetSelectServer() { return &m_ss; }
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:
98 ola::io::SelectServer m_ss;
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
110template <typename ClientClass>
111 class GenericClientWrapper: public BaseClientWrapper {
112 public:
113 explicit GenericClientWrapper(bool auto_start = true):
114 BaseClientWrapper(),
115 m_auto_start(auto_start) {
116 }
117 ~GenericClientWrapper() {}
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),
138 &BaseClientWrapper::SocketClosed));
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(
147 ola::network::IPV4SocketAddress(
148 ola::network::IPV4Address::Loopback(),
149 OLA_DEFAULT_PORT)));
150 }
151 if (m_socket.get()) {
152 m_socket->SetNoDelay();
153 }
154 }
155};
156
160 typedef GenericClientWrapper<OlaClient> OlaClientWrapper;
161
162} // namespace client
163} // namespace ola
164#endif // INCLUDE_OLA_CLIENT_CLIENTWRAPPER_H_
Callback.h
SocketAddress.h
Represents Socket Addresses.
ola::Callback0
A 0 argument callback which can be called multiple times.
Definition Callback.h:129
ola::client::BaseClientWrapper::Cleanup
bool Cleanup()
Reset the connection to the server.
Definition OlaClientWrapper.cpp:62
ola::client::BaseClientWrapper::GetSelectServer
ola::io::SelectServer * GetSelectServer()
Get the SelectServer used by this client.
Definition ClientWrapper.h:74
ola::client::BaseClientWrapper::SetCloseCallback
void SetCloseCallback(CloseCallback *callback)
Set the callback to be run when the client socket is closed.
Definition OlaClientWrapper.cpp:39
ola::client::BaseClientWrapper::SocketClosed
void SocketClosed()
Called internally when the client socket is closed.
Definition OlaClientWrapper.cpp:70
ola::client::BaseClientWrapper::Setup
bool Setup()
Setup the client.
Definition OlaClientWrapper.cpp:43
ola::client::GenericClientWrapper
A templatized ClientWrapper.
Definition ClientWrapper.h:111
ola::client::GenericClientWrapper::GetClient
ClientClass * GetClient() const
Return the underlying client object.
Definition ClientWrapper.h:122
ola::io::SelectServer
A single threaded I/O event management system.
Definition SelectServer.h:63
ola::network::IPV4Address::Loopback
static IPV4Address Loopback()
Returns the loopback address (127.0.0.1).
Definition IPV4Address.cpp:165
ola::NewSingleCallback
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
ola::client
OLA C++ API.
ola::client::OlaClientWrapper
GenericClientWrapper< OlaClient > OlaClientWrapper
A ClientWrapper that uses the OlaClient.
Definition ClientWrapper.h:160
ola
The namespace containing all OLA symbols.
Definition Credentials.cpp:44
ola::OLA_DEFAULT_PORT
static const int OLA_DEFAULT_PORT
The default port which olad listens on for incoming RPC connections.
Definition Constants.h:68