2018-11-10 17:40:34 +01:00
|
|
|
/*******************************************************************************
|
|
|
|
* libretroshare/src/rsserver: p3face-server.cc *
|
|
|
|
* *
|
|
|
|
* libretroshare: retroshare core library *
|
|
|
|
* *
|
2018-11-11 20:42:48 +01:00
|
|
|
* Copyright 2015 by Robert Fernie <retroshare.project@gmail.com> *
|
2018-11-10 17:40:34 +01:00
|
|
|
* *
|
|
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU Lesser General Public License as *
|
|
|
|
* published by the Free Software Foundation, either version 3 of the *
|
|
|
|
* License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program 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 Lesser General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License *
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
|
|
|
|
* *
|
|
|
|
*******************************************************************************/
|
2007-11-15 03:18:48 +00:00
|
|
|
|
2018-01-27 20:22:31 +01:00
|
|
|
#include "util/rstime.h"
|
2007-11-15 03:18:48 +00:00
|
|
|
#include "rsserver/p3face.h"
|
2011-06-16 21:59:26 +00:00
|
|
|
#include "retroshare/rsplugin.h"
|
2008-11-02 11:38:11 +00:00
|
|
|
|
2008-02-28 10:43:33 +00:00
|
|
|
#include "tcponudp/tou.h"
|
2012-04-12 23:29:39 +00:00
|
|
|
#include <unistd.h>
|
2007-11-15 03:18:48 +00:00
|
|
|
|
2010-01-13 20:56:55 +00:00
|
|
|
#include "pqi/authssl.h"
|
2007-11-15 03:18:48 +00:00
|
|
|
#include <sys/time.h>
|
2018-10-07 01:34:05 +02:00
|
|
|
#include "util/rstime.h"
|
2007-11-15 03:18:48 +00:00
|
|
|
|
2011-07-09 18:39:34 +00:00
|
|
|
#include "pqi/p3peermgr.h"
|
|
|
|
#include "pqi/p3linkmgr.h"
|
|
|
|
#include "pqi/p3netmgr.h"
|
|
|
|
|
2011-08-07 22:00:56 +00:00
|
|
|
#include "util/rsdebug.h"
|
|
|
|
|
2019-04-15 00:12:29 +02:00
|
|
|
#include "retroshare/rsevents.h"
|
|
|
|
#include "services/rseventsservice.h"
|
|
|
|
|
2011-07-09 18:39:34 +00:00
|
|
|
|
2008-04-03 12:51:28 +00:00
|
|
|
/****
|
|
|
|
#define DEBUG_TICK 1
|
|
|
|
****/
|
|
|
|
|
2011-08-07 22:00:56 +00:00
|
|
|
#define WARN_BIG_CYCLE_TIME (0.2)
|
2015-05-22 20:54:38 +00:00
|
|
|
#ifdef WINDOWS_SYS
|
2018-10-07 01:34:05 +02:00
|
|
|
#include "util/rstime.h"
|
2015-05-22 20:54:38 +00:00
|
|
|
#include <sys/timeb.h>
|
|
|
|
#endif
|
|
|
|
|
2018-09-19 21:28:26 +02:00
|
|
|
|
|
|
|
/*extern*/ RsControl* rsControl = nullptr;
|
|
|
|
|
2015-05-22 20:54:38 +00:00
|
|
|
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;
|
|
|
|
}
|
2011-08-07 22:00:56 +00:00
|
|
|
|
2016-04-29 18:52:58 -04:00
|
|
|
// These values should be tunable from the GUI, to offer a compromise between speed and CPU use.
|
|
|
|
// In some cases (VOIP) it's likely that we will need to set them temporarily to a very low
|
|
|
|
// value, in order to favor a fast feedback
|
|
|
|
|
2020-04-27 16:08:20 +02:00
|
|
|
const double RsServer::minTickInterval = 0.05;
|
|
|
|
const double RsServer::maxTickInterval = 0.2;
|
2016-03-29 21:22:14 +02:00
|
|
|
|
2011-08-07 22:00:56 +00:00
|
|
|
|
2018-08-30 19:06:20 +02:00
|
|
|
RsServer::RsServer() :
|
2020-04-27 16:08:20 +02:00
|
|
|
coreMutex("RsServer"), mShutdownCallback([](int){}),
|
|
|
|
coreReady(false)
|
2007-11-15 03:18:48 +00:00
|
|
|
{
|
2019-04-15 00:12:29 +02:00
|
|
|
{
|
|
|
|
RsEventsService* tmpRsEvtPtr = new RsEventsService();
|
2019-08-27 11:59:38 +02:00
|
|
|
rsEvents = tmpRsEvtPtr;
|
2019-04-15 00:12:29 +02:00
|
|
|
startServiceThread(tmpRsEvtPtr, "RsEventsService");
|
|
|
|
}
|
|
|
|
|
2014-01-07 22:51:22 +00:00
|
|
|
// This is needed asap.
|
|
|
|
//
|
|
|
|
mNotify = new p3Notify() ;
|
|
|
|
rsNotify = mNotify ;
|
|
|
|
|
2011-07-09 18:39:34 +00:00
|
|
|
mPeerMgr = NULL;
|
|
|
|
mLinkMgr = NULL;
|
|
|
|
mNetMgr = NULL;
|
2015-07-13 11:03:18 +00:00
|
|
|
mHistoryMgr = NULL;
|
2010-08-31 20:00:49 +00:00
|
|
|
|
|
|
|
pqih = NULL;
|
|
|
|
|
2012-08-02 13:17:53 +00:00
|
|
|
mPluginsManager = NULL;
|
|
|
|
|
2010-08-31 20:00:49 +00:00
|
|
|
/* services */
|
2013-09-28 08:09:59 +00:00
|
|
|
mHeart = NULL;
|
|
|
|
mDisc = NULL;
|
2010-08-31 20:00:49 +00:00
|
|
|
msgSrv = NULL;
|
|
|
|
chatSrv = NULL;
|
|
|
|
mStatusSrv = NULL;
|
2020-04-27 16:08:20 +02:00
|
|
|
mGxsTunnels = NULL;
|
2014-01-07 22:51:22 +00:00
|
|
|
|
2020-04-27 16:08:20 +02:00
|
|
|
mLastts = getCurrentTS();
|
|
|
|
mTickInterval = maxTickInterval ;
|
|
|
|
mAvgRunDuration = 0;
|
|
|
|
mLastRunDuration = 0;
|
2015-05-22 20:54:38 +00:00
|
|
|
|
2020-04-27 16:08:20 +02:00
|
|
|
/* caches (that need ticking) */
|
2015-05-22 20:54:38 +00:00
|
|
|
|
2020-04-27 16:08:20 +02:00
|
|
|
/* config */
|
2010-08-31 20:00:49 +00:00
|
|
|
mConfigMgr = NULL;
|
|
|
|
mGeneralConfig = NULL;
|
2007-11-15 03:18:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RsServer::~RsServer()
|
|
|
|
{
|
2017-03-02 02:37:53 +01:00
|
|
|
delete mGxsTrans;
|
2007-11-15 03:18:48 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 16:08:20 +02:00
|
|
|
// General Internal Helper Functions ----> MUST BE LOCKED!
|
2007-11-15 03:18:48 +00:00
|
|
|
|
2019-10-31 18:23:38 +01:00
|
|
|
void RsServer::threadTick()
|
2007-11-15 03:18:48 +00:00
|
|
|
{
|
2020-04-27 16:08:20 +02:00
|
|
|
RsDbg() << "DEBUG_TICK" << std::endl;
|
|
|
|
RsDbg() << "DEBUG_TICK ticking interval "<< mTickInterval << std::endl;
|
2007-11-15 03:18:48 +00:00
|
|
|
|
2020-04-27 16:08:20 +02:00
|
|
|
// we try to tick at a regular interval which depends on the load
|
|
|
|
// if there is time left, we sleep
|
|
|
|
double timeToSleep = mTickInterval - mAvgRunDuration;
|
2008-01-25 07:58:29 +00:00
|
|
|
|
2020-04-27 16:08:20 +02:00
|
|
|
if (timeToSleep > 0)
|
|
|
|
{
|
|
|
|
RsDbg() << "DEBUG_TICK will sleep " << timeToSleep << " ms" << std::endl;
|
|
|
|
rstime::rs_usleep(timeToSleep * 1000000);
|
|
|
|
}
|
2008-11-02 11:38:11 +00:00
|
|
|
|
2020-04-27 16:08:20 +02:00
|
|
|
double ts = getCurrentTS();
|
|
|
|
double delta = ts - mLastts;
|
|
|
|
mLastts = ts;
|
2008-01-25 07:58:29 +00:00
|
|
|
|
2020-04-27 16:08:20 +02:00
|
|
|
// stuff we do always
|
|
|
|
RsDbg() << "DEBUG_TICK ticking server" << std::endl;
|
|
|
|
lockRsCore();
|
|
|
|
int moreToTick = pqih->tick();
|
|
|
|
unlockRsCore();
|
2015-05-22 20:54:38 +00:00
|
|
|
|
2020-04-27 16:08:20 +02:00
|
|
|
// tick the managers
|
|
|
|
RsDbg() << "DEBUG_TICK ticking mPeerMgr" << std::endl;
|
2015-05-22 20:54:38 +00:00
|
|
|
mPeerMgr->tick();
|
2020-04-27 16:08:20 +02:00
|
|
|
RsDbg() << "DEBUG_TICK ticking mLinkMgr" << std::endl;
|
2015-05-22 20:54:38 +00:00
|
|
|
mLinkMgr->tick();
|
2020-04-27 16:08:20 +02:00
|
|
|
RsDbg() << "DEBUG_TICK ticking mNetMgr" << std::endl;
|
2015-05-22 20:54:38 +00:00
|
|
|
mNetMgr->tick();
|
|
|
|
|
|
|
|
|
2020-04-27 16:08:20 +02:00
|
|
|
// stuff we do every second
|
|
|
|
if (delta > 1)
|
2015-05-22 20:54:38 +00:00
|
|
|
{
|
2020-04-27 16:08:20 +02:00
|
|
|
RsDbg() << "DEBUG_TICK every second" << std::endl;
|
|
|
|
// slow services
|
|
|
|
if (rsPlugins)
|
|
|
|
rsPlugins->slowTickPlugins((rstime_t)ts);
|
|
|
|
// UDP keepalive
|
|
|
|
// tou_tick_stunkeepalive();
|
|
|
|
// other stuff to tick
|
|
|
|
// update();
|
|
|
|
}
|
2007-11-15 03:18:48 +00:00
|
|
|
|
2020-04-27 16:08:20 +02:00
|
|
|
// stuff we do every five seconds
|
|
|
|
if (delta > 5)
|
|
|
|
{
|
|
|
|
RsDbg() << "DEBUG_TICK every 5 seconds" << std::endl;
|
|
|
|
// save stuff
|
|
|
|
mConfigMgr->tick();
|
|
|
|
}
|
2011-06-16 21:59:26 +00:00
|
|
|
|
2020-04-27 16:08:20 +02:00
|
|
|
// stuff we do every minute
|
|
|
|
if (delta > 60)
|
|
|
|
{
|
|
|
|
RsDbg() << "DEBUG_TICK 60 seconds" << std::endl;
|
|
|
|
// force saving FileTransferStatus TODO
|
|
|
|
// ftserver->saveFileTransferStatus();
|
|
|
|
// see if we need to resave certs
|
|
|
|
// AuthSSL::getAuthSSL()->CheckSaveCertificates();
|
|
|
|
}
|
2007-11-15 03:18:48 +00:00
|
|
|
|
2020-04-27 16:08:20 +02:00
|
|
|
// stuff we do every hour
|
|
|
|
if (delta > 3600)
|
|
|
|
{
|
|
|
|
RsDbg() << "DEBUG_TICK every hour" << std::endl;
|
|
|
|
}
|
2011-08-07 22:00:56 +00:00
|
|
|
|
2020-04-27 16:08:20 +02:00
|
|
|
// ticking is done, now compute new values of mLastRunDuration, mAvgRunDuration and mTickInterval
|
|
|
|
ts = getCurrentTS();
|
|
|
|
mLastRunDuration = ts - mLastts;
|
|
|
|
mAvgRunDuration = 0.1 * mLastRunDuration + 0.9 * mAvgRunDuration;
|
|
|
|
|
|
|
|
RsDbg() << "DEBUG_TICK new mLastRunDuration " << mLastRunDuration << " mAvgRunDuration " << mAvgRunDuration << std::endl;
|
|
|
|
if (mLastRunDuration > WARN_BIG_CYCLE_TIME)
|
|
|
|
RsDbg() << "DEBUG_TICK excessively long lycle time " << mLastRunDuration << std::endl;
|
|
|
|
|
|
|
|
// if the core has returned that there is more to tick we decrease the ticking interval, else we increse it
|
|
|
|
RsDbg() << "DEBUG_TICK moreToTick " << moreToTick << std::endl;
|
|
|
|
if (moreToTick == 1)
|
|
|
|
mTickInterval = 0.9 * mTickInterval;
|
|
|
|
else
|
|
|
|
mTickInterval = 1.1 * mTickInterval;
|
|
|
|
RsDbg() << "DEBUG_TICK new tick interval " << mTickInterval << std::endl;
|
|
|
|
|
|
|
|
// keep the tick interval within allowed limits
|
|
|
|
if (mTickInterval < minTickInterval)
|
|
|
|
mTickInterval = minTickInterval;
|
|
|
|
else if (mTickInterval > maxTickInterval)
|
|
|
|
mTickInterval = maxTickInterval;
|
|
|
|
RsDbg() << "DEBUG_TICK new tick interval after limiter " << mTickInterval << std::endl;
|
2007-11-15 03:18:48 +00:00
|
|
|
}
|
2020-04-27 16:08:20 +02:00
|
|
|
|