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 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
|
//------------------------------------------------------------------------------
// Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN)
// Author: Lukasz Janyst <ljanyst@cern.ch>
//------------------------------------------------------------------------------
// XRootD 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 3 of the License, or
// (at your option) any later version.
//
// XRootD 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 Lesser General Public License
// along with XRootD. If not, see <http://www.gnu.org/licenses/>.
//------------------------------------------------------------------------------
#include "Server.hh"
#include "Utils.hh"
#include "XrdNet/XrdNetUtils.hh"
#include "XrdCl/XrdClLog.hh"
#include "TestEnv.hh"
#include <cerrno>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
namespace XrdClTests
{
//----------------------------------------------------------------------------
// Define the client helper
//----------------------------------------------------------------------------
struct ClientHelper
{
ClientHandler *handler;
int socket;
pthread_t thread;
std::string name;
};
}
//------------------------------------------------------------------------------
// Stuff that needs C linkage
//------------------------------------------------------------------------------
extern "C"
{
void *HandleConnections( void *arg )
{
XrdClTests::Server *srv = (XrdClTests::Server*)arg;
long ret = srv->HandleConnections();
return (void *)ret;
}
void *HandleClient( void *arg )
{
XrdClTests::ClientHelper *helper = (XrdClTests::ClientHelper*)arg;
helper->handler->HandleConnection( helper->socket );
return 0;
}
}
namespace XrdClTests {
//------------------------------------------------------------------------------
// Constructor
//------------------------------------------------------------------------------
ClientHandler::ClientHandler(): pSentBytes(0), pReceivedBytes(0)
{
pSentChecksum = Utils::ComputeCRC32( 0, 0 );
pReceivedChecksum = Utils::ComputeCRC32( 0, 0 );
}
//------------------------------------------------------------------------------
// Destructor
//------------------------------------------------------------------------------
ClientHandler::~ClientHandler()
{
}
//------------------------------------------------------------------------------
// Update statistics of the received data
//------------------------------------------------------------------------------
void ClientHandler::UpdateSentData( char *buffer, uint32_t size )
{
pSentBytes += size;
pSentChecksum = Utils::UpdateCRC32( pSentChecksum, buffer, size );
}
//------------------------------------------------------------------------------
// Update statistics of the sent data
//------------------------------------------------------------------------------
void ClientHandler::UpdateReceivedData( char *buffer, uint32_t size )
{
pReceivedBytes += size;
pReceivedChecksum = Utils::UpdateCRC32( pReceivedChecksum, buffer, size );
}
//------------------------------------------------------------------------------
// Constructor
//------------------------------------------------------------------------------
Server::Server( ProtocolFamily family ):
pServerThread(0), pListenSocket(-1), pHandlerFactory(0),
pProtocolFamily( family ), pPort(0)
{
}
//------------------------------------------------------------------------------
// Destructor
//------------------------------------------------------------------------------
Server::~Server()
{
delete pHandlerFactory;
close( pListenSocket );
}
//------------------------------------------------------------------------------
// Listen for incoming connections and handle clients
//------------------------------------------------------------------------------
bool Server::Setup( int port, int accept, ClientHandlerFactory *factory )
{
XrdCl::Log *log = TestEnv::GetLog();
log->Debug( 1, "Seting up the server, port: %d, clients: %d", port, accept );
//----------------------------------------------------------------------------
// Set up the handler factory
//----------------------------------------------------------------------------
if( !factory )
{
log->Error( 1, "Invalid client handler factory" );
return false;
}
pHandlerFactory = factory;
//----------------------------------------------------------------------------
// Create the socket
//----------------------------------------------------------------------------
int protocolFamily = AF_INET;
if( pProtocolFamily == Inet6 || pProtocolFamily == Both )
protocolFamily = AF_INET6;
pListenSocket = socket( protocolFamily, SOCK_STREAM, 0 );
if( pListenSocket < 0 )
{
log->Error( 1, "Unable to create listening socket: %s", strerror( errno ) );
return false;
}
int optVal = 1;
if( setsockopt( pListenSocket, SOL_SOCKET, SO_REUSEADDR,
&optVal, sizeof(optVal) ) == -1 )
{
log->Error( 1, "Unable to set the REUSEADDR option: %s", strerror( errno ) );
return false;
}
if( pProtocolFamily == Both )
{
optVal = 0;
if( setsockopt( pListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, &optVal,
sizeof(optVal) ) == -1 )
{
log->Error( 1, "Unable to disable the IPV6_V6ONLY option: %s",
strerror( errno ) );
return false;
}
}
//----------------------------------------------------------------------------
// Bind the socket
//----------------------------------------------------------------------------
sockaddr *servAddr = 0;
sockaddr_in6 servAddr6;
sockaddr_in servAddr4;
socklen_t servAddrSize = 0;
if( pProtocolFamily == Inet4 )
{
memset( &servAddr4, 0, sizeof(servAddr4) );
servAddr4.sin_family = AF_INET;
servAddr4.sin_addr.s_addr = htonl(INADDR_ANY);
servAddr4.sin_port = htons(port);
servAddr = (sockaddr*)&servAddr4;
servAddrSize = sizeof(servAddr4);
}
else
{
memset( &servAddr6, 0, sizeof(servAddr6) );
servAddr6.sin6_family = AF_INET6;
servAddr6.sin6_addr = in6addr_any;
servAddr6.sin6_port = htons( port );
servAddr = (sockaddr*)&servAddr6;
servAddrSize = sizeof(servAddr6);
}
if( bind( pListenSocket, servAddr, servAddrSize ) < 0 )
{
log->Error( 1, "Unable to bind the socket: %s", strerror( errno ) );
return false;
}
if( listen( pListenSocket, accept ) < 0 )
{
log->Error( 1, "Unable to listen on the socket: %s", strerror( errno ) );
return false;
}
if( port == 0 ) {
if( getsockname(pListenSocket, servAddr, &servAddrSize) ) {
log->Error( 1, "Unable to determine server port: %s", strerror( errno ) );
return false;
}
pPort = pProtocolFamily == Inet4 ?
ntohs(servAddr4.sin_port) : ntohs(servAddr6.sin6_port);
} else {
pPort = port;
}
pClients.resize( accept, 0 );
return true;
}
//------------------------------------------------------------------------------
// Start the server
//------------------------------------------------------------------------------
bool Server::Start()
{
XrdCl::Log *log = TestEnv::GetLog();
log->Debug( 1, "Spawning the server thread" );
if( pthread_create( &pServerThread, 0, ::HandleConnections, this ) < 0 )
{
log->Error( 1, "Unable to spawn the server thread: %s", strerror(errno) );
return false;
}
return true;
}
//------------------------------------------------------------------------------
// Stop the server
//------------------------------------------------------------------------------
bool Server::Stop()
{
XrdCl::Log *log = TestEnv::GetLog();
log->Debug( 1, "Waiting for the server thread to finish" );
long ret;
if( pthread_join( pServerThread, (void**)&ret ) < 0 )
{
log->Error( 1, "Unable to join the server thread: %s", strerror(errno) );
return false;
}
if( ret < 0 )
return false;
return true;
}
//------------------------------------------------------------------------------
// Get the statisctics of the sent data
//------------------------------------------------------------------------------
std::pair<uint64_t, uint32_t> Server::GetSentStats( const std::string host) const
{
TransferMap::const_iterator it = pSent.find( host );
if( it == pSent.end() )
return std::make_pair( 0, 0 );
return it->second;
}
//------------------------------------------------------------------------------
// Get the stats of the received data
//------------------------------------------------------------------------------
std::pair<uint64_t, uint32_t> Server::GetReceivedStats( const std::string host ) const
{
TransferMap::const_iterator it = pReceived.find( host );
if( it == pReceived.end() )
return std::make_pair( 0, 0 );
return it->second;
}
//------------------------------------------------------------------------------
// Handle clients
//------------------------------------------------------------------------------
int Server::HandleConnections()
{
XrdCl::Log *log = TestEnv::GetLog();
//----------------------------------------------------------------------------
// Initiate the connections
//----------------------------------------------------------------------------
for( uint32_t i = 0; i < pClients.size(); ++i )
{
//--------------------------------------------------------------------------
// Accept the connection
//--------------------------------------------------------------------------
sockaddr *inetAddr = 0;
socklen_t inetLen = 0;
sockaddr_in inetAddr4;
sockaddr_in6 inetAddr6;
if( pProtocolFamily == Inet4 )
{
inetAddr = (sockaddr*)&inetAddr4;
inetLen = sizeof( inetAddr4 );
}
else
{
inetAddr = (sockaddr*)&inetAddr6;
inetLen = sizeof( inetAddr6 );
}
int connectionSocket = accept( pListenSocket, inetAddr, &inetLen );
if( connectionSocket < 0 )
{
log->Error( 1, "Unable to accept a connection: %s", strerror( errno ) );
return 1;
}
//--------------------------------------------------------------------------
// Create the handler
//--------------------------------------------------------------------------
ClientHandler *handler = pHandlerFactory->CreateHandler();
char nameBuff[1024];
ClientHelper *helper = new ClientHelper();
XrdNetUtils::IPFormat( connectionSocket, nameBuff, sizeof(nameBuff) );
log->Debug( 1, "Accepted a connection from %s", nameBuff );
//--------------------------------------------------------------------------
// Spawn the thread
//--------------------------------------------------------------------------
helper->name = nameBuff;
helper->handler = handler;
helper->socket = connectionSocket;
if( pthread_create( &helper->thread, 0, ::HandleClient, helper ) < 0 )
{
log->Error( 1, "Unable to spawn a new thread for client no %d: %s",
i, nameBuff );
delete handler;
close( connectionSocket );
delete helper;
helper = 0;
}
pClients[i] = helper;
}
//----------------------------------------------------------------------------
// Wait forr the clients to finish
//----------------------------------------------------------------------------
std::vector<ClientHelper*>::iterator it;
for( it = pClients.begin(); it != pClients.end(); ++it )
{
//--------------------------------------------------------------------------
// Have we managed to start properly?
//--------------------------------------------------------------------------
if( *it == 0 )
{
log->Debug( 1, "Skipping client that falied to start" );
continue;
}
//--------------------------------------------------------------------------
// Join the client thread
//--------------------------------------------------------------------------
if( pthread_join( (*it)->thread, 0 ) < 0 )
log->Error( 1, "Unable to join the clint thread for: %s",
(*it)->name.c_str() );
pSent[(*it)->name] = std::make_pair(
(*it)->handler->GetSentBytes(),
(*it)->handler->GetSentChecksum() );
pReceived[(*it)->name] = std::make_pair(
(*it)->handler->GetReceivedBytes(),
(*it)->handler->GetReceivedChecksum() );
delete (*it)->handler;
delete *it;
}
return 0;
}
int Server::GetPort() const
{
return pPort;
}
}
|