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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
/**
@file EchoLinkDirectoryCon.cpp
@brief EchoLink directory server connection
@author Tobias Blomberg / SM0SVX
@date 2013-04-27
\verbatim
EchoLib - A library for EchoLink communication
Copyright (C) 2003-2013 Tobias Blomberg / SM0SVX
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\endverbatim
*/
/****************************************************************************
*
* System Includes
*
****************************************************************************/
#include <errno.h>
/****************************************************************************
*
* Project Includes
*
****************************************************************************/
/****************************************************************************
*
* Local Includes
*
****************************************************************************/
#include "EchoLinkProxy.h"
#include "EchoLinkDirectoryCon.h"
/****************************************************************************
*
* Namespaces to use
*
****************************************************************************/
using namespace std;
using namespace Async;
using namespace EchoLink;
/****************************************************************************
*
* Defines & typedefs
*
****************************************************************************/
/****************************************************************************
*
* Local class definitions
*
****************************************************************************/
/****************************************************************************
*
* Prototypes
*
****************************************************************************/
/****************************************************************************
*
* Exported Global Variables
*
****************************************************************************/
/****************************************************************************
*
* Local Global Variables
*
****************************************************************************/
/****************************************************************************
*
* Public member functions
*
****************************************************************************/
DirectoryCon::DirectoryCon(const vector<string> &servers,
const IpAddress &bind_ip)
: servers(servers), client(0), last_disconnect_reason(0), is_ready(false)
{
Proxy *proxy = Proxy::instance();
if (proxy != 0)
{
proxy->proxyReady.connect(mem_fun(*this, &DirectoryCon::proxyReady));
proxy->tcpConnected.connect(connected.make_slot());
proxy->tcpDisconnected.connect(disconnected.make_slot());
proxy->tcpDataReceived.connect(dataReceived.make_slot());
}
else
{
client = new TcpClient;
client->bind(bind_ip);
client->connected.connect(connected.make_slot());
client->disconnected.connect(mem_fun(*this, &DirectoryCon::onDisconnected));
client->dataReceived.connect(mem_fun(*this, &DirectoryCon::onDataReceived));
is_ready = true;
ready(true);
}
} /* DirectoryCon::DirectoryCon */
DirectoryCon::~DirectoryCon(void)
{
disconnect();
delete client;
client = 0;
} /* DirectoryCon::~DirectoryCon */
void DirectoryCon::connect(void)
{
if (addresses.empty())
{
doDnsLookup();
}
else
{
doConnect();
}
} /* DirectoryCon::connect */
void DirectoryCon::disconnect(void)
{
// Remove all pending DNS queries, if any
vector<DnsLookup*>::iterator it;
for (it = dns_lookups.begin(); it != dns_lookups.end(); ++it)
{
delete *it;
}
dns_lookups.clear();
Proxy *proxy = Proxy::instance();
if (proxy == 0)
{
bool was_idle = client->isIdle();
client->disconnect();
if (!was_idle)
{
last_disconnect_reason = TcpClient::DR_ORDERED_DISCONNECT;
disconnected();
}
}
else
{
last_disconnect_reason = TcpClient::DR_ORDERED_DISCONNECT;
if (!proxy->tcpClose())
{
cerr << "*** ERROR: EchoLink proxy TCP close failed\n";
}
}
} /* DirectoryCon::disconnect */
int DirectoryCon::write(const void *data, unsigned len)
{
Proxy *proxy = Proxy::instance();
if (proxy == 0)
{
return client->write(data, len);
}
else
{
if (proxy->tcpData(data, len))
{
return len;
}
else
{
errno = EIO;
return -1;
}
}
} /* DirectoryCon::write */
bool DirectoryCon::isIdle(void) const
{
Proxy *proxy = Proxy::instance();
if (proxy == 0)
{
return is_ready && !client->isConnected();
}
else
{
return is_ready && (proxy->tcpState() == Proxy::TCP_STATE_DISCONNECTED);
}
} /* DirectoryCon::isIdle */
/****************************************************************************
*
* Protected member functions
*
****************************************************************************/
/****************************************************************************
*
* Private member functions
*
****************************************************************************/
void DirectoryCon::doDnsLookup(void)
{
vector<string>::const_iterator it;
for (it = servers.begin(); it != servers.end(); ++it)
{
DnsLookup *dns_lookup = new DnsLookup(*it);
dns_lookup->resultsReady.connect(
mem_fun(*this, &DirectoryCon::onDnsLookupResultsReady));
dns_lookups.push_back(dns_lookup);
}
} /* DirectoryCon::doDnsLookup */
void DirectoryCon::onDnsLookupResultsReady(DnsLookup &dns)
{
// Check if all lookups are done and also find out how many
// IP addresses we have looked up totally
unsigned num_ips = 0;
vector<DnsLookup*>::iterator it;
for (it = dns_lookups.begin(); it != dns_lookups.end(); ++it)
{
if (!(*it)->resultsAreReady())
{
return;
}
num_ips += (*it)->addresses().size();
}
addresses.clear();
addresses.reserve(num_ips);
// Populate the addresses vector with looked up IP addresses
for (it = dns_lookups.begin(); it != dns_lookups.end(); ++it)
{
vector<IpAddress> addrs = (*it)->addresses();
addresses.insert(addresses.end(), addrs.begin(), addrs.end());
delete *it;
}
dns_lookups.clear();
if (addresses.empty())
{
cerr << "*** ERROR: No IP addresses were returned for the EchoLink "
"directory server DNS query\n";
last_disconnect_reason = TcpConnection::DR_HOST_NOT_FOUND;
disconnected();
return;
}
current_server = addresses.begin();
doConnect();
} /* DirectoryCon::onDnsLookupResultsReady */
void DirectoryCon::doConnect(void)
{
Proxy *proxy = Proxy::instance();
if (proxy == 0)
{
//cout << "### Connecting to " << *current_server << endl;
client->connect(*current_server, DIRECTORY_SERVER_PORT);
}
else
{
last_disconnect_reason = TcpClient::DR_REMOTE_DISCONNECTED;
if (!proxy->tcpOpen(*current_server))
{
cerr << "*** ERROR: Could not connect to EchoLink directory server "
"via proxy\n";
last_disconnect_reason = TcpClient::DR_SYSTEM_ERROR;
errno = ECONNREFUSED;
disconnected();
}
}
} /* DirectoryCon::doConnect */
void DirectoryCon::onDisconnected(TcpConnection *con,
TcpClient::DisconnectReason reason)
{
if (++current_server == addresses.end())
{
addresses.clear();
}
last_disconnect_reason = static_cast<int>(reason);
disconnected();
} /* DirectoryCon::onDisconnected */
int DirectoryCon::onDataReceived(TcpConnection *con, void *data, int len)
{
return dataReceived(data, static_cast<unsigned>(len));
} /* DirectoryCon::onDataReceived */
void DirectoryCon::proxyReady(bool is_ready)
{
this->is_ready = is_ready;
ready(is_ready);
} /* DirectoryCon::proxyReady */
/*
* This file has not been truncated
*/
|