2007-12-11 20:29:14 -05:00
|
|
|
/*
|
|
|
|
* libretroshare/src/pqi pqiservice.cc
|
|
|
|
*
|
|
|
|
* 3P/PQI network interface for RetroShare.
|
|
|
|
*
|
2013-10-01 06:11:34 -04:00
|
|
|
* Copyright 2004-2013 by Robert Fernie.
|
2007-12-11 20:29:14 -05: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 06:11:34 -04:00
|
|
|
* License Version 2.1 as published by the Free Software Foundation.
|
2007-12-11 20:29:14 -05: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 12:29:18 -04:00
|
|
|
#include "util/rsdebug.h"
|
2012-04-13 20:30:23 -04:00
|
|
|
#include "util/rsstring.h"
|
2007-12-11 20:29:14 -05:00
|
|
|
|
|
|
|
const int pqiservicezone = 60478;
|
|
|
|
|
2008-07-10 12:29:18 -04:00
|
|
|
/****
|
|
|
|
* #define SERVICE_DEBUG 1
|
|
|
|
****/
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
void pqiService::setServiceServer(p3ServiceServer *server)
|
|
|
|
{
|
|
|
|
mServiceServer = server;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool pqiService::send(RsRawItem *item)
|
|
|
|
{
|
|
|
|
return mServiceServer->sendItem(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
p3ServiceServer::p3ServiceServer(pqiPublisher *pub) : mPublisher(pub), srvMtx("p3ServiceServer")
|
2007-12-11 20:29:14 -05:00
|
|
|
{
|
2008-11-22 08:15:07 -05:00
|
|
|
RsStackMutex stack(srvMtx); /********* LOCKED *********/
|
2008-07-10 12:29:18 -04:00
|
|
|
|
|
|
|
#ifdef SERVICE_DEBUG
|
2007-12-11 20:29:14 -05:00
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqiservicezone,
|
|
|
|
"p3ServiceServer::p3ServiceServer()");
|
2008-07-10 12:29:18 -04:00
|
|
|
#endif
|
2007-12-11 20:29:14 -05:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int p3ServiceServer::addService(pqiService *ts)
|
|
|
|
{
|
2008-11-22 08:15:07 -05:00
|
|
|
RsStackMutex stack(srvMtx); /********* LOCKED *********/
|
|
|
|
|
2008-07-10 12:29:18 -04:00
|
|
|
#ifdef SERVICE_DEBUG
|
2007-12-11 20:29:14 -05:00
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqiservicezone,
|
|
|
|
"p3ServiceServer::addService()");
|
2008-07-10 12:29:18 -04:00
|
|
|
#endif
|
2007-12-11 20:29:14 -05:00
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
|
2008-01-21 04:22:42 -05:00
|
|
|
std::map<uint32_t, pqiService *>::iterator it;
|
2007-12-11 20:29:14 -05:00
|
|
|
it = services.find(ts -> getType());
|
|
|
|
if (it != services.end())
|
|
|
|
{
|
|
|
|
// it exists already!
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
ts->setServiceServer(this);
|
2007-12-11 20:29:14 -05:00
|
|
|
services[ts -> getType()] = ts;
|
2013-10-01 06:11:34 -04:00
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
bool p3ServiceServer::recvItem(RsRawItem *item)
|
2007-12-11 20:29:14 -05:00
|
|
|
{
|
2008-11-22 08:15:07 -05:00
|
|
|
RsStackMutex stack(srvMtx); /********* LOCKED *********/
|
|
|
|
|
2008-07-10 12:29:18 -04:00
|
|
|
#ifdef SERVICE_DEBUG
|
2007-12-11 20:29:14 -05:00
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqiservicezone,
|
|
|
|
"p3ServiceServer::incoming()");
|
|
|
|
|
|
|
|
{
|
2012-04-13 20:30:23 -04:00
|
|
|
std::string out;
|
|
|
|
rs_sprintf(out, "p3ServiceServer::incoming() PacketId: %x\nLooking for Service: %x\nItem:\n", item -> PacketId(), (item -> PacketId() & 0xffffff00));
|
|
|
|
item -> print_string(out);
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqiservicezone, out);
|
2007-12-11 20:29:14 -05:00
|
|
|
}
|
2008-07-10 12:29:18 -04:00
|
|
|
#endif
|
2007-12-11 20:29:14 -05:00
|
|
|
|
2008-01-21 04:22:42 -05:00
|
|
|
std::map<uint32_t, pqiService *>::iterator it;
|
2007-12-11 20:29:14 -05:00
|
|
|
it = services.find(item -> PacketId() & 0xffffff00);
|
|
|
|
if (it == services.end())
|
|
|
|
{
|
2008-07-10 12:29:18 -04:00
|
|
|
#ifdef SERVICE_DEBUG
|
2007-12-11 20:29:14 -05:00
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqiservicezone,
|
|
|
|
"p3ServiceServer::incoming() Service: No Service - deleting");
|
2008-07-10 12:29:18 -04:00
|
|
|
#endif
|
2007-12-11 20:29:14 -05:00
|
|
|
delete item;
|
2013-10-01 06:11:34 -04:00
|
|
|
return false;
|
2007-12-11 20:29:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2008-07-10 12:29:18 -04:00
|
|
|
#ifdef SERVICE_DEBUG
|
2012-04-13 20:30:23 -04:00
|
|
|
std::string out;
|
|
|
|
rs_sprintf(out, "p3ServiceServer::incoming() Sending to %p", it -> second);
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqiservicezone, out);
|
2008-07-10 12:29:18 -04:00
|
|
|
#endif
|
2007-12-11 20:29:14 -05:00
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
return (it->second) -> recv(item);
|
2007-12-11 20:29:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
delete item;
|
2013-10-01 06:11:34 -04:00
|
|
|
return false;
|
2007-12-11 20:29:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
bool p3ServiceServer::sendItem(RsRawItem *item)
|
2007-12-11 20:29:14 -05:00
|
|
|
{
|
2008-07-10 12:29:18 -04:00
|
|
|
#ifdef SERVICE_DEBUG
|
2013-10-01 06:11:34 -04:00
|
|
|
std::cerr << "p3ServiceServer::sendItem()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
item -> print_string(out);
|
|
|
|
std::cerr << std::endl;
|
2008-07-10 12:29:18 -04:00
|
|
|
#endif
|
2007-12-11 20:29:14 -05:00
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
/* any filtering ??? */
|
2007-12-11 20:29:14 -05:00
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
mPublisher->sendItem(item);
|
|
|
|
return true;
|
2007-12-11 20:29:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int p3ServiceServer::tick()
|
|
|
|
{
|
2008-07-10 12:29:18 -04:00
|
|
|
|
2008-11-22 08:15:07 -05:00
|
|
|
RsStackMutex stack(srvMtx); /********* LOCKED *********/
|
|
|
|
|
2008-07-10 12:29:18 -04:00
|
|
|
#ifdef SERVICE_DEBUG
|
2007-12-11 20:29:14 -05:00
|
|
|
pqioutput(PQL_DEBUG_ALL, pqiservicezone,
|
|
|
|
"p3ServiceServer::tick()");
|
2008-07-10 12:29:18 -04:00
|
|
|
#endif
|
2007-12-11 20:29:14 -05:00
|
|
|
|
2008-01-21 04:22:42 -05:00
|
|
|
std::map<uint32_t, pqiService *>::iterator it;
|
2007-12-11 20:29:14 -05:00
|
|
|
|
|
|
|
// from the beginning to where we started.
|
|
|
|
for(it = services.begin();it != services.end(); it++)
|
|
|
|
{
|
2008-07-10 12:29:18 -04:00
|
|
|
|
|
|
|
#ifdef SERVICE_DEBUG
|
2012-04-13 20:30:23 -04: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 12:29:18 -04:00
|
|
|
#endif
|
2007-12-11 20:29:14 -05:00
|
|
|
|
|
|
|
// now we should actually tick the service.
|
|
|
|
(it -> second) -> tick();
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|