Open Lighting Architecture 0.10.9
Loading...
Searching...
No Matches
AdvancedTCPConnector.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15 *
16 * AdvancedTCPConnector.h
17 * Copyright (C) 2012 Simon Newton
18 */
19
20#ifndef INCLUDE_OLA_NETWORK_ADVANCEDTCPCONNECTOR_H_
21#define INCLUDE_OLA_NETWORK_ADVANCEDTCPCONNECTOR_H_
22
23#include <math.h>
24
25#include <ola/Callback.h>
26#include <ola/base/Macro.h>
27#include <ola/io/SelectServerInterface.h>
28#include <ola/network/TCPConnector.h>
29#include <ola/network/TCPSocketFactory.h>
30#include <ola/util/Backoff.h>
31#include <map>
32#include <utility>
33
34namespace ola {
35namespace network {
36
49 public:
57 TCPSocketFactoryInterface *socket_factory,
58 const ola::TimeInterval &connection_timeout);
59
61
73 void AddEndpoint(const IPV4SocketAddress &endpoint,
74 BackOffPolicy *backoff_policy,
75 bool paused = false);
76
82 void RemoveEndpoint(const IPV4SocketAddress &endpoint);
83
87 unsigned int EndpointCount() const { return m_connections.size(); }
88
97
106 bool GetEndpointState(const IPV4SocketAddress &endpoint,
107 ConnectionState *connected,
108 unsigned int *failed_attempts) const;
109
115 void Disconnect(const IPV4SocketAddress &endpoint, bool pause = false);
116
121 void Resume(const IPV4SocketAddress &endpoint);
122
123 private:
124 typedef struct {
125 ConnectionState state;
126 unsigned int failed_attempts;
127 ola::thread::timeout_id retry_timeout;
128 TCPConnector::TCPConnectionID connection_id;
129 BackOffPolicy *policy;
130 bool reconnect;
131 } ConnectionInfo;
132
133 typedef std::pair<IPV4Address, uint16_t> IPPortPair;
134 typedef std::map<IPPortPair, ConnectionInfo*> ConnectionMap;
135
136 TCPSocketFactoryInterface *m_socket_factory;
138 TCPConnector m_connector;
139 const ola::TimeInterval m_connection_timeout;
140 ConnectionMap m_connections;
141
142 void ScheduleRetry(const IPPortPair &key, ConnectionInfo *info);
143 void RetryTimeout(IPPortPair key);
144 void ConnectionResult(IPPortPair key, int fd, int error);
145 void AttemptConnection(const IPPortPair &key, ConnectionInfo *state);
146 void AbortConnection(ConnectionInfo *state);
147
149 const AdvancedTCPConnector &operator=(const AdvancedTCPConnector &) = delete;
150};
151} // namespace network
152} // namespace ola
153#endif // INCLUDE_OLA_NETWORK_ADVANCEDTCPCONNECTOR_H_
Helper macros.
Definition Backoff.h:33
Definition Clock.h:126
The interface for the SelectServer.
Definition SelectServerInterface.h:42
Attempts to open a TCP connection until a failure limit is reached.
Definition AdvancedTCPConnector.h:48
unsigned int EndpointCount() const
Return the number of connections tracked by this connector.
Definition AdvancedTCPConnector.h:87
ConnectionState
The state of a connection.
Definition AdvancedTCPConnector.h:92
@ PAUSED
Definition AdvancedTCPConnector.h:94
@ DISCONNECTED
Definition AdvancedTCPConnector.h:93
@ CONNECTED
Definition AdvancedTCPConnector.h:95
void RemoveEndpoint(const IPV4SocketAddress &endpoint)
Remove a IP:Port from the connection manager. This won't close the connection if it's already establi...
Definition AdvancedTCPConnector.cpp:75
void Disconnect(const IPV4SocketAddress &endpoint, bool pause=false)
Mark an endpoint as disconnected.
Definition AdvancedTCPConnector.cpp:100
void Resume(const IPV4SocketAddress &endpoint)
Resume trying to connect to a ip:port pair.
Definition AdvancedTCPConnector.cpp:126
bool GetEndpointState(const IPV4SocketAddress &endpoint, ConnectionState *connected, unsigned int *failed_attempts) const
Get the state & number of failed_attempts for an endpoint.
Definition AdvancedTCPConnector.cpp:86
void AddEndpoint(const IPV4SocketAddress &endpoint, BackOffPolicy *backoff_policy, bool paused=false)
Add an endpoint to manage a connection to.
Definition AdvancedTCPConnector.cpp:52
AdvancedTCPConnector(ola::io::SelectServerInterface *ss, TCPSocketFactoryInterface *socket_factory, const ola::TimeInterval &connection_timeout)
Create a new AdvancedTCPConnector.
Definition AdvancedTCPConnector.cpp:32
An IPv4 SocketAddress.
Definition SocketAddress.h:78
const void * TCPConnectionID
The TCPConnectionID.
Definition TCPConnector.h:55
Definition TCPSocketFactory.h:34
void * timeout_id
A timeout handle which can later be used to cancel a timeout.
Definition SchedulerInterface.h:34
The namespace containing all OLA symbols.
Definition Credentials.cpp:44