Open Lighting Architecture  0.9.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
34 namespace ola {
35 namespace 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 
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;
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  DISALLOW_COPY_AND_ASSIGN(AdvancedTCPConnector);
149 };
150 } // namespace network
151 } // namespace ola
152 #endif // INCLUDE_OLA_NETWORK_ADVANCEDTCPCONNECTOR_H_