From 300952d2a66ac8f393a4f7a39707eb9833990816 Mon Sep 17 00:00:00 2001 From: chrisparker126 Date: Thu, 15 Apr 2010 10:47:48 +0000 Subject: [PATCH] finished status service - can be called use rs interface rstatus - does not save last rs session status yet git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@2715 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/dbase/cachestrapper.h | 3 +- libretroshare/src/libretroshare.pro | 8 +- libretroshare/src/rsiface/rsstatus.h | 13 +- libretroshare/src/rsserver/p3face.h | 2 + libretroshare/src/rsserver/p3status.cc | 76 +++++ libretroshare/src/rsserver/p3status.h | 58 ++++ libretroshare/src/rsserver/rsinit.cc | 10 +- libretroshare/src/serialiser/rsstatusitems.h | 2 - libretroshare/src/services/p3chatservice.h | 18 +- libretroshare/src/services/p3statusservice.cc | 296 ++++++++++++++++++ libretroshare/src/services/p3statusservice.h | 93 ++++++ 11 files changed, 560 insertions(+), 19 deletions(-) create mode 100644 libretroshare/src/rsserver/p3status.cc create mode 100644 libretroshare/src/rsserver/p3status.h create mode 100644 libretroshare/src/services/p3statusservice.cc create mode 100644 libretroshare/src/services/p3statusservice.h diff --git a/libretroshare/src/dbase/cachestrapper.h b/libretroshare/src/dbase/cachestrapper.h index 9c0c45f3e..e394884af 100644 --- a/libretroshare/src/dbase/cachestrapper.h +++ b/libretroshare/src/dbase/cachestrapper.h @@ -146,7 +146,8 @@ virtual ~CacheSource() {} */ virtual bool cachesAvailable(RsPeerId pid, std::map &ids); - /* function called at startup to load from + /*! + * function called at startup to load from * configuration file.... * to be overloaded by inherited class */ diff --git a/libretroshare/src/libretroshare.pro b/libretroshare/src/libretroshare.pro index a34b36cd1..a5f4f7edf 100644 --- a/libretroshare/src/libretroshare.pro +++ b/libretroshare/src/libretroshare.pro @@ -16,7 +16,7 @@ debug { # DEFINES *= NET_DEBUG # DEFINES *= DISTRIB_DEBUG # DEFINES *= P3TURTLE_DEBUG FT_DEBUG DEBUG_FTCHUNK MPLEX_DEBUG -# DEFINES *= CONN_DEBUG +# DEFINES *= STATUS_DEBUG SERV_DEBUG RSSERIAL_DEBUG #CONN_DEBUG QMAKE_CXXFLAGS -= -fomit-frame-pointer QMAKE_CXXFLAGS *= -g -fno-omit-frame-pointer @@ -238,6 +238,7 @@ HEADERS += dbase/cachestrapper.h \ rsserver/p3peers.h \ rsserver/p3photo.h \ rsserver/p3rank.h \ + rsserver/p3status.h \ serialiser/rsbaseitems.h \ serialiser/rsbaseserial.h \ serialiser/rschannelitems.h \ @@ -275,7 +276,7 @@ HEADERS += dbase/cachestrapper.h \ services/p3blogs.h \ services/p3ranking.h \ services/p3service.h \ - services/p3status.h \ + services/p3statusservice.h \ turtle/p3turtle.h \ turtle/turtletypes.h \ turtle/rsturtleitem.h \ @@ -312,6 +313,7 @@ SOURCES += \ rsserver/p3photo.cc \ rsserver/p3rank.cc \ rsserver/p3peers.cc \ + rsserver/p3status.cc \ ft/ftcontroller.cc \ ft/ftserver.cc \ ft/ftdbase.cc \ @@ -333,7 +335,7 @@ SOURCES += \ services/p3forums.cc \ services/p3blogs.cc \ services/p3Qblog.cc \ - services/p3status.cc \ + services/p3statusservice.cc \ services/p3distrib.cc \ services/p3photoservice.cc \ services/p3disc.cc \ diff --git a/libretroshare/src/rsiface/rsstatus.h b/libretroshare/src/rsiface/rsstatus.h index baaa7db14..8ced61bc5 100644 --- a/libretroshare/src/rsiface/rsstatus.h +++ b/libretroshare/src/rsiface/rsstatus.h @@ -6,7 +6,7 @@ * * RetroShare C++ . * - * Copyright 2007-2008 by Vinny Do. + * Copyright 2007-2008 by Vinny Do, Chris Evi-Parker. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -33,29 +33,32 @@ extern RsStatus *rsStatus; #include #include #include +#include const uint32_t RS_STATUS_OFFLINE = 0x0001; const uint32_t RS_STATUS_AWAY = 0x0002; const uint32_t RS_STATUS_BUSY = 0x0003; const uint32_t RS_STATUS_ONLINE = 0x0004; -std::string RsStatusString(uint32_t status); class StatusInfo { public: std::string id; uint32_t status; + time_t time_stamp; /// for owner time set, and for their peers time sent }; class RsStatus { public: -virtual bool getStatus(std::string id, StatusInfo& statusInfo) = 0; -virtual bool setStatus(StatusInfo& statusInfo) = 0; +virtual bool getStatus(std::list& statusInfo) = 0; +virtual bool sendStatus(StatusInfo& statusInfo) = 0; +virtual bool statusAvailable() = 0; + +virtual void getStatusString(uint32_t status, std::string& statusString) = 0; }; -std::ostream& operator<<(std::ostream& out, const StatusInfo& statusInfo); #endif diff --git a/libretroshare/src/rsserver/p3face.h b/libretroshare/src/rsserver/p3face.h index bd2fc20ef..1e07e5aca 100644 --- a/libretroshare/src/rsserver/p3face.h +++ b/libretroshare/src/rsserver/p3face.h @@ -44,6 +44,7 @@ #include "services/p3ranking.h" #include "services/p3Qblog.h" #include "services/p3blogs.h" +#include "services/p3statusservice.h" /* The Main Interface Class - for controlling the server */ @@ -172,6 +173,7 @@ class RsServer: public RsControl, public RsThread p3disc *ad; p3MsgService *msgSrv; p3ChatService *chatSrv; + p3StatusService *mStatusSrv; /* caches (that need ticking) */ p3Ranking *mRanking; diff --git a/libretroshare/src/rsserver/p3status.cc b/libretroshare/src/rsserver/p3status.cc new file mode 100644 index 000000000..73126246e --- /dev/null +++ b/libretroshare/src/rsserver/p3status.cc @@ -0,0 +1,76 @@ +/* + * libretroshare/src/rsserver: p3msgs.h + * + * RetroShare C++ Interface. + * + * Copyright 2007-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 "p3status.h" +#include "services/p3statusservice.h" + +p3Status::p3Status(p3StatusService* statusSrv) + : mStatusSrv(statusSrv) { + + +} + +p3Status::~p3Status(){ + return; +} + +bool p3Status::getStatus(std::list& statusInfo){ + + return mStatusSrv->getStatus(statusInfo); +} + + +bool p3Status::sendStatus(StatusInfo& statusInfo){ + + return mStatusSrv->sendStatus(statusInfo); +} + +bool p3Status::statusAvailable(){ + + return mStatusSrv->statusAvailable(); +} + +void p3Status::getStatusString(uint32_t status, std::string& statusString){ + + if (status == RS_STATUS_OFFLINE){ + + statusString = "Offline"; + + }else if (status == RS_STATUS_AWAY){ + + statusString = "Away"; + + }else if (status == RS_STATUS_BUSY){ + + statusString = "Busy"; + + }else if (status == RS_STATUS_ONLINE){ + + statusString = "Online"; + } + + return; +} diff --git a/libretroshare/src/rsserver/p3status.h b/libretroshare/src/rsserver/p3status.h new file mode 100644 index 000000000..2c59d9702 --- /dev/null +++ b/libretroshare/src/rsserver/p3status.h @@ -0,0 +1,58 @@ +#ifndef RS_P3STATUS_INTERFACE_H +#define RS_P3STATUS_INTERFACE_H + +/* + * libretroshare/src/rsserver: p3status.h + * + * RetroShare C++ Interface. + * + * Copyright 2007-2008 by Chris Evi-Parker. + * + * 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 "rsiface/rsstatus.h" + + +class p3StatusService; + +//! Implements abstract interface rsStatus +/*! + * Interfaces with p3StatusService + */ +class p3Status : public RsStatus +{ +public: + + p3Status(p3StatusService* statusSrv); + virtual ~p3Status(); + + + virtual bool getStatus(std::list& statusInfo); + virtual bool sendStatus(StatusInfo& statusInfo); + virtual bool statusAvailable(); + + virtual void getStatusString(uint32_t status, std::string& statusString); + +private: + + p3StatusService* mStatusSrv; + +}; + +#endif /* RS_P3STATUS_INTERFACE_H */ diff --git a/libretroshare/src/rsserver/rsinit.cc b/libretroshare/src/rsserver/rsinit.cc index 03d9da32b..b08f70ec1 100644 --- a/libretroshare/src/rsserver/rsinit.cc +++ b/libretroshare/src/rsserver/rsinit.cc @@ -1818,7 +1818,7 @@ RsTurtle *rsTurtle = NULL ; #include "services/p3photoservice.h" #include "services/p3forums.h" #include "services/p3channels.h" -#include "services/p3status.h" +#include "services/p3statusservice.h" #include "services/p3Qblog.h" #include "services/p3blogs.h" #include "turtle/p3turtle.h" @@ -1839,6 +1839,7 @@ RsTurtle *rsTurtle = NULL ; #include "rsserver/p3discovery.h" #include "rsserver/p3photo.h" #include "rsserver/p3blog.h" +#include "rsserver/p3status.h" #include "rsiface/rsgame.h" #include "pqi/p3notify.h" // HACK - moved to pqi for compilation order. @@ -1848,7 +1849,7 @@ RsTurtle *rsTurtle = NULL ; #define RS_RELEASE 1 ****/ -#define RS_RELEASE 1 +//#define RS_RELEASE 1 RsControl *createRsControl(RsIface &iface, NotifyBase ¬ify) @@ -1975,6 +1976,7 @@ int RsServer::StartupRetroShare() ad = new p3disc(mConnMgr, pqih); msgSrv = new p3MsgService(mConnMgr); chatSrv = new p3ChatService(mConnMgr); + mStatusSrv = new p3StatusService(mConnMgr); p3tunnel *tn = new p3tunnel(mConnMgr, pqih); pqih -> addService(tn); @@ -1988,6 +1990,7 @@ int RsServer::StartupRetroShare() pqih -> addService(ad); pqih -> addService(msgSrv); pqih -> addService(chatSrv); + pqih ->addService(mStatusSrv); /* create Cache Services */ std::string config_dir = RsInitConfig::configDir; @@ -2091,6 +2094,7 @@ int RsServer::StartupRetroShare() mConfigMgr->addConfiguration("channels.cfg", mChannels); mConfigMgr->addConfiguration("turtle.cfg", tr); mConfigMgr->addConfiguration("p3disc.cfg", ad); + mConfigMgr->addConfiguration("p3Status.cfg", mStatusSrv); ftserver->addConfiguration(mConfigMgr); @@ -2207,7 +2211,7 @@ int RsServer::StartupRetroShare() #ifndef RS_RELEASE rsGameLauncher = gameLauncher; rsPhoto = new p3Photo(photoService); - rsStatus = new p3Status(); + rsStatus = new p3Status(mStatusSrv); rsQblog = new p3Blog(mQblog); #else rsGameLauncher = NULL; diff --git a/libretroshare/src/serialiser/rsstatusitems.h b/libretroshare/src/serialiser/rsstatusitems.h index ee9213977..688be6a3c 100644 --- a/libretroshare/src/serialiser/rsstatusitems.h +++ b/libretroshare/src/serialiser/rsstatusitems.h @@ -26,8 +26,6 @@ * */ -#include - #include "serialiser/rsserviceids.h" #include "serialiser/rsserial.h" diff --git a/libretroshare/src/services/p3chatservice.h b/libretroshare/src/services/p3chatservice.h index f9df4d20f..9fe2fef9c 100644 --- a/libretroshare/src/services/p3chatservice.h +++ b/libretroshare/src/services/p3chatservice.h @@ -57,14 +57,22 @@ class p3ChatService: public p3Service, public p3Config void setOwnCustomStateString(const std::string&) ; std::string getOwnCustomStateString() ; - /// gets the peer's avatar in jpeg format, if available. Null otherwise. Also asks the peer to send - /// its avatar, if not already available. Creates a new unsigned char array. It's the caller's - /// responsibility to delete this ones used. - /// + /*! gets the peer's avatar in jpeg format, if available. Null otherwise. Also asks the peer to send + * its avatar, if not already available. Creates a new unsigned char array. It's the caller's + * responsibility to delete this ones used. + */ void getAvatarJpegData(const std::string& peer_id,unsigned char *& data,int& size) ; - /// Sets the avatar data and size. Data is copied, so should be destroyed by the caller. + /*! + * Sets the avatar data and size for client's account + * @param data is copied, so should be destroyed by the caller + * + */ void setOwnAvatarJpegData(const unsigned char *data,int size) ; + + /*! + * Gets the avatar data for clients account + */ void getOwnAvatarJpegData(unsigned char *& data,int& size) ; std::list getChatQueue(); diff --git a/libretroshare/src/services/p3statusservice.cc b/libretroshare/src/services/p3statusservice.cc new file mode 100644 index 000000000..69a567e28 --- /dev/null +++ b/libretroshare/src/services/p3statusservice.cc @@ -0,0 +1,296 @@ +/* + * libretroshare/src/services: p3statusservice.cc + * + * RetroShare C++ . + * + * Copyright 2008 by Vinny Do, Chris Evi-Parker. + * + * 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/p3statusservice.h" +#include "serialiser/rsstatusitems.h" + +#include +#include +#include +#include + +std::ostream& operator<<(std::ostream& out, const StatusInfo& si) +{ + out << "StatusInfo: " << std::endl; + out << "id: " << si.id << std::endl; + out << "status: " << si.status << std::endl; + out << "time_stamp: " << si.time_stamp << std::endl; + return out; +} + +RsStatus *rsStatus = NULL; + +p3StatusService::p3StatusService(p3ConnectMgr *cm) + :p3Service(RS_SERVICE_TYPE_STATUS), p3Config(CONFIG_TYPE_STATUS), mConnMgr(cm) +{ + addSerialType(new RsStatusSerialiser()); + +} + +p3StatusService::~p3StatusService() +{ +} + + +bool p3StatusService::getStatus(std::list& statusInfo) +{ + + time_t time_now = time(NULL); + +#ifdef STATUS_DEBUG + std::cerr << "p3StatusService::getStatus() " << std::endl; +#endif + + statusInfo.clear(); + + std::list status_items = getStatusQueue(); + std::list::iterator rit; + std::map::iterator mit; + std::list peers, peersOnline; + std::list::iterator pit, pit_online; + + { + RsStackMutex stack(mStatusMtx); + + /* first update map */ + + mConnMgr->getFriendList(peers); + mConnMgr->getOnlineList(peersOnline); + pit_online = peersOnline.begin(); + + // ensure member map is up to date with all client's peers + for(pit = peers.begin(); pit != peers.end(); pit++){ + + mit = mStatusInfoMap.find(*pit); + + if(mit == mStatusInfoMap.end()){ + + StatusInfo info; + info.id = *pit; + info.status = RS_STATUS_ONLINE; + info.time_stamp = time_now; + std::pair pr(*pit, info); + mStatusInfoMap.insert(pr); + } + + } + + // now note members who have sent specific status updates + for(rit = status_items.begin(); rit != status_items.end(); rit++){ + + RsStatusItem* si = dynamic_cast(*rit); + + if(si == NULL){ + std::cerr << "p3Status::getStatus() " << "Failed to cast Item \n" << std::endl; + } + + + mit = mStatusInfoMap.find(si->PeerId()); + + +#ifdef STATUS_DEBUG + if(mit == mStatusInfoMap.end()){ + std::cerr << "p3GetStatus() " << "Could not find Peer" << si->PeerId(); + std::cerr << std::endl; + } +#endif + + mit->second.id = si->PeerId(); + mit->second.status = si->status; + mit->second.time_stamp = si->sendTime; + + } + + // then fill up statusInfo list with this information + for(mit = mStatusInfoMap.begin(); mit != mStatusInfoMap.end(); mit++){ + statusInfo.push_back(mit->second); + } + } + + return true; +} + +bool p3StatusService::sendStatus(StatusInfo& statusInfo) +{ + std::list onlineList; + + { + RsStackMutex stack(mStatusMtx); + + if(statusInfo.id != mConnMgr->getOwnId()) + return false; + + mStatusInfoMap[statusInfo.id] = statusInfo; + mConnMgr->getOnlineList(onlineList); + } + + + + //statusItem->PeerId(statusInfo.id); + + std::list::iterator it; + + + +#ifdef STATUS_DEBUG + std::cerr << "p3StatusService::sendStatus() " << std::endl; + std::cerr << statusInfo; +#endif + + // send to all peers online + for(it = onlineList.begin(); it != onlineList.end(); it++){ + RsStatusItem* statusItem = new RsStatusItem(); + statusItem->sendTime = time(NULL); + statusItem->status = statusInfo.status; + statusItem->PeerId(*it); + sendItem(statusItem); + } + + + return true; +} + + +bool p3StatusService::statusAvailable(){ + return receivedItems(); +} + +/******************************/ + +std::list p3StatusService::getStatusQueue(){ + + + time_t time_now = time(NULL); + + RsItem* item; + std::list ilist; + + while(NULL != (item = recvItem())){ + + RsStatusItem* status_item = dynamic_cast(item); + + if(status_item != NULL){ +#ifdef STATUS_DEBUG + std::cerr << "p3StatusService::getStatusQueue()" << std::endl; + std::cerr << "PeerId : " << status_item->PeerId() << std::endl; + std::cerr << "Status: " << status_item->status << std::endl; + std::cerr << "Got status Item" << std::endl; +#endif + status_item->recvTime = time_now; + ilist.push_back(status_item); + } + } + + return ilist; +} + +/* p3Config */ + +RsSerialiser* p3StatusService::setupSerialiser(){ + + RsSerialiser *rss = new RsSerialiser; + rss->addSerialType(new RsStatusSerialiser); + + return rss; +} + +std::list p3StatusService::saveList(bool& cleanup){ + + // save your status before quiting + cleanup = true; + RsStatusItem* own_status = new RsStatusItem; + StatusInfo own_info; + std::list ilist; + std::map::iterator it; + + { + RsStackMutex stack(mStatusMtx); + it = mStatusInfoMap.find(mConnMgr->getOwnId()); + + if(it == mStatusInfoMap.end()){ + std::cerr << "p3StatusService::saveList() :" << "Did not find your status" + << mConnMgr->getOwnId() << std::endl; + delete own_status; + return ilist; + } + + own_info = it->second; + } + + own_status->PeerId(own_info.id); + own_status->sendTime = own_info.time_stamp; + own_status->status = own_info.status; + + ilist.push_back(own_status); + + return ilist; +} + +bool p3StatusService::loadList(std::list load){ + + // load your status from last rs session + StatusInfo own_info; + std::list::iterator it = load.begin(); + + if(it == load.end()){ + std::cerr << "p3StatusService::loadList(): Failed to load " << std::endl; + return false; + } + + RsStatusItem* own_status = dynamic_cast(*it); + + + if(own_status != NULL){ + + own_info.id = own_status->PeerId(); + own_info.status = own_status->status; + own_info.time_stamp = own_status->sendTime; + + { + RsStackMutex stack(mStatusMtx); + std::pair pr(own_info.id, own_info); + mStatusInfoMap.insert(pr); + } + + return true; + }else{ + std::cerr << "p3StatusService::loadList " << "Failed to load list " + << std::endl; + } + + return false; +} + + +int p3StatusService::tick(){ + return 0; +} + +int p3StatusService::status(){ + return 1; +} + + + diff --git a/libretroshare/src/services/p3statusservice.h b/libretroshare/src/services/p3statusservice.h new file mode 100644 index 000000000..552b837e2 --- /dev/null +++ b/libretroshare/src/services/p3statusservice.h @@ -0,0 +1,93 @@ +#ifndef RS_P3_STATUS_INTERFACE_H +#define RS_P3_STATUS_INTERFACE_H + +/* + * libretroshare/src/services: p3statusService.h + * + * RetroShare C++ + * + * Copyright 2008 by Vinny Do, Chris Evi-Parker. + * + * 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 +#include + +#include "serialiser/rsstatusitems.h" +#include "rsiface/rsstatus.h" +#include "services/p3service.h" +#include "pqi/p3connmgr.h" + +//! handles standard status messages (busy, away, online, offline) set by user +/*! + * The is a retroshare service which allows peers + * to inform each other about their status in a standard way, as opposed to + * custom string. + * @see rsiface/rsstatus.h for status constants + */ +class p3StatusService: public p3Service, public p3Config +{ + public: + + p3StatusService(p3ConnectMgr* ); +virtual ~p3StatusService(); + +/***** overloaded from p3Service *****/ +virtual int tick(); +virtual int status(); + +/********* RsStatus ***********/ + +virtual bool getStatus(std::list& statusInfo); +virtual bool sendStatus(StatusInfo& statusInfo); +virtual bool statusAvailable(); + +/******************************/ + + +/** implemented from p3Config **/ + +/*! + * @return The serialiser the enables storage of save info + */ +virtual RsSerialiser *setupSerialiser(); + +/*! + * This stores information on what your status was before you exited rs + */ +virtual std::list saveList(bool& cleanup); + +/*! + * @param load Should contain a single item which is clients status from last rs session + */ +virtual bool loadList(std::list load); + + private: + +virtual std::list getStatusQueue(); + +p3ConnectMgr *mConnMgr; + +std::map mStatusInfoMap; + +RsMutex mStatusMtx; + +}; + +#endif