| 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 |
#include <gadget/gadgetConfig.h> |
|---|
| 28 |
|
|---|
| 29 |
#include <boost/bind.hpp> |
|---|
| 30 |
|
|---|
| 31 |
#include <gadget/Acceptor.h> |
|---|
| 32 |
#include <gadget/Node.h> |
|---|
| 33 |
#include <gadget/Util/Debug.h> |
|---|
| 34 |
|
|---|
| 35 |
#include <cluster/Packets/ConnectionAck.h> |
|---|
| 36 |
#include <cluster/Packets/Packet.h> |
|---|
| 37 |
#include <cluster/ClusterDelta.h> |
|---|
| 38 |
|
|---|
| 39 |
#include <vpr/IO/Socket/SocketStream.h> |
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
namespace gadget |
|---|
| 43 |
{ |
|---|
| 44 |
Acceptor::Acceptor( AbstractNetworkManager* network) |
|---|
| 45 |
: mNetworkManager( network ), mAcceptThread( NULL ) |
|---|
| 46 |
{;} |
|---|
| 47 |
|
|---|
| 48 |
Acceptor::~Acceptor() |
|---|
| 49 |
{ |
|---|
| 50 |
shutdown(); |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
bool Acceptor::startListening( const int& listen_port, bool accept_anonymous ) |
|---|
| 54 |
{ |
|---|
| 55 |
mAcceptAnonymous = accept_anonymous; |
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
if ( mAcceptThread == NULL ) |
|---|
| 59 |
{ |
|---|
| 60 |
|
|---|
| 61 |
if ( listen_port > 0 ) |
|---|
| 62 |
{ |
|---|
| 63 |
mListenAddr.setPort( listen_port ); |
|---|
| 64 |
|
|---|
| 65 |
vprDEBUG( gadgetDBG_NET_MGR, vprDBG_CONFIG_LVL ) |
|---|
| 66 |
<< clrOutBOLD( clrMAGENTA, "[Acceptor]" ) |
|---|
| 67 |
<< "Starting the listening thread...\n" << vprDEBUG_FLUSH; |
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
try |
|---|
| 71 |
{ |
|---|
| 72 |
mAcceptThread = |
|---|
| 73 |
new vpr::Thread(boost::bind(&Acceptor::acceptLoop, this)); |
|---|
| 74 |
vprASSERT(mAcceptThread->valid()); |
|---|
| 75 |
|
|---|
| 76 |
return true; |
|---|
| 77 |
} |
|---|
| 78 |
catch (vpr::Exception& ex) |
|---|
| 79 |
{ |
|---|
| 80 |
vprDEBUG(gadgetDBG_NET_MGR, vprDBG_CRITICAL_LVL) |
|---|
| 81 |
<< clrOutBOLD(clrRED, "ERROR") |
|---|
| 82 |
<< ": Failed to spawn acceptor thread!\n" << vprDEBUG_FLUSH; |
|---|
| 83 |
vprDEBUG_NEXT(gadgetDBG_NET_MGR, vprDBG_CRITICAL_LVL) |
|---|
| 84 |
<< ex.what() << std::endl << vprDEBUG_FLUSH; |
|---|
| 85 |
|
|---|
| 86 |
return false; |
|---|
| 87 |
} |
|---|
| 88 |
} |
|---|
| 89 |
else |
|---|
| 90 |
{ |
|---|
| 91 |
vprDEBUG( gadgetDBG_NET_MGR, vprDBG_CONFIG_LVL ) |
|---|
| 92 |
<< clrOutBOLD( clrMAGENTA, "[Acceptor]" ) |
|---|
| 93 |
<< "startListening() Can NOT listen on port: " |
|---|
| 94 |
<< listen_port << "\n" << vprDEBUG_FLUSH; |
|---|
| 95 |
return false; |
|---|
| 96 |
} |
|---|
| 97 |
} |
|---|
| 98 |
else |
|---|
| 99 |
{ |
|---|
| 100 |
vprDEBUG( gadgetDBG_NET_MGR, vprDBG_CONFIG_LVL ) |
|---|
| 101 |
<< clrOutBOLD( clrMAGENTA,"[Acceptor]" ) |
|---|
| 102 |
<< "startListening() Listening thread already active." |
|---|
| 103 |
<< std::endl << vprDEBUG_FLUSH; |
|---|
| 104 |
return false; |
|---|
| 105 |
} |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
void Acceptor::acceptLoop() |
|---|
| 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 |
vpr::SocketStream sock( mListenAddr, vpr::InetAddr::AnyAddr ); |
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
try |
|---|
| 138 |
{ |
|---|
| 139 |
|
|---|
| 140 |
sock.openServer(true); |
|---|
| 141 |
|
|---|
| 142 |
vprDEBUG( gadgetDBG_NET_MGR, vprDBG_CONFIG_LVL ) |
|---|
| 143 |
<< clrOutBOLD( clrMAGENTA, "[Acceptor]" ) |
|---|
| 144 |
<< " Listening on Port: " << mListenAddr.getPort() |
|---|
| 145 |
<< std::endl << vprDEBUG_FLUSH; |
|---|
| 146 |
} |
|---|
| 147 |
catch (vpr::IOException&) |
|---|
| 148 |
{ |
|---|
| 149 |
vprDEBUG( gadgetDBG_NET_MGR, vprDBG_CRITICAL_LVL ) |
|---|
| 150 |
<< clrSetBOLD( clrRED ) << "[Acceptor]" |
|---|
| 151 |
<< " Unable to open listening socket on port: " |
|---|
| 152 |
<< mListenAddr.getPort() << std::endl |
|---|
| 153 |
<< clrRESET << vprDEBUG_FLUSH; |
|---|
| 154 |
|
|---|
| 155 |
exit(0); |
|---|
| 156 |
} |
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
vpr::SocketStream* client_sock = new vpr::SocketStream(); |
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
bool mRunning = true; |
|---|
| 163 |
|
|---|
| 164 |
while ( mRunning ) |
|---|
| 165 |
{ |
|---|
| 166 |
try |
|---|
| 167 |
{ |
|---|
| 168 |
|
|---|
| 169 |
sock.accept( *client_sock, vpr::Interval::NoTimeout ); |
|---|
| 170 |
|
|---|
| 171 |
vprDEBUG(gadgetDBG_NET_MGR, vprDBG_CONFIG_LVL) |
|---|
| 172 |
<< clrOutBOLD(clrMAGENTA, "[Acceptor]") |
|---|
| 173 |
<< " Receiving a connection on Port: " |
|---|
| 174 |
<< mListenAddr.getPort() << std::endl << vprDEBUG_FLUSH; |
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
client_sock->setNoDelay( true ); |
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
const std::string remote_host_name( |
|---|
| 181 |
client_sock->getRemoteAddr().getHostname() |
|---|
| 182 |
); |
|---|
| 183 |
vpr::Uint16 port = client_sock->getRemoteAddr().getPort(); |
|---|
| 184 |
|
|---|
| 185 |
vprDEBUG( gadgetDBG_NET_MGR, vprDBG_CONFIG_LVL ) |
|---|
| 186 |
<< clrOutBOLD( clrMAGENTA, "[Acceptor]" ) |
|---|
| 187 |
<< " Received from: " << remote_host_name |
|---|
| 188 |
<< ":" << port << std::endl << vprDEBUG_FLUSH; |
|---|
| 189 |
|
|---|
| 190 |
Node* remote_node = mNetworkManager->getNodeByHostname( remote_host_name ); |
|---|
| 191 |
|
|---|
| 192 |
if ( NULL == remote_node ) |
|---|
| 193 |
{ |
|---|
| 194 |
vprDEBUG( gadgetDBG_NET_MGR,vprDBG_VERB_LVL ) |
|---|
| 195 |
<< clrOutBOLD( clrMAGENTA,"[Acceptor]" ) |
|---|
| 196 |
<< " Nodes not configured yet." |
|---|
| 197 |
<< std::endl << vprDEBUG_FLUSH; |
|---|
| 198 |
|
|---|
| 199 |
client_sock->close(); |
|---|
| 200 |
delete client_sock; |
|---|
| 201 |
client_sock = new vpr::SocketStream; |
|---|
| 202 |
} |
|---|
| 203 |
else |
|---|
| 204 |
{ |
|---|
| 205 |
|
|---|
| 206 |
const vpr::InetAddr local = vpr::InetAddr::getLocalHost(); |
|---|
| 207 |
const std::string local_hostname = local.getHostname(); |
|---|
| 208 |
cluster::ConnectionAck* temp = NULL; |
|---|
| 209 |
|
|---|
| 210 |
if( remote_node->getStatus() == Node::CONNECTED || |
|---|
| 211 |
remote_node->getStatus() == Node::NEWCONNECTION ) |
|---|
| 212 |
{ |
|---|
| 213 |
vprDEBUG( gadgetDBG_NET_MGR,vprDBG_STATE_LVL ) |
|---|
| 214 |
<< clrOutBOLD( clrMAGENTA, "[Acceptor]" ) |
|---|
| 215 |
<< " Node is already connected, no need to respond to request." |
|---|
| 216 |
<< std::endl << vprDEBUG_FLUSH; |
|---|
| 217 |
|
|---|
| 218 |
temp = new cluster::ConnectionAck( local_hostname, mListenAddr.getPort(), false ); |
|---|
| 219 |
} |
|---|
| 220 |
else if ( Node::PENDING == remote_node->getStatus() ) |
|---|
| 221 |
{ |
|---|
| 222 |
vprASSERT( NULL != remote_node && "Remote node must nut be equal to NULL" ); |
|---|
| 223 |
|
|---|
| 224 |
vprDEBUG( gadgetDBG_NET_MGR, vprDBG_STATE_LVL ) |
|---|
| 225 |
<< clrOutBOLD( clrMAGENTA, "[Acceptor]") |
|---|
| 226 |
<< " Pending Node exists, we must decide which" |
|---|
| 227 |
<< " side should initiate the connection." |
|---|
| 228 |
<< std::endl << vprDEBUG_FLUSH; |
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
vpr::Uint32 remote_value = client_sock->getRemoteAddr().getAddressValue(); |
|---|
| 232 |
vpr::Uint32 local_value = local.getAddressValue(); |
|---|
| 233 |
|
|---|
| 234 |
vprDEBUG( gadgetDBG_NET_MGR, vprDBG_STATE_LVL ) |
|---|
| 235 |
<< "Remote: " << remote_value |
|---|
| 236 |
<< std::endl << vprDEBUG_FLUSH; |
|---|
| 237 |
|
|---|
| 238 |
vprDEBUG( gadgetDBG_NET_MGR, vprDBG_STATE_LVL ) |
|---|
| 239 |
<< "Local: " << local_value |
|---|
| 240 |
<< std::endl << vprDEBUG_FLUSH; |
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
if ( remote_value < local_value ) |
|---|
| 244 |
{ |
|---|
| 245 |
vprDEBUG( gadgetDBG_NET_MGR, vprDBG_STATE_LVL ) |
|---|
| 246 |
<< "Result: (remote address < local address)" |
|---|
| 247 |
<< " Create NACK" << std::endl << vprDEBUG_FLUSH; |
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 |
temp = new cluster::ConnectionAck( local_hostname, mListenAddr.getPort(), false ); |
|---|
| 251 |
} |
|---|
| 252 |
else |
|---|
| 253 |
{ |
|---|
| 254 |
vprDEBUG( gadgetDBG_NET_MGR, vprDBG_STATE_LVL ) |
|---|
| 255 |
<< "Result: (remote address >= local address)" |
|---|
| 256 |
<< "Create ACK" << std::endl << vprDEBUG_FLUSH; |
|---|
| 257 |
|
|---|
| 258 |
temp = new cluster::ConnectionAck( local_hostname, mListenAddr.getPort(), true ); |
|---|
| 259 |
} |
|---|
| 260 |
} |
|---|
| 261 |
else |
|---|
| 262 |
{ |
|---|
| 263 |
vprDEBUG( gadgetDBG_NET_MGR, vprDBG_STATE_LVL ) |
|---|
| 264 |
<< clrOutBOLD( clrMAGENTA, "[Acceptor]" ) |
|---|
| 265 |
<< " Node is not pending, create ACK." |
|---|
| 266 |
<< std::endl << vprDEBUG_FLUSH; |
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 |
temp = new cluster::ConnectionAck( local_hostname, mListenAddr.getPort(), true ); |
|---|
| 270 |
} |
|---|
| 271 |
|
|---|
| 272 |
vprDEBUG( gadgetDBG_NET_MGR, vprDBG_STATE_LVL ) |
|---|
| 273 |
<< clrOutBOLD( clrMAGENTA,"[Acceptor]" ) |
|---|
| 274 |
<< " Set SockStream and send responce." |
|---|
| 275 |
<< std::endl << vprDEBUG_FLUSH; |
|---|
| 276 |
|
|---|
| 277 |
vpr::SocketStream* old_stream = remote_node->getSockStream(); |
|---|
| 278 |
remote_node->setSockStream( client_sock ); |
|---|
| 279 |
remote_node->send( temp ); |
|---|
| 280 |
remote_node->setSockStream( old_stream ); |
|---|
| 281 |
|
|---|
| 282 |
if ( temp->getAck() ) |
|---|
| 283 |
{ |
|---|
| 284 |
vprDEBUG( gadgetDBG_NET_MGR,vprDBG_STATE_LVL ) |
|---|
| 285 |
<< clrOutBOLD( clrMAGENTA,"[Acceptor]" ) |
|---|
| 286 |
<< " Set new Node as a NEWCONNECTION." |
|---|
| 287 |
<< std::endl << vprDEBUG_FLUSH; |
|---|
| 288 |
|
|---|
| 289 |
remote_node->setSockStream( client_sock ); |
|---|
| 290 |
|
|---|
| 291 |
|
|---|
| 292 |
|
|---|
| 293 |
|
|---|
| 294 |
|
|---|
| 295 |
|
|---|
| 296 |
|
|---|
| 297 |
remote_node->setStatus( Node::NEWCONNECTION ); |
|---|
| 298 |
|
|---|
| 299 |
remote_node->debugDump( vprDBG_CONFIG_LVL ); |
|---|
| 300 |
|
|---|
| 301 |
|
|---|
| 302 |
|
|---|
| 303 |
|
|---|
| 304 |
} |
|---|
| 305 |
else |
|---|
| 306 |
{ |
|---|
| 307 |
vprDEBUG( gadgetDBG_NET_MGR,vprDBG_STATE_LVL ) |
|---|
| 308 |
<< clrOutBOLD( clrMAGENTA,"[Acceptor]" ) |
|---|
| 309 |
<< " Delete unused client sock." |
|---|
| 310 |
<< std::endl << vprDEBUG_FLUSH; |
|---|
| 311 |
|
|---|
| 312 |
client_sock->close(); |
|---|
| 313 |
remote_node->setSockStream( NULL ); |
|---|
| 314 |
delete client_sock; |
|---|
| 315 |
} |
|---|
| 316 |
|
|---|
| 317 |
|
|---|
| 318 |
|
|---|
| 319 |
|
|---|
| 320 |
client_sock = new vpr::SocketStream; |
|---|
| 321 |
} |
|---|
| 322 |
} |
|---|
| 323 |
catch (vpr::IOException&) |
|---|
| 324 |
{ |
|---|
| 325 |
if ( client_sock->isOpen() ) |
|---|
| 326 |
{ |
|---|
| 327 |
client_sock->close(); |
|---|
| 328 |
} |
|---|
| 329 |
|
|---|
| 330 |
delete client_sock; |
|---|
| 331 |
client_sock = new vpr::SocketStream; |
|---|
| 332 |
} |
|---|
| 333 |
} |
|---|
| 334 |
} |
|---|
| 335 |
|
|---|
| 336 |
void Acceptor::shutdown() |
|---|
| 337 |
{ |
|---|
| 338 |
|
|---|
| 339 |
|
|---|
| 340 |
|
|---|
| 341 |
|
|---|
| 342 |
|
|---|
| 343 |
if ( mAcceptThread ) |
|---|
| 344 |
{ |
|---|
| 345 |
mAcceptThread->kill(); |
|---|
| 346 |
delete mAcceptThread; |
|---|
| 347 |
mAcceptThread = NULL; |
|---|
| 348 |
} |
|---|
| 349 |
} |
|---|
| 350 |
} |
|---|
| 351 |
|
|---|