1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
/*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* AdvancedTCPConnector.cpp
* Copyright (C) 2012 Simon Newton
*/
#include <ola/Callback.h>
#include <ola/Logging.h>
#include <ola/io/SelectServerInterface.h>
#include <ola/network/AdvancedTCPConnector.h>
#include <ola/network/TCPConnector.h>
namespace ola {
namespace network {
using std::pair;
AdvancedTCPConnector::AdvancedTCPConnector(
ola::io::SelectServerInterface *ss,
TCPSocketFactoryInterface *socket_factory,
const ola::TimeInterval &connection_timeout)
: m_socket_factory(socket_factory),
m_ss(ss),
m_connector(ss),
m_connection_timeout(connection_timeout) {
}
AdvancedTCPConnector::~AdvancedTCPConnector() {
ConnectionMap::iterator iter = m_connections.begin();
for (; iter != m_connections.end(); ++iter) {
AbortConnection(iter->second);
delete iter->second;
}
m_connections.clear();
}
void AdvancedTCPConnector::AddEndpoint(const IPV4SocketAddress &endpoint,
BackOffPolicy *backoff_policy,
bool paused) {
IPPortPair key(endpoint.Host(), endpoint.Port());
ConnectionMap::iterator iter = m_connections.find(key);
if (iter != m_connections.end())
return;
// new ip:port
ConnectionInfo *state = new ConnectionInfo;
state->state = paused ? PAUSED : DISCONNECTED;
state->failed_attempts = 0;
state->retry_timeout = ola::thread::INVALID_TIMEOUT;
state->connection_id = 0;
state->policy = backoff_policy;
state->reconnect = true;
m_connections[key] = state;
if (!paused)
AttemptConnection(key, state);
}
void AdvancedTCPConnector::RemoveEndpoint(const IPV4SocketAddress &endpoint) {
IPPortPair key(endpoint.Host(), endpoint.Port());
ConnectionMap::iterator iter = m_connections.find(key);
if (iter == m_connections.end())
return;
AbortConnection(iter->second);
delete iter->second;
m_connections.erase(iter);
}
bool AdvancedTCPConnector::GetEndpointState(
const IPV4SocketAddress &endpoint,
ConnectionState *connected,
unsigned int *failed_attempts) const {
IPPortPair key(endpoint.Host(), endpoint.Port());
ConnectionMap::const_iterator iter = m_connections.find(key);
if (iter == m_connections.end())
return false;
*connected = iter->second->state;
*failed_attempts = iter->second->failed_attempts;
return true;
}
void AdvancedTCPConnector::Disconnect(const IPV4SocketAddress &endpoint,
bool pause) {
IPPortPair key(endpoint.Host(), endpoint.Port());
ConnectionMap::iterator iter = m_connections.find(key);
if (iter == m_connections.end())
return;
if (iter->second->state != CONNECTED)
return;
iter->second->failed_attempts = 0;
if (pause) {
iter->second->state = PAUSED;
} else {
// schedule a retry as if this endpoint failed once
iter->second->state = DISCONNECTED;
iter->second->retry_timeout = m_ss->RegisterSingleTimeout(
iter->second->policy->BackOffTime(1),
ola::NewSingleCallback(
this,
&AdvancedTCPConnector::RetryTimeout,
iter->first));
}
}
void AdvancedTCPConnector::Resume(const IPV4SocketAddress &endpoint) {
IPPortPair key(endpoint.Host(), endpoint.Port());
ConnectionMap::iterator iter = m_connections.find(key);
if (iter == m_connections.end())
return;
if (iter->second->state == PAUSED) {
iter->second->state = DISCONNECTED;
AttemptConnection(iter->first, iter->second);
}
}
/**
* Schedule the re-try attempt for this connection
*/
void AdvancedTCPConnector::ScheduleRetry(const IPPortPair &key,
ConnectionInfo *info) {
info->retry_timeout = m_ss->RegisterSingleTimeout(
info->policy->BackOffTime(info->failed_attempts),
ola::NewSingleCallback(
this,
&AdvancedTCPConnector::RetryTimeout,
key));
}
/**
* Called when it's time to retry
*/
void AdvancedTCPConnector::RetryTimeout(IPPortPair key) {
ConnectionMap::iterator iter = m_connections.find(key);
if (iter == m_connections.end()) {
OLA_FATAL << "Re-connect timer expired but unable to find state entry for "
<< key.first << ":" << key.second;
return;
}
iter->second->retry_timeout = ola::thread::INVALID_TIMEOUT;
AttemptConnection(key, iter->second);
}
/**
* Called by the TCPConnector when a connection is ready or it times out.
*/
void AdvancedTCPConnector::ConnectionResult(IPPortPair key, int fd, int) {
if (fd != -1) {
OLA_INFO << "TCP Connection established to " << key.first << ":" <<
key.second;
}
ConnectionMap::iterator iter = m_connections.find(key);
if (iter == m_connections.end()) {
OLA_FATAL << "Unable to find state for " << key.first << ":" <<
key.second << ", leaking sockets";
return;
}
ConnectionInfo *info = iter->second;
info->connection_id = 0;
if (fd != -1) {
// ok
info->state = CONNECTED;
m_socket_factory->NewTCPSocket(fd);
} else {
// error
info->failed_attempts++;
if (info->reconnect) {
ScheduleRetry(key, info);
}
}
}
/**
* Initiate a connection to this ip:port pair
*/
void AdvancedTCPConnector::AttemptConnection(const IPPortPair &key,
ConnectionInfo *state) {
state->connection_id = m_connector.Connect(
IPV4SocketAddress(key.first, key.second),
m_connection_timeout,
ola::NewSingleCallback(this,
&AdvancedTCPConnector::ConnectionResult,
key));
}
/**
* Abort and clean up a pending connection
* @param state the ConnectionInfo to cleanup.
*/
void AdvancedTCPConnector::AbortConnection(ConnectionInfo *state) {
if (state->connection_id) {
state->reconnect = false;
if (!m_connector.Cancel(state->connection_id))
OLA_WARN << "Failed to cancel connection " << state->connection_id;
}
if (state->retry_timeout != ola::thread::INVALID_TIMEOUT)
m_ss->RemoveTimeout(state->retry_timeout);
}
} // namespace network
} // namespace ola
|