2007-12-11 20:43:17 -05:00
|
|
|
/*
|
|
|
|
* libretroshare/src/services p3service.cc
|
|
|
|
*
|
|
|
|
* 3P/PQI network interface for RetroShare.
|
|
|
|
*
|
|
|
|
* Copyright 2004-2008 by Robert Fernie.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License Version 2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* 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/pqi.h"
|
|
|
|
#include "services/p3service.h"
|
2008-04-15 12:35:46 -04:00
|
|
|
#include <sstream>
|
|
|
|
#include <iomanip>
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2008-07-10 12:29:18 -04:00
|
|
|
/*****
|
|
|
|
* #define SERV_DEBUG 1
|
|
|
|
****/
|
2008-02-04 12:55:13 -05:00
|
|
|
|
2007-12-11 20:43:17 -05:00
|
|
|
void p3Service::addSerialType(RsSerialType *st)
|
|
|
|
{
|
|
|
|
rsSerialiser->addSerialType(st);
|
|
|
|
}
|
|
|
|
|
|
|
|
RsItem *p3Service::recvItem()
|
|
|
|
{
|
|
|
|
srvMtx.lock(); /***** LOCK MUTEX *****/
|
|
|
|
|
|
|
|
if (recv_queue.size() == 0)
|
|
|
|
{
|
|
|
|
srvMtx.unlock(); /***** UNLOCK MUTEX *****/
|
|
|
|
return NULL; /* nothing there! */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get something off front */
|
|
|
|
RsItem *item = recv_queue.front();
|
|
|
|
recv_queue.pop_front();
|
|
|
|
|
|
|
|
srvMtx.unlock(); /***** UNLOCK MUTEX *****/
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool p3Service::receivedItems()
|
|
|
|
{
|
|
|
|
srvMtx.lock(); /***** LOCK MUTEX *****/
|
|
|
|
|
|
|
|
bool moreData = (recv_queue.size() != 0);
|
|
|
|
|
|
|
|
srvMtx.unlock(); /***** UNLOCK MUTEX *****/
|
|
|
|
|
|
|
|
return moreData;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int p3Service::sendItem(RsItem *item)
|
|
|
|
{
|
|
|
|
srvMtx.lock(); /***** LOCK MUTEX *****/
|
|
|
|
|
|
|
|
send_queue.push_back(item);
|
|
|
|
|
|
|
|
srvMtx.unlock(); /***** UNLOCK MUTEX *****/
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// overloaded pqiService interface.
|
|
|
|
int p3Service::receive(RsRawItem *raw)
|
|
|
|
{
|
|
|
|
srvMtx.lock(); /***** LOCK MUTEX *****/
|
|
|
|
|
2008-02-04 12:55:13 -05:00
|
|
|
#ifdef SERV_DEBUG
|
|
|
|
std::cerr << "p3Service::receive()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2007-12-11 20:43:17 -05:00
|
|
|
|
|
|
|
/* convert to RsServiceItem */
|
|
|
|
uint32_t size = raw->getRawLength();
|
|
|
|
RsItem *item = rsSerialiser->deserialise(raw->getRawData(), &size);
|
|
|
|
if ((!item) || (size != raw->getRawLength()))
|
|
|
|
{
|
|
|
|
/* error in conversion */
|
2008-02-04 12:55:13 -05:00
|
|
|
#ifdef SERV_DEBUG
|
2007-12-11 20:43:17 -05:00
|
|
|
std::cerr << "p3Service::receive() Error" << std::endl;
|
|
|
|
std::cerr << "p3Service::receive() Size: " << size << std::endl;
|
|
|
|
std::cerr << "p3Service::receive() RawLength: " << raw->getRawLength() << std::endl;
|
2008-02-04 12:55:13 -05:00
|
|
|
#endif
|
|
|
|
|
2007-12-11 20:43:17 -05:00
|
|
|
if (item)
|
|
|
|
{
|
2008-02-04 12:55:13 -05:00
|
|
|
#ifdef SERV_DEBUG
|
|
|
|
std::cerr << "p3Service::receive() Bad Item:";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
item->print(std::cerr, 0);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2007-12-11 20:43:17 -05:00
|
|
|
delete item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* if we have something - pass it on */
|
|
|
|
if (item)
|
|
|
|
{
|
2008-02-04 12:55:13 -05:00
|
|
|
#ifdef SERV_DEBUG
|
|
|
|
std::cerr << "p3Service::receive() item:";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
item->print(std::cerr, 0);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2007-12-11 20:43:17 -05:00
|
|
|
/* ensure PeerId is transferred */
|
|
|
|
item->PeerId(raw->PeerId());
|
|
|
|
recv_queue.push_back(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cleanup input */
|
|
|
|
delete raw;
|
|
|
|
|
|
|
|
srvMtx.unlock(); /***** UNLOCK MUTEX *****/
|
|
|
|
|
|
|
|
return (item != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
RsRawItem *p3Service::send()
|
|
|
|
{
|
|
|
|
srvMtx.lock(); /***** LOCK MUTEX *****/
|
|
|
|
|
|
|
|
if (send_queue.size() == 0)
|
|
|
|
{
|
|
|
|
srvMtx.unlock(); /***** UNLOCK MUTEX *****/
|
|
|
|
return NULL; /* nothing there! */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get something off front */
|
|
|
|
RsItem *si = send_queue.front();
|
|
|
|
send_queue.pop_front();
|
|
|
|
|
2008-02-04 12:55:13 -05:00
|
|
|
#ifdef SERV_DEBUG
|
|
|
|
std::cerr << "p3Service::send() Sending item:";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
si->print(std::cerr, 0);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2007-12-11 20:43:17 -05:00
|
|
|
/* try to convert */
|
|
|
|
uint32_t size = rsSerialiser->size(si);
|
|
|
|
if (!size)
|
|
|
|
{
|
2008-02-04 12:55:13 -05:00
|
|
|
#ifdef SERV_DEBUG
|
|
|
|
std::cerr << "p3Service::send() ERROR size == 0";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2007-12-11 20:43:17 -05:00
|
|
|
/* can't convert! */
|
|
|
|
delete si;
|
|
|
|
srvMtx.unlock(); /***** UNLOCK MUTEX *****/
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
RsRawItem *raw = new RsRawItem(si->PacketId(), size);
|
|
|
|
if (!rsSerialiser->serialise(si, raw->getRawData(), &size))
|
|
|
|
{
|
2008-02-04 12:55:13 -05:00
|
|
|
#ifdef SERV_DEBUG
|
|
|
|
std::cerr << "p3Service::send() ERROR serialise failed";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2007-12-11 20:43:17 -05:00
|
|
|
delete raw;
|
|
|
|
raw = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((raw) && (size != raw->getRawLength()))
|
|
|
|
{
|
2008-02-04 12:55:13 -05:00
|
|
|
#ifdef SERV_DEBUG
|
|
|
|
std::cerr << "p3Service::send() ERROR serialise size mismatch";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2007-12-11 20:43:17 -05:00
|
|
|
delete raw;
|
|
|
|
raw = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ensure PeerId is transferred */
|
2009-05-25 07:38:47 -04:00
|
|
|
if (raw)
|
|
|
|
{
|
|
|
|
raw->PeerId(si->PeerId());
|
|
|
|
}
|
2007-12-11 20:43:17 -05:00
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
delete si;
|
|
|
|
|
|
|
|
srvMtx.unlock(); /***** UNLOCK MUTEX *****/
|
2009-12-13 16:59:26 -05:00
|
|
|
#ifdef SERV_DEBUG
|
|
|
|
std::cerr << "p3Service::send() returning RawItem.";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2007-12-11 20:43:17 -05:00
|
|
|
return raw;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-15 12:35:46 -04:00
|
|
|
std::string generateRandomServiceId()
|
|
|
|
{
|
|
|
|
std::ostringstream out;
|
|
|
|
out << std::hex;
|
|
|
|
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
|
|
|
|
#ifndef WINDOWS_SYS
|
|
|
|
/* 4 bytes per random number: 4 x 4 = 16 bytes */
|
|
|
|
for(int i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
out << std::setw(8) << std::setfill('0');
|
|
|
|
uint32_t rint = random();
|
|
|
|
out << rint;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
srand(time(NULL));
|
|
|
|
/* 2 bytes per random number: 8 x 2 = 16 bytes */
|
|
|
|
for(int i = 0; i < 8; i++)
|
|
|
|
{
|
|
|
|
out << std::setw(4) << std::setfill('0');
|
|
|
|
uint16_t rint = rand(); /* only gives 16 bits */
|
|
|
|
out << rint;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
|
|
|
|
return out.str();
|
|
|
|
}
|
|
|
|
|
2007-12-11 20:43:17 -05:00
|
|
|
|