2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* "$Id: p3face-server.cc,v 1.5 2007-04-15 18:45:23 rmf24 Exp $"
|
|
|
|
*
|
|
|
|
* RetroShare C++ Interface.
|
|
|
|
*
|
|
|
|
* Copyright 2004-2006 by Robert Fernie.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License Version 2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA.
|
|
|
|
*
|
|
|
|
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "rsserver/p3face.h"
|
2008-11-02 06:38:11 -05:00
|
|
|
|
2008-02-28 05:43:33 -05:00
|
|
|
#include "tcponudp/tou.h"
|
2008-02-07 11:18:34 -05:00
|
|
|
#include <sstream>
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2010-01-13 15:56:55 -05:00
|
|
|
#include "pqi/authssl.h"
|
2007-11-14 22:18:48 -05:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
2008-04-03 08:51:28 -04:00
|
|
|
/****
|
|
|
|
#define DEBUG_TICK 1
|
|
|
|
****/
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
RsServer::RsServer(RsIface &i, NotifyBase &callback)
|
|
|
|
:RsControl(i, callback)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RsServer::~RsServer()
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* General Internal Helper Functions
|
|
|
|
----> MUST BE LOCKED!
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef WINDOWS_SYS
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/timeb.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static double getCurrentTS()
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifndef WINDOWS_SYS
|
|
|
|
struct timeval cts_tmp;
|
|
|
|
gettimeofday(&cts_tmp, NULL);
|
|
|
|
double cts = (cts_tmp.tv_sec) + ((double) cts_tmp.tv_usec) / 1000000.0;
|
|
|
|
#else
|
|
|
|
struct _timeb timebuf;
|
|
|
|
_ftime( &timebuf);
|
|
|
|
double cts = (timebuf.time) + ((double) timebuf.millitm) / 1000.0;
|
|
|
|
#endif
|
|
|
|
return cts;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Thread Fn: Run the Core */
|
|
|
|
void RsServer::run()
|
|
|
|
{
|
|
|
|
|
|
|
|
double timeDelta = 0.25;
|
|
|
|
double minTimeDelta = 0.1; // 25;
|
2008-04-04 08:19:50 -04:00
|
|
|
double maxTimeDelta = 0.5;
|
|
|
|
double kickLimit = 0.15;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
double avgTickRate = timeDelta;
|
|
|
|
|
|
|
|
double lastts, ts;
|
|
|
|
lastts = ts = getCurrentTS();
|
|
|
|
|
|
|
|
long lastSec = 0; /* for the slower ticked stuff */
|
|
|
|
|
|
|
|
int min = 0;
|
|
|
|
int loop = 0;
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
#ifndef WINDOWS_SYS
|
|
|
|
usleep((int) (timeDelta * 1000000));
|
|
|
|
#else
|
|
|
|
Sleep((int) (timeDelta * 1000));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ts = getCurrentTS();
|
|
|
|
double delta = ts - lastts;
|
|
|
|
|
|
|
|
/* for the fast ticked stuff */
|
|
|
|
if (delta > timeDelta)
|
|
|
|
{
|
2008-04-03 08:51:28 -04:00
|
|
|
#ifdef DEBUG_TICK
|
|
|
|
std::cerr << "Delta: " << delta << std::endl;
|
|
|
|
std::cerr << "Time Delta: " << timeDelta << std::endl;
|
|
|
|
std::cerr << "Avg Tick Rate: " << avgTickRate << std::endl;
|
|
|
|
#endif
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
lastts = ts;
|
|
|
|
|
|
|
|
/******************************** RUN SERVER *****************/
|
|
|
|
lockRsCore();
|
2008-01-25 02:58:29 -05:00
|
|
|
|
2008-11-02 06:38:11 -05:00
|
|
|
int moreToTick = ftserver -> tick();
|
|
|
|
|
2008-04-03 08:51:28 -04:00
|
|
|
#ifdef DEBUG_TICK
|
2008-11-02 06:38:11 -05:00
|
|
|
std::cerr << "RsServer::run() ftserver->tick(): moreToTick: " << moreToTick << std::endl;
|
2008-04-03 08:51:28 -04:00
|
|
|
#endif
|
2008-01-25 02:58:29 -05:00
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
unlockRsCore();
|
2008-01-25 02:58:29 -05:00
|
|
|
|
|
|
|
/* tick the connection Manager */
|
|
|
|
mConnMgr->tick();
|
2007-11-14 22:18:48 -05:00
|
|
|
/******************************** RUN SERVER *****************/
|
|
|
|
|
|
|
|
/* adjust tick rate depending on whether there is more.
|
|
|
|
*/
|
|
|
|
|
|
|
|
avgTickRate = 0.2 * timeDelta + 0.8 * avgTickRate;
|
|
|
|
|
|
|
|
if (1 == moreToTick)
|
|
|
|
{
|
|
|
|
timeDelta = 0.9 * avgTickRate;
|
|
|
|
if (timeDelta > kickLimit)
|
|
|
|
{
|
|
|
|
/* force next tick in one sec
|
|
|
|
* if we are reading data.
|
|
|
|
*/
|
|
|
|
timeDelta = kickLimit;
|
|
|
|
avgTickRate = kickLimit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
timeDelta = 1.1 * avgTickRate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* limiter */
|
|
|
|
if (timeDelta < minTimeDelta)
|
|
|
|
{
|
|
|
|
timeDelta = minTimeDelta;
|
|
|
|
}
|
|
|
|
else if (timeDelta > maxTimeDelta)
|
|
|
|
{
|
|
|
|
timeDelta = maxTimeDelta;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fast Updates */
|
|
|
|
|
|
|
|
|
|
|
|
/* now we have the slow ticking stuff */
|
|
|
|
/* stuff ticked once a second (but can be slowed down) */
|
|
|
|
if ((int) ts > lastSec)
|
|
|
|
{
|
|
|
|
lastSec = (int) ts;
|
|
|
|
|
2008-02-28 05:43:33 -05:00
|
|
|
// Every second! (UDP keepalive).
|
|
|
|
tou_tick_stunkeepalive();
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
// every five loops (> 5 secs)
|
|
|
|
if (loop % 5 == 0)
|
|
|
|
{
|
|
|
|
// update_quick_stats();
|
|
|
|
|
|
|
|
// Update All Every 5 Seconds.
|
|
|
|
// These Update Functions do the locking themselves.
|
2008-04-03 08:51:28 -04:00
|
|
|
#ifdef DEBUG_TICK
|
|
|
|
std::cerr << "RsServer::run() Updates()" << std::endl;
|
|
|
|
#endif
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
// These two have been completed!
|
|
|
|
//std::cerr << "RsServer::run() UpdateAllCerts()" << std::endl;
|
2008-01-25 02:58:29 -05:00
|
|
|
//UpdateAllCerts();
|
2007-11-14 22:18:48 -05:00
|
|
|
//std::cerr << "RsServer::run() UpdateAllNetwork()" << std::endl;
|
2008-01-25 02:58:29 -05:00
|
|
|
//UpdateAllNetwork();
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
// currently Dummy Functions.
|
|
|
|
//std::cerr << "RsServer::run() UpdateAllTransfers()" << std::endl;
|
2008-11-02 06:38:11 -05:00
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
//std::cerr << "RsServer::run() ";
|
|
|
|
//std::cerr << "UpdateRemotePeople()"<<std::endl;
|
|
|
|
//UpdateRemotePeople();
|
|
|
|
|
|
|
|
//std::cerr << "RsServer::run() UpdateAllFiles()" << std::endl;
|
|
|
|
//UpdateAllFiles();
|
|
|
|
|
|
|
|
//std::cerr << "RsServer::run() UpdateAllConfig()" << std::endl;
|
|
|
|
UpdateAllConfig();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//std::cerr << "RsServer::run() CheckDHT()" << std::endl;
|
2008-01-25 02:58:29 -05:00
|
|
|
//CheckNetworking();
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2008-02-05 08:45:04 -05:00
|
|
|
|
|
|
|
/* Tick slow services */
|
2008-04-03 10:34:52 -04:00
|
|
|
if (mRanking)
|
|
|
|
mRanking->tick();
|
2008-06-04 06:59:24 -04:00
|
|
|
|
2008-06-06 19:40:43 -04:00
|
|
|
|
2008-06-04 06:59:24 -04:00
|
|
|
if(mQblog)
|
2008-06-06 19:40:43 -04:00
|
|
|
mQblog->tick();
|
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
std::string opt;
|
|
|
|
std::string val = "VALUE";
|
|
|
|
{
|
|
|
|
std::ostringstream out;
|
|
|
|
out << "SEC:" << lastSec;
|
|
|
|
opt = out.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
mGeneralConfig->setSetting(opt, val);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mConfigMgr->tick(); /* saves stuff */
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// every 60 loops (> 1 min)
|
|
|
|
if (++loop >= 60)
|
|
|
|
{
|
|
|
|
loop = 0;
|
|
|
|
|
2008-11-13 18:03:46 -05:00
|
|
|
/* force saving FileTransferStatus TODO */
|
2008-11-02 06:38:11 -05:00
|
|
|
//ftserver->saveFileTransferStatus();
|
2008-02-08 07:39:40 -05:00
|
|
|
|
|
|
|
/* see if we need to resave certs */
|
2010-01-13 15:58:58 -05:00
|
|
|
AuthSSL::getAuthSSL()->CheckSaveCertificates();
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
/* hour loop */
|
|
|
|
if (++min >= 60)
|
|
|
|
{
|
|
|
|
min = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// slow update tick as well.
|
|
|
|
// update();
|
|
|
|
} // end of slow tick.
|
|
|
|
|
|
|
|
} // end of only once a second.
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|