Open Lighting Architecture  0.9.0
 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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/io/SelectServerInterface.h>
27 #include <ola/network/TCPConnector.h>
28 #include <ola/network/TCPSocketFactory.h>
29 #include <ola/util/Backoff.h>
30 #include <map>
31 #include <utility>
32 
33 namespace ola {
34 namespace network {
35 
47  public:
49  TCPSocketFactoryInterface *socket_factory,
50  const ola::TimeInterval &connection_timeout);
51  virtual ~AdvancedTCPConnector();
52 
53  void AddEndpoint(const IPV4SocketAddress &endpoint,
54  BackOffPolicy *backoff_policy,
55  bool paused = false);
56  void RemoveEndpoint(const IPV4SocketAddress &endpoint);
57 
58  unsigned int EndpointCount() const { return m_connections.size(); }
59 
60  enum ConnectionState {
61  DISCONNECTED, // socket is disconnected
62  PAUSED, // disconnected, and we don't want to reconnect right now.
63  CONNECTED, // socket is connected
64  };
65 
66  bool GetEndpointState(const IPV4SocketAddress &endpoint,
67  ConnectionState *connected,
68  unsigned int *failed_attempts) const;
69 
70  void Disconnect(const IPV4SocketAddress &endpoint, bool pause = false);
71  void Resume(const IPV4SocketAddress &endpoint);
72 
73  protected:
74  typedef struct {
75  ConnectionState state;
76  unsigned int failed_attempts;
77  ola::thread::timeout_id retry_timeout;
78  TCPConnector::TCPConnectionID connection_id;
79  BackOffPolicy *policy;
81 
82  typedef std::pair<IPV4Address, uint16_t> IPPortPair;
83 
84  TCPSocketFactoryInterface *m_socket_factory;
85 
89  virtual void TakeAction(const IPPortPair &key,
90  ConnectionInfo *info,
91  int fd,
92  int error);
93  void ScheduleRetry(const IPPortPair &key, ConnectionInfo *info);
94 
95  private:
97 
98  TCPConnector m_connector;
99  const ola::TimeInterval m_connection_timeout;
100 
101  typedef std::map<IPPortPair, ConnectionInfo*> ConnectionMap;
102  ConnectionMap m_connections;
103 
104  void RetryTimeout(IPPortPair key);
105  void ConnectionResult(IPPortPair key, int fd, int error);
106  void AttemptConnection(const IPPortPair &key, ConnectionInfo *state);
107  void AbortConnection(ConnectionInfo *state);
108 };
109 } // namespace network
110 } // namespace ola
111 #endif // INCLUDE_OLA_NETWORK_ADVANCEDTCPCONNECTOR_H_