Improved the dht msg history storage.

- specify how long we store for.
 - cleanup old msgs.
 - improve printing of history.
 - add timeline storage as well.
 - disabled by default, enable USE_HISTORY in bdnode.c

There appears to be a bug related to copying bdId's around.
Some of the bootstrap ids are malformed, and this crashes rs.



git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@5724 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
drbob 2012-10-25 22:36:22 +00:00
parent 83795a7c7d
commit cc9e933362
6 changed files with 239 additions and 28 deletions

View file

@ -2,12 +2,15 @@
#include "bitdht/bdhistory.h"
#include "bitdht/bdstddht.h"
#include "bitdht/bdmsgs.h"
#define MIN_RESEND_PERIOD 60
void bdMsgHistoryList::addMsg(time_t ts, uint32_t msgType, bool incoming)
{
// std::cerr << "bdMsgHistoryList::addMsg()";
// std::cerr << std::endl;
uint32_t msg = msgType | (incoming ? MSG_DIRECTION_INCOMING : MSG_DIRECTION_OUTGOING);
msgHistory.insert(std::make_pair(ts, msg));
}
@ -23,9 +26,26 @@ int bdMsgHistoryList::msgCount(time_t start_ts, time_t end_ts)
return count;
}
void bdMsgHistoryList::msgClear()
bool bdMsgHistoryList::msgClear(time_t before)
{
msgHistory.clear();
if (before == 0)
{
msgHistory.clear();
return true;
}
// Delete the old stuff in the list.
while((msgHistory.begin() != msgHistory.end()) && (msgHistory.begin()->first < before))
{
msgHistory.erase(msgHistory.begin());
}
// return true if empty.
if (msgHistory.begin() == msgHistory.end())
{
return true;
}
return false;
}
@ -68,13 +88,25 @@ void bdMsgHistoryList::printHistory(std::ostream &out, int mode, time_t start_ts
if (MSG_DIRECTION_INCOMING & it->second)
{
out << " =>" << it->second - MSG_DIRECTION_INCOMING;
out << " ";
uint32_t type = it->second-MSG_DIRECTION_INCOMING;
out << "( => ";
std::string name;
if (bitdht_msgtype(type, name))
{
out << name;
}
out << " )";
}
else
{
out << " ";
out << it->second - MSG_DIRECTION_OUTGOING << "=> ";
out << "( ";
uint32_t type = it->second - MSG_DIRECTION_OUTGOING;
std::string name;
if (bitdht_msgtype(type, name))
{
out << name;
}
out << " <= )";
}
}
@ -146,11 +178,25 @@ bool bdMsgHistoryList::validPeer()
return false;
}
bdHistory::bdHistory(time_t store_period)
:mStorePeriod(store_period) { return; }
void bdHistory::addMsg(const bdId *id, bdToken * /*transId*/, uint32_t msgType, bool incoming)
{
//std::cerr << "bdHistory::addMsg() ";
//bdStdPrintId(std::cerr, id);
//std::cerr << std::endl;
time_t now = time(NULL);
std::map<bdId, bdMsgHistoryList>::iterator it;
bdMsgHistoryList &histRef = mHistory[*id]; /* will instaniate empty */
histRef.addMsg(time(NULL), msgType, incoming);
histRef.addMsg(now, msgType, incoming);
/* add to mMsgTimeline */
mMsgTimeline.insert(std::make_pair(now, MsgRegister(id, msgType, incoming)));
}
void bdHistory::printMsgs()
@ -158,6 +204,10 @@ void bdHistory::printMsgs()
/* print and clear msgs */
std::ostream &out = std::cerr;
std::cerr << "bdHistory::printMsgs()";
std::cerr << std::endl;
std::map<bdId, bdMsgHistoryList> ::iterator it;
for(it = mHistory.begin(); it != mHistory.end(); it++)
{
@ -171,8 +221,78 @@ void bdHistory::printMsgs()
it->second.printHistory(out, 0, 0, time(NULL));
}
}
out << "Msg Timeline:";
time_t now = time(NULL);
std::multimap<time_t, MsgRegister>::iterator hit;
for(hit = mMsgTimeline.begin(); hit != mMsgTimeline.end(); hit++)
{
out << now - hit->first << " ";
bdStdPrintId(out, &(hit->second.id));
if (hit->second.incoming)
{
out << " => ";
}
else
{
out << " <= ";
}
std::string name;
if (bitdht_msgtype(hit->second.msgType, name))
{
out << name;
}
else
{
out << "UNKNOWN MSG";
}
out << std::endl;
}
}
void bdHistory::cleanupOldMsgs()
{
std::cerr << "bdHistory::cleanupOldMsgs()";
std::cerr << std::endl;
if (mStorePeriod == 0)
{
return; // no cleanup
}
std::list<bdId> to_cleanup;
std::list<bdId>::iterator cit;
time_t before = time(NULL) - mStorePeriod;
// Delete the old stuff in the list.
while((mMsgTimeline.begin() != mMsgTimeline.end()) && (mMsgTimeline.begin()->first < before))
{
std::multimap<time_t, MsgRegister>::iterator it = mMsgTimeline.begin();
to_cleanup.push_back(it->second.id);
mMsgTimeline.erase(it);
}
// remove old msgs, delete entry if its empty.
std::map<bdId, bdMsgHistoryList>::iterator hit;
for(cit = to_cleanup.begin(); cit != to_cleanup.end(); cit++)
{
hit = mHistory.find(*cit);
if (hit != mHistory.end())
{
if (hit->second.msgClear(before))
{
mHistory.erase(hit);
}
}
}
}
void bdHistory::clearHistory()
{
mHistory.clear();
@ -180,7 +300,6 @@ void bdHistory::clearHistory()
bool bdHistory::canSend(const bdId *id)
{
std::map<bdId, bdMsgHistoryList> ::iterator it;
it = mHistory.find(*id);
if (it != mHistory.end())