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"
|
2011-06-16 17:59:26 -04:00
|
|
|
#include "retroshare/rsplugin.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>
|
|
|
|
|
2011-07-09 14:39:34 -04:00
|
|
|
#include "pqi/p3peermgr.h"
|
|
|
|
#include "pqi/p3linkmgr.h"
|
|
|
|
#include "pqi/p3netmgr.h"
|
|
|
|
|
2011-08-07 18:00:56 -04:00
|
|
|
int rsserverzone = 101;
|
|
|
|
|
|
|
|
#include "util/rsdebug.h"
|
|
|
|
|
2011-07-09 14:39:34 -04:00
|
|
|
|
2008-04-03 08:51:28 -04:00
|
|
|
/****
|
|
|
|
#define DEBUG_TICK 1
|
|
|
|
****/
|
|
|
|
|
2011-08-07 18:00:56 -04:00
|
|
|
#define WARN_BIG_CYCLE_TIME (0.2)
|
|
|
|
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
RsServer::RsServer(RsIface &i, NotifyBase &callback)
|
2011-07-04 18:59:39 -04:00
|
|
|
:RsControl(i, callback), coreMutex("RsServer")
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2010-08-31 16:00:49 -04:00
|
|
|
ftserver = NULL;
|
|
|
|
|
2011-07-09 14:39:34 -04:00
|
|
|
mPeerMgr = NULL;
|
|
|
|
mLinkMgr = NULL;
|
|
|
|
mNetMgr = NULL;
|
2010-08-31 16:00:49 -04:00
|
|
|
|
|
|
|
pqih = NULL;
|
|
|
|
|
|
|
|
/* services */
|
|
|
|
ad = NULL;
|
|
|
|
msgSrv = NULL;
|
|
|
|
chatSrv = NULL;
|
|
|
|
mStatusSrv = NULL;
|
|
|
|
mChannels = NULL;
|
2011-04-03 12:20:47 -04:00
|
|
|
mForums = NULL;
|
2010-08-31 16:00:49 -04:00
|
|
|
/* caches (that need ticking) */
|
|
|
|
|
|
|
|
/* Config */
|
|
|
|
mConfigMgr = NULL;
|
|
|
|
mGeneralConfig = NULL;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
|
2011-06-16 17:59:26 -04:00
|
|
|
double timeDelta = 0.25;
|
|
|
|
double minTimeDelta = 0.1; // 25;
|
|
|
|
double maxTimeDelta = 0.5;
|
|
|
|
double kickLimit = 0.15;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2011-06-16 17:59:26 -04:00
|
|
|
double avgTickRate = timeDelta;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2011-06-16 17:59:26 -04:00
|
|
|
double lastts, ts;
|
|
|
|
lastts = ts = getCurrentTS();
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2011-06-16 17:59:26 -04:00
|
|
|
long lastSec = 0; /* for the slower ticked stuff */
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2011-06-16 17:59:26 -04:00
|
|
|
int min = 0;
|
|
|
|
int loop = 0;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2011-06-16 17:59:26 -04:00
|
|
|
while(isRunning())
|
|
|
|
{
|
2007-11-14 22:18:48 -05:00
|
|
|
#ifndef WINDOWS_SYS
|
2011-10-08 13:47:36 -04:00
|
|
|
usleep((int) (timeDelta * 1000000));
|
2007-11-14 22:18:48 -05:00
|
|
|
#else
|
2011-10-08 13:47:36 -04:00
|
|
|
Sleep((int) (timeDelta * 1000));
|
2007-11-14 22:18:48 -05:00
|
|
|
#endif
|
|
|
|
|
2011-06-16 17:59:26 -04:00
|
|
|
ts = getCurrentTS();
|
|
|
|
double delta = ts - lastts;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2011-06-16 17:59:26 -04:00
|
|
|
/* for the fast ticked stuff */
|
|
|
|
if (delta > timeDelta)
|
|
|
|
{
|
2008-04-03 08:51:28 -04:00
|
|
|
#ifdef DEBUG_TICK
|
2011-06-16 17:59:26 -04:00
|
|
|
std::cerr << "Delta: " << delta << std::endl;
|
|
|
|
std::cerr << "Time Delta: " << timeDelta << std::endl;
|
|
|
|
std::cerr << "Avg Tick Rate: " << avgTickRate << std::endl;
|
2008-04-03 08:51:28 -04:00
|
|
|
#endif
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2011-06-16 17:59:26 -04:00
|
|
|
lastts = ts;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2011-06-16 17:59:26 -04:00
|
|
|
/******************************** RUN SERVER *****************/
|
|
|
|
lockRsCore();
|
2008-01-25 02:58:29 -05:00
|
|
|
|
2011-06-16 17:59:26 -04:00
|
|
|
int moreToTick = ftserver -> tick();
|
2008-11-02 06:38:11 -05:00
|
|
|
|
2008-04-03 08:51:28 -04:00
|
|
|
#ifdef DEBUG_TICK
|
2011-06-16 17:59:26 -04: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
|
|
|
|
2011-06-16 17:59:26 -04:00
|
|
|
unlockRsCore();
|
2008-01-25 02:58:29 -05:00
|
|
|
|
2011-07-09 14:39:34 -04:00
|
|
|
/* tick the Managers */
|
|
|
|
mPeerMgr->tick();
|
|
|
|
mLinkMgr->tick();
|
|
|
|
mNetMgr->tick();
|
2011-06-16 17:59:26 -04:00
|
|
|
/******************************** RUN SERVER *****************/
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2011-06-16 17:59:26 -04:00
|
|
|
/* adjust tick rate depending on whether there is more.
|
|
|
|
*/
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2011-06-16 17:59:26 -04:00
|
|
|
avgTickRate = 0.2 * timeDelta + 0.8 * avgTickRate;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2011-06-16 17:59:26 -04:00
|
|
|
if (1 == moreToTick)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2011-06-16 17:59:26 -04:00
|
|
|
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;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
2011-06-16 17:59:26 -04:00
|
|
|
/* limiter */
|
|
|
|
if (timeDelta < minTimeDelta)
|
|
|
|
{
|
|
|
|
timeDelta = minTimeDelta;
|
|
|
|
}
|
|
|
|
else if (timeDelta > maxTimeDelta)
|
|
|
|
{
|
|
|
|
timeDelta = maxTimeDelta;
|
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2011-06-16 17:59:26 -04:00
|
|
|
/* Fast Updates */
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
|
2011-06-16 17:59:26 -04:00
|
|
|
/* now we have the slow ticking stuff */
|
|
|
|
/* stuff ticked once a second (but can be slowed down) */
|
|
|
|
if ((int) ts > lastSec)
|
|
|
|
{
|
|
|
|
lastSec = (int) ts;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2011-06-16 17:59:26 -04:00
|
|
|
// Every second! (UDP keepalive).
|
|
|
|
//tou_tick_stunkeepalive();
|
2008-02-28 05:43:33 -05:00
|
|
|
|
2011-06-16 17:59:26 -04:00
|
|
|
// every five loops (> 5 secs)
|
|
|
|
if (loop % 5 == 0)
|
|
|
|
{
|
|
|
|
// update_quick_stats();
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2011-06-16 17:59:26 -04:00
|
|
|
// Update All Every 5 Seconds.
|
|
|
|
// These Update Functions do the locking themselves.
|
2008-04-03 08:51:28 -04:00
|
|
|
#ifdef DEBUG_TICK
|
2011-06-16 17:59:26 -04:00
|
|
|
std::cerr << "RsServer::run() Updates()" << std::endl;
|
2008-04-03 08:51:28 -04:00
|
|
|
#endif
|
2011-06-16 17:59:26 -04:00
|
|
|
UpdateAllConfig();
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
|
2011-06-16 17:59:26 -04:00
|
|
|
mConfigMgr->tick(); /* saves stuff */
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
}
|
|
|
|
|
2011-06-16 17:59:26 -04:00
|
|
|
// every 60 loops (> 1 min)
|
|
|
|
if (++loop >= 60)
|
|
|
|
{
|
|
|
|
loop = 0;
|
2008-02-07 11:18:34 -05:00
|
|
|
|
2011-06-16 17:59:26 -04:00
|
|
|
/* force saving FileTransferStatus TODO */
|
|
|
|
//ftserver->saveFileTransferStatus();
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2011-06-16 17:59:26 -04:00
|
|
|
/* see if we need to resave certs */
|
|
|
|
//AuthSSL::getAuthSSL()->CheckSaveCertificates();
|
2008-02-08 07:39:40 -05:00
|
|
|
|
2011-06-16 17:59:26 -04:00
|
|
|
/* hour loop */
|
|
|
|
if (++min >= 60)
|
|
|
|
{
|
|
|
|
min = 0;
|
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
2011-06-16 17:59:26 -04:00
|
|
|
/* Tick slow services */
|
|
|
|
if(rsPlugins)
|
|
|
|
rsPlugins->slowTickPlugins((time_t)ts);
|
|
|
|
|
|
|
|
// slow update tick as well.
|
|
|
|
// update();
|
|
|
|
} // end of slow tick.
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2011-06-16 17:59:26 -04:00
|
|
|
} // end of only once a second.
|
2011-08-07 18:00:56 -04:00
|
|
|
|
|
|
|
double endCycleTs = getCurrentTS();
|
|
|
|
double cycleTime = endCycleTs - ts;
|
|
|
|
if (cycleTime > WARN_BIG_CYCLE_TIME)
|
|
|
|
{
|
|
|
|
std::ostringstream out;
|
|
|
|
out << "RsServer::run() WARNING Excessively Long Cycle Time: " << cycleTime;
|
|
|
|
out << " secs => Please DEBUG";
|
|
|
|
std::cerr << out.str() << std::endl;
|
|
|
|
|
|
|
|
rslog(RSL_ALERT, rsserverzone, out.str());
|
|
|
|
}
|
2011-06-16 17:59:26 -04:00
|
|
|
}
|
|
|
|
return;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|