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
|
|
|
|
|
2016-04-26 23:42:44 -04:00
|
|
|
const double RsServer::minTimeDelta = 0.05; // 25;
|
2016-04-29 18:52:58 -04:00
|
|
|
const double RsServer::maxTimeDelta = 0.2;
|
2016-03-29 21:22:14 +02:00
|
|
|
const double RsServer::kickLimit = 0.15;
|
|
|
|
|
2011-08-07 22:00:56 +00:00
|
|
|
|
2018-08-30 19:06:20 +02:00
|
|
|
RsServer::RsServer() :
|
2018-09-21 01:39:48 +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;
|
2016-01-01 11:49:03 +01:00
|
|
|
mGxsTunnels = NULL;
|
2014-01-07 22:51:22 +00:00
|
|
|
|
2015-05-22 20:54:38 +00:00
|
|
|
mMin = 0;
|
|
|
|
mLoop = 0;
|
|
|
|
|
|
|
|
|
|
|
|
mLastts = getCurrentTS();
|
|
|
|
mLastSec = 0; /* for the slower ticked stuff */
|
|
|
|
mTimeDelta = 0.25 ;
|
|
|
|
|
2015-05-30 21:18:10 +00:00
|
|
|
mAvgTickRate = mTimeDelta;
|
|
|
|
|
2015-05-22 20:54:38 +00:00
|
|
|
/* caches (that need ticking) */
|
2010-08-31 20:00:49 +00:00
|
|
|
|
|
|
|
/* Config */
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/* General Internal Helper Functions
|
|
|
|
----> MUST BE LOCKED!
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Thread Fn: Run the Core */
|
2019-10-31 18:23:38 +01:00
|
|
|
void RsServer::threadTick()
|
2007-11-15 03:18:48 +00:00
|
|
|
{
|
2018-01-27 20:22:31 +01:00
|
|
|
rstime::rs_usleep(mTimeDelta * 1000000);
|
2007-11-15 03:18:48 +00:00
|
|
|
|
2015-05-22 20:54:38 +00:00
|
|
|
double ts = getCurrentTS();
|
|
|
|
double delta = ts - mLastts;
|
2016-04-26 23:42:44 -04:00
|
|
|
|
2015-05-22 20:54:38 +00:00
|
|
|
/* for the fast ticked stuff */
|
|
|
|
if (delta > mTimeDelta)
|
|
|
|
{
|
2008-04-03 12:51:28 +00:00
|
|
|
#ifdef DEBUG_TICK
|
2015-05-22 20:54:38 +00:00
|
|
|
std::cerr << "Delta: " << delta << std::endl;
|
|
|
|
std::cerr << "Time Delta: " << mTimeDelta << std::endl;
|
|
|
|
std::cerr << "Avg Tick Rate: " << mAvgTickRate << std::endl;
|
2008-04-03 12:51:28 +00:00
|
|
|
#endif
|
2007-11-15 03:18:48 +00:00
|
|
|
|
2015-05-22 20:54:38 +00:00
|
|
|
mLastts = ts;
|
2007-11-15 03:18:48 +00:00
|
|
|
|
2015-05-22 20:54:38 +00:00
|
|
|
/******************************** RUN SERVER *****************/
|
|
|
|
lockRsCore();
|
2008-01-25 07:58:29 +00:00
|
|
|
|
2015-05-22 20:54:38 +00:00
|
|
|
int moreToTick = pqih->tick();
|
2008-11-02 11:38:11 +00:00
|
|
|
|
2008-04-03 12:51:28 +00:00
|
|
|
#ifdef DEBUG_TICK
|
2015-05-22 20:54:38 +00:00
|
|
|
std::cerr << "RsServer::run() ftserver->tick(): moreToTick: " << moreToTick << std::endl;
|
2008-04-03 12:51:28 +00:00
|
|
|
#endif
|
2008-01-25 07:58:29 +00:00
|
|
|
|
2015-05-22 20:54:38 +00:00
|
|
|
unlockRsCore();
|
|
|
|
|
|
|
|
/* tick the Managers */
|
|
|
|
mPeerMgr->tick();
|
|
|
|
mLinkMgr->tick();
|
|
|
|
mNetMgr->tick();
|
|
|
|
/******************************** RUN SERVER *****************/
|
|
|
|
|
|
|
|
/* adjust tick rate depending on whether there is more.
|
|
|
|
*/
|
|
|
|
|
|
|
|
mAvgTickRate = 0.2 * mTimeDelta + 0.8 * mAvgTickRate;
|
|
|
|
|
|
|
|
if (1 == moreToTick)
|
|
|
|
{
|
|
|
|
mTimeDelta = 0.9 * mAvgTickRate;
|
|
|
|
if (mTimeDelta > kickLimit)
|
|
|
|
{
|
|
|
|
/* force next tick in one sec
|
|
|
|
* if we are reading data.
|
|
|
|
*/
|
|
|
|
mTimeDelta = kickLimit;
|
|
|
|
mAvgTickRate = kickLimit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mTimeDelta = 1.1 * mAvgTickRate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* limiter */
|
|
|
|
if (mTimeDelta < minTimeDelta)
|
|
|
|
{
|
|
|
|
mTimeDelta = minTimeDelta;
|
|
|
|
}
|
|
|
|
else if (mTimeDelta > maxTimeDelta)
|
|
|
|
{
|
|
|
|
mTimeDelta = maxTimeDelta;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fast Updates */
|
|
|
|
|
|
|
|
|
|
|
|
/* now we have the slow ticking stuff */
|
|
|
|
/* stuff ticked once a second (but can be slowed down) */
|
|
|
|
if ((int) ts > mLastSec)
|
|
|
|
{
|
|
|
|
mLastSec = (int) ts;
|
|
|
|
|
|
|
|
// Every second! (UDP keepalive).
|
|
|
|
//tou_tick_stunkeepalive();
|
|
|
|
|
|
|
|
// every five loops (> 5 secs)
|
|
|
|
if (mLoop % 5 == 0)
|
|
|
|
{
|
|
|
|
// update_quick_stats();
|
|
|
|
|
|
|
|
// Update All Every 5 Seconds.
|
|
|
|
// These Update Functions do the locking themselves.
|
2008-04-03 12:51:28 +00:00
|
|
|
#ifdef DEBUG_TICK
|
2015-05-22 20:54:38 +00:00
|
|
|
std::cerr << "RsServer::run() Updates()" << std::endl;
|
2008-04-03 12:51:28 +00:00
|
|
|
#endif
|
2007-11-15 03:18:48 +00:00
|
|
|
|
2015-05-22 20:54:38 +00:00
|
|
|
mConfigMgr->tick(); /* saves stuff */
|
2007-11-15 03:18:48 +00:00
|
|
|
|
2015-05-22 20:54:38 +00:00
|
|
|
}
|
2008-02-07 16:18:34 +00:00
|
|
|
|
2015-05-22 20:54:38 +00:00
|
|
|
// every 60 loops (> 1 min)
|
|
|
|
if (++mLoop >= 60)
|
|
|
|
{
|
|
|
|
mLoop = 0;
|
2008-02-07 16:18:34 +00:00
|
|
|
|
2015-05-22 20:54:38 +00:00
|
|
|
/* force saving FileTransferStatus TODO */
|
|
|
|
//ftserver->saveFileTransferStatus();
|
2007-11-15 03:18:48 +00:00
|
|
|
|
2015-05-22 20:54:38 +00:00
|
|
|
/* see if we need to resave certs */
|
|
|
|
//AuthSSL::getAuthSSL()->CheckSaveCertificates();
|
2008-02-08 12:39:40 +00:00
|
|
|
|
2015-05-22 20:54:38 +00:00
|
|
|
/* hour loop */
|
|
|
|
if (++mMin >= 60)
|
|
|
|
{
|
|
|
|
mMin = 0;
|
|
|
|
}
|
|
|
|
}
|
2007-11-15 03:18:48 +00:00
|
|
|
|
2015-05-22 20:54:38 +00:00
|
|
|
/* Tick slow services */
|
|
|
|
if(rsPlugins)
|
2018-10-07 01:34:05 +02:00
|
|
|
rsPlugins->slowTickPlugins((rstime_t)ts);
|
2011-06-16 21:59:26 +00:00
|
|
|
|
2015-05-22 20:54:38 +00:00
|
|
|
// slow update tick as well.
|
|
|
|
// update();
|
|
|
|
} // end of slow tick.
|
2007-11-15 03:18:48 +00:00
|
|
|
|
2015-05-22 20:54:38 +00:00
|
|
|
} // end of only once a second.
|
2011-08-07 22:00:56 +00:00
|
|
|
|
2015-06-04 20:26:57 +00:00
|
|
|
#ifdef DEBUG_TICK
|
2015-05-22 20:54:38 +00:00
|
|
|
double endCycleTs = getCurrentTS();
|
|
|
|
double cycleTime = endCycleTs - ts;
|
|
|
|
if (cycleTime > WARN_BIG_CYCLE_TIME)
|
|
|
|
{
|
|
|
|
std::string out;
|
|
|
|
rs_sprintf(out, "RsServer::run() WARNING Excessively Long Cycle Time: %g secs => Please DEBUG", cycleTime);
|
|
|
|
std::cerr << out << std::endl;
|
|
|
|
}
|
2015-06-04 20:26:57 +00:00
|
|
|
#endif
|
2007-11-15 03:18:48 +00:00
|
|
|
}
|