2012-02-13 13:22:35 -05:00
|
|
|
/*
|
|
|
|
* libretroshare/src/services p3idservice.cc
|
|
|
|
*
|
|
|
|
* Id interface for RetroShare.
|
|
|
|
*
|
|
|
|
* Copyright 2012-2012 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
|
2012-06-13 20:27:28 -04:00
|
|
|
* License Version 2.1 as published by the Free Software Foundation.
|
2012-02-13 13:22:35 -05:00
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA.
|
|
|
|
*
|
|
|
|
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "services/p3idservice.h"
|
2012-10-21 11:48:18 -04:00
|
|
|
#include "serialiser/rsgxsiditems.h"
|
2013-02-28 16:58:38 -05:00
|
|
|
#include "retroshare/rsgxsflags.h"
|
2012-02-13 13:22:35 -05:00
|
|
|
#include "util/rsrandom.h"
|
2012-11-05 17:28:08 -05:00
|
|
|
#include "util/rsstring.h"
|
|
|
|
|
|
|
|
#include "pqi/authgpg.h"
|
|
|
|
|
2012-02-13 13:22:35 -05:00
|
|
|
#include <retroshare/rspeers.h>
|
2012-10-21 11:48:18 -04:00
|
|
|
|
2012-02-13 13:22:35 -05:00
|
|
|
#include <sstream>
|
2012-07-30 07:35:26 -04:00
|
|
|
#include <stdio.h>
|
2012-02-13 13:22:35 -05:00
|
|
|
|
|
|
|
/****
|
2013-02-27 16:16:03 -05:00
|
|
|
* #define DEBUG_IDS 1
|
2013-02-10 10:40:07 -05:00
|
|
|
* #define GXSID_GEN_DUMMY_DATA 1
|
2012-02-13 13:22:35 -05:00
|
|
|
****/
|
2012-11-30 19:16:24 -05:00
|
|
|
|
2013-02-10 10:40:07 -05:00
|
|
|
#define GXSID_GEN_DUMMY_DATA 1
|
2012-02-13 13:22:35 -05:00
|
|
|
|
2012-06-07 13:10:47 -04:00
|
|
|
#define ID_REQUEST_LIST 0x0001
|
|
|
|
#define ID_REQUEST_IDENTITY 0x0002
|
|
|
|
#define ID_REQUEST_REPUTATION 0x0003
|
|
|
|
#define ID_REQUEST_OPINION 0x0004
|
|
|
|
|
2012-02-13 13:22:35 -05:00
|
|
|
RsIdentity *rsIdentity = NULL;
|
|
|
|
|
2012-10-22 16:36:28 -04:00
|
|
|
|
2012-11-05 17:28:08 -05:00
|
|
|
/******
|
|
|
|
* Some notes:
|
|
|
|
* Identity tasks:
|
|
|
|
* - Provide keys for signing / validating author signatures.
|
|
|
|
* - Reputations
|
|
|
|
* - Identify Known Friend's IDs.
|
|
|
|
* - Provide details to other services (nicknames, reputations, gpg ids, etc)
|
|
|
|
*
|
|
|
|
* Background services:
|
|
|
|
* - Lookup and cache keys / details of identities.
|
|
|
|
* - Check GPGHashes.
|
|
|
|
* - Calculate Reputations.
|
|
|
|
*
|
|
|
|
* We have a lot of information to store in Service Strings.
|
|
|
|
* - GPGId or last check ts.
|
|
|
|
* - Reputation stuff.
|
|
|
|
*/
|
|
|
|
|
2012-10-21 11:48:18 -04:00
|
|
|
#define RSGXSID_MAX_SERVICE_STRING 1024
|
2012-02-13 13:22:35 -05:00
|
|
|
|
2012-11-06 14:18:11 -05:00
|
|
|
#define BG_PGPHASH 1
|
|
|
|
#define BG_REPUTATION 2
|
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
|
|
|
|
#define GXSIDREQ_CACHELOAD 0x0001
|
|
|
|
#define GXSIDREQ_CACHEOWNIDS 0x0002
|
|
|
|
|
|
|
|
#define GXSIDREQ_PGPHASH 0x0010
|
|
|
|
#define GXSIDREQ_REPUTATION 0x0020
|
|
|
|
|
|
|
|
#define GXSIDREQ_CACHETEST 0x1000
|
|
|
|
|
|
|
|
// Events.
|
|
|
|
#define GXSID_EVENT_CACHEOWNIDS 0x0001
|
|
|
|
#define GXSID_EVENT_CACHELOAD 0x0002
|
|
|
|
|
|
|
|
#define GXSID_EVENT_PGPHASH 0x0010
|
|
|
|
#define GXSID_EVENT_PGPHASH_PROC 0x0011
|
|
|
|
|
|
|
|
#define GXSID_EVENT_REPUTATION 0x0020
|
|
|
|
|
|
|
|
#define GXSID_EVENT_CACHETEST 0x1000
|
|
|
|
|
2012-11-30 19:16:24 -05:00
|
|
|
#define GXSID_EVENT_DUMMYDATA 0x2000
|
|
|
|
#define GXSID_EVENT_DUMMY_OWNIDS 0x2001
|
|
|
|
#define GXSID_EVENT_DUMMY_PGPID 0x2002
|
|
|
|
#define GXSID_EVENT_DUMMY_UNKNOWN_PGPID 0x2003
|
|
|
|
#define GXSID_EVENT_DUMMY_PSEUDOID 0x2004
|
|
|
|
|
|
|
|
|
|
|
|
/* delays */
|
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
#define CACHETEST_PERIOD 60
|
|
|
|
|
|
|
|
#define OWNID_RELOAD_DELAY 10
|
|
|
|
|
|
|
|
#define PGPHASH_PERIOD 60
|
|
|
|
#define PGPHASH_RETRY_PERIOD 11
|
|
|
|
#define PGPHASH_PROC_PERIOD 1
|
|
|
|
|
|
|
|
#define REPUTATION_PERIOD 60
|
|
|
|
#define REPUTATION_RETRY_PERIOD 13
|
|
|
|
#define REPUTATION_PROC_PERIOD 1
|
|
|
|
|
2012-06-13 20:27:28 -04:00
|
|
|
/********************************************************************************/
|
|
|
|
/******************* Startup / Tick ******************************************/
|
|
|
|
/********************************************************************************/
|
|
|
|
|
2012-10-17 20:28:54 -04:00
|
|
|
p3IdService::p3IdService(RsGeneralDataService *gds, RsNetworkExchangeService *nes)
|
2012-11-19 17:00:05 -05:00
|
|
|
: RsGxsIdExchange(gds, nes, new RsGxsIdSerialiser(), RS_SERVICE_GXSV1_TYPE_GXSID),
|
2012-11-29 17:48:28 -05:00
|
|
|
RsIdentity(this), GxsTokenQueue(this), RsTickEvent(), mIdMtx("p3IdService"),
|
|
|
|
mPublicKeyCache(DEFAULT_MEM_CACHE_SIZE, "GxsIdPublicKeyCache"),
|
|
|
|
mPrivateKeyCache(DEFAULT_MEM_CACHE_SIZE, "GxsIdPrivateKeyCache")
|
2012-10-17 20:28:54 -04:00
|
|
|
{
|
2012-11-06 14:18:11 -05:00
|
|
|
mBgSchedule_Mode = 0;
|
|
|
|
mBgSchedule_Active = false;
|
2012-07-29 09:09:36 -04:00
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
// Kick off Cache Testing, + Others.
|
|
|
|
RsTickEvent::schedule_in(GXSID_EVENT_PGPHASH, PGPHASH_PERIOD);
|
|
|
|
RsTickEvent::schedule_in(GXSID_EVENT_REPUTATION, REPUTATION_PERIOD);
|
|
|
|
RsTickEvent::schedule_now(GXSID_EVENT_CACHEOWNIDS);
|
2012-11-30 19:16:24 -05:00
|
|
|
|
2013-02-10 10:40:07 -05:00
|
|
|
#ifndef GXS_DEV_TESTNET // NO RESET, OR DUMMYDATA for TESTNET
|
|
|
|
|
|
|
|
RsTickEvent::schedule_in(GXSID_EVENT_CACHETEST, CACHETEST_PERIOD);
|
|
|
|
|
|
|
|
#ifdef GXSID_GEN_DUMMY_DATA
|
2012-11-30 19:16:24 -05:00
|
|
|
RsTickEvent::schedule_now(GXSID_EVENT_DUMMYDATA);
|
2013-02-10 10:40:07 -05:00
|
|
|
#endif
|
2012-11-30 19:16:24 -05:00
|
|
|
#endif
|
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
}
|
2012-11-01 20:45:50 -04:00
|
|
|
|
2012-10-21 15:15:46 -04:00
|
|
|
|
2012-11-06 14:18:11 -05:00
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
void p3IdService::service_tick()
|
|
|
|
{
|
|
|
|
RsTickEvent::tick_events();
|
|
|
|
GxsTokenQueue::checkRequests(); // GxsTokenQueue handles all requests.
|
2012-10-28 19:13:15 -04:00
|
|
|
return;
|
2012-02-13 13:22:35 -05:00
|
|
|
}
|
|
|
|
|
2012-10-28 19:13:15 -04:00
|
|
|
void p3IdService::notifyChanges(std::vector<RsGxsNotify *> &changes)
|
|
|
|
{
|
2012-11-01 20:45:50 -04:00
|
|
|
std::cerr << "p3IdService::notifyChanges()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
2013-02-28 16:58:38 -05:00
|
|
|
RsGxsIfaceHelper::receiveChanges(changes);
|
2012-10-28 19:13:15 -04:00
|
|
|
}
|
2012-06-07 13:10:47 -04:00
|
|
|
|
2012-10-17 20:28:54 -04:00
|
|
|
/********************************************************************************/
|
|
|
|
/******************* RsIdentity Interface ***************************************/
|
|
|
|
/********************************************************************************/
|
2012-06-07 13:10:47 -04:00
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
#if 0
|
2012-10-21 11:48:18 -04:00
|
|
|
bool p3IdService:: getNickname(const RsGxsId &id, std::string &nickname)
|
2012-06-07 13:10:47 -04:00
|
|
|
{
|
2012-10-17 20:28:54 -04:00
|
|
|
return false;
|
2012-06-07 13:10:47 -04:00
|
|
|
}
|
2012-11-19 17:00:05 -05:00
|
|
|
#endif
|
2012-06-07 13:10:47 -04:00
|
|
|
|
2012-10-21 11:48:18 -04:00
|
|
|
bool p3IdService:: getIdDetails(const RsGxsId &id, RsIdentityDetails &details)
|
2012-06-07 13:10:47 -04:00
|
|
|
{
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::getIdDetails(" << id << ")";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
RsGxsIdCache data;
|
|
|
|
if (mPublicKeyCache.fetch(id, data))
|
|
|
|
{
|
|
|
|
details = data.details;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* try private cache too */
|
|
|
|
if (mPrivateKeyCache.fetch(id, data))
|
|
|
|
{
|
|
|
|
details = data.details;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* it isn't there - add to public requests */
|
|
|
|
cache_request_load(id);
|
|
|
|
|
2012-10-17 20:28:54 -04:00
|
|
|
return false;
|
2012-06-07 13:10:47 -04:00
|
|
|
}
|
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
|
2012-10-21 11:48:18 -04:00
|
|
|
bool p3IdService:: getOwnIds(std::list<RsGxsId> &ownIds)
|
2012-06-07 13:10:47 -04:00
|
|
|
{
|
2012-11-19 17:00:05 -05:00
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
ownIds = mOwnIds;
|
|
|
|
|
|
|
|
return true;
|
2012-06-07 13:10:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-17 20:28:54 -04:00
|
|
|
//
|
|
|
|
bool p3IdService::submitOpinion(uint32_t& token, RsIdOpinion &opinion)
|
2012-06-07 13:10:47 -04:00
|
|
|
{
|
2012-10-17 20:28:54 -04:00
|
|
|
return false;
|
2012-06-13 20:27:28 -04:00
|
|
|
}
|
2012-06-07 13:10:47 -04:00
|
|
|
|
2012-10-17 20:28:54 -04:00
|
|
|
bool p3IdService::createIdentity(uint32_t& token, RsIdentityParameters ¶ms)
|
2012-06-13 20:27:28 -04:00
|
|
|
{
|
2012-11-07 16:38:43 -05:00
|
|
|
|
|
|
|
RsGxsIdGroup id;
|
|
|
|
|
|
|
|
id.mMeta.mGroupName = params.nickname;
|
|
|
|
if (params.isPgpLinked)
|
|
|
|
{
|
|
|
|
id.mMeta.mGroupFlags = RSGXSID_GROUPFLAG_REALID;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
id.mMeta.mGroupFlags = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
createGroup(token, id);
|
2012-11-19 17:00:05 -05:00
|
|
|
|
2012-11-07 16:38:43 -05:00
|
|
|
return true;
|
2012-06-07 13:10:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-17 20:28:54 -04:00
|
|
|
/********************************************************************************/
|
|
|
|
/******************* RsGixs Interface ***************************************/
|
|
|
|
/********************************************************************************/
|
2012-06-07 13:10:47 -04:00
|
|
|
|
2012-10-21 11:48:18 -04:00
|
|
|
bool p3IdService::haveKey(const RsGxsId &id)
|
2012-06-13 20:27:28 -04:00
|
|
|
{
|
2012-11-03 10:07:26 -04:00
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
return mPublicKeyCache.is_cached(id);
|
2012-06-13 20:27:28 -04:00
|
|
|
}
|
2012-06-07 13:10:47 -04:00
|
|
|
|
2012-10-21 11:48:18 -04:00
|
|
|
bool p3IdService::havePrivateKey(const RsGxsId &id)
|
2012-06-13 20:27:28 -04:00
|
|
|
{
|
2012-11-03 10:07:26 -04:00
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
return mPrivateKeyCache.is_cached(id);
|
2012-06-07 13:10:47 -04:00
|
|
|
}
|
|
|
|
|
2012-10-21 11:48:18 -04:00
|
|
|
bool p3IdService::requestKey(const RsGxsId &id, const std::list<PeerId> &peers)
|
2012-07-06 19:14:18 -04:00
|
|
|
{
|
2012-11-03 10:07:26 -04:00
|
|
|
if (haveKey(id))
|
2012-10-22 16:36:28 -04:00
|
|
|
return true;
|
|
|
|
return cache_request_load(id);
|
2012-07-06 19:14:18 -04:00
|
|
|
}
|
2012-06-07 13:10:47 -04:00
|
|
|
|
2012-10-21 11:48:18 -04:00
|
|
|
int p3IdService::getKey(const RsGxsId &id, RsTlvSecurityKey &key)
|
2012-06-13 20:27:28 -04:00
|
|
|
{
|
2012-11-03 10:07:26 -04:00
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
2012-10-22 16:36:28 -04:00
|
|
|
RsGxsIdCache data;
|
2012-11-03 10:07:26 -04:00
|
|
|
if (mPublicKeyCache.fetch(id, data))
|
2012-10-22 16:36:28 -04:00
|
|
|
{
|
|
|
|
key = data.pubkey;
|
|
|
|
return 1;
|
|
|
|
}
|
2012-10-17 20:28:54 -04:00
|
|
|
return -1;
|
2012-07-06 19:14:18 -04:00
|
|
|
}
|
|
|
|
|
2012-11-03 07:57:27 -04:00
|
|
|
bool p3IdService::requestPrivateKey(const RsGxsId &id)
|
|
|
|
{
|
2012-11-03 10:07:26 -04:00
|
|
|
if (havePrivateKey(id))
|
|
|
|
return true;
|
|
|
|
return cache_request_load(id);
|
2012-11-03 07:57:27 -04:00
|
|
|
}
|
|
|
|
|
2012-10-21 11:48:18 -04:00
|
|
|
int p3IdService::getPrivateKey(const RsGxsId &id, RsTlvSecurityKey &key)
|
2012-07-06 19:14:18 -04:00
|
|
|
{
|
2012-11-03 10:07:26 -04:00
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
RsGxsIdCache data;
|
|
|
|
if (mPrivateKeyCache.fetch(id, data))
|
|
|
|
{
|
|
|
|
key = data.pubkey;
|
|
|
|
return 1;
|
|
|
|
}
|
2012-10-17 20:28:54 -04:00
|
|
|
return -1;
|
2012-06-13 20:27:28 -04:00
|
|
|
}
|
2012-06-07 13:10:47 -04:00
|
|
|
|
|
|
|
|
2012-10-17 20:28:54 -04:00
|
|
|
/********************************************************************************/
|
|
|
|
/******************* RsGixsReputation ***************************************/
|
|
|
|
/********************************************************************************/
|
2012-02-13 13:22:35 -05:00
|
|
|
|
2012-10-21 11:48:18 -04:00
|
|
|
bool p3IdService::getReputation(const RsGxsId &id, const GixsReputation &rep)
|
2012-06-13 20:27:28 -04:00
|
|
|
{
|
|
|
|
return false;
|
2012-02-13 13:22:35 -05:00
|
|
|
}
|
|
|
|
|
2012-06-07 13:10:47 -04:00
|
|
|
|
2012-10-17 20:28:54 -04:00
|
|
|
/********************************************************************************/
|
|
|
|
/******************* Get/Set Data ******************************************/
|
|
|
|
/********************************************************************************/
|
2012-02-13 13:22:35 -05:00
|
|
|
|
2012-10-21 11:48:18 -04:00
|
|
|
bool p3IdService::getGroupData(const uint32_t &token, std::vector<RsGxsIdGroup> &groups)
|
2012-06-13 20:27:28 -04:00
|
|
|
{
|
|
|
|
|
2012-10-17 20:28:54 -04:00
|
|
|
std::vector<RsGxsGrpItem*> grpData;
|
|
|
|
bool ok = RsGenExchange::getGroupData(token, grpData);
|
2012-06-13 20:27:28 -04:00
|
|
|
|
2012-10-17 20:28:54 -04:00
|
|
|
if(ok)
|
|
|
|
{
|
|
|
|
std::vector<RsGxsGrpItem*>::iterator vit = grpData.begin();
|
2012-07-06 19:14:18 -04:00
|
|
|
|
2012-10-17 20:28:54 -04:00
|
|
|
for(; vit != grpData.end(); vit++)
|
|
|
|
{
|
|
|
|
RsGxsIdGroupItem* item = dynamic_cast<RsGxsIdGroupItem*>(*vit);
|
2012-10-21 11:48:18 -04:00
|
|
|
RsGxsIdGroup group = item->group;
|
|
|
|
group.mMeta = item->meta;
|
2012-11-06 14:18:11 -05:00
|
|
|
|
|
|
|
// Decode information from serviceString.
|
|
|
|
SSGxsIdGroup ssdata;
|
|
|
|
if (ssdata.load(group.mMeta.mServiceString))
|
|
|
|
{
|
|
|
|
group.mPgpKnown = ssdata.pgp.idKnown;
|
|
|
|
group.mPgpId = ssdata.pgp.pgpId;
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-07 16:38:43 -05:00
|
|
|
std::cerr << "p3IdService::getGroupData() Success decoding ServiceString";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "\t mGpgKnown: " << group.mPgpKnown;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "\t mGpgId: " << group.mPgpId;
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-06 14:18:11 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
group.mPgpKnown = false;
|
|
|
|
group.mPgpId = "";
|
2012-11-07 16:38:43 -05:00
|
|
|
|
|
|
|
std::cerr << "p3IdService::getGroupData() Failed to decode ServiceString";
|
|
|
|
std::cerr << std::endl;
|
2012-11-06 14:18:11 -05:00
|
|
|
}
|
|
|
|
|
2012-10-17 20:28:54 -04:00
|
|
|
groups.push_back(group);
|
|
|
|
}
|
|
|
|
}
|
2012-06-13 20:27:28 -04:00
|
|
|
|
2012-10-17 20:28:54 -04:00
|
|
|
return ok;
|
2012-02-13 13:22:35 -05:00
|
|
|
}
|
|
|
|
|
2012-06-13 20:27:28 -04:00
|
|
|
|
2012-10-21 11:48:18 -04:00
|
|
|
bool p3IdService::getMsgData(const uint32_t &token, std::vector<RsGxsIdOpinion> &opinions)
|
2012-02-13 13:22:35 -05:00
|
|
|
{
|
2012-10-17 20:28:54 -04:00
|
|
|
GxsMsgDataMap msgData;
|
|
|
|
bool ok = RsGenExchange::getMsgData(token, msgData);
|
2012-02-13 13:22:35 -05:00
|
|
|
|
2012-10-17 20:28:54 -04:00
|
|
|
if(ok)
|
2012-02-13 13:22:35 -05:00
|
|
|
{
|
2012-10-17 20:28:54 -04:00
|
|
|
GxsMsgDataMap::iterator mit = msgData.begin();
|
|
|
|
|
|
|
|
for(; mit != msgData.end(); mit++)
|
|
|
|
{
|
|
|
|
RsGxsGroupId grpId = mit->first;
|
|
|
|
std::vector<RsGxsMsgItem*>& msgItems = mit->second;
|
|
|
|
std::vector<RsGxsMsgItem*>::iterator vit = msgItems.begin();
|
|
|
|
|
|
|
|
for(; vit != msgItems.end(); vit++)
|
|
|
|
{
|
2012-10-21 11:48:18 -04:00
|
|
|
RsGxsIdOpinionItem* item = dynamic_cast<RsGxsIdOpinionItem*>(*vit);
|
2012-10-17 20:28:54 -04:00
|
|
|
|
2012-10-21 11:48:18 -04:00
|
|
|
if (item)
|
2012-10-17 20:28:54 -04:00
|
|
|
{
|
2012-10-21 11:48:18 -04:00
|
|
|
RsGxsIdOpinion opinion = item->opinion;
|
|
|
|
opinion.mMeta = item->meta;
|
|
|
|
opinions.push_back(opinion);
|
2012-10-17 20:28:54 -04:00
|
|
|
delete item;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-10-21 11:48:18 -04:00
|
|
|
std::cerr << "Not a IdOpinion Item, deleting!" << std::endl;
|
2012-10-17 20:28:54 -04:00
|
|
|
delete *vit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-13 20:27:28 -04:00
|
|
|
}
|
2012-10-17 20:28:54 -04:00
|
|
|
return ok;
|
2012-06-13 20:27:28 -04:00
|
|
|
}
|
|
|
|
|
2012-10-17 20:28:54 -04:00
|
|
|
/********************************************************************************/
|
|
|
|
/********************************************************************************/
|
|
|
|
/********************************************************************************/
|
2012-06-13 20:27:28 -04:00
|
|
|
|
2012-10-21 11:48:18 -04:00
|
|
|
bool p3IdService::createGroup(uint32_t& token, RsGxsIdGroup &group)
|
2012-06-13 20:27:28 -04:00
|
|
|
{
|
2012-10-17 20:28:54 -04:00
|
|
|
RsGxsIdGroupItem* item = new RsGxsIdGroupItem();
|
|
|
|
item->group = group;
|
|
|
|
item->meta = group.mMeta;
|
|
|
|
RsGenExchange::publishGroup(token, item);
|
|
|
|
return true;
|
2012-06-13 20:27:28 -04:00
|
|
|
}
|
|
|
|
|
2012-10-21 11:48:18 -04:00
|
|
|
bool p3IdService::createMsg(uint32_t& token, RsGxsIdOpinion &opinion)
|
2012-06-13 20:27:28 -04:00
|
|
|
{
|
2012-10-21 11:48:18 -04:00
|
|
|
RsGxsIdOpinionItem* item = new RsGxsIdOpinionItem();
|
|
|
|
item->opinion = opinion;
|
|
|
|
item->meta = opinion.mMeta;
|
2012-10-17 20:28:54 -04:00
|
|
|
RsGenExchange::publishMsg(token, item);
|
|
|
|
return true;
|
2012-06-13 20:27:28 -04:00
|
|
|
}
|
|
|
|
|
2012-10-21 15:15:46 -04:00
|
|
|
/************************************************************************************/
|
|
|
|
/************************************************************************************/
|
|
|
|
/************************************************************************************/
|
|
|
|
|
2012-11-05 17:28:08 -05:00
|
|
|
/* Encoding / Decoding Group Service String stuff
|
|
|
|
*
|
|
|
|
* Pgp stuff.
|
|
|
|
*
|
|
|
|
* If flagged as pgp id....
|
|
|
|
* then we need to know if its been matched, or when we last tried to match.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool SSGxsIdPgp::load(const std::string &input)
|
|
|
|
{
|
|
|
|
char pgpline[RSGXSID_MAX_SERVICE_STRING];
|
|
|
|
int timestamp = 0;
|
|
|
|
if (1 == sscanf(input.c_str(), "K:1 I:%[^)]", pgpline))
|
|
|
|
{
|
|
|
|
idKnown = true;
|
|
|
|
pgpId = pgpline;
|
|
|
|
}
|
|
|
|
else if (1 == sscanf(input.c_str(), "K:0 T:%d", ×tamp))
|
|
|
|
{
|
|
|
|
lastCheckTs = timestamp;
|
|
|
|
idKnown = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string SSGxsIdPgp::save() const
|
|
|
|
{
|
|
|
|
std::string output;
|
|
|
|
if (idKnown)
|
|
|
|
{
|
|
|
|
output += "K:1 I:";
|
|
|
|
output += pgpId;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rs_sprintf(output, "K:0 T:%d", lastCheckTs);
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SSGxsIdScore::load(const std::string &input)
|
|
|
|
{
|
|
|
|
return (1 == sscanf(input.c_str(), "%d", &score));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string SSGxsIdScore::save() const
|
|
|
|
{
|
|
|
|
std::string output;
|
|
|
|
rs_sprintf(output, "%d", score);
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SSGxsIdCumulator::load(const std::string &input)
|
|
|
|
{
|
|
|
|
return (4 == sscanf(input.c_str(), "%d %d %lf %lf", &count, &nullcount, &sum, &sumsq));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string SSGxsIdCumulator::save() const
|
|
|
|
{
|
|
|
|
std::string output;
|
|
|
|
rs_sprintf(output, "%d %d %lf %lf", count, nullcount, sum, sumsq);
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SSGxsIdGroup::load(const std::string &input)
|
|
|
|
{
|
|
|
|
char pgpstr[RSGXSID_MAX_SERVICE_STRING];
|
|
|
|
char scorestr[RSGXSID_MAX_SERVICE_STRING];
|
|
|
|
char opinionstr[RSGXSID_MAX_SERVICE_STRING];
|
|
|
|
char repstr[RSGXSID_MAX_SERVICE_STRING];
|
|
|
|
|
|
|
|
// split into two parts.
|
2012-11-07 16:38:43 -05:00
|
|
|
if (4 != sscanf(input.c_str(), "v1 {P:%[^}]} {Y:%[^}]} {O:%[^}]} {R:%[^}]}", pgpstr, scorestr, opinionstr, repstr))
|
2012-11-05 17:28:08 -05:00
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
std::cerr << "SSGxsIdGroup::load() Failed to extract 4 Parts";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ok = true;
|
2012-11-07 16:38:43 -05:00
|
|
|
if (pgp.load(pgpstr))
|
2012-11-05 17:28:08 -05:00
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
std::cerr << "SSGxsIdGroup::load() pgpstr: " << pgpstr;
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
std::cerr << "SSGxsIdGroup::load() Invalid pgpstr: " << pgpstr;
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
|
2012-11-07 16:38:43 -05:00
|
|
|
if (score.load(scorestr))
|
2012-11-05 17:28:08 -05:00
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
std::cerr << "SSGxsIdGroup::load() scorestr: " << scorestr;
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
std::cerr << "SSGxsIdGroup::load() Invalid scorestr: " << scorestr;
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
|
2012-11-07 16:38:43 -05:00
|
|
|
if (opinion.load(opinionstr))
|
2012-11-05 17:28:08 -05:00
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
std::cerr << "SSGxsIdGroup::load() opinionstr: " << opinionstr;
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
std::cerr << "SSGxsIdGroup::load() Invalid opinionstr: " << opinionstr;
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
|
2012-11-07 16:38:43 -05:00
|
|
|
if (reputation.load(repstr))
|
2012-11-05 17:28:08 -05:00
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
std::cerr << "SSGxsIdGroup::load() repstr: " << repstr;
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
std::cerr << "SSGxsIdGroup::load() Invalid repstr: " << repstr;
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-07 16:38:43 -05:00
|
|
|
std::cerr << "SSGxsIdGroup::load() regurgitated: " << save();
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
std::cerr << "SSGxsIdGroup::load() isOkay?: " << ok;
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string SSGxsIdGroup::save() const
|
|
|
|
{
|
|
|
|
std::string output = "v1 ";
|
|
|
|
|
|
|
|
output += "{P:";
|
|
|
|
output += pgp.save();
|
|
|
|
output += "}";
|
|
|
|
|
|
|
|
output += "{Y:";
|
|
|
|
output += score.save();
|
|
|
|
output += "}";
|
|
|
|
|
|
|
|
output += "{O:";
|
|
|
|
output += opinion.save();
|
|
|
|
output += "}";
|
|
|
|
|
|
|
|
output += "{R:";
|
|
|
|
output += reputation.save();
|
|
|
|
output += "}";
|
|
|
|
|
2012-11-07 16:38:43 -05:00
|
|
|
//std::cerr << "SSGxsIdGroup::save() output: " << output;
|
|
|
|
//std::cerr << std::endl;
|
2012-11-05 17:28:08 -05:00
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/************************************************************************************/
|
|
|
|
/************************************************************************************/
|
|
|
|
/************************************************************************************/
|
|
|
|
|
2012-10-21 15:15:46 -04:00
|
|
|
/* Cache of recently used keys
|
|
|
|
*
|
|
|
|
* It is expensive to fetch the keys, so we want to keep them around if possible.
|
|
|
|
* It only stores the immutable stuff.
|
|
|
|
*
|
|
|
|
* This is probably crude and crap to start with.
|
|
|
|
* Want Least Recently Used (LRU) discard policy, without having to search whole cache.
|
|
|
|
* Use two maps:
|
|
|
|
* - CacheMap[key] => data.
|
|
|
|
* - LRUMultiMap[AccessTS] => key
|
|
|
|
*
|
|
|
|
* NOTE: This could be moved to a seperate class and templated to make generic
|
|
|
|
* as it might be generally useful.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-10-22 16:36:28 -04:00
|
|
|
RsGxsIdCache::RsGxsIdCache()
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-03 07:57:27 -04:00
|
|
|
RsGxsIdCache::RsGxsIdCache(const RsGxsIdGroupItem *item, const RsTlvSecurityKey &in_pkey)
|
2012-10-22 16:36:28 -04:00
|
|
|
{
|
2012-11-19 17:00:05 -05:00
|
|
|
// Save Keys.
|
2012-11-03 07:57:27 -04:00
|
|
|
pubkey = in_pkey;
|
2012-10-22 16:36:28 -04:00
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
// Fill in Details.
|
|
|
|
details.mNickname = item->meta.mGroupName;
|
|
|
|
details.mId = item->meta.mGroupId;
|
|
|
|
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "RsGxsIdCache::RsGxsIdCache() for: " << details.mId;
|
2012-11-01 20:45:50 -04:00
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-01 20:45:50 -04:00
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
details.mIsOwnId = (item->meta.mSubscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_ADMIN);
|
|
|
|
|
|
|
|
details.mPgpLinked = (item->meta.mGroupFlags & RSGXSID_GROUPFLAG_REALID);
|
|
|
|
|
|
|
|
/* rest must be retrived from ServiceString */
|
|
|
|
updateServiceString(item->meta.mServiceString);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RsGxsIdCache::updateServiceString(std::string serviceString)
|
|
|
|
{
|
|
|
|
SSGxsIdGroup ssdata;
|
|
|
|
if (ssdata.load(serviceString))
|
|
|
|
{
|
|
|
|
if (details.mPgpLinked)
|
|
|
|
{
|
|
|
|
details.mPgpKnown = ssdata.pgp.idKnown;
|
|
|
|
if (details.mPgpKnown)
|
|
|
|
{
|
|
|
|
details.mPgpId = ssdata.pgp.pgpId;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
details.mPgpId = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
details.mOpinion = 0;
|
|
|
|
details.mReputation = ssdata.score.score;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
details.mPgpKnown = false;
|
|
|
|
details.mPgpId = "";
|
2012-10-22 16:36:28 -04:00
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
details.mOpinion = 0;
|
|
|
|
details.mReputation = 0;
|
|
|
|
}
|
2012-11-03 07:57:27 -04:00
|
|
|
}
|
2012-10-22 16:36:28 -04:00
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-22 16:36:28 -04:00
|
|
|
bool p3IdService::cache_store(const RsGxsIdGroupItem *item)
|
2012-10-21 15:15:46 -04:00
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::cache_store() Item: " << item->meta.mGroupId;
|
2012-11-01 20:45:50 -04:00
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-01 20:45:50 -04:00
|
|
|
//item->print(std::cerr, 0); NEEDS CONST!!!! TODO
|
2012-11-19 17:00:05 -05:00
|
|
|
//std::cerr << std::endl;
|
2012-11-01 20:45:50 -04:00
|
|
|
|
2012-11-03 07:57:27 -04:00
|
|
|
/* extract key from keys */
|
|
|
|
RsTlvSecurityKeySet keySet;
|
|
|
|
RsTlvSecurityKey pubkey;
|
2012-11-03 10:07:26 -04:00
|
|
|
RsTlvSecurityKey fullkey;
|
|
|
|
bool pub_key_ok = false;
|
|
|
|
bool full_key_ok = false;
|
2012-11-03 07:57:27 -04:00
|
|
|
|
2012-11-03 10:07:26 -04:00
|
|
|
RsGxsId id = item->meta.mGroupId;
|
|
|
|
if (!getGroupKeys(id, keySet))
|
2012-11-03 07:57:27 -04:00
|
|
|
{
|
|
|
|
std::cerr << "p3IdService::cache_store() ERROR getting GroupKeys for: ";
|
|
|
|
std::cerr << item->meta.mGroupId;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<std::string, RsTlvSecurityKey>::iterator kit;
|
|
|
|
|
|
|
|
//std::cerr << "p3IdService::cache_store() KeySet is:";
|
|
|
|
//keySet.print(std::cerr, 10);
|
|
|
|
|
|
|
|
for (kit = keySet.keys.begin(); kit != keySet.keys.end(); kit++)
|
|
|
|
{
|
2013-02-07 18:04:16 -05:00
|
|
|
if (kit->second.keyFlags & RSTLV_KEY_DISTRIB_ADMIN)
|
2012-11-03 07:57:27 -04:00
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-03 10:07:26 -04:00
|
|
|
std::cerr << "p3IdService::cache_store() Found Admin Key";
|
2012-11-03 07:57:27 -04:00
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-03 07:57:27 -04:00
|
|
|
|
2012-11-03 10:07:26 -04:00
|
|
|
/* save full key - if we have it */
|
2013-02-07 18:04:16 -05:00
|
|
|
if (kit->second.keyFlags & RSTLV_KEY_TYPE_FULL)
|
2012-11-03 10:07:26 -04:00
|
|
|
{
|
|
|
|
fullkey = kit->second;
|
|
|
|
full_key_ok = true;
|
|
|
|
}
|
|
|
|
|
2013-02-10 11:50:30 -05:00
|
|
|
/* cache public key always
|
|
|
|
* we don't need to check the keyFlags,
|
|
|
|
* as both FULL and PUBLIC_ONLY keys contain the PUBLIC key
|
|
|
|
*/
|
|
|
|
pubkey = kit->second;
|
|
|
|
pub_key_ok = true;
|
|
|
|
|
2012-11-03 07:57:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-03 10:07:26 -04:00
|
|
|
if (!pub_key_ok)
|
2012-11-03 07:57:27 -04:00
|
|
|
{
|
|
|
|
std::cerr << "p3IdService::cache_store() ERROR No Public Key Found";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-21 15:15:46 -04:00
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
|
|
|
|
// Create Cache Data.
|
2012-11-03 10:07:26 -04:00
|
|
|
RsGxsIdCache pubcache(item, pubkey);
|
|
|
|
mPublicKeyCache.store(id, pubcache);
|
2012-11-29 17:48:28 -05:00
|
|
|
mPublicKeyCache.resize();
|
2012-10-21 15:15:46 -04:00
|
|
|
|
2012-11-03 10:07:26 -04:00
|
|
|
if (full_key_ok)
|
2012-10-21 15:15:46 -04:00
|
|
|
{
|
2012-11-03 10:07:26 -04:00
|
|
|
RsGxsIdCache fullcache(item, fullkey);
|
|
|
|
mPrivateKeyCache.store(id, fullcache);
|
2012-11-29 17:48:28 -05:00
|
|
|
mPrivateKeyCache.resize();
|
2012-10-21 15:15:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/***** BELOW LOADS THE CACHE FROM GXS DATASTORE *****/
|
|
|
|
|
|
|
|
#define MIN_CYCLE_GAP 2
|
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
bool p3IdService::cache_request_load(const RsGxsId &id)
|
2012-10-21 15:15:46 -04:00
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::cache_request_load(" << id << ")";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-10-21 15:15:46 -04:00
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
2012-11-19 17:00:05 -05:00
|
|
|
mCacheLoad_ToCache.push_back(id);
|
2012-10-21 15:15:46 -04:00
|
|
|
}
|
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
if (RsTickEvent::event_count(GXSID_EVENT_CACHELOAD) > 0)
|
2012-10-21 15:15:46 -04:00
|
|
|
{
|
2012-11-19 17:00:05 -05:00
|
|
|
/* its already scheduled */
|
|
|
|
return true;
|
2012-10-21 15:15:46 -04:00
|
|
|
}
|
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
int32_t age = 0;
|
|
|
|
if (RsTickEvent::prev_event_ago(GXSID_EVENT_CACHELOAD, age))
|
2012-10-21 15:15:46 -04:00
|
|
|
{
|
2012-11-19 17:00:05 -05:00
|
|
|
if (age < MIN_CYCLE_GAP)
|
2012-10-21 15:15:46 -04:00
|
|
|
{
|
2012-11-19 17:00:05 -05:00
|
|
|
RsTickEvent::schedule_in(GXSID_EVENT_CACHELOAD, MIN_CYCLE_GAP - age);
|
|
|
|
return true;
|
2012-10-21 15:15:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
RsTickEvent::schedule_now(GXSID_EVENT_CACHELOAD);
|
2012-10-21 15:15:46 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool p3IdService::cache_start_load()
|
|
|
|
{
|
|
|
|
/* trigger request to load missing ids into cache */
|
|
|
|
std::list<RsGxsGroupId> groupIds;
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
|
|
|
|
/* now we process the modGroupList -> a map so we can use it easily later, and create id list too */
|
|
|
|
std::list<RsGxsId>::iterator it;
|
|
|
|
for(it = mCacheLoad_ToCache.begin(); it != mCacheLoad_ToCache.end(); it++)
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::cache_start_load() GroupId: " << *it;
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-10-21 15:15:46 -04:00
|
|
|
groupIds.push_back(*it); // might need conversion?
|
|
|
|
}
|
|
|
|
|
|
|
|
mCacheLoad_ToCache.clear();
|
|
|
|
}
|
|
|
|
|
2012-11-01 20:45:50 -04:00
|
|
|
if (groupIds.size() > 0)
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-01 20:45:50 -04:00
|
|
|
std::cerr << "p3IdService::cache_start_load() #Groups: " << groupIds.size();
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-10-21 15:15:46 -04:00
|
|
|
|
2012-11-01 20:45:50 -04:00
|
|
|
uint32_t ansType = RS_TOKREQ_ANSTYPE_DATA;
|
|
|
|
RsTokReqOptions opts;
|
|
|
|
opts.mReqType = GXS_REQUEST_TYPE_GROUP_DATA;
|
|
|
|
uint32_t token = 0;
|
|
|
|
|
|
|
|
RsGenExchange::getTokenService()->requestGroupInfo(token, ansType, opts, groupIds);
|
2012-11-19 17:00:05 -05:00
|
|
|
GxsTokenQueue::queueRequest(token, GXSIDREQ_CACHELOAD);
|
2012-10-21 15:15:46 -04:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool p3IdService::cache_load_for_token(uint32_t token)
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-10-21 15:15:46 -04:00
|
|
|
std::cerr << "p3IdService::cache_load_for_token() : " << token;
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-10-21 15:15:46 -04:00
|
|
|
|
2012-10-22 16:36:28 -04:00
|
|
|
std::vector<RsGxsGrpItem*> grpData;
|
|
|
|
bool ok = RsGenExchange::getGroupData(token, grpData);
|
|
|
|
|
|
|
|
if(ok)
|
|
|
|
{
|
|
|
|
std::vector<RsGxsGrpItem*>::iterator vit = grpData.begin();
|
2012-10-21 15:15:46 -04:00
|
|
|
|
2012-10-22 16:36:28 -04:00
|
|
|
for(; vit != grpData.end(); vit++)
|
|
|
|
{
|
|
|
|
RsGxsIdGroupItem* item = dynamic_cast<RsGxsIdGroupItem*>(*vit);
|
|
|
|
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-10-22 16:36:28 -04:00
|
|
|
std::cerr << "p3IdService::cache_load_for_token() Loaded Id with Meta: ";
|
|
|
|
std::cerr << item->meta;
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-10-22 16:36:28 -04:00
|
|
|
|
|
|
|
/* cache the data */
|
|
|
|
cache_store(item);
|
|
|
|
delete item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2012-10-21 15:15:46 -04:00
|
|
|
{
|
|
|
|
std::cerr << "p3IdService::cache_load_for_token() ERROR no data";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-03 07:57:27 -04:00
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
bool p3IdService::cache_update_if_cached(const RsGxsId &id, std::string serviceString)
|
2012-11-01 20:45:50 -04:00
|
|
|
{
|
2012-11-19 17:00:05 -05:00
|
|
|
/* if these entries are cached - update with new info */
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::cache_update_if_cached(" << id << ")";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
|
|
|
|
/* retrieve - update, save */
|
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
|
|
|
|
RsGxsIdCache pub_data;
|
|
|
|
if (mPublicKeyCache.fetch(id, pub_data))
|
2012-11-01 20:45:50 -04:00
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::cache_update_if_cached() Updating Public Cache";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
|
|
|
|
pub_data.updateServiceString(serviceString);
|
|
|
|
mPublicKeyCache.store(id, pub_data);
|
2012-11-01 20:45:50 -04:00
|
|
|
}
|
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
|
|
|
|
RsGxsIdCache priv_data;
|
|
|
|
if (mPrivateKeyCache.fetch(id, priv_data))
|
2012-11-01 20:45:50 -04:00
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::cache_update_if_cached() Updating Private Cache";
|
2012-11-03 07:57:27 -04:00
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
|
|
|
|
priv_data.updateServiceString(serviceString);
|
|
|
|
mPrivateKeyCache.store(id, priv_data);
|
2012-11-01 20:45:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
/************************************************************************************/
|
|
|
|
/************************************************************************************/
|
|
|
|
|
|
|
|
bool p3IdService::cache_request_ownids()
|
2012-11-01 20:45:50 -04:00
|
|
|
{
|
2012-11-19 17:00:05 -05:00
|
|
|
/* trigger request to load missing ids into cache */
|
|
|
|
std::list<RsGxsGroupId> groupIds;
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::cache_request_ownids()";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-03 07:57:27 -04:00
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
uint32_t ansType = RS_TOKREQ_ANSTYPE_DATA;
|
|
|
|
RsTokReqOptions opts;
|
|
|
|
opts.mReqType = GXS_REQUEST_TYPE_GROUP_DATA;
|
|
|
|
//opts.mSubscribeFlags = GXS_SERV::GROUP_SUBSCRIBE_ADMIN;
|
|
|
|
|
|
|
|
uint32_t token = 0;
|
|
|
|
|
|
|
|
RsGenExchange::getTokenService()->requestGroupInfo(token, ansType, opts);
|
|
|
|
GxsTokenQueue::queueRequest(token, GXSIDREQ_CACHEOWNIDS);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool p3IdService::cache_load_ownids(uint32_t token)
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::cache_load_ownids() : " << token;
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
|
|
|
|
std::vector<RsGxsGrpItem*> grpData;
|
|
|
|
bool ok = RsGenExchange::getGroupData(token, grpData);
|
|
|
|
|
|
|
|
if(ok)
|
|
|
|
{
|
|
|
|
std::vector<RsGxsGrpItem*>::iterator vit = grpData.begin();
|
|
|
|
|
|
|
|
// Save List
|
2012-11-01 20:45:50 -04:00
|
|
|
{
|
2012-11-19 17:00:05 -05:00
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
|
|
|
|
mOwnIds.clear();
|
|
|
|
for(vit = grpData.begin(); vit != grpData.end(); vit++)
|
|
|
|
{
|
|
|
|
RsGxsIdGroupItem* item = dynamic_cast<RsGxsIdGroupItem*>(*vit);
|
|
|
|
|
|
|
|
|
|
|
|
if (item->meta.mSubscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_ADMIN)
|
|
|
|
{
|
|
|
|
mOwnIds.push_back(item->meta.mGroupId);
|
|
|
|
}
|
|
|
|
}
|
2012-11-01 20:45:50 -04:00
|
|
|
}
|
2012-11-19 17:00:05 -05:00
|
|
|
|
2012-12-15 18:07:12 -05:00
|
|
|
// No need to cache these items...
|
|
|
|
// as it just causes the cache to be flushed.
|
|
|
|
#if 0
|
2012-11-19 17:00:05 -05:00
|
|
|
// Cache Items too.
|
|
|
|
for(vit = grpData.begin(); vit != grpData.end(); vit++)
|
|
|
|
{
|
|
|
|
RsGxsIdGroupItem* item = dynamic_cast<RsGxsIdGroupItem*>(*vit);
|
|
|
|
if (item->meta.mSubscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_ADMIN)
|
|
|
|
{
|
|
|
|
|
|
|
|
std::cerr << "p3IdService::cache_load_ownids() Loaded Id with Meta: ";
|
|
|
|
std::cerr << item->meta;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
/* cache the data */
|
|
|
|
cache_store(item);
|
|
|
|
}
|
|
|
|
delete item;
|
2012-12-15 18:07:12 -05:00
|
|
|
}
|
|
|
|
#endif
|
2012-11-19 17:00:05 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "p3IdService::cache_load_ownids() ERROR no data";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
return false;
|
2012-11-01 20:45:50 -04:00
|
|
|
}
|
2012-11-19 17:00:05 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/************************************************************************************/
|
|
|
|
/************************************************************************************/
|
2012-11-01 20:45:50 -04:00
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
bool p3IdService::cachetest_getlist()
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-03 07:57:27 -04:00
|
|
|
std::cerr << "p3IdService::cachetest_getlist() making request";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-03 07:57:27 -04:00
|
|
|
|
2012-11-01 20:45:50 -04:00
|
|
|
uint32_t ansType = RS_TOKREQ_ANSTYPE_LIST;
|
|
|
|
RsTokReqOptions opts;
|
2012-11-03 07:57:27 -04:00
|
|
|
opts.mReqType = GXS_REQUEST_TYPE_GROUP_IDS;
|
2012-11-01 20:45:50 -04:00
|
|
|
uint32_t token = 0;
|
|
|
|
|
|
|
|
RsGenExchange::getTokenService()->requestGroupInfo(token, ansType, opts);
|
2012-11-19 17:00:05 -05:00
|
|
|
GxsTokenQueue::queueRequest(token, GXSIDREQ_CACHETEST);
|
|
|
|
|
|
|
|
// Schedule Next Event.
|
|
|
|
RsTickEvent::schedule_in(GXSID_EVENT_CACHETEST, CACHETEST_PERIOD);
|
2012-11-01 20:45:50 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
bool p3IdService::cachetest_handlerequest(uint32_t token)
|
2012-11-01 20:45:50 -04:00
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::cachetest_handlerequest() token: " << token;
|
2012-11-03 07:57:27 -04:00
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-03 07:57:27 -04:00
|
|
|
|
2013-02-27 16:16:03 -05:00
|
|
|
std::list<RsGxsId> grpIds;
|
|
|
|
bool ok = RsGenExchange::getGroupList(token, grpIds);
|
2012-11-01 20:45:50 -04:00
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
if(ok)
|
|
|
|
{
|
|
|
|
std::list<RsGxsId>::iterator vit = grpIds.begin();
|
|
|
|
for(; vit != grpIds.end(); vit++)
|
|
|
|
{
|
|
|
|
/* 5% chance of checking it! */
|
|
|
|
if (RSRandom::random_f32() < 0.25)
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::cachetest_request() Testing Id: " << *vit;
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-03 07:57:27 -04:00
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
/* try the cache! */
|
|
|
|
if (!haveKey(*vit))
|
2012-11-01 20:45:50 -04:00
|
|
|
{
|
2012-11-19 17:00:05 -05:00
|
|
|
std::list<PeerId> nullpeers;
|
|
|
|
requestKey(*vit, nullpeers);
|
2012-11-03 07:57:27 -04:00
|
|
|
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::cachetest_request() Requested Key Id: " << *vit;
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RsTlvSecurityKey seckey;
|
|
|
|
if (getKey(*vit, seckey))
|
2012-11-01 20:45:50 -04:00
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::cachetest_request() Got Key OK Id: " << *vit;
|
2012-11-03 07:57:27 -04:00
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-03 07:57:27 -04:00
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
// success!
|
2012-11-03 07:57:27 -04:00
|
|
|
seckey.print(std::cerr, 10);
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << std::endl;
|
2012-11-03 07:57:27 -04:00
|
|
|
|
2012-11-01 20:45:50 -04:00
|
|
|
|
|
|
|
}
|
2012-11-19 17:00:05 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "p3IdService::cachetest_request() ERROR no Key for Id: " << *vit;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
}
|
2012-11-01 20:45:50 -04:00
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
/* try private key too! */
|
|
|
|
if (!havePrivateKey(*vit))
|
|
|
|
{
|
|
|
|
requestPrivateKey(*vit);
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::cachetest_request() Requested PrivateKey Id: " << *vit;
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RsTlvSecurityKey seckey;
|
|
|
|
if (getPrivateKey(*vit, seckey))
|
2012-11-01 20:45:50 -04:00
|
|
|
{
|
2012-11-19 17:00:05 -05:00
|
|
|
// success!
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::cachetest_request() Got PrivateKey OK Id: " << *vit;
|
2012-11-03 07:57:27 -04:00
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-01 20:45:50 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::cachetest_request() ERROR no PrivateKey for Id: " << *vit;
|
|
|
|
std::cerr << std::endl;
|
2012-11-01 20:45:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-19 17:00:05 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "p3IdService::cache_load_for_token() ERROR no data";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
return false;
|
2012-11-01 20:45:50 -04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-06 14:18:11 -05:00
|
|
|
/************************************************************************************/
|
|
|
|
/************************************************************************************/
|
|
|
|
/************************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We have two background tasks that use the ServiceString: PGPHash & Reputation.
|
|
|
|
*
|
|
|
|
* Only one task can be run at a time - otherwise potential overwrite issues.
|
|
|
|
* So this part coordinates that part of the code.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
bool p3IdService::CacheArbitration(uint32_t mode)
|
2012-11-06 14:18:11 -05:00
|
|
|
{
|
2012-11-19 17:00:05 -05:00
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
2012-11-06 14:18:11 -05:00
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
if (!mBgSchedule_Active)
|
2012-11-07 16:38:43 -05:00
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::CacheArbitration() Okay";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
|
|
|
|
mBgSchedule_Active = true;
|
|
|
|
mBgSchedule_Mode = mode;
|
|
|
|
return true;
|
2012-11-07 16:38:43 -05:00
|
|
|
}
|
2012-11-06 14:18:11 -05:00
|
|
|
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::CacheArbitration() Is busy...";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-06 14:18:11 -05:00
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void p3IdService::CacheArbitrationDone(uint32_t mode)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
|
|
|
|
if (mBgSchedule_Mode != mode)
|
2012-11-06 14:18:11 -05:00
|
|
|
{
|
2012-11-19 17:00:05 -05:00
|
|
|
/* issues */
|
|
|
|
std::cerr << "p3IdService::CacheArbitrationDone() ERROR Wrong Current Mode";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
return;
|
2012-11-06 14:18:11 -05:00
|
|
|
}
|
|
|
|
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::CacheArbitrationDone()";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-06 14:18:11 -05:00
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
mBgSchedule_Active = false;
|
|
|
|
}
|
2012-11-06 14:18:11 -05:00
|
|
|
|
2012-11-05 17:28:08 -05:00
|
|
|
/************************************************************************************/
|
|
|
|
/************************************************************************************/
|
|
|
|
/************************************************************************************/
|
|
|
|
|
|
|
|
/* Task to determine GPGHash matches
|
|
|
|
*
|
|
|
|
* Info to be stored in GroupServiceString + Cache.
|
|
|
|
*
|
|
|
|
* Actually - it must be a Signature here - otherwise, you could
|
|
|
|
* put in a hash from someone else!
|
|
|
|
*
|
|
|
|
* Don't think that we need to match very often - maybe once a day?
|
|
|
|
* Actually - we should scale the matching based on number of keys we have.
|
|
|
|
*
|
|
|
|
* imagine - 10^6 rsa keys + 10^3 gpg keys => 10^9 combinations.
|
|
|
|
* -- far too many to check all quickly.
|
|
|
|
*
|
|
|
|
* Need to grab and cache data we need... then check over slowly.
|
|
|
|
*
|
|
|
|
* maybe grab a list of all gpgids - that we know of: store id list.
|
|
|
|
* then big GroupRequest, and iterate through these.
|
|
|
|
**/
|
|
|
|
|
|
|
|
//const int SHA_DIGEST_LENGTH = 20;
|
|
|
|
|
|
|
|
typedef t_RsGenericIdType<SHA_DIGEST_LENGTH> GxsIdPgpHash;
|
|
|
|
|
|
|
|
static void calcPGPHash(const RsGxsId &id, const PGPFingerprintType &pgp, GxsIdPgpHash &hash);
|
|
|
|
|
|
|
|
|
2012-11-30 19:16:24 -05:00
|
|
|
// Must Use meta.
|
2012-11-05 17:28:08 -05:00
|
|
|
void p3IdService::service_CreateGroup(RsGxsGrpItem* grpItem, RsTlvSecurityKeySet& keySet)
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2013-02-10 10:40:07 -05:00
|
|
|
std::cerr << "p3IdService::service_CreateGroup()";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2013-02-10 10:40:07 -05:00
|
|
|
|
2012-11-05 17:28:08 -05:00
|
|
|
RsGxsIdGroupItem *item = dynamic_cast<RsGxsIdGroupItem *>(grpItem);
|
|
|
|
if (!item)
|
|
|
|
{
|
|
|
|
std::cerr << "p3IdService::service_CreateGroup() ERROR invalid cast";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
2012-11-30 19:16:24 -05:00
|
|
|
|
2012-11-07 16:38:43 -05:00
|
|
|
/********************* TEMP HACK UNTIL GXS FILLS IN GROUP_ID *****************/
|
|
|
|
// find private admin key
|
|
|
|
std::map<std::string, RsTlvSecurityKey>::iterator mit = keySet.keys.begin();
|
|
|
|
for(; mit != keySet.keys.end(); mit++)
|
|
|
|
{
|
|
|
|
RsTlvSecurityKey& pk = mit->second;
|
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
if(pk.keyFlags == (RSTLV_KEY_DISTRIB_ADMIN | RSTLV_KEY_TYPE_FULL))
|
2012-11-07 16:38:43 -05:00
|
|
|
{
|
|
|
|
item->group.mMeta.mGroupId = pk.keyId;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(mit == keySet.keys.end())
|
|
|
|
{
|
|
|
|
std::cerr << "p3IdService::service_CreateGroup() ERROR no admin key";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/********************* TEMP HACK UNTIL GXS FILLS IN GROUP_ID *****************/
|
2012-11-05 17:28:08 -05:00
|
|
|
|
2012-11-30 19:16:24 -05:00
|
|
|
// SANITY CHECK.
|
|
|
|
if (item->group.mMeta.mAuthorId != item->meta.mAuthorId)
|
|
|
|
{
|
|
|
|
std::cerr << "p3IdService::service_CreateGroup() AuthorId mismatch(";
|
|
|
|
std::cerr << item->group.mMeta.mAuthorId;
|
|
|
|
std::cerr << " vs ";
|
|
|
|
std::cerr << item->meta.mAuthorId;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item->group.mMeta.mGroupId != item->meta.mGroupId)
|
|
|
|
{
|
|
|
|
std::cerr << "p3IdService::service_CreateGroup() GroupId mismatch(";
|
|
|
|
std::cerr << item->group.mMeta.mGroupId;
|
|
|
|
std::cerr << " vs ";
|
|
|
|
std::cerr << item->meta.mGroupId;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (item->group.mMeta.mGroupFlags != item->meta.mGroupFlags)
|
|
|
|
{
|
|
|
|
std::cerr << "p3IdService::service_CreateGroup() GroupFlags mismatch(";
|
|
|
|
std::cerr << item->group.mMeta.mGroupFlags;
|
|
|
|
std::cerr << " vs ";
|
|
|
|
std::cerr << item->meta.mGroupFlags;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
std::cerr << "p3IdService::service_CreateGroup() for : " << item->group.mMeta.mGroupId;
|
|
|
|
std::cerr << std::endl;
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::service_CreateGroup() Alt GroupId : " << item->meta.mGroupId;
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
|
2012-11-30 19:16:24 -05:00
|
|
|
if (item->group.mMeta.mGroupFlags & RSGXSID_GROUPFLAG_REALID)
|
2012-11-05 17:28:08 -05:00
|
|
|
{
|
|
|
|
/* create the hash */
|
|
|
|
GxsIdPgpHash hash;
|
|
|
|
|
|
|
|
/* */
|
|
|
|
PGPFingerprintType ownFinger;
|
|
|
|
PGPIdType ownId(AuthGPG::getAuthGPG()->getGPGOwnId());
|
2013-02-10 10:40:07 -05:00
|
|
|
|
|
|
|
#ifdef GXSID_GEN_DUMMY_DATA
|
|
|
|
if (item->group.mMeta.mAuthorId != "")
|
|
|
|
{
|
|
|
|
ownId = PGPIdType(item->group.mMeta.mAuthorId);
|
|
|
|
}
|
2012-11-30 19:16:24 -05:00
|
|
|
#endif
|
2012-11-05 17:28:08 -05:00
|
|
|
|
|
|
|
if (!AuthGPG::getAuthGPG()->getKeyFingerprint(ownId,ownFinger))
|
|
|
|
{
|
|
|
|
std::cerr << "p3IdService::service_CreateGroup() ERROR Own Finger is stuck";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
return; // abandon attempt!
|
|
|
|
}
|
|
|
|
|
|
|
|
calcPGPHash(item->group.mMeta.mGroupId, ownFinger, hash);
|
|
|
|
item->group.mPgpIdHash = hash.toStdString();
|
|
|
|
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2013-02-10 10:40:07 -05:00
|
|
|
std::cerr << "p3IdService::service_CreateGroup() Calculated PgpIdHash : " << item->group.mPgpIdHash;
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2013-02-10 10:40:07 -05:00
|
|
|
|
2012-11-05 17:28:08 -05:00
|
|
|
/* do signature */
|
|
|
|
|
2012-11-06 14:18:11 -05:00
|
|
|
#if ENABLE_PGP_SIGNATURES
|
2012-11-05 17:28:08 -05:00
|
|
|
#define MAX_SIGN_SIZE 2048
|
|
|
|
uint8_t signarray[MAX_SIGN_SIZE];
|
|
|
|
unsigned int sign_size = MAX_SIGN_SIZE;
|
|
|
|
if (!AuthGPG::getAuthGPG()->SignDataBin((void *) hash.toByteArray(), hash.SIZE_IN_BYTES, signarray, &sign_size))
|
|
|
|
{
|
|
|
|
/* error */
|
|
|
|
std::cerr << "p3IdService::service_CreateGroup() ERROR Signing stuff";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* push binary into string -> really bad! */
|
|
|
|
item->group.mPgpIdSign = "";
|
|
|
|
for(unsigned int i = 0; i < sign_size; i++)
|
|
|
|
{
|
|
|
|
item->group.mPgpIdSign += signarray[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* done! */
|
2012-11-06 14:18:11 -05:00
|
|
|
#else
|
|
|
|
item->group.mPgpIdSign = "";
|
|
|
|
#endif
|
|
|
|
|
2012-11-05 17:28:08 -05:00
|
|
|
}
|
2012-11-19 17:00:05 -05:00
|
|
|
|
2012-11-30 19:16:24 -05:00
|
|
|
// Enforce no AuthorId.
|
|
|
|
item->meta.mAuthorId = "";
|
|
|
|
item->group.mMeta.mAuthorId = "";
|
|
|
|
// copy meta data to be sure its all the same.
|
|
|
|
//item->group.mMeta = item->meta;
|
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
// Reload in a little bit.
|
|
|
|
// HACK to get it to work.
|
|
|
|
RsTickEvent::schedule_in(GXSID_EVENT_CACHEOWNIDS, OWNID_RELOAD_DELAY);
|
2012-11-05 17:28:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-06 14:18:11 -05:00
|
|
|
#define HASHPGP_PERIOD 180
|
2012-11-05 17:28:08 -05:00
|
|
|
|
|
|
|
|
2012-11-06 14:18:11 -05:00
|
|
|
bool p3IdService::pgphash_start()
|
2012-11-05 17:28:08 -05:00
|
|
|
{
|
2012-11-19 17:00:05 -05:00
|
|
|
if (!CacheArbitration(BG_PGPHASH))
|
2012-11-05 17:28:08 -05:00
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::pgphash_start() Other Events running... Rescheduling";
|
2012-11-05 17:28:08 -05:00
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-07 16:38:43 -05:00
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
/* reschedule in a bit */
|
|
|
|
RsTickEvent::schedule_in(GXSID_EVENT_PGPHASH, PGPHASH_RETRY_PERIOD);
|
|
|
|
return false;
|
2012-11-06 14:18:11 -05:00
|
|
|
}
|
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
// SCHEDULE NEXT ONE.
|
|
|
|
RsTickEvent::schedule_in(GXSID_EVENT_PGPHASH, PGPHASH_PERIOD);
|
2012-11-06 14:18:11 -05:00
|
|
|
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::pgphash_start() making request";
|
2012-11-05 17:28:08 -05:00
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
|
2012-11-07 16:38:43 -05:00
|
|
|
getPgpIdList();
|
|
|
|
|
|
|
|
// ACTUALLY only need summary - but have written code for data.
|
|
|
|
// Also need to use opts.groupFlags to filter stuff properly to REALID's only.
|
|
|
|
// TODO
|
|
|
|
|
|
|
|
//uint32_t ansType = RS_TOKREQ_ANSTYPE_SUMMARY;
|
|
|
|
uint32_t ansType = RS_TOKREQ_ANSTYPE_DATA;
|
2012-11-05 17:28:08 -05:00
|
|
|
RsTokReqOptions opts;
|
2012-11-07 16:38:43 -05:00
|
|
|
//opts.mReqType = GXS_REQUEST_TYPE_GROUP_META;
|
|
|
|
opts.mReqType = GXS_REQUEST_TYPE_GROUP_DATA;
|
2012-11-05 17:28:08 -05:00
|
|
|
uint32_t token = 0;
|
|
|
|
|
|
|
|
RsGenExchange::getTokenService()->requestGroupInfo(token, ansType, opts);
|
2012-11-19 17:00:05 -05:00
|
|
|
GxsTokenQueue::queueRequest(token, GXSIDREQ_PGPHASH);
|
2012-11-05 17:28:08 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
bool p3IdService::pgphash_handlerequest(uint32_t token)
|
2012-11-05 17:28:08 -05:00
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::pgphash_handlerequest(" << token << ")";
|
2012-11-05 17:28:08 -05:00
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
// We need full data - for access to Hash & Signature.
|
|
|
|
// Perhaps we will change this to an initial pass through Meta,
|
|
|
|
// and use this to discard lots of things.
|
2012-11-05 17:28:08 -05:00
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
// Even better - we can set flags in the Meta Data, (IdType),
|
|
|
|
// And use GXS to filter out all the AnonIds, and only have to process
|
|
|
|
// Proper Ids.
|
2012-11-05 17:28:08 -05:00
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
// We Will do this later!
|
2012-11-05 17:28:08 -05:00
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
std::vector<RsGxsIdGroup> groups;
|
|
|
|
std::vector<RsGxsIdGroup> groupsToProcess;
|
|
|
|
bool ok = getGroupData(token, groups);
|
2012-11-05 17:28:08 -05:00
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
if(ok)
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::pgphash_request() Have " << groups.size() << " Groups";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
std::vector<RsGxsIdGroup>::iterator vit;
|
|
|
|
for(vit = groups.begin(); vit != groups.end(); vit++)
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::pgphash_request() Group Id: " << vit->mMeta.mGroupId;
|
2012-11-07 16:38:43 -05:00
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-07 16:38:43 -05:00
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
/* Filter based on IdType */
|
|
|
|
if (!(vit->mMeta.mGroupFlags & RSGXSID_GROUPFLAG_REALID))
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::pgphash_request() discarding AnonID";
|
2012-11-07 16:38:43 -05:00
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
continue;
|
|
|
|
}
|
2012-11-07 16:38:43 -05:00
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
/* now we need to decode the Service String - see what is saved there */
|
|
|
|
SSGxsIdGroup ssdata;
|
|
|
|
if (ssdata.load(vit->mMeta.mServiceString))
|
|
|
|
{
|
|
|
|
if (ssdata.pgp.idKnown)
|
2012-11-05 17:28:08 -05:00
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::pgphash_request() discarding Already Known";
|
2012-11-05 17:28:08 -05:00
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
/* Have a linear attempt policy -
|
|
|
|
* if zero checks - try now.
|
|
|
|
* if 1 check, at least a day.
|
|
|
|
* if 2 checks: 2days, etc.
|
|
|
|
*/
|
2012-11-05 17:28:08 -05:00
|
|
|
|
|
|
|
#define SECS_PER_DAY (3600 * 24)
|
2012-11-19 17:00:05 -05:00
|
|
|
time_t age = time(NULL) - ssdata.pgp.lastCheckTs;
|
|
|
|
time_t wait_period = ssdata.pgp.checkAttempts * SECS_PER_DAY;
|
|
|
|
if (wait_period > 30 * SECS_PER_DAY)
|
|
|
|
{
|
|
|
|
wait_period = 30 * SECS_PER_DAY;
|
2012-11-05 17:28:08 -05:00
|
|
|
}
|
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
if (age < wait_period)
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::pgphash_request() discarding Recent Check";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
continue;
|
|
|
|
}
|
2012-11-05 17:28:08 -05:00
|
|
|
}
|
2012-11-19 17:00:05 -05:00
|
|
|
|
|
|
|
/* if we get here -> then its to be processed */
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::pgphash_request() ToProcess Group: " << vit->mMeta.mGroupId;
|
2012-11-07 16:38:43 -05:00
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-07 16:38:43 -05:00
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
mGroupsToProcess.push_back(*vit);
|
|
|
|
}
|
2012-11-05 17:28:08 -05:00
|
|
|
}
|
2012-11-19 17:00:05 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "p3IdService::pgphash_request() getGroupData ERROR";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Schedule Processing.
|
|
|
|
RsTickEvent::schedule_in(GXSID_EVENT_PGPHASH_PROC, PGPHASH_PROC_PERIOD);
|
2012-11-05 17:28:08 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool p3IdService::pgphash_process()
|
|
|
|
{
|
|
|
|
/* each time this is called - process one Id from mGroupsToProcess */
|
2012-11-07 16:38:43 -05:00
|
|
|
RsGxsIdGroup pg;
|
2012-11-19 17:00:05 -05:00
|
|
|
bool isDone = false;
|
2012-11-05 17:28:08 -05:00
|
|
|
{
|
2012-11-07 16:38:43 -05:00
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
2012-11-19 17:00:05 -05:00
|
|
|
if (!mGroupsToProcess.empty())
|
2012-11-07 16:38:43 -05:00
|
|
|
{
|
2012-11-19 17:00:05 -05:00
|
|
|
pg = mGroupsToProcess.front();
|
|
|
|
mGroupsToProcess.pop_front();
|
|
|
|
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::pgphash_process() Popped Group: " << pg.mMeta.mGroupId;
|
2012-11-07 16:38:43 -05:00
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-07 16:38:43 -05:00
|
|
|
}
|
2012-11-19 17:00:05 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
isDone = true;
|
|
|
|
}
|
|
|
|
}
|
2012-11-07 16:38:43 -05:00
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
if (isDone)
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::pgphash_process() List Empty... Done";
|
2012-11-07 16:38:43 -05:00
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
// FINISHED.
|
|
|
|
CacheArbitrationDone(BG_PGPHASH);
|
|
|
|
return true;
|
2012-11-07 16:38:43 -05:00
|
|
|
}
|
2012-11-19 17:00:05 -05:00
|
|
|
|
2012-11-05 17:28:08 -05:00
|
|
|
|
|
|
|
SSGxsIdGroup ssdata;
|
|
|
|
ssdata.load(pg.mMeta.mServiceString); // attempt load - okay if fails.
|
|
|
|
|
|
|
|
PGPIdType pgpId;
|
|
|
|
|
|
|
|
if (checkId(pg, pgpId))
|
|
|
|
{
|
|
|
|
/* found a match - update everything */
|
|
|
|
/* Consistency issues here - what if Reputation was recently updated? */
|
|
|
|
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-07 16:38:43 -05:00
|
|
|
std::cerr << "p3IdService::pgphash_process() CheckId Success for Group: " << pg.mMeta.mGroupId;
|
|
|
|
std::cerr << " PgpId: " << pgpId.toStdString();
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
|
|
|
|
/* update */
|
|
|
|
ssdata.pgp.idKnown = true;
|
|
|
|
ssdata.pgp.pgpId = pgpId.toStdString();
|
|
|
|
|
|
|
|
// SHOULD BE PUSHED TO CACHE!
|
|
|
|
#if 0
|
|
|
|
id.mGpgIdKnown = true;
|
|
|
|
|
|
|
|
id.mGpgId = *it;
|
|
|
|
id.mGpgName = details.name;
|
|
|
|
id.mGpgEmail = details.email;
|
|
|
|
|
|
|
|
if (*it == ownId)
|
|
|
|
{
|
|
|
|
id.mIdType |= RSID_RELATION_YOURSELF;
|
|
|
|
}
|
|
|
|
else if (rsPeers->isGPGAccepted(*it))
|
|
|
|
{
|
|
|
|
id.mIdType |= RSID_RELATION_FRIEND;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
id.mIdType |= RSID_RELATION_OTHER;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-07 16:38:43 -05:00
|
|
|
std::cerr << "p3IdService::pgphash_process() No Match for Group: " << pg.mMeta.mGroupId;
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-07 16:38:43 -05:00
|
|
|
|
2012-11-05 17:28:08 -05:00
|
|
|
ssdata.pgp.lastCheckTs = time(NULL);
|
|
|
|
ssdata.pgp.checkAttempts++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set new Group ServiceString */
|
|
|
|
uint32_t dummyToken = 0;
|
|
|
|
std::string serviceString = ssdata.save();
|
|
|
|
setGroupServiceString(dummyToken, pg.mMeta.mGroupId, serviceString);
|
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
cache_update_if_cached(pg.mMeta.mGroupId, serviceString);
|
|
|
|
|
|
|
|
// Schedule Next Processing.
|
|
|
|
RsTickEvent::schedule_in(GXSID_EVENT_PGPHASH_PROC, PGPHASH_PROC_PERIOD);
|
2012-11-07 16:38:43 -05:00
|
|
|
return false; // as there are more items on the queue to process.
|
2012-11-05 17:28:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool p3IdService::checkId(const RsGxsIdGroup &grp, PGPIdType &pgpId)
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
std::cerr << "p3IdService::checkId() Starting Match Check for RsGxsId: ";
|
|
|
|
std::cerr << grp.mMeta.mGroupId;
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
|
2013-02-10 10:40:07 -05:00
|
|
|
/* some sanity checking... make sure hash is the right size */
|
|
|
|
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2013-02-10 10:40:07 -05:00
|
|
|
std::cerr << "p3IdService::checkId() PgpIdHash is: " << grp.mPgpIdHash;
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
|
|
|
|
2013-02-10 10:40:07 -05:00
|
|
|
|
|
|
|
if (grp.mPgpIdHash.length() != SHA_DIGEST_LENGTH * 2)
|
|
|
|
{
|
|
|
|
std::cerr << "ERROR PgpIdHash len:" << grp.mPgpIdHash.length();
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
|
2012-11-05 17:28:08 -05:00
|
|
|
/* iterate through and check hash */
|
|
|
|
GxsIdPgpHash ans(grp.mPgpIdHash);
|
|
|
|
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-07 16:38:43 -05:00
|
|
|
std::cerr << "\tExpected Answer: " << ans.toStdString();
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-07 16:38:43 -05:00
|
|
|
|
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
|
2012-11-05 17:28:08 -05:00
|
|
|
std::map<PGPIdType, PGPFingerprintType>::iterator mit;
|
|
|
|
for(mit = mPgpFingerprintMap.begin(); mit != mPgpFingerprintMap.end(); mit++)
|
|
|
|
{
|
|
|
|
GxsIdPgpHash hash;
|
|
|
|
calcPGPHash(grp.mMeta.mGroupId, mit->second, hash);
|
|
|
|
if (ans == hash)
|
|
|
|
{
|
|
|
|
std::cerr << "p3IdService::checkId() HASH MATCH!";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
2012-11-06 14:18:11 -05:00
|
|
|
#if ENABLE_PGP_SIGNATURES
|
2012-11-05 17:28:08 -05:00
|
|
|
/* miracle match! */
|
|
|
|
/* check signature too */
|
|
|
|
if (AuthGPG::getAuthGPG()->VerifySignBin((void *) hash.toByteArray(), hash.SIZE_IN_BYTES,
|
|
|
|
(unsigned char *) grp.mPgpIdSign.c_str(), grp.mPgpIdSign.length(),
|
|
|
|
mit->second.toStdString()))
|
|
|
|
{
|
|
|
|
std::cerr << "p3IdService::checkId() Signature Okay too!";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
pgpId = mit->first;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* error */
|
|
|
|
std::cerr << "p3IdService::checkId() ERROR Signature Failed";
|
|
|
|
std::cerr << std::endl;
|
2012-11-06 14:18:11 -05:00
|
|
|
#else
|
2012-11-07 16:38:43 -05:00
|
|
|
pgpId = mit->first;
|
2013-02-27 16:16:03 -05:00
|
|
|
|
|
|
|
#ifdef DEBUG_IDS
|
2012-11-06 14:18:11 -05:00
|
|
|
std::cerr << "p3IdService::checkId() Skipping Signature check for now... Hash Okay";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
|
|
|
|
2012-11-06 14:18:11 -05:00
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
|
2012-11-05 17:28:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
std::cerr << "p3IdService::checkId() Checked " << mPgpFingerprintMap.size() << " Hashes without Match";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* worker functions */
|
|
|
|
void p3IdService::getPgpIdList()
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
std::cerr << "p3IdService::getPgpIdList() Starting....";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
|
|
|
|
std::list<std::string> list;
|
|
|
|
AuthGPG::getAuthGPG()->getGPGFilteredList(list);
|
|
|
|
|
2012-11-07 16:38:43 -05:00
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
|
|
|
|
mPgpFingerprintMap.clear();
|
|
|
|
|
2012-11-05 17:28:08 -05:00
|
|
|
std::list<std::string>::iterator it;
|
|
|
|
for(it = list.begin(); it != list.end(); it++)
|
|
|
|
{
|
|
|
|
PGPIdType pgpId(*it);
|
|
|
|
PGPFingerprintType fp;
|
|
|
|
AuthGPG::getAuthGPG()->getKeyFingerprint(pgpId, fp);
|
|
|
|
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-07 16:38:43 -05:00
|
|
|
std::cerr << "p3IdService::getPgpIdList() Id: " << pgpId.toStdString() << " => " << fp.toStdString();
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-07 16:38:43 -05:00
|
|
|
|
2012-11-05 17:28:08 -05:00
|
|
|
mPgpFingerprintMap[pgpId] = fp;
|
|
|
|
}
|
|
|
|
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
std::cerr << "p3IdService::getPgpIdList() Items: " << mPgpFingerprintMap.size();
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void calcPGPHash(const RsGxsId &id, const PGPFingerprintType &pgp, GxsIdPgpHash &hash)
|
|
|
|
{
|
|
|
|
unsigned char signature[SHA_DIGEST_LENGTH];
|
|
|
|
/* hash id + pubkey => pgphash */
|
|
|
|
SHA_CTX *sha_ctx = new SHA_CTX;
|
|
|
|
SHA1_Init(sha_ctx);
|
|
|
|
|
|
|
|
SHA1_Update(sha_ctx, id.c_str(), id.length()); // TO FIX ONE DAY.
|
|
|
|
SHA1_Update(sha_ctx, pgp.toByteArray(), pgp.SIZE_IN_BYTES);
|
|
|
|
SHA1_Final(signature, sha_ctx);
|
|
|
|
hash = GxsIdPgpHash(signature);
|
|
|
|
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-07 16:38:43 -05:00
|
|
|
std::cerr << "calcPGPHash():";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "\tRsGxsId: " << id;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "\tFingerprint: " << pgp.toStdString();
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "\tFinal Hash: " << hash.toStdString();
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-07 16:38:43 -05:00
|
|
|
|
2012-11-05 17:28:08 -05:00
|
|
|
delete sha_ctx;
|
|
|
|
}
|
|
|
|
|
2012-10-21 15:15:46 -04:00
|
|
|
|
2012-06-13 20:27:28 -04:00
|
|
|
/************************************************************************************/
|
|
|
|
/************************************************************************************/
|
|
|
|
/************************************************************************************/
|
|
|
|
/************************************************************************************/
|
|
|
|
/************************************************************************************/
|
|
|
|
|
2012-11-30 19:16:24 -05:00
|
|
|
|
|
|
|
std::string p3IdService::genRandomId(int len)
|
2012-02-13 13:22:35 -05:00
|
|
|
{
|
2012-10-17 20:28:54 -04:00
|
|
|
std::string randomId;
|
2012-11-30 19:16:24 -05:00
|
|
|
for(int i = 0; i < len; i++)
|
2012-02-13 13:22:35 -05:00
|
|
|
{
|
2012-11-30 19:16:24 -05:00
|
|
|
int val = RSRandom::random_u32() % 16;
|
|
|
|
if (val < 10)
|
|
|
|
{
|
|
|
|
randomId += (char) ('0' + val);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
randomId += (char) ('a' + (val - 10));
|
|
|
|
}
|
2012-02-13 13:22:35 -05:00
|
|
|
}
|
|
|
|
|
2012-10-17 20:28:54 -04:00
|
|
|
return randomId;
|
2012-02-13 13:22:35 -05:00
|
|
|
}
|
|
|
|
|
2012-12-10 14:21:53 -05:00
|
|
|
#define MAX_KNOWN_PGPIDS 20
|
|
|
|
#define MAX_UNKNOWN_PGPIDS 20
|
|
|
|
#define MAX_PSEUDOIDS 20
|
2012-11-30 19:16:24 -05:00
|
|
|
|
|
|
|
#define DUMMY_GXSID_DELAY 5
|
|
|
|
|
2012-02-13 13:22:35 -05:00
|
|
|
void p3IdService::generateDummyData()
|
|
|
|
{
|
|
|
|
|
2012-11-30 19:16:24 -05:00
|
|
|
generateDummy_OwnIds();
|
2012-02-13 13:22:35 -05:00
|
|
|
|
2012-11-30 19:16:24 -05:00
|
|
|
time_t age = 0;
|
|
|
|
for(int i = 0; i < MAX_KNOWN_PGPIDS; i++)
|
|
|
|
{
|
|
|
|
age += DUMMY_GXSID_DELAY;
|
|
|
|
RsTickEvent::schedule_in(GXSID_EVENT_DUMMY_PGPID, age);
|
|
|
|
}
|
2012-02-13 13:22:35 -05:00
|
|
|
|
2012-11-30 19:16:24 -05:00
|
|
|
for(int i = 0; i < MAX_PSEUDOIDS; i++)
|
2012-02-13 13:22:35 -05:00
|
|
|
{
|
2012-11-30 19:16:24 -05:00
|
|
|
age += DUMMY_GXSID_DELAY;
|
|
|
|
RsTickEvent::schedule_in(GXSID_EVENT_DUMMY_PSEUDOID, age);
|
|
|
|
}
|
2012-02-13 13:22:35 -05:00
|
|
|
|
2012-11-30 19:16:24 -05:00
|
|
|
for(int i = 0; i < MAX_UNKNOWN_PGPIDS; i++)
|
|
|
|
{
|
|
|
|
age += DUMMY_GXSID_DELAY;
|
|
|
|
RsTickEvent::schedule_in(GXSID_EVENT_DUMMY_UNKNOWN_PGPID, age);
|
|
|
|
}
|
|
|
|
}
|
2012-02-13 13:22:35 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-11-30 19:16:24 -05:00
|
|
|
void p3IdService::generateDummy_OwnIds()
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
2012-11-03 07:57:27 -04:00
|
|
|
|
2012-11-30 19:16:24 -05:00
|
|
|
/* grab all the gpg ids... and make some ids */
|
2012-02-13 13:22:35 -05:00
|
|
|
|
2012-11-30 19:16:24 -05:00
|
|
|
std::string ownId = rsPeers->getGPGOwnId();
|
2012-02-13 13:22:35 -05:00
|
|
|
|
2012-11-30 19:16:24 -05:00
|
|
|
// generate some ownIds.
|
|
|
|
int genCount = 0;
|
|
|
|
int i;
|
2012-02-13 13:22:35 -05:00
|
|
|
|
2012-11-30 19:16:24 -05:00
|
|
|
int nIds = 2 + (RSRandom::random_u32() % 2);
|
|
|
|
for(i = 0; i < nIds; i++)
|
2012-02-13 13:22:35 -05:00
|
|
|
{
|
2012-10-21 11:48:18 -04:00
|
|
|
RsGxsIdGroup id;
|
2012-11-30 19:16:24 -05:00
|
|
|
RsPeerDetails details;
|
2012-02-13 13:22:35 -05:00
|
|
|
|
2012-11-30 19:16:24 -05:00
|
|
|
id.mMeta.mGroupFlags = RSGXSID_GROUPFLAG_REALID;
|
2012-02-13 13:22:35 -05:00
|
|
|
|
2012-11-30 19:16:24 -05:00
|
|
|
// HACK FOR DUMMY GENERATION.
|
|
|
|
id.mMeta.mAuthorId = ownId;
|
|
|
|
if (rsPeers->getPeerDetails(ownId, details))
|
|
|
|
{
|
|
|
|
std::ostringstream out;
|
|
|
|
out << details.name << "_" << i + 1;
|
2012-11-05 17:28:08 -05:00
|
|
|
|
2012-11-30 19:16:24 -05:00
|
|
|
id.mMeta.mGroupName = out.str();
|
|
|
|
}
|
2012-02-13 13:22:35 -05:00
|
|
|
|
2012-10-21 11:48:18 -04:00
|
|
|
uint32_t dummyToken = 0;
|
|
|
|
createGroup(dummyToken, id);
|
2012-02-13 13:22:35 -05:00
|
|
|
}
|
2012-11-30 19:16:24 -05:00
|
|
|
}
|
2012-02-13 13:22:35 -05:00
|
|
|
|
|
|
|
|
2012-11-30 19:16:24 -05:00
|
|
|
void p3IdService::generateDummy_FriendPGP()
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
2012-02-13 13:22:35 -05:00
|
|
|
|
2012-11-30 19:16:24 -05:00
|
|
|
// Now Generate for friends.
|
|
|
|
std::list<std::string> gpgids;
|
|
|
|
std::list<std::string>::iterator it;
|
|
|
|
rsPeers->getGPGAllList(gpgids);
|
2012-11-05 17:28:08 -05:00
|
|
|
|
2012-11-30 19:16:24 -05:00
|
|
|
RsGxsIdGroup id;
|
2012-11-05 17:28:08 -05:00
|
|
|
|
2012-11-30 19:16:24 -05:00
|
|
|
id.mMeta.mGroupFlags = RSGXSID_GROUPFLAG_REALID;
|
2012-02-13 13:22:35 -05:00
|
|
|
|
2012-11-30 19:16:24 -05:00
|
|
|
int idx = RSRandom::random_f32() * (gpgids.size() - 1);
|
|
|
|
it = gpgids.begin();
|
|
|
|
for(int j = 0; j < idx; j++, it++);
|
|
|
|
|
|
|
|
// HACK FOR DUMMY GENERATION.
|
|
|
|
id.mMeta.mAuthorId = *it;
|
|
|
|
|
|
|
|
RsPeerDetails details;
|
|
|
|
if (rsPeers->getPeerDetails(*it, details))
|
|
|
|
{
|
|
|
|
std::ostringstream out;
|
|
|
|
out << details.name << "_" << RSRandom::random_u32() % 1000;
|
|
|
|
id.mMeta.mGroupName = out.str();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "p3IdService::generateDummy_FriendPGP() missing" << std::endl;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
id.mMeta.mGroupName = genRandomId();
|
2012-02-13 13:22:35 -05:00
|
|
|
}
|
|
|
|
|
2012-11-30 19:16:24 -05:00
|
|
|
uint32_t dummyToken = 0;
|
|
|
|
createGroup(dummyToken, id);
|
|
|
|
}
|
2012-02-13 13:22:35 -05:00
|
|
|
|
2012-11-30 19:16:24 -05:00
|
|
|
|
|
|
|
void p3IdService::generateDummy_UnknownPGP()
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
|
|
|
|
RsGxsIdGroup id;
|
|
|
|
|
|
|
|
// FAKE DATA.
|
|
|
|
id.mMeta.mGroupFlags = RSGXSID_GROUPFLAG_REALID;
|
|
|
|
id.mPgpIdHash = genRandomId(40);
|
|
|
|
id.mPgpIdSign = genRandomId(40);
|
|
|
|
id.mMeta.mGroupName = genRandomId();
|
|
|
|
|
|
|
|
uint32_t dummyToken = 0;
|
|
|
|
createGroup(dummyToken, id);
|
2012-02-13 13:22:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-30 19:16:24 -05:00
|
|
|
void p3IdService::generateDummy_UnknownPseudo()
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
|
|
|
|
RsGxsIdGroup id;
|
|
|
|
|
|
|
|
// FAKE DATA.
|
|
|
|
id.mMeta.mGroupFlags = 0;
|
|
|
|
id.mMeta.mGroupName = genRandomId();
|
|
|
|
|
|
|
|
uint32_t dummyToken = 0;
|
|
|
|
createGroup(dummyToken, id);
|
|
|
|
}
|
|
|
|
|
2012-02-13 13:22:35 -05:00
|
|
|
|
|
|
|
std::string rsIdTypeToString(uint32_t idtype)
|
|
|
|
{
|
|
|
|
std::string str;
|
|
|
|
if (idtype & RSID_TYPE_REALID)
|
|
|
|
{
|
|
|
|
str += "GPGID ";
|
|
|
|
}
|
|
|
|
if (idtype & RSID_TYPE_PSEUDONYM)
|
|
|
|
{
|
|
|
|
str += "PSEUDO ";
|
|
|
|
}
|
|
|
|
if (idtype & RSID_RELATION_YOURSELF)
|
|
|
|
{
|
|
|
|
str += "YOURSELF ";
|
|
|
|
}
|
|
|
|
if (idtype & RSID_RELATION_FRIEND)
|
|
|
|
{
|
|
|
|
str += "FRIEND ";
|
|
|
|
}
|
|
|
|
if (idtype & RSID_RELATION_FOF)
|
|
|
|
{
|
|
|
|
str += "FOF ";
|
|
|
|
}
|
|
|
|
if (idtype & RSID_RELATION_OTHER)
|
|
|
|
{
|
|
|
|
str += "OTHER ";
|
|
|
|
}
|
|
|
|
if (idtype & RSID_RELATION_UNKNOWN)
|
|
|
|
{
|
|
|
|
str += "UNKNOWN ";
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-13 20:27:28 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-07-27 08:45:52 -04:00
|
|
|
/************************************************************************************/
|
|
|
|
/************************************************************************************/
|
|
|
|
/************************************************************************************/
|
|
|
|
/************************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/* here we are running a background process that calculates the reputation scores
|
|
|
|
* for each of the IDs....
|
|
|
|
*
|
|
|
|
* As this class will be extensively used by many other threads... it is best
|
|
|
|
* that we don't block at all. This should be in a background thread.
|
|
|
|
* Perhaps a generic method to handle this will be advisable.... but we do that later.
|
|
|
|
*
|
|
|
|
* To start with we will work from the Posted service.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* So Reputation....
|
|
|
|
* Three components:
|
|
|
|
* 1) Your Opinion: Should override everything else.
|
|
|
|
* 2) Implicit Factors: Know the associated GPG Key.
|
|
|
|
* 3) Your Friends Opinions:
|
|
|
|
* 4) Your Friends Calculated Reputation Scores.
|
|
|
|
*
|
|
|
|
* Must make sure that there is no Feedback loop in the Reputation calculation.
|
|
|
|
*
|
|
|
|
* So: Our Score + Friends Scores => Local Reputation.
|
|
|
|
* Local Reputation + Friends Reputations => Final Reputation?
|
|
|
|
*
|
|
|
|
* Do we need to 'ignore' Non-scores?
|
|
|
|
* ---> This becomes like the "Best Comment" algorithm from Reddit...
|
|
|
|
* Use a statistical mechanism to work out a lower bound on Reputation.
|
|
|
|
*
|
|
|
|
* But what if your opinion is wrong?.... well likely your friends will
|
|
|
|
* get their messages and reply... you'll see the missing message - request it - check reputation etc.
|
|
|
|
*
|
|
|
|
* So we are going to have three different scores (Own, Peers, (the neighbour) Hood)...
|
|
|
|
*
|
|
|
|
* So next question, when do we need to incrementally calculate the score?
|
|
|
|
* .... how often do we need to recalculate everything -> this could lead to a flux of messages.
|
2012-07-29 09:09:36 -04:00
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* MORE NOTES:
|
|
|
|
*
|
|
|
|
* The Opinion Messages will have to be signed by PGP or SSL Keys, to guarantee that we don't
|
|
|
|
* multiple votes per person... As the message system doesn't handle uniqueness in this respect,
|
|
|
|
* we might have to do FULL_CALC for everything - This bit TODO.
|
|
|
|
*
|
|
|
|
* This will make IdService quite different to the other GXS services.
|
2012-07-27 08:45:52 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/************************************************************************************/
|
|
|
|
/*
|
|
|
|
* Processing Algorithm:
|
|
|
|
* - Grab all Groups which have received messages.
|
|
|
|
* (opt 1)-> grab latest msgs for each of these and process => score.
|
|
|
|
* (opt 2)-> try incremental system (people probably won't change opinions often -> just set them once)
|
|
|
|
* --> if not possible, fallback to full calculation.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-11-06 14:18:11 -05:00
|
|
|
bool p3IdService::reputation_start()
|
|
|
|
{
|
2012-11-19 17:00:05 -05:00
|
|
|
if (!CacheArbitration(BG_REPUTATION))
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::reputation_start() Other Events running... Rescheduling";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-06 14:18:11 -05:00
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
/* reschedule in a bit */
|
|
|
|
RsTickEvent::schedule_in(GXSID_EVENT_REPUTATION, REPUTATION_RETRY_PERIOD);
|
|
|
|
return false;
|
|
|
|
}
|
2012-11-06 14:18:11 -05:00
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
CacheArbitrationDone(BG_REPUTATION);
|
|
|
|
// SCHEDULE NEXT ONE.
|
|
|
|
RsTickEvent::schedule_in(GXSID_EVENT_REPUTATION, REPUTATION_PERIOD);
|
2012-11-06 14:18:11 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-27 08:45:52 -04:00
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
|
2012-07-27 08:45:52 -04:00
|
|
|
#define ID_BACKGROUND_PERIOD 60
|
|
|
|
|
|
|
|
int p3IdService::background_tick()
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-07-27 08:45:52 -04:00
|
|
|
std::cerr << "p3IdService::background_tick()";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-07-27 08:45:52 -04:00
|
|
|
|
|
|
|
// Run Background Stuff.
|
|
|
|
background_checkTokenRequest();
|
|
|
|
|
|
|
|
/* every minute - run a background check */
|
|
|
|
time_t now = time(NULL);
|
|
|
|
bool doCheck = false;
|
|
|
|
{
|
2012-07-29 09:09:36 -04:00
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
2012-07-27 08:45:52 -04:00
|
|
|
if (now - mLastBgCheck > ID_BACKGROUND_PERIOD)
|
|
|
|
{
|
|
|
|
doCheck = true;
|
|
|
|
mLastBgCheck = now;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doCheck)
|
|
|
|
{
|
2012-07-29 09:09:36 -04:00
|
|
|
//addExtraDummyData();
|
2012-07-27 08:45:52 -04:00
|
|
|
background_requestGroups();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add in new votes + comments.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/***** Background Processing ****
|
|
|
|
*
|
|
|
|
* Process Each Message - as it arrives.
|
|
|
|
*
|
|
|
|
* Update
|
|
|
|
*
|
|
|
|
*/
|
2012-07-29 09:09:36 -04:00
|
|
|
#define ID_BG_IDLE 0
|
|
|
|
#define ID_BG_REQUEST_GROUPS 1
|
2012-07-27 08:45:52 -04:00
|
|
|
#define ID_BG_REQUEST_UNPROCESSED 2
|
2012-07-29 09:09:36 -04:00
|
|
|
#define ID_BG_REQUEST_FULLCALC 3
|
2012-07-27 08:45:52 -04:00
|
|
|
|
|
|
|
bool p3IdService::background_checkTokenRequest()
|
|
|
|
{
|
|
|
|
uint32_t token = 0;
|
|
|
|
uint32_t phase = 0;
|
|
|
|
{
|
2012-07-29 09:09:36 -04:00
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
2012-07-27 08:45:52 -04:00
|
|
|
if (!mBgProcessing)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
token = mBgToken;
|
|
|
|
phase = mBgPhase;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t status;
|
|
|
|
uint32_t reqtype;
|
|
|
|
uint32_t anstype;
|
|
|
|
time_t ts;
|
2012-10-21 11:48:18 -04:00
|
|
|
|
|
|
|
|
|
|
|
status = RsGenExchange::getTokenService()->requestStatus(token);
|
|
|
|
//checkRequestStatus(token, status, reqtype, anstype, ts);
|
2012-07-27 08:45:52 -04:00
|
|
|
|
2012-10-21 11:48:18 -04:00
|
|
|
if (status == RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE)
|
2012-07-27 08:45:52 -04:00
|
|
|
{
|
|
|
|
switch(phase)
|
|
|
|
{
|
|
|
|
case ID_BG_REQUEST_GROUPS:
|
|
|
|
background_requestNewMessages();
|
|
|
|
break;
|
|
|
|
case ID_BG_REQUEST_UNPROCESSED:
|
|
|
|
background_processNewMessages();
|
|
|
|
break;
|
2012-07-29 09:09:36 -04:00
|
|
|
case ID_BG_REQUEST_FULLCALC:
|
|
|
|
background_processFullCalc();
|
2012-07-27 08:45:52 -04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool p3IdService::background_requestGroups()
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-07-27 08:45:52 -04:00
|
|
|
std::cerr << "p3IdService::background_requestGroups()";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-07-27 08:45:52 -04:00
|
|
|
|
|
|
|
// grab all the subscribed groups.
|
|
|
|
uint32_t token = 0;
|
|
|
|
|
|
|
|
{
|
2012-07-29 09:09:36 -04:00
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
2012-07-27 08:45:52 -04:00
|
|
|
|
|
|
|
if (mBgProcessing)
|
|
|
|
{
|
|
|
|
std::cerr << "p3IdService::background_requestGroups() ERROR Already processing, Skip this cycle";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mBgProcessing = true;
|
|
|
|
mBgPhase = ID_BG_REQUEST_GROUPS;
|
|
|
|
mBgToken = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ansType = RS_TOKREQ_ANSTYPE_SUMMARY;
|
2012-10-21 15:45:35 -04:00
|
|
|
RsTokReqOptions opts;
|
2012-07-27 08:45:52 -04:00
|
|
|
std::list<std::string> groupIds;
|
|
|
|
|
2012-10-21 11:48:18 -04:00
|
|
|
/**
|
|
|
|
TODO
|
2012-07-27 08:45:52 -04:00
|
|
|
opts.mStatusFilter = RSGXS_GROUP_STATUS_NEWMSG;
|
|
|
|
opts.mStatusMask = RSGXS_GROUP_STATUS_NEWMSG;
|
2012-10-21 11:48:18 -04:00
|
|
|
**/
|
2012-07-27 08:45:52 -04:00
|
|
|
|
2012-10-21 11:48:18 -04:00
|
|
|
RsGenExchange::getTokenService()->requestGroupInfo(token, ansType, opts, groupIds);
|
2012-07-27 08:45:52 -04:00
|
|
|
{
|
2012-07-29 09:09:36 -04:00
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
2012-07-27 08:45:52 -04:00
|
|
|
mBgToken = token;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool p3IdService::background_requestNewMessages()
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-07-27 08:45:52 -04:00
|
|
|
std::cerr << "p3IdService::background_requestNewMessages()";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-07-27 08:45:52 -04:00
|
|
|
|
2012-07-29 09:09:36 -04:00
|
|
|
std::list<RsGroupMetaData> modGroupList;
|
|
|
|
std::list<RsGroupMetaData>::iterator it;
|
|
|
|
|
2012-07-27 08:45:52 -04:00
|
|
|
std::list<std::string> groupIds;
|
|
|
|
uint32_t token = 0;
|
|
|
|
|
|
|
|
{
|
2012-07-29 09:09:36 -04:00
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
2012-07-27 08:45:52 -04:00
|
|
|
token = mBgToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!getGroupSummary(token, modGroupList))
|
|
|
|
{
|
|
|
|
std::cerr << "p3IdService::background_requestNewMessages() ERROR No Group List";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
background_cleanup();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2012-07-29 09:09:36 -04:00
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
2012-07-27 08:45:52 -04:00
|
|
|
mBgPhase = ID_BG_REQUEST_UNPROCESSED;
|
|
|
|
mBgToken = 0;
|
|
|
|
|
|
|
|
/* now we process the modGroupList -> a map so we can use it easily later, and create id list too */
|
|
|
|
for(it = modGroupList.begin(); it != modGroupList.end(); it++)
|
|
|
|
{
|
2012-10-21 11:48:18 -04:00
|
|
|
/*** TODO
|
|
|
|
uint32_t dummyToken = 0;
|
|
|
|
setGroupStatusFlags(dummyToken, it->mGroupId, 0, RSGXS_GROUP_STATUS_NEWMSG);
|
|
|
|
***/
|
2012-07-27 08:45:52 -04:00
|
|
|
|
|
|
|
mBgGroupMap[it->mGroupId] = *it;
|
2012-07-29 09:09:36 -04:00
|
|
|
groupIds.push_back(it->mGroupId);
|
2012-07-27 08:45:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ansType = RS_TOKREQ_ANSTYPE_SUMMARY;
|
2012-10-21 15:45:35 -04:00
|
|
|
RsTokReqOptions opts;
|
2012-07-27 08:45:52 -04:00
|
|
|
token = 0;
|
|
|
|
|
2012-10-21 11:48:18 -04:00
|
|
|
/* TODO
|
2012-07-27 08:45:52 -04:00
|
|
|
opts.mStatusFilter = RSGXS_MSG_STATUS_UNPROCESSED;
|
|
|
|
opts.mStatusMask = RSGXS_MSG_STATUS_UNPROCESSED;
|
2012-10-21 11:48:18 -04:00
|
|
|
*/
|
2012-07-27 08:45:52 -04:00
|
|
|
|
2012-10-21 11:48:18 -04:00
|
|
|
RsGenExchange::getTokenService()->requestMsgInfo(token, ansType, opts, groupIds);
|
2012-07-27 08:45:52 -04:00
|
|
|
|
|
|
|
{
|
2012-07-29 09:09:36 -04:00
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
2012-07-27 08:45:52 -04:00
|
|
|
mBgToken = token;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool p3IdService::background_processNewMessages()
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-07-27 08:45:52 -04:00
|
|
|
std::cerr << "p3IdService::background_processNewMessages()";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-07-27 08:45:52 -04:00
|
|
|
|
2012-10-21 11:48:18 -04:00
|
|
|
GxsMsgMetaMap newMsgMap;
|
|
|
|
GxsMsgMetaMap::iterator it;
|
2012-07-27 08:45:52 -04:00
|
|
|
uint32_t token = 0;
|
|
|
|
|
|
|
|
{
|
2012-07-29 09:09:36 -04:00
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
2012-07-27 08:45:52 -04:00
|
|
|
token = mBgToken;
|
|
|
|
}
|
|
|
|
|
2012-10-21 11:48:18 -04:00
|
|
|
if (!getMsgSummary(token, newMsgMap))
|
2012-07-27 08:45:52 -04:00
|
|
|
{
|
|
|
|
std::cerr << "p3IdService::background_processNewMessages() ERROR No New Msgs";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
background_cleanup();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* iterate through the msgs.. update the mBgGroupMap with new data,
|
|
|
|
* and flag these items as modified - so we rewrite them to the db later.
|
|
|
|
*
|
|
|
|
* If a message is not an original -> store groupId for requiring full analysis later.
|
|
|
|
*/
|
|
|
|
|
2012-07-29 09:09:36 -04:00
|
|
|
std::map<std::string, RsGroupMetaData>::iterator mit;
|
2012-07-27 08:45:52 -04:00
|
|
|
|
2012-10-21 11:48:18 -04:00
|
|
|
for (it = newMsgMap.begin(); it != newMsgMap.end(); it++)
|
|
|
|
{
|
|
|
|
std::vector<RsMsgMetaData>::iterator vit;
|
|
|
|
for(vit = it->second.begin(); vit != it->second.end(); vit++)
|
2012-07-27 08:45:52 -04:00
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-10-21 11:48:18 -04:00
|
|
|
std::cerr << "p3IdService::background_processNewMessages() new MsgId: " << vit->mMsgId;
|
2012-07-29 09:09:36 -04:00
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-10-21 11:48:18 -04:00
|
|
|
|
|
|
|
/* flag each new vote as processed */
|
|
|
|
/**
|
|
|
|
TODO
|
|
|
|
uint32_t dummyToken = 0;
|
|
|
|
setMsgStatusFlags(dummyToken, it->mMsgId, 0, RSGXS_MSG_STATUS_UNPROCESSED);
|
|
|
|
**/
|
|
|
|
|
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
|
|
|
|
mit = mBgGroupMap.find(it->first);
|
|
|
|
if (mit == mBgGroupMap.end())
|
2012-07-27 08:45:52 -04:00
|
|
|
{
|
2012-10-21 11:48:18 -04:00
|
|
|
std::cerr << "p3IdService::background_processNewMessages() ERROR missing GroupId: ";
|
|
|
|
std::cerr << vit->mGroupId;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
2012-07-27 08:45:52 -04:00
|
|
|
/* error */
|
2012-10-21 11:48:18 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mit->second.mGroupStatus & ID_LOCAL_STATUS_FULL_CALC_FLAG)
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-10-21 11:48:18 -04:00
|
|
|
std::cerr << "p3IdService::background_processNewMessages() Group Already marked FULL_CALC";
|
2012-07-29 09:09:36 -04:00
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-10-21 11:48:18 -04:00
|
|
|
|
|
|
|
/* already marked */
|
|
|
|
continue;
|
2012-07-27 08:45:52 -04:00
|
|
|
}
|
2012-10-21 11:48:18 -04:00
|
|
|
|
|
|
|
if (vit->mMsgId != vit->mOrigMsgId)
|
2012-07-27 08:45:52 -04:00
|
|
|
{
|
2012-10-21 11:48:18 -04:00
|
|
|
/*
|
|
|
|
* not original -> hard, redo calc (alt: could substract previous score)
|
|
|
|
*/
|
|
|
|
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-10-21 11:48:18 -04:00
|
|
|
std::cerr << "p3IdService::background_processNewMessages() Update, mark for FULL_CALC";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-10-21 11:48:18 -04:00
|
|
|
|
|
|
|
mit->second.mGroupStatus |= ID_LOCAL_STATUS_FULL_CALC_FLAG;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Try incremental calculation.
|
|
|
|
* - extract parameters from group.
|
|
|
|
* - increment, & save back.
|
|
|
|
* - flag group as modified.
|
|
|
|
*/
|
|
|
|
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-10-21 11:48:18 -04:00
|
|
|
std::cerr << "p3IdService::background_processNewMessages() NewOpt, Try Inc Calc";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-10-21 11:48:18 -04:00
|
|
|
|
|
|
|
mit->second.mGroupStatus |= ID_LOCAL_STATUS_INC_CALC_FLAG;
|
|
|
|
|
2012-11-05 17:28:08 -05:00
|
|
|
SSGxsIdGroup ssdata;
|
|
|
|
if (!ssdata.load(mit->second.mServiceString))
|
2012-10-21 11:48:18 -04:00
|
|
|
{
|
|
|
|
/* error */
|
|
|
|
std::cerr << "p3IdService::background_processNewMessages() ERROR Extracting";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-10-21 11:48:18 -04:00
|
|
|
/* do calcs */
|
|
|
|
std::cerr << "p3IdService::background_processNewMessages() Extracted: ";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
/* store it back in */
|
|
|
|
std::cerr << "p3IdService::background_processNewMessages() Stored: ";
|
2012-07-29 09:09:36 -04:00
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
|
|
|
|
std::string serviceString = ssdata.save();
|
|
|
|
if (0)
|
2012-10-21 11:48:18 -04:00
|
|
|
{
|
|
|
|
/* error */
|
|
|
|
std::cerr << "p3IdService::background_processNewMessages() ERROR Storing";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
2012-07-27 08:45:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* now iterate through groups again
|
|
|
|
* -> update status as we go
|
|
|
|
* -> record one requiring a full analyssis
|
|
|
|
*/
|
|
|
|
|
|
|
|
{
|
2012-07-29 09:09:36 -04:00
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-07-29 09:09:36 -04:00
|
|
|
std::cerr << "p3IdService::background_processNewMessages() Checking Groups for Calc Type";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-07-27 08:45:52 -04:00
|
|
|
|
|
|
|
for(mit = mBgGroupMap.begin(); mit != mBgGroupMap.end(); mit++)
|
|
|
|
{
|
2012-07-29 09:09:36 -04:00
|
|
|
if (mit->second.mGroupStatus & ID_LOCAL_STATUS_FULL_CALC_FLAG)
|
2012-07-27 08:45:52 -04:00
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-07-29 09:09:36 -04:00
|
|
|
std::cerr << "p3IdService::background_processNewMessages() FullCalc for: ";
|
|
|
|
std::cerr << mit->second.mGroupId;
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-07-29 09:09:36 -04:00
|
|
|
|
|
|
|
mBgFullCalcGroups.push_back(mit->second.mGroupId);
|
2012-07-27 08:45:52 -04:00
|
|
|
}
|
2012-07-29 09:09:36 -04:00
|
|
|
else if (mit->second.mGroupStatus & ID_LOCAL_STATUS_INC_CALC_FLAG)
|
2012-07-27 08:45:52 -04:00
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-07-29 09:09:36 -04:00
|
|
|
std::cerr << "p3IdService::background_processNewMessages() IncCalc done for: ";
|
|
|
|
std::cerr << mit->second.mGroupId;
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-07-29 09:09:36 -04:00
|
|
|
|
2012-07-27 08:45:52 -04:00
|
|
|
/* set Cache */
|
2012-10-21 11:48:18 -04:00
|
|
|
uint32_t dummyToken = 0;
|
|
|
|
setGroupServiceString(dummyToken, mit->second.mGroupId, mit->second.mServiceString);
|
2012-07-27 08:45:52 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* why is it here? error. */
|
2012-07-29 09:09:36 -04:00
|
|
|
std::cerr << "p3IdService::background_processNewMessages() ERROR for: ";
|
|
|
|
std::cerr << mit->second.mGroupId;
|
|
|
|
std::cerr << std::endl;
|
2012-07-27 08:45:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-29 09:09:36 -04:00
|
|
|
return background_FullCalcRequest();
|
2012-07-27 08:45:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool p3IdService::background_FullCalcRequest()
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* grab an GroupId from List.
|
|
|
|
* - If empty, we are finished.
|
|
|
|
* - request all latest mesgs
|
|
|
|
*/
|
2012-07-29 09:09:36 -04:00
|
|
|
|
2012-07-27 08:45:52 -04:00
|
|
|
std::list<std::string> groupIds;
|
|
|
|
{
|
2012-07-29 09:09:36 -04:00
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
mBgPhase = ID_BG_REQUEST_FULLCALC;
|
2012-07-27 08:45:52 -04:00
|
|
|
mBgToken = 0;
|
|
|
|
mBgGroupMap.clear();
|
|
|
|
|
|
|
|
if (mBgFullCalcGroups.empty())
|
|
|
|
{
|
|
|
|
/* finished! */
|
2012-07-29 09:09:36 -04:00
|
|
|
background_cleanup();
|
|
|
|
return true;
|
2012-07-27 08:45:52 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
groupIds.push_back(mBgFullCalcGroups.front());
|
|
|
|
mBgFullCalcGroups.pop_front();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* request the summary info from the parents */
|
|
|
|
uint32_t ansType = RS_TOKREQ_ANSTYPE_DATA;
|
2012-07-29 09:09:36 -04:00
|
|
|
uint32_t token = 0;
|
2012-10-21 15:45:35 -04:00
|
|
|
RsTokReqOptions opts;
|
2012-07-29 09:09:36 -04:00
|
|
|
opts.mOptions = RS_TOKREQOPT_MSG_LATEST;
|
2012-07-27 08:45:52 -04:00
|
|
|
|
2012-10-21 11:48:18 -04:00
|
|
|
RsGenExchange::getTokenService()->requestMsgInfo(token, ansType, opts, groupIds);
|
2012-07-27 08:45:52 -04:00
|
|
|
|
|
|
|
{
|
2012-07-29 09:09:36 -04:00
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
2012-07-27 08:45:52 -04:00
|
|
|
mBgToken = token;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool p3IdService::background_processFullCalc()
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-07-27 08:45:52 -04:00
|
|
|
std::cerr << "p3IdService::background_processFullCalc()";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-07-27 08:45:52 -04:00
|
|
|
|
|
|
|
std::list<RsMsgMetaData> msgList;
|
|
|
|
std::list<RsMsgMetaData>::iterator it;
|
|
|
|
|
|
|
|
|
|
|
|
bool validmsgs = false;
|
|
|
|
|
|
|
|
/* calc variables */
|
|
|
|
uint32_t opinion_count = 0;
|
|
|
|
uint32_t opinion_nullcount = 0;
|
|
|
|
double opinion_sum = 0;
|
|
|
|
double opinion_sumsq = 0;
|
|
|
|
|
|
|
|
uint32_t rep_count = 0;
|
|
|
|
uint32_t rep_nullcount = 0;
|
|
|
|
double rep_sum = 0;
|
|
|
|
double rep_sumsq = 0;
|
|
|
|
|
2012-10-21 11:48:18 -04:00
|
|
|
std::vector<RsGxsIdOpinion> opinions;
|
|
|
|
std::vector<RsGxsIdOpinion>::iterator vit;
|
|
|
|
|
|
|
|
if (!getMsgData(mBgToken, opinions))
|
2012-07-27 08:45:52 -04:00
|
|
|
{
|
2012-10-21 11:48:18 -04:00
|
|
|
std::cerr << "p3IdService::background_processFullCalc() ERROR Failed to get Opinions";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string groupId;
|
|
|
|
for(vit = opinions.begin(); vit != opinions.end(); vit++)
|
|
|
|
{
|
|
|
|
RsGxsIdOpinion &opinion = *vit;
|
|
|
|
|
|
|
|
/* These should all be same group - should check for sanity! */
|
|
|
|
if (groupId == "")
|
|
|
|
{
|
|
|
|
groupId = opinion.mMeta.mGroupId;
|
|
|
|
}
|
|
|
|
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-07-27 08:45:52 -04:00
|
|
|
std::cerr << "p3IdService::background_processFullCalc() Msg:";
|
2012-10-21 11:48:18 -04:00
|
|
|
std::cerr << opinion;
|
2012-07-27 08:45:52 -04:00
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-07-27 08:45:52 -04:00
|
|
|
|
|
|
|
validmsgs = true;
|
|
|
|
|
2012-10-21 11:48:18 -04:00
|
|
|
/* for each opinion.... extract score, and reputation */
|
|
|
|
if (opinion.mOpinion != 0)
|
2012-07-27 08:45:52 -04:00
|
|
|
{
|
|
|
|
opinion_count++;
|
2012-10-21 11:48:18 -04:00
|
|
|
opinion_sum += opinion.mOpinion;
|
|
|
|
opinion_sum += (opinion.mOpinion * opinion.mOpinion);
|
2012-07-27 08:45:52 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
opinion_nullcount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-21 11:48:18 -04:00
|
|
|
/* for each opinion.... extract score, and reputation */
|
|
|
|
if (opinion.mReputation != 0)
|
2012-07-27 08:45:52 -04:00
|
|
|
{
|
|
|
|
rep_nullcount++;
|
2012-10-21 11:48:18 -04:00
|
|
|
rep_sum += opinion.mReputation;
|
|
|
|
rep_sum += (opinion.mReputation * opinion.mReputation);
|
2012-07-27 08:45:52 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rep_nullcount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double opinion_avg = 0;
|
|
|
|
double opinion_var = 0;
|
|
|
|
double opinion_frac = 0;
|
|
|
|
|
|
|
|
double rep_avg = 0;
|
|
|
|
double rep_var = 0;
|
|
|
|
double rep_frac = 0;
|
|
|
|
|
|
|
|
|
|
|
|
if (opinion_count)
|
|
|
|
{
|
|
|
|
opinion_avg = opinion_sum / opinion_count;
|
|
|
|
opinion_var = (opinion_sumsq - opinion_count * opinion_avg * opinion_avg) / opinion_count;
|
|
|
|
opinion_frac = opinion_count / ((float) (opinion_count + opinion_nullcount));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rep_count)
|
|
|
|
{
|
|
|
|
rep_avg = rep_sum / rep_count;
|
|
|
|
rep_var = (rep_sumsq - rep_count * rep_avg * rep_avg) / rep_count;
|
|
|
|
rep_frac = rep_count / ((float) (rep_count + rep_nullcount));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (validmsgs)
|
|
|
|
{
|
2012-11-05 17:28:08 -05:00
|
|
|
SSGxsIdGroup ssdata;
|
|
|
|
std::string serviceString = ssdata.save();
|
|
|
|
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
std::cerr << "p3IdService::background_updateVoteCounts() Encoded String: " << serviceString;
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-05 17:28:08 -05:00
|
|
|
/* store new result */
|
|
|
|
uint32_t dummyToken = 0;
|
|
|
|
setGroupServiceString(dummyToken, groupId, serviceString);
|
2012-07-27 08:45:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2012-07-29 09:09:36 -04:00
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
mBgPhase = ID_BG_IDLE;
|
2012-07-27 08:45:52 -04:00
|
|
|
mBgToken = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return background_FullCalcRequest();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool p3IdService::background_cleanup()
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-07-27 08:45:52 -04:00
|
|
|
std::cerr << "p3IdService::background_cleanup()";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-07-27 08:45:52 -04:00
|
|
|
|
2012-07-29 09:09:36 -04:00
|
|
|
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
|
2012-07-27 08:45:52 -04:00
|
|
|
|
|
|
|
// Cleanup.
|
|
|
|
mBgProcessing = false;
|
2012-07-29 09:09:36 -04:00
|
|
|
mBgPhase = ID_BG_IDLE;
|
2012-07-27 08:45:52 -04:00
|
|
|
mBgToken = 0;
|
2012-07-29 09:09:36 -04:00
|
|
|
mBgGroupMap.clear();
|
|
|
|
mBgFullCalcGroups.clear();
|
|
|
|
|
2012-07-27 08:45:52 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-21 11:48:18 -04:00
|
|
|
std::ostream &operator<<(std::ostream &out, const RsGxsIdGroup &grp)
|
2012-07-29 09:09:36 -04:00
|
|
|
{
|
2012-10-21 11:48:18 -04:00
|
|
|
out << "RsGxsIdGroup: Meta: " << grp.mMeta;
|
2012-11-05 17:28:08 -05:00
|
|
|
out << " PgpIdHash: " << grp.mPgpIdHash;
|
|
|
|
out << " PgpIdSign: [binary]"; // << grp.mPgpIdSign;
|
2012-07-29 09:09:36 -04:00
|
|
|
out << std::endl;
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2012-10-21 11:48:18 -04:00
|
|
|
std::ostream &operator<<(std::ostream &out, const RsGxsIdOpinion &opinion)
|
2012-07-29 09:09:36 -04:00
|
|
|
{
|
2012-10-21 11:48:18 -04:00
|
|
|
out << "RsGxsIdOpinion: Meta: " << opinion.mMeta;
|
2012-07-29 09:09:36 -04:00
|
|
|
out << std::endl;
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Overloaded from GxsTokenQueue for Request callbacks.
|
|
|
|
void p3IdService::handleResponse(uint32_t token, uint32_t req_type)
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::handleResponse(" << token << "," << req_type << ")";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
|
|
|
|
// stuff.
|
|
|
|
switch(req_type)
|
|
|
|
{
|
|
|
|
case GXSIDREQ_CACHEOWNIDS:
|
|
|
|
cache_load_ownids(token);
|
|
|
|
break;
|
|
|
|
case GXSIDREQ_CACHELOAD:
|
|
|
|
cache_load_for_token(token);
|
|
|
|
break;
|
|
|
|
case GXSIDREQ_PGPHASH:
|
|
|
|
pgphash_handlerequest(token);
|
|
|
|
break;
|
|
|
|
case GXSIDREQ_CACHETEST:
|
|
|
|
cachetest_handlerequest(token);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GXSIDREQ_REPUTATION:
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* error */
|
|
|
|
std::cerr << "p3IdService::handleResponse() Unknown Request Type: " << req_type;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Overloaded from RsTickEvent for Event callbacks.
|
2012-11-29 17:48:28 -05:00
|
|
|
void p3IdService::handle_event(uint32_t event_type, const std::string &elabel)
|
2012-11-19 17:00:05 -05:00
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "p3IdService::handle_event(" << event_type << ")";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_IDS
|
2012-11-19 17:00:05 -05:00
|
|
|
|
|
|
|
// stuff.
|
|
|
|
switch(event_type)
|
|
|
|
{
|
|
|
|
case GXSID_EVENT_CACHEOWNIDS:
|
|
|
|
cache_request_ownids();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GXSID_EVENT_CACHELOAD:
|
|
|
|
cache_start_load();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GXSID_EVENT_CACHETEST:
|
|
|
|
cachetest_getlist();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GXSID_EVENT_REPUTATION:
|
|
|
|
reputation_start();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GXSID_EVENT_PGPHASH:
|
|
|
|
pgphash_start();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GXSID_EVENT_PGPHASH_PROC:
|
|
|
|
pgphash_process();
|
|
|
|
break;
|
|
|
|
|
2012-11-30 19:16:24 -05:00
|
|
|
case GXSID_EVENT_DUMMYDATA:
|
|
|
|
generateDummyData();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GXSID_EVENT_DUMMY_OWNIDS:
|
|
|
|
generateDummy_OwnIds();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GXSID_EVENT_DUMMY_PGPID:
|
|
|
|
generateDummy_FriendPGP();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GXSID_EVENT_DUMMY_UNKNOWN_PGPID:
|
|
|
|
generateDummy_UnknownPGP();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GXSID_EVENT_DUMMY_PSEUDOID:
|
|
|
|
generateDummy_UnknownPseudo();
|
|
|
|
break;
|
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
default:
|
|
|
|
/* error */
|
|
|
|
std::cerr << "p3IdService::handle_event() Unknown Event Type: " << event_type;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|