Final changes to add the new serialiser.

- added Chat / Msg and Disc services.
- expanded rsiface to handle new serialiser.
- mods to rsserver with new conversions etc.
- added service directory to Makefile.
- removed PROXY / CHANNELS from make.opt



git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@276 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
drbob 2007-12-12 01:43:17 +00:00
parent d9efb76b01
commit 46a001af47
19 changed files with 3605 additions and 156 deletions

View file

@ -0,0 +1,25 @@
RS_TOP_DIR = ..
##### Define any flags that are needed for this section #######
###############################################################
###############################################################
include $(RS_TOP_DIR)/scripts/config.mk
###############################################################
RSOBJ = p3service.o p3disc.o p3chatservice.o p3msgservice.o
#TESTOBJ =
#TESTS =
all: librs tests
#tlvbase_test : tlvbase_test.o
# $(CC) $(CFLAGS) -o tlvbase_test tlvbase_test.o $(OBJ) $(LIBS)
###############################################################
include $(RS_TOP_DIR)/scripts/rules.mk
###############################################################

View file

@ -0,0 +1,145 @@
/*
* "$Id: p3ChatService.cc,v 1.24 2007-05-05 16:10:06 rmf24 Exp $"
*
* Other Bits for RetroShare.
*
* Copyright 2004-2006 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 "services/p3chatservice.h"
#include "pqi/pqidebug.h"
#include <sstream>
/**************** PQI_USE_XPGP ******************/
#if defined(PQI_USE_XPGP)
#include "pqi/xpgpcert.h"
#else /* X509 Certificates */
/**************** PQI_USE_XPGP ******************/
#include "pqi/sslcert.h"
#endif /* X509 Certificates */
/**************** PQI_USE_XPGP ******************/
const int p3chatzone = 1745;
p3ChatService::p3ChatService()
:p3Service(RS_SERVICE_TYPE_CHAT)
{
addSerialType(new RsChatSerialiser());
}
int p3ChatService::tick()
{
pqioutput(PQL_DEBUG_BASIC, p3chatzone,
"p3ChatService::tick()");
return 0;
}
int p3ChatService::status()
{
pqioutput(PQL_DEBUG_BASIC, p3chatzone,
"p3ChatService::status()");
return 1;
}
/***************** Chat Stuff **********************/
int p3ChatService::sendChat(std::string msg)
{
/* go through all the peers */
sslroot *sslr = getSSLRoot();
std::list<cert *>::iterator it;
std::list<cert *> &certs = sslr -> getCertList();
for(it = certs.begin(); it != certs.end(); it++)
{
pqioutput(PQL_DEBUG_BASIC, p3chatzone,
"p3ChatService::sendChat()");
RsChatItem *ci = new RsChatItem();
ci->PeerId((*it)->PeerId());
ci->chatFlags = 0;
ci->sendTime = time(NULL);
ci->message = msg;
{
std::ostringstream out;
out << "Chat Item we are sending:" << std::endl;
ci -> print(out);
pqioutput(PQL_DEBUG_BASIC, p3chatzone, out.str());
}
sendItem(ci);
}
return 1;
}
int p3ChatService::sendPrivateChat(std::string msg, std::string id)
{
// make chat item....
pqioutput(PQL_DEBUG_BASIC, p3chatzone,
"p3ChatService::sendPrivateChat()");
RsChatItem *ci = new RsChatItem();
ci->PeerId(id);
ci->chatFlags = RS_CHAT_FLAG_PRIVATE;
ci->sendTime = time(NULL);
ci->message = msg;
{
std::ostringstream out;
out << "Private Chat Item we are sending:" << std::endl;
ci -> print(out);
out << "Sending to:" << id << std::endl;
pqioutput(PQL_DEBUG_BASIC, p3chatzone, out.str());
}
sendItem(ci);
return 1;
}
std::list<RsChatItem *> p3ChatService::getChatQueue()
{
time_t now = time(NULL);
RsChatItem *ci = NULL;
std::list<RsChatItem *> ilist;
while(NULL != (ci = (RsChatItem *) recvItem()))
{
ci->recvTime = now;
ilist.push_back(ci);
}
return ilist;
}

View file

@ -0,0 +1,57 @@
/*
* libretroshare/src/services chatservice.h
*
* Services 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".
*
*/
#ifndef SERVICE_CHAT_HEADER
#define SERVICE_CHAT_HEADER
/*
* The basic Chat service.
*
*/
#include <list>
#include <string>
#include "serialiser/rsmsgitems.h"
#include "services/p3service.h"
class p3ChatService: public p3Service
{
public:
p3ChatService();
/* overloaded */
virtual int tick();
virtual int status();
int sendChat(std::string msg);
int sendPrivateChat(std::string msg, std::string id);
std::list<RsChatItem *> getChatQueue();
};
#endif // SERVICE_CHAT_HEADER

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,169 @@
/*
* libretroshare/src/services: p3disc.h
*
* Services 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".
*
*/
#ifndef MRK_PQI_AUTODISC_H
#define MRK_PQI_AUTODISC_H
// The AutoDiscovery Class
#include <string>
#include <list>
// system specific network headers
#include "pqi/pqinetwork.h"
#include "pqi/pqi.h"
/**************** PQI_USE_XPGP ******************/
#if defined(PQI_USE_XPGP)
#include "pqi/xpgpcert.h"
#else /* X509 Certificates */
/**************** PQI_USE_XPGP ******************/
#include "pqi/sslcert.h"
#endif /* X509 Certificates */
/**************** PQI_USE_XPGP ******************/
#include "serialiser/rsdiscitems.h"
#include "services/p3service.h"
class autoserver
{
public:
autoserver()
:id(NULL), ca(NULL), connect(false), c_ts(0),
listen(false), l_ts(0), discFlags(0) { return;}
Person *id;
Person *ca;
bool connect;
unsigned int c_ts; // this is connect_tf converted to timestamp, 0 invalid.
bool listen;
unsigned int l_ts; // this is receive_tf converted to timestamp, 0 invalid.
struct sockaddr_in local_addr;
struct sockaddr_in server_addr;
unsigned long discFlags;
};
class autoneighbour: public autoserver
{
public:
autoneighbour()
:autoserver(), local(false), active(false) {}
bool local;
bool active; // meaning in ssl's list.
std::list<autoserver *> neighbour_of;
};
class p3disc: public p3Service
{
public:
bool local_disc;
bool remote_disc;
//sslroot *sslbase;
p3disc(sslroot *r);
virtual ~p3disc();
// Overloaded from p3Service functions.
virtual int tick();
// For Proxy Information.
std::list<sockaddr_in> requestStunServers();
std::list<cert *> potentialproxy(cert *target);
// load and save configuration to sslroot.
int save_configuration();
int load_configuration();
int ts_lastcheck;
int idServers();
// Handle Local Discovery.
int localListen();
int localSetup();
int lsock; // local discovery socket.
struct sockaddr_in laddr; // local addr
struct sockaddr_in baddr; // local broadcast addr.
struct sockaddr_in saddr; // pqi ssl server addr.
// bonus configuration flags.
bool local_firewalled;
bool local_forwarded;
// local message construction/destruction.
void *ldata;
int ldlen;
int ldlenmax;
bool std_port; // if we have bound to default.
int ts_nextlp; // -1 for never (if on default)
// helper functions.
int setLocalAddress(struct sockaddr_in srvaddr);
int determineLocalNetAddr();
int setupLocalPacket(int type, struct sockaddr_in *home,
struct sockaddr_in *server);
int localPing(struct sockaddr_in);
int localReply(struct sockaddr_in);
int addLocalNeighbour(struct sockaddr_in*, struct sockaddr_in*);
// remote discovery function.
int newRequests();
int handleReplies();
int handleDiscoveryData(RsDiscReply *di);
int handleDiscoveryPing(RsDiscItem *di);
int sendDiscoveryReply(cert *);
int collectCerts();
int distillData();
//cert *checkDuplicateX509(X509 *x509);
std::list<cert *> &getDiscovered();
// Main Storage
std::list<autoneighbour *> neighbours;
std::list<cert *> ad_init;
std::list<cert *> discovered;
sslroot *sroot;
};
#endif // MRK_PQI_AUTODISC_H

View file

@ -0,0 +1,383 @@
/*
* libretroshare/src/services msgservice.cc
*
* Services 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/pqibin.h"
#include "pqi/pqiarchive.h"
#include "pqi/pqidebug.h"
#include "services/p3msgservice.h"
#include <sstream>
#include <iomanip>
/**************** PQI_USE_XPGP ******************/
#if defined(PQI_USE_XPGP)
#include "pqi/xpgpcert.h"
#else /* X509 Certificates */
/**************** PQI_USE_XPGP ******************/
#include "pqi/sslcert.h"
#endif /* X509 Certificates */
/**************** PQI_USE_XPGP ******************/
const int msgservicezone = 54319;
/* Another little hack ..... unique message Ids
* will be handled in this class.....
* These are unique within this run of the server,
* and are not stored long term....
*
* Only 3 entry points:
* (1) from network....
* (2) from local send
* (3) from storage...
*/
static unsigned int msgUniqueId = 1;
unsigned int getNewUniqueMsgId()
{
return msgUniqueId++;
}
p3MsgService::p3MsgService()
:p3Service(RS_SERVICE_TYPE_MSG),
msgChanged(1), msgMajorChanged(1)
{
sslr = getSSLRoot();
}
int p3MsgService::tick()
{
pqioutput(PQL_DEBUG_BASIC, msgservicezone,
"p3MsgService::tick()");
/* don't worry about increasing tick rate!
* (handled by p3service)
*/
incomingMsgs();
checkOutgoingMessages();
return 0;
}
int p3MsgService::status()
{
pqioutput(PQL_DEBUG_BASIC, msgservicezone,
"p3MsgService::status()");
return 1;
}
int p3MsgService::incomingMsgs()
{
RsMsgItem *mi;
int i = 0;
while((mi = (RsMsgItem *) recvItem()) != NULL)
{
++i;
mi -> recvTime = time(NULL);
std::string mesg;
if (mi -> PeerId() == sslr->getOwnCert()->PeerId())
{
/* from the loopback device */
mi -> msgFlags = RS_MSG_FLAGS_OUTGOING;
}
else
{
/* from a peer */
mi -> msgFlags = 0;
}
/* new as well! */
mi -> msgFlags |= RS_MSG_FLAGS_NEW;
/* STORE MsgID */
mi -> msgId = getNewUniqueMsgId();
imsg.push_back(mi);
msgChanged.IndicateChanged();
}
return 1;
}
std::list<RsMsgItem *> &p3MsgService::getMsgList()
{
return imsg;
}
std::list<RsMsgItem *> &p3MsgService::getMsgOutList()
{
return msgOutgoing;
}
/* remove based on the unique mid (stored in sid) */
int p3MsgService::removeMsgId(uint32_t mid)
{
std::list<RsMsgItem *>::iterator it;
for(it = imsg.begin(); it != imsg.end(); it++)
{
if ((*it)->msgId == mid)
{
RsMsgItem *mi = (*it);
imsg.erase(it);
delete mi;
msgChanged.IndicateChanged();
msgMajorChanged.IndicateChanged();
return 1;
}
}
/* try with outgoing messages otherwise */
for(it = msgOutgoing.begin(); it != msgOutgoing.end(); it++)
{
if ((*it)->msgId == mid)
{
RsMsgItem *mi = (*it);
msgOutgoing.erase(it);
delete mi;
msgChanged.IndicateChanged();
msgMajorChanged.IndicateChanged();
return 1;
}
}
return 0;
}
int p3MsgService::markMsgIdRead(uint32_t mid)
{
std::list<RsMsgItem *>::iterator it;
for(it = imsg.begin(); it != imsg.end(); it++)
{
if ((*it)->msgId == mid)
{
RsMsgItem *mi = (*it);
mi -> msgFlags &= ~(RS_MSG_FLAGS_NEW);
msgChanged.IndicateChanged();
return 1;
}
}
return 0;
}
int p3MsgService::sendMessage(RsMsgItem *item)
{
pqioutput(PQL_DEBUG_BASIC, msgservicezone,
"p3MsgService::sendMessage()");
/* add pending flag */
item->msgFlags |=
(RS_MSG_FLAGS_OUTGOING |
RS_MSG_FLAGS_PENDING);
/* STORE MsgID */
item -> msgId = getNewUniqueMsgId();
msgOutgoing.push_back(item);
return 1;
}
int p3MsgService::checkOutgoingMessages()
{
/* iterate through the outgoing queue
*
* if online, send
*/
std::list<RsMsgItem *>::iterator it;
for(it = msgOutgoing.begin(); it != msgOutgoing.end();)
{
/* find the certificate */
certsign sign;
convert_to_certsign((*it)->PeerId(), sign);
cert *peer = sslr -> findcertsign(sign);
/* if online, send it */
if ((peer -> Status() & PERSON_STATUS_CONNECTED)
|| (peer == sslr->getOwnCert()))
{
/* send msg */
pqioutput(PQL_ALERT, msgservicezone,
"p3MsgService::checkOutGoingMessages() Sending out message");
/* remove the pending flag */
(*it)->msgFlags &= ~RS_MSG_FLAGS_PENDING;
sendItem(*it);
it = msgOutgoing.erase(it);
}
else
{
pqioutput(PQL_ALERT, msgservicezone,
"p3MsgService::checkOutGoingMessages() Delaying until available...");
it++;
}
}
return 0;
}
int p3MsgService::save_config()
{
std::list<std::string>::iterator it;
std::string empty("");
pqioutput(PQL_DEBUG_BASIC, msgservicezone,
"p3MsgService::save_config()");
/* now we create a pqiarchive, and stream all the msgs into it
*/
std::string statelog = config_dir + "/msgs.rst";
RsSerialiser *rss = new RsSerialiser();
rss->addSerialType(new RsMsgSerialiser());
BinFileInterface *out = new BinFileInterface((char *) statelog.c_str(), BIN_FLAGS_WRITEABLE);
pqiarchive *pa_out = new pqiarchive(rss, out, BIN_FLAGS_WRITEABLE | BIN_FLAGS_NO_DELETE);
bool written = false;
std::list<RsMsgItem *>::iterator mit;
for(mit = imsg.begin(); mit != imsg.end(); mit++)
{
//RsMsgItem *mi = (*mit)->clone();
if (pa_out -> SendItem(*mit))
{
written = true;
}
}
for(mit = msgOutgoing.begin(); mit != msgOutgoing.end(); mit++)
{
//RsMsgItem *mi = (*mit)->clone();
//mi -> msgFlags |= RS_MSG_FLAGS_PENDING;
if (pa_out -> SendItem(*mit))
{
written = true;
}
}
if (!written)
{
/* need to push something out to overwrite old data! (For WINDOWS ONLY) */
}
delete pa_out;
return 1;
}
int p3MsgService::load_config()
{
std::list<std::string>::iterator it;
std::string empty("");
std::string dir("notempty");
std::string str_true("true");
/* load msg/ft */
std::string statelog = config_dir + "/msgs.rst";
RsSerialiser *rss = new RsSerialiser();
rss->addSerialType(new RsMsgSerialiser());
BinFileInterface *in = new BinFileInterface((char *) statelog.c_str(), BIN_FLAGS_READABLE);
pqiarchive *pa_in = new pqiarchive(rss, in, BIN_FLAGS_READABLE);
RsItem *item;
RsMsgItem *mitem;
while((item = pa_in -> GetItem()))
{
if (NULL != (mitem = dynamic_cast<RsMsgItem *>(item)))
{
/* switch depending on the PENDING
* flags
*/
/* STORE MsgID */
mitem->msgId = getNewUniqueMsgId();
if (mitem -> msgFlags & RS_MSG_FLAGS_PENDING)
{
std::cerr << "MSG_PENDING";
std::cerr << std::endl;
mitem->print(std::cerr);
msgOutgoing.push_back(mitem);
}
else
{
imsg.push_back(mitem);
}
}
else
{
delete item;
}
}
delete pa_in;
return 1;
}
void p3MsgService::loadWelcomeMsg()
{
/* Load Welcome Message */
RsMsgItem *msg = new RsMsgItem();
msg -> PeerId(sslr->getOwnCert()->PeerId());
msg -> sendTime = 0;
msg -> subject = "Welcome to Retroshare";
msg -> message = "Send and receive messages\n";
msg -> message += "with your friends...\n\n";
msg -> message += "These can hold recommendations\n";
msg -> message += "from your local shared files\n\n";
msg -> message += "Add recommendations through\n";
msg -> message += "the Local Files Dialog\n\n";
msg -> message += "Enjoy.\n";
imsg.push_back(msg);
}

View file

@ -0,0 +1,84 @@
/*
* libretroshare/src/services msgservice.h
*
* Services 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".
*
*/
#ifndef MESSAGE_SERVICE_HEADER
#define MESSAGE_SERVICE_HEADER
#include <list>
#include <map>
#include <iostream>
#include "pqi/pqi.h"
#include "pqi/pqiindic.h"
#include "services/p3service.h"
#include "serialiser/rsmsgitems.h"
#include "rsiface/rsiface.h"
class pqimonitor;
class sslroot;
class p3MsgService: public p3Service
{
public:
p3MsgService();
void loadWelcomeMsg(); /* startup message */
int sendMessage(RsMsgItem *item);
int checkOutgoingMessages();
std::list<RsMsgItem *> &getMsgList();
std::list<RsMsgItem *> &getMsgOutList();
// cleaning up....
int removeMsgId(uint32_t mid); /* id stored in sid */
int markMsgIdRead(uint32_t mid);
int load_config();
int save_config();
int tick();
int status();
private:
int incomingMsgs();
std::list<RsMsgItem *> imsg;
std::list<RsMsgItem *> msgOutgoing; /* ones that haven't made it out yet! */
// bool state flags.
public:
Indicator msgChanged;
Indicator msgMajorChanged;
sslroot *sslr;
std::string config_dir;
};
#endif // MESSAGE_SERVICE_HEADER

View file

@ -0,0 +1,163 @@
/*
* 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"
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 *****/
std::cerr << "p3Service::receive()" << std::endl;
/* convert to RsServiceItem */
uint32_t size = raw->getRawLength();
RsItem *item = rsSerialiser->deserialise(raw->getRawData(), &size);
if ((!item) || (size != raw->getRawLength()))
{
/* error in conversion */
std::cerr << "p3Service::receive() Error" << std::endl;
std::cerr << "p3Service::receive() Size: " << size << std::endl;
std::cerr << "p3Service::receive() RawLength: " << raw->getRawLength() << std::endl;
if (item)
{
delete item;
}
}
/* if we have something - pass it on */
if (item)
{
/* 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();
/* try to convert */
uint32_t size = rsSerialiser->size(si);
if (!size)
{
/* 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))
{
delete raw;
raw = NULL;
}
if ((raw) && (size != raw->getRawLength()))
{
delete raw;
raw = NULL;
}
/* ensure PeerId is transferred */
raw->PeerId(si->PeerId());
/* cleanup */
delete si;
srvMtx.unlock(); /***** UNLOCK MUTEX *****/
return raw;
}

View file

@ -0,0 +1,116 @@
/*
* libretroshare/src/services p3service.h
*
* 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".
*
*/
#ifndef P3_GENERIC_SERVICE_HEADER
#define P3_GENERIC_SERVICE_HEADER
#include "pqi/pqi.h"
#include "pqi/pqiservice.h"
#include "util/rsthreads.h"
/* This provides easy to use extensions to the pqiservice class provided in src/pqi.
*
* We will have a number of different strains.
*
* (1) p3Service -> pqiService
*
* Basic service with serialisation handled by a RsSerialiser.
*
* (2) p3ThreadedService -> p3service.
*
* Independent thread with mutex locks for i/o Queues.
* ideal for games etc.
*
* (3) p3CacheService -> p3service + CacheSource + CacheStore.
*
* For both Cached and Messages.
*/
class p3Service: public pqiService
{
protected:
p3Service(uint16_t type)
:pqiService((((uint32_t) RS_PKT_VERSION_SERVICE) << 24) + (((uint32_t) type) << 8)),
rsSerialiser(NULL)
{
rsSerialiser = new RsSerialiser();
return;
}
public:
virtual ~p3Service() { delete rsSerialiser; return; }
/*************** INTERFACE ******************************/
/* called from Thread/tick/GUI */
int sendItem(RsItem *);
RsItem * recvItem();
bool receivedItems();
virtual int tick() { return 0; }
/*************** INTERFACE ******************************/
public:
// overloaded pqiService interface.
virtual int receive(RsRawItem *);
virtual RsRawItem * send();
protected:
void addSerialType(RsSerialType *);
private:
RsMutex srvMtx;
/* below locked by Mutex */
RsSerialiser *rsSerialiser;
std::list<RsItem *> recv_queue, send_queue;
};
#if 0
class p3ThreadedService: public p3Service, public RsThread
{
protected:
p3ThreadedService(RsSerialiser *rss, uint32_t type)
:p3Service(rss, type) { return; }
public:
virtual ~p3ThreadedService() { return; }
private:
};
#endif
#endif // P3_GENERIC_SERVICE_HEADER