2007-12-12 01:29:14 +00:00
/*
* libretroshare / src / pqi pqiservice . cc
*
* 3 P / PQI network interface for RetroShare .
*
2013-10-01 10:11:34 +00:00
* Copyright 2004 - 2013 by Robert Fernie .
2007-12-12 01:29:14 +00:00
*
* This library is free software ; you can redistribute it and / or
* modify it under the terms of the GNU Library General Public
2013-10-01 10:11:34 +00:00
* License Version 2.1 as published by the Free Software Foundation .
2007-12-12 01:29:14 +00:00
*
* 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
* Library General Public License for more details .
*
* You should have received a copy of the GNU Library General Public
* License along with this library ; if not , write to the Free Software
* Foundation , Inc . , 59 Temple Place , Suite 330 , Boston , MA 02111 - 1307
* USA .
*
* Please report all bugs and problems to " retroshare@lunamutt.com " .
*
*/
# include "pqi/pqiservice.h"
2008-07-10 16:29:18 +00:00
# include "util/rsdebug.h"
2012-04-14 00:30:23 +00:00
# include "util/rsstring.h"
2007-12-12 01:29:14 +00:00
const int pqiservicezone = 60478 ;
2008-07-10 16:29:18 +00:00
/****
* # define SERVICE_DEBUG 1
* * * */
2014-04-18 21:58:14 +00:00
void pqiService : : setServiceServer ( p3ServiceServerIface * server )
2013-10-01 10:11:34 +00:00
{
mServiceServer = server ;
}
bool pqiService : : send ( RsRawItem * item )
{
return mServiceServer - > sendItem ( item ) ;
}
2014-03-22 03:53:44 +00:00
p3ServiceServer : : p3ServiceServer ( pqiPublisher * pub , p3ServiceControl * ctrl ) : mPublisher ( pub ) , mServiceControl ( ctrl ) , srvMtx ( " p3ServiceServer " )
2007-12-12 01:29:14 +00:00
{
2008-11-22 13:15:07 +00:00
RsStackMutex stack ( srvMtx ) ; /********* LOCKED *********/
2008-07-10 16:29:18 +00:00
# ifdef SERVICE_DEBUG
2007-12-12 01:29:14 +00:00
pqioutput ( PQL_DEBUG_BASIC , pqiservicezone ,
" p3ServiceServer::p3ServiceServer() " ) ;
2008-07-10 16:29:18 +00:00
# endif
2007-12-12 01:29:14 +00:00
return ;
}
2014-03-22 03:53:44 +00:00
int p3ServiceServer : : addService ( pqiService * ts , bool defaultOn )
2007-12-12 01:29:14 +00:00
{
2008-11-22 13:15:07 +00:00
RsStackMutex stack ( srvMtx ) ; /********* LOCKED *********/
2008-07-10 16:29:18 +00:00
# ifdef SERVICE_DEBUG
2007-12-12 01:29:14 +00:00
pqioutput ( PQL_DEBUG_BASIC , pqiservicezone ,
" p3ServiceServer::addService() " ) ;
2008-07-10 16:29:18 +00:00
# endif
2007-12-12 01:29:14 +00:00
2014-03-22 03:53:44 +00:00
RsServiceInfo info = ts - > getServiceInfo ( ) ;
2008-01-21 09:22:42 +00:00
std : : map < uint32_t , pqiService * > : : iterator it ;
2014-03-22 03:53:44 +00:00
it = services . find ( info . mServiceType ) ;
2007-12-12 01:29:14 +00:00
if ( it ! = services . end ( ) )
{
2014-03-22 03:53:44 +00:00
std : : cerr < < " p3ServiceServer::addService(): Service already added with id " < < info . mServiceType < < " ! " < < std : : endl ;
2007-12-12 01:29:14 +00:00
// it exists already!
return - 1 ;
}
2013-10-01 10:11:34 +00:00
ts - > setServiceServer ( this ) ;
2014-03-22 03:53:44 +00:00
services [ info . mServiceType ] = ts ;
// This doesn't need to be in Mutex.
mServiceControl - > registerService ( info , defaultOn ) ;
2013-10-01 10:11:34 +00:00
2007-12-12 01:29:14 +00:00
return 1 ;
}
2015-06-16 14:20:59 +00:00
int p3ServiceServer : : removeService ( pqiService * ts )
{
RsStackMutex stack ( srvMtx ) ; /********* LOCKED *********/
# ifdef SERVICE_DEBUG
pqioutput ( PQL_DEBUG_BASIC , pqiservicezone , " p3ServiceServer::removeService() " ) ;
# endif
RsServiceInfo info = ts - > getServiceInfo ( ) ;
// This doesn't need to be in Mutex.
mServiceControl - > deregisterService ( info . mServiceType ) ;
std : : map < uint32_t , pqiService * > : : iterator it = services . find ( info . mServiceType ) ;
if ( it = = services . end ( ) )
{
std : : cerr < < " p3ServiceServer::removeService(): Service not found with id " < < info . mServiceType < < " ! " < < std : : endl ;
return - 1 ;
}
services . erase ( it ) ;
return 1 ;
}
2013-10-01 10:11:34 +00:00
bool p3ServiceServer : : recvItem ( RsRawItem * item )
2007-12-12 01:29:14 +00:00
{
2008-11-22 13:15:07 +00:00
RsStackMutex stack ( srvMtx ) ; /********* LOCKED *********/
2008-07-10 16:29:18 +00:00
# ifdef SERVICE_DEBUG
2014-04-27 13:14:07 +00:00
std : : cerr < < " p3ServiceServer::incoming() " ;
std : : cerr < < std : : endl ;
2007-12-12 01:29:14 +00:00
{
2012-04-14 00:30:23 +00:00
std : : string out ;
rs_sprintf ( out , " p3ServiceServer::incoming() PacketId: %x \n Looking for Service: %x \n Item: \n " , item - > PacketId ( ) , ( item - > PacketId ( ) & 0xffffff00 ) ) ;
item - > print_string ( out ) ;
2014-04-27 13:14:07 +00:00
std : : cerr < < out ;
std : : cerr < < std : : endl ;
2007-12-12 01:29:14 +00:00
}
2008-07-10 16:29:18 +00:00
# endif
2007-12-12 01:29:14 +00:00
2014-03-22 03:53:44 +00:00
// Packet Filtering.
// This doesn't need to be in Mutex.
if ( ! mServiceControl - > checkFilter ( item - > PacketId ( ) & 0xffffff00 , item - > PeerId ( ) ) )
{
2015-05-30 19:34:30 +00:00
# ifdef SERVICE_DEBUG
std : : cerr < < " p3ServiceServer::recvItem() Fails Filtering " < < std : : endl ;
# endif
2014-03-22 03:53:44 +00:00
delete item ;
return false ;
}
2008-01-21 09:22:42 +00:00
std : : map < uint32_t , pqiService * > : : iterator it ;
2007-12-12 01:29:14 +00:00
it = services . find ( item - > PacketId ( ) & 0xffffff00 ) ;
if ( it = = services . end ( ) )
{
2008-07-10 16:29:18 +00:00
# ifdef SERVICE_DEBUG
2014-04-27 13:14:07 +00:00
std : : cerr < < " p3ServiceServer::incoming() Service: No Service - deleting " ;
std : : cerr < < std : : endl ;
2008-07-10 16:29:18 +00:00
# endif
2007-12-12 01:29:14 +00:00
delete item ;
2013-10-01 10:11:34 +00:00
return false ;
2007-12-12 01:29:14 +00:00
}
{
2008-07-10 16:29:18 +00:00
# ifdef SERVICE_DEBUG
2014-04-27 13:14:07 +00:00
std : : cerr < < " p3ServiceServer::incoming() Sending to : " < < ( void * ) it - > second ;
std : : cerr < < std : : endl ;
2008-07-10 16:29:18 +00:00
# endif
2007-12-12 01:29:14 +00:00
2013-10-01 10:11:34 +00:00
return ( it - > second ) - > recv ( item ) ;
2007-12-12 01:29:14 +00:00
}
delete item ;
2013-10-01 10:11:34 +00:00
return false ;
2007-12-12 01:29:14 +00:00
}
2013-10-01 10:11:34 +00:00
bool p3ServiceServer : : sendItem ( RsRawItem * item )
2007-12-12 01:29:14 +00:00
{
2008-07-10 16:29:18 +00:00
# ifdef SERVICE_DEBUG
2013-10-01 10:11:34 +00:00
std : : cerr < < " p3ServiceServer::sendItem() " ;
std : : cerr < < std : : endl ;
2014-04-27 13:14:07 +00:00
item - > print ( std : : cerr ) ;
2013-10-01 10:11:34 +00:00
std : : cerr < < std : : endl ;
2008-07-10 16:29:18 +00:00
# endif
2014-03-22 03:53:44 +00:00
if ( ! item )
{
std : : cerr < < " p3ServiceServer::sendItem() Caught Null item " ;
std : : cerr < < std : : endl ;
return false ;
}
2007-12-12 01:29:14 +00:00
2014-03-22 03:53:44 +00:00
// Packet Filtering.
if ( ! mServiceControl - > checkFilter ( item - > PacketId ( ) & 0xffffff00 , item - > PeerId ( ) ) )
{
2015-07-12 21:51:17 +00:00
std : : cerr < < " p3ServiceServer::sendItem() Fails Filtering for packet id= " < < std : : hex < < item - > PacketId ( ) < < std : : dec < < " , and peer " < < item - > PeerId ( ) < < std : : endl ;
2014-03-22 03:53:44 +00:00
delete item ;
return false ;
}
2007-12-12 01:29:14 +00:00
2013-10-01 10:11:34 +00:00
mPublisher - > sendItem ( item ) ;
return true ;
2007-12-12 01:29:14 +00:00
}
int p3ServiceServer : : tick ( )
{
2008-07-10 16:29:18 +00:00
2014-03-23 03:30:59 +00:00
mServiceControl - > tick ( ) ;
2008-11-22 13:15:07 +00:00
RsStackMutex stack ( srvMtx ) ; /********* LOCKED *********/
2008-07-10 16:29:18 +00:00
# ifdef SERVICE_DEBUG
2007-12-12 01:29:14 +00:00
pqioutput ( PQL_DEBUG_ALL , pqiservicezone ,
" p3ServiceServer::tick() " ) ;
2008-07-10 16:29:18 +00:00
# endif
2007-12-12 01:29:14 +00:00
2008-01-21 09:22:42 +00:00
std : : map < uint32_t , pqiService * > : : iterator it ;
2007-12-12 01:29:14 +00:00
// from the beginning to where we started.
2014-10-24 22:07:26 +00:00
for ( it = services . begin ( ) ; it ! = services . end ( ) ; + + it )
2007-12-12 01:29:14 +00:00
{
2008-07-10 16:29:18 +00:00
# ifdef SERVICE_DEBUG
2012-04-14 00:30:23 +00:00
std : : string out ;
rs_sprintf ( out , " p3ServiceServer::service id: %u -> Service: %p " , it - > first , it - > second ) ;
pqioutput ( PQL_DEBUG_ALL , pqiservicezone , out ) ;
2008-07-10 16:29:18 +00:00
# endif
2007-12-12 01:29:14 +00:00
// now we should actually tick the service.
( it - > second ) - > tick ( ) ;
}
return 1 ;
}