mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-04-11 18:29:16 -04:00
* Addition of almost completed p3ranking class - still streaming / ranking to do.
* Major modifications to p3disc to use the new AuthMgr and ConnMgr. * Modified RsDiscItems to match new p3disc. * Modified ConnMgr to accept information from p3disc. * Addition of new Load/Save Certificate From/To Binary to AuthMgr. * Corrected default build to Linux. git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@325 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
3451af6152
commit
06aae24efd
libretroshare/src
@ -50,7 +50,9 @@ xPGP_vfy.h:#define TRUST_SIGN_BAD -1
|
||||
/********************************************************************************/
|
||||
/********************************************************************************/
|
||||
|
||||
#define AUTHXPGP_DEBUG 1
|
||||
/***********
|
||||
** #define AUTHXPGP_DEBUG 1
|
||||
**********/
|
||||
|
||||
// the single instance of this.
|
||||
static AuthXPGP instance_sslroot;
|
||||
@ -563,7 +565,57 @@ bool AuthXPGP::SaveCertificateToFile(std::string id, std::string filename)
|
||||
xpgpMtx.unlock(); /**** UNLOCK ****/
|
||||
return valid;
|
||||
}
|
||||
|
||||
|
||||
/**** To/From DER format ***/
|
||||
|
||||
bool AuthXPGP::LoadCertificateFromBinary(const uint8_t *ptr, uint32_t len, std::string &id)
|
||||
{
|
||||
#ifdef AUTHXPGP_DEBUG
|
||||
std::cerr << "AuthXPGP::LoadCertificateFromFile() " << id;
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
|
||||
XPGP *xpgp = loadXPGPFromDER(ptr, len);
|
||||
if (!xpgp)
|
||||
return false;
|
||||
|
||||
return ProcessXPGP(xpgp, id);
|
||||
|
||||
}
|
||||
|
||||
bool AuthXPGP::SaveCertificateToBinary(std::string id, uint8_t **ptr, uint32_t *len)
|
||||
{
|
||||
#ifdef AUTHXPGP_DEBUG
|
||||
std::cerr << "AuthXPGP::SaveCertificateToBinary() " << id;
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
|
||||
xpgpMtx.lock(); /***** LOCK *****/
|
||||
|
||||
/* get the cert first */
|
||||
xpgpcert *cert = NULL;
|
||||
bool valid = false;
|
||||
std::string hash;
|
||||
|
||||
if (id == mOwnId)
|
||||
{
|
||||
cert = mOwnCert;
|
||||
valid = true;
|
||||
}
|
||||
else if (locked_FindCert(id, &cert))
|
||||
{
|
||||
valid = true;
|
||||
}
|
||||
if (valid)
|
||||
{
|
||||
valid = saveXPGPToDER(cert->certificate, ptr, len);
|
||||
}
|
||||
|
||||
xpgpMtx.unlock(); /**** UNLOCK ****/
|
||||
return valid;
|
||||
}
|
||||
|
||||
|
||||
/* Signatures */
|
||||
bool AuthXPGP::SignCertificate(std::string id)
|
||||
{
|
||||
@ -940,17 +992,44 @@ XPGP *AuthXPGP::loadXPGPFromPEM(std::string pem)
|
||||
return pc;
|
||||
}
|
||||
|
||||
XPGP *AuthXPGP::loadXPGPFromDER(char *data, uint32_t len)
|
||||
XPGP *AuthXPGP::loadXPGPFromDER(const uint8_t *ptr, uint32_t len)
|
||||
{
|
||||
#ifdef AUTHXPGP_DEBUG
|
||||
std::cerr << "AuthXPGP::LoadXPGPFromDER()";
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
|
||||
/**** TODO ****/
|
||||
return NULL;
|
||||
XPGP *tmp = NULL;
|
||||
unsigned char **certptr = (unsigned char **) &ptr;
|
||||
XPGP *xpgp = d2i_XPGP(&tmp, certptr, len);
|
||||
|
||||
return xpgp;
|
||||
}
|
||||
|
||||
bool AuthXPGP::saveXPGPToDER(XPGP *xpgp, uint8_t **ptr, uint32_t *len)
|
||||
{
|
||||
#ifdef AUTHXPGP_DEBUG
|
||||
std::cerr << "AuthXPGP::saveXPGPToDER()";
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
|
||||
int certlen = i2d_XPGP(xpgp, (unsigned char **) ptr);
|
||||
if (certlen > 0)
|
||||
{
|
||||
*len = certlen;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
*len = 0;
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
bool AuthXPGP::ProcessXPGP(XPGP *xpgp, std::string &id)
|
||||
{
|
||||
#ifdef AUTHXPGP_DEBUG
|
||||
@ -1294,7 +1373,7 @@ std::string getX509CNString(X509_NAME *name)
|
||||
}
|
||||
|
||||
|
||||
std::string getX509TypeString(X509_NAME *name, char *type, int len)
|
||||
std::string getX509TypeString(X509_NAME *name, const char *type, int len)
|
||||
{
|
||||
std::string namestr;
|
||||
for(int i = 0; i < X509_NAME_entry_count(name); i++)
|
||||
|
@ -103,11 +103,14 @@ virtual std::string getName(std::string id);
|
||||
virtual bool getDetails(std::string id, pqiAuthDetails &details);
|
||||
|
||||
/* Load/Save certificates */
|
||||
|
||||
|
||||
virtual bool LoadCertificateFromString(std::string pem, std::string &id);
|
||||
virtual std::string SaveCertificateToString(std::string id);
|
||||
virtual bool LoadCertificateFromFile(std::string filename, std::string &id);
|
||||
virtual bool SaveCertificateToFile(std::string id, std::string filename);
|
||||
|
||||
virtual bool LoadCertificateFromBinary(const uint8_t *ptr, uint32_t len, std::string &id);
|
||||
virtual bool SaveCertificateToBinary(std::string id, uint8_t **ptr, uint32_t *len);
|
||||
|
||||
/* Signatures */
|
||||
|
||||
@ -128,17 +131,21 @@ bool ValidateCertificateXPGP(XPGP *xpgp, std::string &peerId); /* validate + ge
|
||||
bool FailedCertificateXPGP(XPGP *xpgp, bool incoming); /* store for discovery */
|
||||
bool CheckCertificateXPGP(std::string peerId, XPGP *xpgp); /* check that they are exact match */
|
||||
|
||||
|
||||
private:
|
||||
|
||||
/* Helper Functions */
|
||||
|
||||
bool getXPGPid(XPGP *xpgp, std::string &xpgpid);
|
||||
bool ProcessXPGP(XPGP *xpgp, std::string &id);
|
||||
XPGP * loadXPGPFromDER(char *data, uint32_t len);
|
||||
|
||||
XPGP * loadXPGPFromPEM(std::string pem);
|
||||
XPGP * loadXPGPFromFile(std::string fname, std::string hash);
|
||||
bool saveXPGPToFile(XPGP *xpgp, std::string fname, std::string &hash);
|
||||
|
||||
XPGP * loadXPGPFromDER(const uint8_t *ptr, uint32_t len);
|
||||
bool saveXPGPToDER(XPGP *xpgp, uint8_t **ptr, uint32_t *len);
|
||||
|
||||
/*********** LOCKED Functions ******/
|
||||
bool locked_FindCert(std::string id, xpgpcert **cert);
|
||||
|
||||
|
@ -204,6 +204,15 @@ bool p3DummyAuthMgr::LoadCertificateFromFile(std::string filename, std::string &
|
||||
}
|
||||
|
||||
bool p3DummyAuthMgr::SaveCertificateToFile(std::string id, std::string filename)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
bool p3DummyAuthMgr::LoadCertificateFromBinary(const uint8_t *ptr, uint32_t len, std::string &id)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool p3DummyAuthMgr::SaveCertificateToBinary(std::string id, uint8_t **ptr, uint32_t *len)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
@ -102,6 +102,12 @@ virtual std::string SaveCertificateToString(std::string id) = 0;
|
||||
virtual bool LoadCertificateFromFile(std::string filename, std::string &id) = 0;
|
||||
virtual bool SaveCertificateToFile(std::string id, std::string filename) = 0;
|
||||
|
||||
/* specific OpenSSL ones -> careful with pointers....
|
||||
* save will allocate space,
|
||||
*/
|
||||
virtual bool LoadCertificateFromBinary(const uint8_t *ptr, uint32_t len, std::string &id) = 0;
|
||||
virtual bool SaveCertificateToBinary(std::string id, uint8_t **ptr, uint32_t *len) = 0;
|
||||
|
||||
/* Signatures */
|
||||
virtual bool AuthCertificate(std::string uid) = 0;
|
||||
virtual bool SignCertificate(std::string id) = 0;
|
||||
@ -149,6 +155,8 @@ virtual std::string SaveCertificateToString(std::string id);
|
||||
virtual bool LoadCertificateFromFile(std::string filename, std::string &id);
|
||||
virtual bool SaveCertificateToFile(std::string id, std::string filename);
|
||||
|
||||
virtual bool LoadCertificateFromBinary(const uint8_t *ptr, uint32_t len, std::string &id);
|
||||
virtual bool SaveCertificateToBinary(std::string id, uint8_t **ptr, uint32_t *len);
|
||||
/* Signatures */
|
||||
|
||||
virtual bool AuthCertificate(std::string uid);
|
||||
|
@ -43,10 +43,8 @@ const uint32_t RS_STUN_LIST_MIN = 100;
|
||||
|
||||
const uint32_t MAX_UPNP_INIT = 10; /* seconds UPnP timeout */
|
||||
|
||||
|
||||
#define CONN_DEBUG 1
|
||||
|
||||
|
||||
p3ConnectMgr::p3ConnectMgr(p3AuthMgr *am)
|
||||
:p3Config(CONFIG_TYPE_PEERS, "peers.cfg"),
|
||||
mAuthMgr(am), mDhtMgr(NULL), mUpnpMgr(NULL), mNetStatus(RS_NET_UNKNOWN),
|
||||
@ -91,8 +89,10 @@ peerConnectState::peerConnectState()
|
||||
netMode(RS_NET_MODE_UNKNOWN), visState(RS_VIS_STATE_STD),
|
||||
source(0),
|
||||
inConnAttempt(0), connAttemptTS(0),
|
||||
lc_timestamp(0), lr_timestamp(0),
|
||||
nc_timestamp(0), nc_timeintvl(0)
|
||||
lastcontact(0)
|
||||
|
||||
//lc_timestamp(0), lr_timestamp(0),
|
||||
//nc_timestamp(0), nc_timeintvl(0)
|
||||
{
|
||||
lastaddr.sin_family = AF_INET;
|
||||
lastaddr.sin_addr.s_addr = 0;
|
||||
@ -212,7 +212,7 @@ void p3ConnectMgr::netTick()
|
||||
{
|
||||
|
||||
#ifdef CONN_DEBUG
|
||||
std::cerr << "p3ConnectMgr::netTick()" << std::endl;
|
||||
//std::cerr << "p3ConnectMgr::netTick()" << std::endl;
|
||||
#endif
|
||||
|
||||
connMtx.lock(); /* LOCK MUTEX */
|
||||
@ -258,7 +258,7 @@ void p3ConnectMgr::netTick()
|
||||
|
||||
case RS_NET_DONE:
|
||||
#ifdef CONN_DEBUG
|
||||
std::cerr << "p3ConnectMgr::netTick() STATUS: DONE" << std::endl;
|
||||
//std::cerr << "p3ConnectMgr::netTick() STATUS: DONE" << std::endl;
|
||||
#endif
|
||||
default:
|
||||
break;
|
||||
@ -830,7 +830,7 @@ void p3ConnectMgr::getOnlineList(std::list<std::string> &peers)
|
||||
connMtx.unlock(); /* UNLOCK MUTEX */
|
||||
for(it = mFriendList.begin(); it != mFriendList.end(); it++)
|
||||
{
|
||||
if (it->second.state & RS_PEER_S_ONLINE)
|
||||
if (it->second.state & RS_PEER_S_CONNECTED)
|
||||
{
|
||||
peers.push_back(it->first);
|
||||
}
|
||||
@ -923,12 +923,13 @@ bool p3ConnectMgr::connectResult(std::string id, bool success, uint32_t flags)
|
||||
it->second.connAddrs.clear();
|
||||
mDhtMgr->dropPeer(id);
|
||||
|
||||
/* update address */
|
||||
/* update address (will come through from DISC) */
|
||||
|
||||
/* change state */
|
||||
it->second.state |= RS_PEER_S_CONNECTED;
|
||||
it->second.actions |= RS_PEER_CONNECTED;
|
||||
mStatusChanged = true;
|
||||
it->second.lastcontact = time(NULL); /* time of connect */
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -939,6 +940,8 @@ bool p3ConnectMgr::connectResult(std::string id, bool success, uint32_t flags)
|
||||
it->second.state ^= RS_PEER_S_CONNECTED;
|
||||
it->second.actions |= RS_PEER_DISCONNECTED;
|
||||
|
||||
it->second.lastcontact = time(NULL); /* time of disconnect */
|
||||
|
||||
mDhtMgr->findPeer(id);
|
||||
if (it->second.visState & RS_VIS_STATE_NODHT)
|
||||
{
|
||||
@ -973,7 +976,7 @@ bool p3ConnectMgr::connectResult(std::string id, bool success, uint32_t flags)
|
||||
|
||||
void p3ConnectMgr::peerStatus(std::string id,
|
||||
struct sockaddr_in laddr, struct sockaddr_in raddr,
|
||||
uint32_t type, uint32_t mode, uint32_t source)
|
||||
uint32_t type, uint32_t flags, uint32_t source)
|
||||
{
|
||||
std::cerr << "p3ConnectMgr::peerStatus()";
|
||||
std::cerr << " id: " << id;
|
||||
@ -982,7 +985,7 @@ void p3ConnectMgr::peerStatus(std::string id,
|
||||
std::cerr << " raddr: " << inet_ntoa(raddr.sin_addr);
|
||||
std::cerr << " rport: " << ntohs(raddr.sin_port);
|
||||
std::cerr << " type: " << type;
|
||||
std::cerr << " mode: " << mode;
|
||||
std::cerr << " flags: " << flags;
|
||||
std::cerr << " source: " << source;
|
||||
std::cerr << std::endl;
|
||||
|
||||
@ -1027,6 +1030,9 @@ void p3ConnectMgr::peerStatus(std::string id,
|
||||
*/
|
||||
it->second.source = RS_CB_DHT;
|
||||
it->second.dht = details;
|
||||
|
||||
/* If we get a info -> then they are online */
|
||||
it->second.state |= RS_PEER_S_ONLINE;
|
||||
}
|
||||
else if (source == RS_CB_DISC)
|
||||
{
|
||||
@ -1036,16 +1042,62 @@ void p3ConnectMgr::peerStatus(std::string id,
|
||||
*/
|
||||
it->second.source = RS_CB_DISC;
|
||||
it->second.disc = details;
|
||||
|
||||
if (flags & RS_NET_FLAGS_ONLINE)
|
||||
{
|
||||
it->second.actions |= RS_PEER_ONLINE;
|
||||
it->second.state |= RS_PEER_S_ONLINE;
|
||||
mStatusChanged = true;
|
||||
}
|
||||
|
||||
/* not updating VIS status??? */
|
||||
}
|
||||
else if (source == RS_CB_PERSON)
|
||||
{
|
||||
/* PERSON can tell us about
|
||||
* 1) online / offline
|
||||
* 2) connect address
|
||||
* -> update all!
|
||||
*/
|
||||
|
||||
it->second.source = RS_CB_PERSON;
|
||||
it->second.peer = details;
|
||||
|
||||
it->second.localaddr = laddr;
|
||||
it->second.serveraddr = raddr;
|
||||
it->second.state |= RS_PEER_S_ONLINE;
|
||||
|
||||
/* must be online to recv info (should be connected too!)
|
||||
* but no need for action as should be connected already
|
||||
*/
|
||||
|
||||
if (flags & RS_NET_FLAGS_EXTERNAL_ADDR)
|
||||
{
|
||||
it->second.netMode = RS_NET_MODE_EXT;
|
||||
}
|
||||
else
|
||||
{
|
||||
it->second.netMode = RS_NET_MODE_UDP;
|
||||
}
|
||||
|
||||
/* always update VIS status */
|
||||
if (flags & RS_NET_FLAGS_USE_DISC)
|
||||
{
|
||||
it->second.visState &= (~RS_VIS_STATE_NODISC);
|
||||
}
|
||||
else
|
||||
{
|
||||
it->second.visState |= RS_VIS_STATE_NODISC;
|
||||
}
|
||||
|
||||
if (flags & RS_NET_FLAGS_USE_DHT)
|
||||
{
|
||||
it->second.visState &= (~RS_VIS_STATE_NODHT);
|
||||
}
|
||||
else
|
||||
{
|
||||
it->second.visState |= RS_VIS_STATE_NODHT;
|
||||
}
|
||||
}
|
||||
|
||||
if (!isFriend)
|
||||
|
@ -59,6 +59,12 @@ const uint32_t RS_NET_CONN_TCP_EXTERNAL = 0x0002;
|
||||
const uint32_t RS_NET_CONN_UDP_DHT_SYNC = 0x0010;
|
||||
|
||||
|
||||
/* flags of peerStatus */
|
||||
const uint32_t RS_NET_FLAGS_USE_DISC = 0x0001;
|
||||
const uint32_t RS_NET_FLAGS_USE_DHT = 0x0002;
|
||||
const uint32_t RS_NET_FLAGS_ONLINE = 0x0004;
|
||||
const uint32_t RS_NET_FLAGS_EXTERNAL_ADDR = 0x0008;
|
||||
|
||||
class peerAddrInfo
|
||||
{
|
||||
public:
|
||||
@ -119,15 +125,15 @@ class peerConnectState
|
||||
peerConnectAddress currentConnAddr;
|
||||
std::list<peerConnectAddress> connAddrs;
|
||||
|
||||
time_t lastcontact;
|
||||
|
||||
/* stuff here un-used at the moment */
|
||||
|
||||
//time_t c_timestamp; // last connect timestamp
|
||||
//time_t lr_timestamp; // last receive timestamp
|
||||
|
||||
time_t lc_timestamp; // last connect timestamp
|
||||
time_t lr_timestamp; // last receive timestamp
|
||||
|
||||
time_t nc_timestamp; // next connect timestamp.
|
||||
time_t nc_timeintvl; // next connect time interval.
|
||||
//time_t nc_timestamp; // next connect timestamp.
|
||||
//time_t nc_timeintvl; // next connect time interval.
|
||||
};
|
||||
|
||||
|
||||
@ -182,7 +188,7 @@ void removeMonitor(pqiMonitor *mon);
|
||||
/******* overloaded from pqiConnectCb *************/
|
||||
virtual void peerStatus(std::string id,
|
||||
struct sockaddr_in laddr, struct sockaddr_in raddr,
|
||||
uint32_t type, uint32_t mode, uint32_t source);
|
||||
uint32_t type, uint32_t flags, uint32_t source);
|
||||
virtual void peerConnectRequest(std::string id, uint32_t type);
|
||||
virtual void stunStatus(std::string id, struct sockaddr_in addr, uint32_t flags);
|
||||
|
||||
|
@ -44,15 +44,16 @@ const uint32_t RS_PEER_ACTION_MASK = 0xff00;
|
||||
|
||||
/* STATE */
|
||||
const uint32_t RS_PEER_S_FRIEND = 0x0001;
|
||||
const uint32_t RS_PEER_S_ONLINE = 0x0002;
|
||||
const uint32_t RS_PEER_S_CONNECTED = 0x0004; /* heard from recently..*/
|
||||
const uint32_t RS_PEER_S_ONLINE = 0x0002; /* heard from recently..*/
|
||||
const uint32_t RS_PEER_S_CONNECTED = 0x0004;
|
||||
|
||||
/* ACTIONS */
|
||||
const uint32_t RS_PEER_NEW = 0x0001; /* new Peer */
|
||||
const uint32_t RS_PEER_MOVED = 0x0002; /* moved from F->O or O->F */
|
||||
const uint32_t RS_PEER_CONNECTED = 0x0004;
|
||||
const uint32_t RS_PEER_DISCONNECTED = 0x0008;
|
||||
const uint32_t RS_PEER_CONNECT_REQ = 0x0010;
|
||||
const uint32_t RS_PEER_ONLINE = 0x0004;
|
||||
const uint32_t RS_PEER_CONNECTED = 0x0008;
|
||||
const uint32_t RS_PEER_DISCONNECTED = 0x0010;
|
||||
const uint32_t RS_PEER_CONNECT_REQ = 0x0020;
|
||||
|
||||
/* Stun Status Flags */
|
||||
//const uint32_t RS_STUN_SRC_DHT = 0x0001;
|
||||
@ -105,7 +106,7 @@ class pqiConnectCb
|
||||
virtual ~pqiConnectCb() { return; }
|
||||
virtual void peerStatus(std::string id,
|
||||
struct sockaddr_in laddr, struct sockaddr_in raddr,
|
||||
uint32_t type, uint32_t mode, uint32_t source) = 0;
|
||||
uint32_t type, uint32_t flags, uint32_t source) = 0;
|
||||
|
||||
virtual void peerConnectRequest(std::string id, uint32_t type) = 0;
|
||||
|
||||
|
@ -14,7 +14,8 @@ RSOBJ = p3face-file.o \
|
||||
p3face-startup.o \
|
||||
rstypes.o \
|
||||
rsiface.o \
|
||||
p3peers.o
|
||||
p3peers.o \
|
||||
p3rank.o
|
||||
|
||||
# pqistrings.o \
|
||||
# p3face-people.o
|
||||
|
@ -39,10 +39,11 @@
|
||||
#include "dht/opendhtmgr.h"
|
||||
|
||||
// Removed temporarily...
|
||||
//#include "services/p3disc.h"
|
||||
#include "services/p3disc.h"
|
||||
#include "services/p3msgservice.h"
|
||||
#include "services/p3chatservice.h"
|
||||
#include "services/p3gamelauncher.h"
|
||||
#include "services/p3ranking.h"
|
||||
|
||||
#include <list>
|
||||
#include <string>
|
||||
@ -54,6 +55,7 @@
|
||||
#include "pqi/pqidebug.h"
|
||||
#include "rsserver/p3face.h"
|
||||
#include "rsserver/p3peers.h"
|
||||
#include "rsserver/p3rank.h"
|
||||
#include "rsiface/rsgame.h"
|
||||
|
||||
/**************** PQI_USE_XPGP ******************/
|
||||
@ -602,18 +604,23 @@ int RsServer::StartupRetroShare(RsInit *config)
|
||||
//server->load_config();
|
||||
|
||||
/* create Services */
|
||||
//ad = new p3disc(sslr); // XXX
|
||||
ad = new p3disc(mAuthMgr, mConnMgr);
|
||||
msgSrv = new p3MsgService(mConnMgr);
|
||||
chatSrv = new p3ChatService(mConnMgr);
|
||||
p3GameLauncher *gameLauncher = new p3GameLauncher();
|
||||
p3Ranking *ranking = new p3Ranking(0, NULL, "", "", 3600 * 24 * 30);
|
||||
|
||||
//pqih -> addService(ad);
|
||||
pqih -> addService(ad);
|
||||
pqih -> addService(msgSrv);
|
||||
pqih -> addService(chatSrv);
|
||||
pqih -> addService(gameLauncher);
|
||||
|
||||
/* so need to Monitor too! */
|
||||
mConnMgr->addMonitor(ad);
|
||||
|
||||
/* setup the gui */
|
||||
rsGameLauncher = gameLauncher;
|
||||
rsRanks = new p3Rank(ranking);
|
||||
|
||||
/* put a welcome message in! */
|
||||
if (config->firsttime_run)
|
||||
|
95
libretroshare/src/rsserver/p3rank.cc
Normal file
95
libretroshare/src/rsserver/p3rank.cc
Normal file
@ -0,0 +1,95 @@
|
||||
/*
|
||||
* libretroshare/src/rsserver: p3rank.cc
|
||||
*
|
||||
* 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 "rsserver/p3rank.h"
|
||||
|
||||
RsRanks *rsRanks = NULL;
|
||||
|
||||
p3Rank::p3Rank(p3Ranking *ranking)
|
||||
:mRank(ranking)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
p3Rank::~p3Rank()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/* Set Sort Methods */
|
||||
bool p3Rank::setSortPeriod(uint32_t period)
|
||||
{
|
||||
return mRank->setSortPeriod(period);
|
||||
}
|
||||
|
||||
bool p3Rank::setSortMethod(uint32_t type)
|
||||
{
|
||||
return mRank->setSortMethod(type);
|
||||
}
|
||||
|
||||
bool p3Rank::clearPeerFilter()
|
||||
{
|
||||
return mRank->clearPeerFilter();
|
||||
}
|
||||
|
||||
bool p3Rank::setPeerFilter(std::list<std::string> peers)
|
||||
{
|
||||
return mRank->setPeerFilter(peers);
|
||||
}
|
||||
|
||||
/* get Ids */
|
||||
uint32_t p3Rank::getRankingsCount()
|
||||
{
|
||||
return mRank->getRankingsCount();
|
||||
}
|
||||
|
||||
float p3Rank::getMaxRank()
|
||||
{
|
||||
return mRank->getMaxRank();
|
||||
}
|
||||
|
||||
bool p3Rank::getRankings(uint32_t first, uint32_t count, std::list<std::string> &rids)
|
||||
{
|
||||
return mRank->getRankings(first, count, rids);
|
||||
}
|
||||
|
||||
bool p3Rank::getRankDetails(std::string rid, RsRankDetails &details)
|
||||
{
|
||||
return mRank->getRankDetails(rid, details);
|
||||
}
|
||||
|
||||
|
||||
/* Add New Comment / Msg */
|
||||
std::string p3Rank::newRankMsg(std::wstring link, std::wstring title, std::wstring comment)
|
||||
{
|
||||
return mRank->newRankMsg(link, title, comment);
|
||||
}
|
||||
|
||||
bool p3Rank::updateComment(std::string rid, std::wstring comment)
|
||||
{
|
||||
return mRank->updateComment(rid, comment);
|
||||
}
|
||||
|
||||
|
60
libretroshare/src/rsserver/p3rank.h
Normal file
60
libretroshare/src/rsserver/p3rank.h
Normal file
@ -0,0 +1,60 @@
|
||||
#ifndef RETROSHARE_P3_RANKING_INTERFACE_H
|
||||
#define RETROSHARE_P3_RANKING_INTERFACE_H
|
||||
|
||||
/*
|
||||
* libretroshare/src/rsserver: p3rank.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 "rsiface/rsrank.h"
|
||||
#include "services/p3ranking.h"
|
||||
|
||||
class p3Rank: public RsRanks
|
||||
{
|
||||
public:
|
||||
|
||||
p3Rank(p3Ranking *ranking);
|
||||
virtual ~p3Rank();
|
||||
|
||||
/* Set Sort Methods */
|
||||
virtual bool setSortPeriod(uint32_t period);
|
||||
virtual bool setSortMethod(uint32_t type);
|
||||
virtual bool clearPeerFilter();
|
||||
virtual bool setPeerFilter(std::list<std::string> peers);
|
||||
|
||||
/* get Ids */
|
||||
virtual uint32_t getRankingsCount();
|
||||
virtual float getMaxRank();
|
||||
virtual bool getRankings(uint32_t first, uint32_t count, std::list<std::string> &rids);
|
||||
virtual bool getRankDetails(std::string rid, RsRankDetails &details);
|
||||
|
||||
/* Add New Comment / Msg */
|
||||
virtual std::string newRankMsg(std::wstring link, std::wstring title, std::wstring comment);
|
||||
virtual bool updateComment(std::string rid, std::wstring comment);
|
||||
|
||||
private:
|
||||
|
||||
p3Ranking *mRank;
|
||||
};
|
||||
|
||||
#endif
|
@ -4,8 +4,8 @@
|
||||
###########################################################################
|
||||
#Define OS.
|
||||
#
|
||||
#OS = Linux
|
||||
OS = MacOSX
|
||||
OS = Linux
|
||||
#OS = MacOSX
|
||||
#OS = Cygwin
|
||||
#OS = Win # MinGw.
|
||||
###########################################################################
|
||||
|
@ -113,8 +113,7 @@ void RsDiscItem::clear()
|
||||
{
|
||||
memset(&laddr, 0, sizeof(laddr));
|
||||
memset(&saddr, 0, sizeof(laddr));
|
||||
connect_tr = 0;
|
||||
receive_tr = 0;
|
||||
contact_tf = 0;
|
||||
discFlags = 0;
|
||||
}
|
||||
|
||||
@ -132,8 +131,8 @@ std::ostream &RsDiscItem::print(std::ostream &out, uint16_t indent)
|
||||
out << " Port: " << ntohs(saddr.sin_port) << std::endl;
|
||||
|
||||
printIndent(out, int_Indent);
|
||||
out << "C/R Tr: " << connect_tr;
|
||||
out << " / " << receive_tr << std::endl;
|
||||
out << "Contact TimeFrame: " << contact_tf;
|
||||
out << std::endl;
|
||||
|
||||
printIndent(out, int_Indent);
|
||||
out << "DiscFlags: " << discFlags << std::endl;
|
||||
@ -148,8 +147,7 @@ uint32_t RsDiscSerialiser::sizeItem(RsDiscItem *item)
|
||||
uint32_t s = 8; /* header */
|
||||
s += GetTlvIpAddrPortV4Size(); /* laddr */
|
||||
s += GetTlvIpAddrPortV4Size(); /* saddr */
|
||||
s += 2; /* connect_tr */
|
||||
s += 2; /* receive_tr */
|
||||
s += 2; /* contact_tf */
|
||||
s += 4; /* discFlags */
|
||||
|
||||
return s;
|
||||
@ -183,10 +181,8 @@ bool RsDiscSerialiser::serialiseItem(RsDiscItem *item, void *data, uint32_t
|
||||
ok &= SetTlvIpAddrPortV4(data, tlvsize, &offset,
|
||||
TLV_TYPE_IPV4_SERVER, &(item->saddr));
|
||||
std::cerr << "RsDiscSerialiser::serialiseItem() saddress: " << ok << std::endl;
|
||||
ok &= setRawUInt16(data, tlvsize, &offset, item->connect_tr);
|
||||
std::cerr << "RsDiscSerialiser::serialiseItem() connect_tr: " << ok << std::endl;
|
||||
ok &= setRawUInt16(data, tlvsize, &offset, item->receive_tr);
|
||||
std::cerr << "RsDiscSerialiser::serialiseItem() receive_tr: " << ok << std::endl;
|
||||
ok &= setRawUInt16(data, tlvsize, &offset, item->contact_tf);
|
||||
std::cerr << "RsDiscSerialiser::serialiseItem() contact_tf: " << ok << std::endl;
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, item->discFlags);
|
||||
std::cerr << "RsDiscSerialiser::serialiseItem() discFlags: " << ok << std::endl;
|
||||
|
||||
@ -241,10 +237,8 @@ RsDiscItem *RsDiscSerialiser::deserialiseItem(void *data, uint32_t *pktsize)
|
||||
ok &= GetTlvIpAddrPortV4(data, rssize, &offset,
|
||||
TLV_TYPE_IPV4_SERVER, &(item->saddr));
|
||||
std::cerr << "RsDiscSerialiser::deserialiseItem() saddress: " << ok << std::endl;
|
||||
ok &= getRawUInt16(data, rssize, &offset, &(item->connect_tr));
|
||||
std::cerr << "RsDiscSerialiser::deserialiseItem() connect_tr: " << ok << std::endl;
|
||||
ok &= getRawUInt16(data, rssize, &offset, &(item->receive_tr));
|
||||
std::cerr << "RsDiscSerialiser::deserialiseItem() receive_tr: " << ok << std::endl;
|
||||
ok &= getRawUInt16(data, rssize, &offset, &(item->contact_tf));
|
||||
std::cerr << "RsDiscSerialiser::deserialiseItem() contact_tf: " << ok << std::endl;
|
||||
ok &= getRawUInt32(data, rssize, &offset, &(item->discFlags));
|
||||
std::cerr << "RsDiscSerialiser::deserialiseItem() discFlags: " << ok << std::endl;
|
||||
|
||||
@ -279,9 +273,9 @@ void RsDiscReply::clear()
|
||||
{
|
||||
memset(&laddr, 0, sizeof(laddr));
|
||||
memset(&saddr, 0, sizeof(laddr));
|
||||
connect_tr = 0;
|
||||
receive_tr = 0;
|
||||
contact_tf = 0;
|
||||
discFlags = 0;
|
||||
aboutId.clear();
|
||||
certDER.TlvClear();
|
||||
}
|
||||
|
||||
@ -299,12 +293,14 @@ std::ostream &RsDiscReply::print(std::ostream &out, uint16_t indent)
|
||||
out << " Port: " << ntohs(saddr.sin_port) << std::endl;
|
||||
|
||||
printIndent(out, int_Indent);
|
||||
out << "C/R Tr: " << connect_tr;
|
||||
out << " / " << receive_tr << std::endl;
|
||||
out << "Contact TimeFrame: " << contact_tf;
|
||||
out << std::endl;
|
||||
|
||||
printIndent(out, int_Indent);
|
||||
out << "DiscFlags: " << discFlags << std::endl;
|
||||
|
||||
printIndent(out, int_Indent);
|
||||
out << "AboutId: " << aboutId << std::endl;
|
||||
certDER.print(out, int_Indent);
|
||||
|
||||
printRsItemEnd(out, "RsDiscReply", indent);
|
||||
@ -318,8 +314,8 @@ uint32_t RsDiscSerialiser::sizeReply(RsDiscReply *item)
|
||||
s += GetTlvIpAddrPortV4Size(); /* laddr */
|
||||
s += GetTlvIpAddrPortV4Size(); /* saddr */
|
||||
s += 2; /* connect_tr */
|
||||
s += 2; /* receive_tr */
|
||||
s += 4; /* discFlags */
|
||||
s += GetTlvStringSize(item->aboutId);
|
||||
s += item->certDER.TlvSize();
|
||||
|
||||
return s;
|
||||
@ -353,13 +349,15 @@ bool RsDiscSerialiser::serialiseReply(RsDiscReply *item, void *data, uint32_
|
||||
ok &= SetTlvIpAddrPortV4(data, tlvsize, &offset,
|
||||
TLV_TYPE_IPV4_SERVER, &(item->saddr));
|
||||
std::cerr << "RsDiscSerialiser::serialiseReply() saddress: " << ok << std::endl;
|
||||
ok &= setRawUInt16(data, tlvsize, &offset, item->connect_tr);
|
||||
std::cerr << "RsDiscSerialiser::serialiseReply() connect_tr: " << ok << std::endl;
|
||||
ok &= setRawUInt16(data, tlvsize, &offset, item->receive_tr);
|
||||
std::cerr << "RsDiscSerialiser::serialiseReply() receive_tr: " << ok << std::endl;
|
||||
ok &= setRawUInt16(data, tlvsize, &offset, item->contact_tf);
|
||||
std::cerr << "RsDiscSerialiser::serialiseReply() contact_tf: " << ok << std::endl;
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, item->discFlags);
|
||||
std::cerr << "RsDiscSerialiser::serialiseReply() discFlags: " << ok << std::endl;
|
||||
|
||||
ok &= SetTlvString(data, tlvsize, &offset,
|
||||
TLV_TYPE_STR_PEERID, item->aboutId);
|
||||
std::cerr << "RsDiscSerialiser::serialiseReply() aboutId: " << ok << std::endl;
|
||||
|
||||
ok &= item->certDER.SetTlv(data, tlvsize, &offset);
|
||||
std::cerr << "RsDiscSerialiser::serialiseReply() discFlags: " << ok << std::endl;
|
||||
|
||||
@ -414,12 +412,14 @@ RsDiscReply *RsDiscSerialiser::deserialiseReply(void *data, uint32_t *pktsize)
|
||||
ok &= GetTlvIpAddrPortV4(data, rssize, &offset,
|
||||
TLV_TYPE_IPV4_SERVER, &(item->saddr));
|
||||
std::cerr << "RsDiscSerialiser::deserialiseReply() saddress: " << ok << std::endl;
|
||||
ok &= getRawUInt16(data, rssize, &offset, &(item->connect_tr));
|
||||
std::cerr << "RsDiscSerialiser::deserialiseReply() connect_tr: " << ok << std::endl;
|
||||
ok &= getRawUInt16(data, rssize, &offset, &(item->receive_tr));
|
||||
std::cerr << "RsDiscSerialiser::deserialiseReply() receive_tr: " << ok << std::endl;
|
||||
ok &= getRawUInt16(data, rssize, &offset, &(item->contact_tf));
|
||||
std::cerr << "RsDiscSerialiser::deserialiseReply() contact_tf: " << ok << std::endl;
|
||||
ok &= getRawUInt32(data, rssize, &offset, &(item->discFlags));
|
||||
std::cerr << "RsDiscSerialiser::deserialiseReply() discFlags: " << ok << std::endl;
|
||||
|
||||
ok &= GetTlvString(data, rssize, &offset,
|
||||
TLV_TYPE_STR_PEERID, item->aboutId);
|
||||
std::cerr << "RsDiscSerialiser::deserialiseReply() aboutId: " << ok << std::endl;
|
||||
ok &= item->certDER.GetTlv(data, rssize, &offset);
|
||||
std::cerr << "RsDiscSerialiser::deserialiseReply() certDER: " << ok << std::endl;
|
||||
|
||||
|
@ -60,8 +60,7 @@ virtual std::ostream &print(std::ostream &out, uint16_t indent = 0);
|
||||
struct sockaddr_in saddr;
|
||||
|
||||
// time frame of recent connections.
|
||||
uint16_t connect_tr;
|
||||
uint16_t receive_tr;
|
||||
uint16_t contact_tf;
|
||||
// flags...
|
||||
uint32_t discFlags;
|
||||
};
|
||||
@ -80,6 +79,7 @@ virtual ~RsDiscReply();
|
||||
virtual void clear();
|
||||
virtual std::ostream &print(std::ostream &out, uint16_t indent = 0);
|
||||
|
||||
std::string aboutId;
|
||||
RsTlvBinaryData certDER;
|
||||
};
|
||||
|
||||
|
@ -7,8 +7,8 @@ RS_TOP_DIR = ..
|
||||
include $(RS_TOP_DIR)/scripts/config.mk
|
||||
###############################################################
|
||||
|
||||
RSOBJ = p3service.o p3chatservice.o p3msgservice.o p3gamelauncher.o
|
||||
#p3disc.o
|
||||
RSOBJ = p3service.o p3chatservice.o p3msgservice.o \
|
||||
p3gamelauncher.o p3ranking.o p3disc.o
|
||||
|
||||
#TESTOBJ =
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -39,6 +39,7 @@
|
||||
class p3ConnectMgr;
|
||||
class p3AuthMgr;
|
||||
|
||||
#include "pqi/pqimonitor.h"
|
||||
#include "serialiser/rsdiscitems.h"
|
||||
#include "services/p3service.h"
|
||||
|
||||
@ -46,20 +47,14 @@ class autoserver
|
||||
{
|
||||
public:
|
||||
autoserver()
|
||||
:id(NULL), ca(NULL), connect(false), c_ts(0),
|
||||
listen(false), l_ts(0), discFlags(0) { return;}
|
||||
:ts(0), discFlags(0) { return;}
|
||||
|
||||
std::string id;
|
||||
std::string ca;
|
||||
bool connect;
|
||||
unsigned int c_ts; // this is connect_tf converted to timestamp, 0 invalid.
|
||||
struct sockaddr_in localAddr;
|
||||
struct sockaddr_in remoteAddr;
|
||||
|
||||
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;
|
||||
time_t ts;
|
||||
uint32_t discFlags;
|
||||
};
|
||||
|
||||
|
||||
@ -67,95 +62,69 @@ class autoneighbour: public autoserver
|
||||
{
|
||||
public:
|
||||
autoneighbour()
|
||||
:autoserver(), local(false), active(false) {}
|
||||
:autoserver(), authoritative(false) {}
|
||||
|
||||
bool local;
|
||||
bool active; // meaning in ssl's list.
|
||||
std::list<autoserver *> neighbour_of;
|
||||
bool authoritative;
|
||||
bool validAddrs;
|
||||
|
||||
std::map<std::string, autoserver> neighbour_of;
|
||||
|
||||
};
|
||||
|
||||
class p3AuthMgr;
|
||||
class p3ConnectMgr;
|
||||
|
||||
class p3disc: public p3Service
|
||||
|
||||
class p3disc: public p3Service, public pqiMonitor
|
||||
{
|
||||
public:
|
||||
bool local_disc;
|
||||
bool remote_disc;
|
||||
//sslroot *sslbase;
|
||||
|
||||
p3disc(p3AuthMgr *am, p3ConnectMgr *cm);
|
||||
virtual ~p3disc();
|
||||
|
||||
// Overloaded from p3Service functions.
|
||||
virtual int tick();
|
||||
|
||||
|
||||
// For Proxy Information.
|
||||
std::list<sockaddr_in> requestStunServers();
|
||||
std::list<cert *> potentialproxy(cert *target);
|
||||
p3disc(p3AuthMgr *am, p3ConnectMgr *cm);
|
||||
|
||||
// load and save configuration to sslroot.
|
||||
int save_configuration();
|
||||
int load_configuration();
|
||||
/************* from pqiMonitor *******************/
|
||||
virtual void statusChange(const std::list<pqipeer> &plist);
|
||||
/************* from pqiMonitor *******************/
|
||||
|
||||
int ts_lastcheck;
|
||||
int tick();
|
||||
|
||||
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;
|
||||
private:
|
||||
|
||||
|
||||
// local message construction/destruction.
|
||||
void *ldata;
|
||||
int ldlen;
|
||||
int ldlenmax;
|
||||
void respondToPeer(std::string id);
|
||||
|
||||
/* Network Output */
|
||||
void sendOwnDetails(std::string to);
|
||||
void sendPeerDetails(std::string to, std::string about);
|
||||
|
||||
bool std_port; // if we have bound to default.
|
||||
int ts_nextlp; // -1 for never (if on default)
|
||||
/* Network Input */
|
||||
int handleIncoming();
|
||||
void recvPeerOwnMsg(RsDiscItem *item);
|
||||
void recvPeerFriendMsg(RsDiscReply *item);
|
||||
|
||||
// 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*);
|
||||
/* handle network shape */
|
||||
int addDiscoveryData(std::string fromId, std::string aboutId,
|
||||
struct sockaddr_in laddr, struct sockaddr_in raddr,
|
||||
uint32_t flags, time_t ts);
|
||||
|
||||
// remote discovery function.
|
||||
int newRequests();
|
||||
int handleReplies();
|
||||
bool potentialproxies(std::string id, std::list<std::string> proxyIds);
|
||||
int idServers();
|
||||
|
||||
int handleDiscoveryData(RsDiscReply *di);
|
||||
int handleDiscoveryPing(RsDiscItem *di);
|
||||
int sendDiscoveryReply(cert *);
|
||||
int collectCerts();
|
||||
int distillData();
|
||||
/* data */
|
||||
|
||||
#if 0
|
||||
//cert *checkDuplicateX509(X509 *x509);
|
||||
std::list<cert *> &getDiscovered();
|
||||
private:
|
||||
|
||||
// Main Storage
|
||||
std::list<cert *> ad_init;
|
||||
std::list<cert *> discovered;
|
||||
#endif
|
||||
std::list<autoneighbour *> neighbours;
|
||||
p3AuthMgr *mAuthMgr;
|
||||
p3ConnectMgr *mConnMgr;
|
||||
|
||||
bool mRemoteDisc;
|
||||
bool mLocalDisc;
|
||||
|
||||
std::map<std::string, autoneighbour> neighbours;
|
||||
|
||||
p3AuthMgr *mAuthMgr;
|
||||
p3ConnectMgr *mConnMgr;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
#endif // MRK_PQI_AUTODISC_H
|
||||
|
435
libretroshare/src/services/p3ranking.cc
Normal file
435
libretroshare/src/services/p3ranking.cc
Normal file
@ -0,0 +1,435 @@
|
||||
/*
|
||||
* libretroshare/src/services p3ranking.cc
|
||||
*
|
||||
* 3P/PQI network interface for RetroShare.
|
||||
*
|
||||
* 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 "services/p3ranking.h"
|
||||
#include "pqi/pqibin.h"
|
||||
#include "pqi/p3authmgr.h"
|
||||
|
||||
const uint32_t RANK_MAX_FWD_OFFSET = (60 * 60 * 24 * 2); /* 2 Days */
|
||||
|
||||
std::string generateRandomLinkId();
|
||||
|
||||
/*****
|
||||
* TODO
|
||||
* (1) Streaming.
|
||||
* (2) Ranking.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
p3Ranking::p3Ranking(uint16_t subtype, CacheTransfer *cft,
|
||||
std::string sourcedir, std::string storedir,
|
||||
uint32_t storePeriod)
|
||||
:CacheSource(subtype, true, sourcedir),
|
||||
CacheStore(subtype, true, cft, storedir),
|
||||
mStorePeriod(storePeriod)
|
||||
{
|
||||
mOwnId = getAuthMgr()->OwnId();
|
||||
createDummyData();
|
||||
return;
|
||||
}
|
||||
|
||||
bool p3Ranking::loadLocalCache(const CacheData &data)
|
||||
{
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int p3Ranking::loadCache(const CacheData &data)
|
||||
{
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void p3Ranking::loadRankFile(std::string filename, std::string src)
|
||||
{
|
||||
|
||||
#if 0
|
||||
/* create the serialiser to load info */
|
||||
pqistreamer *streamer = createStreamer(filename, src, BIN_FLAGS_READABLE);
|
||||
|
||||
time_t now = time(NULL);
|
||||
time_t min = now - mStorePeriod;
|
||||
time_t max = now + RANK_MAX_FWD_OFFSET;
|
||||
|
||||
RsItem *item;
|
||||
RsRankMsg *newMsg;
|
||||
while(NULL != (item = streamer->GetItem()))
|
||||
{
|
||||
newMsg = (RsRankMsg *) item;
|
||||
|
||||
/* check timestamp */
|
||||
if ((newMsg->timestamp < min) || (newMsg->timestamp > max))
|
||||
{
|
||||
/* if outside range -> remove */
|
||||
delete newMsg;
|
||||
}
|
||||
else
|
||||
{
|
||||
addRankMsg(newMsg);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void p3Ranking::publishMsgs()
|
||||
{
|
||||
|
||||
#if 0
|
||||
/* create a serialiser */
|
||||
std::string file;
|
||||
pqistreamer *stream = createStreamer(file, mOwnId, BIN_FLAGS_NO_DELETE | BIN_FLAGS_HASH_DATA);
|
||||
|
||||
/* iterate through list */
|
||||
std::map<std::string, RankGroup>::iterator it;
|
||||
for(it = mData.begin(); it != mData.end(); it++)
|
||||
{
|
||||
if (it->second.ownTag)
|
||||
{
|
||||
/* write to serialiser */
|
||||
RsItem *item = it->second.comments[mOwnId];
|
||||
if (item)
|
||||
stream->SendItem(item);
|
||||
}
|
||||
}
|
||||
|
||||
CacheData data;
|
||||
data.pid = mOwnId;
|
||||
data.cid = CacheId(CacheSource::getCacheType(), 0);
|
||||
data.name = file;
|
||||
|
||||
refreshCache(data);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
void p3Ranking::addRankMsg(RsRankMsg *msg)
|
||||
{
|
||||
/* find msg */
|
||||
std::string id = msg->PeerId();
|
||||
std::string rid = msg->rid;
|
||||
|
||||
std::map<std::string, RankGroup>::iterator it;
|
||||
it = mData.find(rid);
|
||||
if (it == mData.end())
|
||||
{
|
||||
/* add a new one */
|
||||
RankGroup grp;
|
||||
grp.rid = rid;
|
||||
grp.ownTag = false;
|
||||
|
||||
/******** LINK SPECIFIC ****/
|
||||
grp.link = msg->link;
|
||||
grp.title = msg->title;
|
||||
|
||||
mData[rid] = grp;
|
||||
it = mData.find(rid);
|
||||
}
|
||||
|
||||
/* check for old comment */
|
||||
std::map<std::string, RsRankMsg *>::iterator cit;
|
||||
cit = (it->second).comments.find(id);
|
||||
if ((it->second).comments.end() != cit)
|
||||
{
|
||||
(it->second).comments.erase(cit);
|
||||
}
|
||||
|
||||
(it->second).comments[id] = msg;
|
||||
|
||||
if (id == mOwnId)
|
||||
{
|
||||
it->second.ownTag = true;
|
||||
mRepublish = true;
|
||||
}
|
||||
|
||||
reSortGroup(it->second);
|
||||
}
|
||||
|
||||
|
||||
/***************** Sorting ****************/
|
||||
|
||||
bool p3Ranking::setSortPeriod(uint32_t period)
|
||||
{
|
||||
mViewPeriod = period;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool p3Ranking::setSortMethod(uint32_t type)
|
||||
{
|
||||
mSortType = type;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool p3Ranking::clearPeerFilter()
|
||||
{
|
||||
mPeerFilter.clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool p3Ranking::setPeerFilter(std::list<std::string> peers)
|
||||
{
|
||||
mPeerFilter = peers;
|
||||
return true;
|
||||
}
|
||||
|
||||
float p3Ranking::locked_calcRank(RankGroup &grp) /* returns 0->100 */
|
||||
{
|
||||
|
||||
#if 0
|
||||
/* where all the work is done */
|
||||
bool doScore = (mSortType ==
|
||||
bool doTime = (mSortType ==
|
||||
bool doFilter = (mPeerFilter.size() > 0);
|
||||
float rank = 0;
|
||||
|
||||
for(it = grp.comments.begin(); it != grp.comments.end(); it++)
|
||||
{
|
||||
|
||||
if (doFilter)
|
||||
{
|
||||
/* do first so we can discard */
|
||||
|
||||
|
||||
if (doScore)
|
||||
if (mSortType
|
||||
#endif
|
||||
|
||||
return 100;
|
||||
}
|
||||
|
||||
void p3Ranking::reSortGroup(RankGroup &grp)
|
||||
{
|
||||
std::string rid = grp.rid;
|
||||
float rank = grp.rank;
|
||||
|
||||
/* remove from existings rankings */
|
||||
std::multimap<float, std::string>::iterator rit;
|
||||
rit = mRankings.lower_bound(grp.rank);
|
||||
for(; (rit != mRankings.end()) && (rit->first == grp.rank); rit++)
|
||||
{
|
||||
if (rit->second == rid)
|
||||
{
|
||||
mRankings.erase(rit);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* add it back in */
|
||||
grp.rank = locked_calcRank(grp);
|
||||
mRankings.insert(
|
||||
std::pair<float, std::string>(grp.rank, rid));
|
||||
}
|
||||
|
||||
void p3Ranking::sortAllMsgs()
|
||||
{
|
||||
/* iterate through list and re-score each one */
|
||||
std::map<std::string, RankGroup>::iterator it;
|
||||
|
||||
mRankings.clear();
|
||||
|
||||
for(it = mData.begin(); it != mData.end(); it++)
|
||||
{
|
||||
(it->second).rank = locked_calcRank(it->second);
|
||||
if (it->second.rank > 0)
|
||||
{
|
||||
mRankings.insert(
|
||||
std::pair<float, std::string>
|
||||
(it->second.rank, it->first));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/******** ACCESS *************/
|
||||
|
||||
/* get Ids */
|
||||
uint32_t p3Ranking::getRankingsCount()
|
||||
{
|
||||
return mRankings.size();
|
||||
}
|
||||
|
||||
float p3Ranking::getMaxRank()
|
||||
{
|
||||
if (mRankings.size() == 0)
|
||||
return 0;
|
||||
|
||||
return mRankings.rbegin()->first;
|
||||
}
|
||||
|
||||
bool p3Ranking::getRankings(uint32_t first, uint32_t count, std::list<std::string> &rids)
|
||||
{
|
||||
uint32_t i = 0;
|
||||
std::multimap<float, std::string>::iterator rit;
|
||||
for(rit = mRankings.begin(); (i < first) && (rit != mRankings.end()); rit++);
|
||||
|
||||
i = 0;
|
||||
for(; (i < count) && (rit != mRankings.end()); rit++)
|
||||
{
|
||||
rids.push_back(rit->second);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool p3Ranking::getRankDetails(std::string rid, RsRankDetails &details)
|
||||
{
|
||||
/* get the details. */
|
||||
std::map<std::string, RankGroup>::iterator it;
|
||||
it = mData.find(rid);
|
||||
if (mData.end() == it)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
details.rid = it->first;
|
||||
details.link = (it->second).link;
|
||||
details.title = (it->second).title;
|
||||
details.rank = (it->second).rank;
|
||||
details.ownTag = (it->second).ownTag;
|
||||
|
||||
std::map<std::string, RsRankMsg *>::iterator cit;
|
||||
for(cit = (it->second).comments.begin();
|
||||
cit != (it->second).comments.end(); cit++)
|
||||
{
|
||||
RsRankComment comm;
|
||||
comm.id = (cit->second)->PeerId();
|
||||
comm.timestamp = (cit->second)->timestamp;
|
||||
comm.comment = (cit->second)->comment;
|
||||
|
||||
details.comments.push_back(comm);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void p3Ranking::tick()
|
||||
{
|
||||
if (mRepublish)
|
||||
{
|
||||
publishMsgs();
|
||||
mRepublish = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/***** NEW CONTENT *****/
|
||||
std::string p3Ranking::newRankMsg(std::wstring link, std::wstring title, std::wstring comment)
|
||||
{
|
||||
/* generate an id */
|
||||
std::string rid = generateRandomLinkId();
|
||||
|
||||
RsRankMsg *msg = new RsRankMsg();
|
||||
|
||||
time_t now = time(NULL);
|
||||
|
||||
msg->PeerId(mOwnId);
|
||||
msg->rid = rid;
|
||||
msg->title = title;
|
||||
msg->timestamp = now;
|
||||
msg->link = link;
|
||||
msg->comment = comment;
|
||||
|
||||
addRankMsg(msg);
|
||||
|
||||
return rid;
|
||||
}
|
||||
|
||||
bool p3Ranking::updateComment(std::string rid, std::wstring comment)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
pqistreamer *createStreamer(std::string file, std::string src, uint32_t bioflags)
|
||||
{
|
||||
|
||||
#if 0
|
||||
RsSerialiser *rsSerialiser = new RsSerialiser();
|
||||
RsSerialType *serialType = new RsRankSerial(); /* TODO */
|
||||
|
||||
rsSerialiser->addSerialType(serialType);
|
||||
|
||||
BinInterface *bio = BinFileInterface(file.c_str(), bioflags);
|
||||
pqistreamer *streamer = new pqistreamer(rsSerialiser, src, bio, 0);
|
||||
|
||||
return streamer;
|
||||
#endif
|
||||
return NULL;
|
||||
}
|
||||
|
||||
std::string generateRandomLinkId()
|
||||
{
|
||||
std::ostringstream out;
|
||||
out << std::hex;
|
||||
|
||||
/* 4 bytes per random number: 4 x 4 = 16 bytes */
|
||||
for(int i = 0; i < 4; i++)
|
||||
{
|
||||
uint32_t rint = random();
|
||||
out << rint;
|
||||
}
|
||||
return out.str();
|
||||
}
|
||||
|
||||
|
||||
void p3Ranking::createDummyData()
|
||||
{
|
||||
RsRankMsg *msg = new RsRankMsg();
|
||||
|
||||
time_t now = time(NULL);
|
||||
|
||||
msg->PeerId(mOwnId);
|
||||
msg->rid = "0001";
|
||||
msg->title = L"Original Awesome Site!";
|
||||
msg->timestamp = now - 12345;
|
||||
msg->link = L"http://www.retroshare.org";
|
||||
msg->comment = L"Retroshares Website";
|
||||
|
||||
addRankMsg(msg);
|
||||
|
||||
msg = new RsRankMsg();
|
||||
msg->PeerId(mOwnId);
|
||||
msg->rid = "0002";
|
||||
msg->title = L"Awesome Site!";
|
||||
msg->timestamp = now - 123;
|
||||
msg->link = L"http://www.lunamutt.org";
|
||||
msg->comment = L"Lunamutt's Website";
|
||||
|
||||
addRankMsg(msg);
|
||||
|
||||
msg = new RsRankMsg();
|
||||
msg->PeerId("ALTID");
|
||||
msg->rid = "0002";
|
||||
msg->title = L"Awesome Site!";
|
||||
msg->timestamp = now - 12345;
|
||||
msg->link = L"http://www.lunamutt.org";
|
||||
msg->comment = L"Lunamutt's Website (TWO) How Long can this comment be!\n";
|
||||
msg->comment += L"What happens to the second line?\n";
|
||||
msg->comment += L"And a 3rd!";
|
||||
|
||||
addRankMsg(msg);
|
||||
}
|
||||
|
158
libretroshare/src/services/p3ranking.h
Normal file
158
libretroshare/src/services/p3ranking.h
Normal file
@ -0,0 +1,158 @@
|
||||
/*
|
||||
* libretroshare/src/services: p3ranking.h
|
||||
*
|
||||
* 3P/PQI network interface for RetroShare.
|
||||
*
|
||||
* 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".
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef P3_GENERIC_RANKING_HEADER
|
||||
#define P3_GENERIC_RANKING_HEADER
|
||||
|
||||
#include "dbase/cachestrapper.h"
|
||||
#include "pqi/pqiservice.h"
|
||||
#include "pqi/pqistreamer.h"
|
||||
//#include "util/rsthreads.h"
|
||||
|
||||
#include "serialiser/rsserial.h"
|
||||
|
||||
#include "rsiface/rsrank.h"
|
||||
|
||||
/*
|
||||
* A Generic Ranking system.
|
||||
* Each User provides one cache...
|
||||
*
|
||||
* can be overloaded for specific types
|
||||
* (links, shares, photos etc)
|
||||
*/
|
||||
|
||||
class RsRankMsg: public RsItem
|
||||
{
|
||||
public:
|
||||
|
||||
//std::string peerId; /* From */
|
||||
std::string rid; /* Random Id */
|
||||
time_t timestamp;
|
||||
std::wstring link;
|
||||
std::wstring title;
|
||||
std::wstring comment;
|
||||
|
||||
RsRankMsg():RsItem(0) { return; }
|
||||
virtual void clear()
|
||||
{ return; }
|
||||
virtual std::ostream& print(std::ostream &out, uint16_t)
|
||||
{ return out; }
|
||||
|
||||
};
|
||||
|
||||
class RsRankSerial: public RsSerialType
|
||||
{
|
||||
public:
|
||||
|
||||
RsRankSerial()
|
||||
:RsSerialType(0,0,0)
|
||||
{ return; }
|
||||
|
||||
};
|
||||
|
||||
/* group these together */
|
||||
|
||||
class RankGroup
|
||||
{
|
||||
public:
|
||||
|
||||
std::string rid; /* Random Id */
|
||||
std::wstring link;
|
||||
std::wstring title;
|
||||
float rank;
|
||||
bool ownTag;
|
||||
std::map<std::string, RsRankMsg *> comments;
|
||||
};
|
||||
|
||||
|
||||
class p3Ranking: public CacheSource, public CacheStore
|
||||
{
|
||||
public:
|
||||
|
||||
p3Ranking(uint16_t subtype, CacheTransfer *cft,
|
||||
std::string sourcedir, std::string storedir,
|
||||
uint32_t storePeriod);
|
||||
|
||||
/******************************* CACHE SOURCE / STORE Interface *********************/
|
||||
|
||||
/* overloaded functions from Cache Source */
|
||||
virtual bool loadLocalCache(const CacheData &data);
|
||||
|
||||
/* overloaded functions from Cache Store */
|
||||
virtual int loadCache(const CacheData &data);
|
||||
|
||||
/******************************* CACHE SOURCE / STORE Interface *********************/
|
||||
|
||||
public:
|
||||
|
||||
/************* Extern Interface *******/
|
||||
|
||||
/* Set Sort Methods */
|
||||
virtual bool setSortPeriod(uint32_t period);
|
||||
virtual bool setSortMethod(uint32_t type);
|
||||
virtual bool clearPeerFilter();
|
||||
virtual bool setPeerFilter(std::list<std::string> peers);
|
||||
|
||||
/* get Ids */
|
||||
virtual uint32_t getRankingsCount();
|
||||
virtual float getMaxRank();
|
||||
virtual bool getRankings(uint32_t first, uint32_t count, std::list<std::string> &rids);
|
||||
virtual bool getRankDetails(std::string rid, RsRankDetails &details);
|
||||
|
||||
/* Add New Comment / Msg */
|
||||
virtual std::string newRankMsg(std::wstring link, std::wstring title, std::wstring comment);
|
||||
virtual bool updateComment(std::string rid, std::wstring comment);
|
||||
|
||||
|
||||
void tick();
|
||||
|
||||
void loadRankFile(std::string filename, std::string src);
|
||||
void addRankMsg(RsRankMsg *msg);
|
||||
void publishMsgs();
|
||||
float locked_calcRank(RankGroup &grp); /* returns 0->100 */
|
||||
void reSortGroup(RankGroup &grp);
|
||||
void sortAllMsgs();
|
||||
pqistreamer *createStreamer(std::string file, std::string src, uint32_t bioflags);
|
||||
|
||||
private:
|
||||
|
||||
void createDummyData();
|
||||
|
||||
bool mRepublish;
|
||||
uint32_t mStorePeriod;
|
||||
|
||||
std::string mOwnId;
|
||||
|
||||
std::map<std::string, RankGroup> mData;
|
||||
std::multimap<float, std::string> mRankings;
|
||||
|
||||
/* Filter/Sort params */
|
||||
std::list<std::string> mPeerFilter;
|
||||
uint32_t mViewPeriod;
|
||||
uint32_t mSortType;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user