Open Lighting Architecture: include/ola/network/AdvancedTCPConnector.h Source File

| 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
48 class AdvancedTCPConnector {
49 public:
56 AdvancedTCPConnector(ola::io::SelectServerInterface *ss,
57 TCPSocketFactoryInterface *socket_factory,
58 const ola::TimeInterval &connection_timeout);
59
60 ~AdvancedTCPConnector();
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
92 enum ConnectionState {
93 DISCONNECTED,
94 PAUSED,
95 CONNECTED,
96 };
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;
137 ola::io::SelectServerInterface *m_ss;
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
148 AdvancedTCPConnector(const AdvancedTCPConnector &) = delete;
149 const AdvancedTCPConnector &operator=(const AdvancedTCPConnector &) = delete;
150};
151} // namespace network
152} // namespace ola
153#endif // INCLUDE_OLA_NETWORK_ADVANCEDTCPCONNECTOR_H_
Callback.h
Macro.h
Helper macros.
ola::BackOffPolicy
Definition Backoff.h:33
ola::TimeInterval
Definition Clock.h:126
ola::io::SelectServerInterface
The interface for the SelectServer.
Definition SelectServerInterface.h:42
ola::network::AdvancedTCPConnector::EndpointCount
unsigned int EndpointCount() const
Return the number of connections tracked by this connector.
Definition AdvancedTCPConnector.h:87
ola::network::AdvancedTCPConnector::ConnectionState
ConnectionState
The state of a connection.
Definition AdvancedTCPConnector.h:92
ola::network::AdvancedTCPConnector::PAUSED
@ PAUSED
Definition AdvancedTCPConnector.h:94
ola::network::AdvancedTCPConnector::DISCONNECTED
@ DISCONNECTED
Definition AdvancedTCPConnector.h:93
ola::network::AdvancedTCPConnector::CONNECTED
@ CONNECTED
Definition AdvancedTCPConnector.h:95
ola::network::AdvancedTCPConnector::RemoveEndpoint
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
ola::network::AdvancedTCPConnector::Disconnect
void Disconnect(const IPV4SocketAddress &endpoint, bool pause=false)
Mark an endpoint as disconnected.
Definition AdvancedTCPConnector.cpp:100
ola::network::AdvancedTCPConnector::Resume
void Resume(const IPV4SocketAddress &endpoint)
Resume trying to connect to a ip:port pair.
Definition AdvancedTCPConnector.cpp:126
ola::network::AdvancedTCPConnector::GetEndpointState
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
ola::network::AdvancedTCPConnector::AddEndpoint
void AddEndpoint(const IPV4SocketAddress &endpoint, BackOffPolicy *backoff_policy, bool paused=false)
Add an endpoint to manage a connection to.
Definition AdvancedTCPConnector.cpp:52
ola::network::AdvancedTCPConnector::AdvancedTCPConnector
AdvancedTCPConnector(ola::io::SelectServerInterface *ss, TCPSocketFactoryInterface *socket_factory, const ola::TimeInterval &connection_timeout)
Create a new AdvancedTCPConnector.
Definition AdvancedTCPConnector.cpp:32
ola::network::IPV4SocketAddress
An IPv4 SocketAddress.
Definition SocketAddress.h:78
ola::network::TCPConnector::TCPConnectionID
const void * TCPConnectionID
The TCPConnectionID.
Definition TCPConnector.h:55
ola::network::TCPSocketFactoryInterface
Definition TCPSocketFactory.h:34
ola::network
Code for network communication.
Definition AdvancedTCPConnector.cpp:28
ola::thread::timeout_id
void * timeout_id
A timeout handle which can later be used to cancel a timeout.
Definition SchedulerInterface.h:34
ola
The namespace containing all OLA symbols.
Definition Credentials.cpp:44