/*************************************************************************
* UrBackup - Client/Server backup system
* Copyright (C) 2011-2014 Martin Raiber
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
**************************************************************************/
#ifndef CLIENT_ONLY
#include "server_status.h"
#include "../Interface/Server.h"
#include "action_header.h"
#include
IMutex *ServerStatus::mutex=NULL;
std::map ServerStatus::status;
int64 ServerStatus::last_status_update;
int ServerStatus::server_nospc_stalled=0;
bool ServerStatus::server_nospc_fatal=false;
const unsigned int inactive_time_const=30*60*1000;
void ServerStatus::init_mutex(void)
{
mutex=Server->createMutex();
last_status_update=Server->getTimeMS();
}
void ServerStatus::destroy_mutex(void)
{
Server->destroy(mutex);
}
void ServerStatus::setServerStatus(const SStatus &pStatus, bool setactive)
{
IScopedLock lock(mutex);
SStatus *s=&status[pStatus.client];
s->hashqueuesize=pStatus.hashqueuesize;
s->prepare_hashqueuesize=pStatus.prepare_hashqueuesize;
s->starttime=pStatus.starttime;
s->pcdone=pStatus.pcdone;
s->has_status=true;
s->statusaction=pStatus.statusaction;
s->clientid=pStatus.clientid;
s->eta_ms = pStatus.eta_ms;
s->eta_set_time = pStatus.eta_set_time;
if(setactive)
{
last_status_update=Server->getTimeMS();
}
}
void ServerStatus::updateActive(void)
{
IScopedLock lock(mutex);
last_status_update=Server->getTimeMS();
}
void ServerStatus::setOnline(const std::wstring &clientname, bool bonline)
{
IScopedLock lock(mutex);
SStatus *s=&status[clientname];
if(bonline)
{
*s=SStatus();
}
s->online=bonline;
s->client=clientname;
s->done=false;
s->has_status=true;
s->r_online=bonline;
if(bonline)
{
last_status_update=Server->getTimeMS();
}
}
void ServerStatus::setROnline(const std::wstring &clientname, bool bonline)
{
IScopedLock lock(mutex);
SStatus *s=&status[clientname];
s->r_online=bonline;
if(bonline)
{
last_status_update=Server->getTimeMS();
}
}
void ServerStatus::setDone(const std::wstring &clientname, bool bdone)
{
IScopedLock lock(mutex);
SStatus *s=&status[clientname];
s->done=bdone;
}
void ServerStatus::setIP(const std::wstring &clientname, unsigned int ip)
{
IScopedLock lock(mutex);
SStatus *s=&status[clientname];
s->ip_addr=ip;
}
void ServerStatus::setStatusError(const std::wstring &clientname, SStatusError se)
{
IScopedLock lock(mutex);
SStatus *s=&status[clientname];
s->status_error=se;
}
void ServerStatus::setCommPipe(const std::wstring &clientname, IPipe *p)
{
IScopedLock lock(mutex);
SStatus *s=&status[clientname];
s->comm_pipe=p;
}
void ServerStatus::stopBackup(const std::wstring &clientname, bool b)
{
IScopedLock lock(mutex);
SStatus *s=&status[clientname];
s->stop_backup=b;
}
bool ServerStatus::isBackupStopped(const std::wstring &clientname)
{
IScopedLock lock(mutex);
SStatus *s=&status[clientname];
return s->stop_backup;
}
std::vector ServerStatus::getStatus(void)
{
IScopedLock lock(mutex);
std::vector ret;
for(std::map::iterator it=status.begin();it!=status.end();++it)
{
ret.push_back(it->second);
}
return ret;
}
SStatus ServerStatus::getStatus(const std::wstring &clientname)
{
IScopedLock lock(mutex);
std::map::iterator iter=status.find(clientname);
if(iter!=status.end())
return iter->second;
else
return SStatus();
}
bool ServerStatus::isActive(void)
{
IScopedLock lock(mutex);
if( Server->getTimeMS()-last_status_update>inactive_time_const)
{
return false;
}
else
{
return true;
}
}
void ServerStatus::incrementServerNospcStalled(int add)
{
IScopedLock lock(mutex);
server_nospc_stalled+=add;
}
void ServerStatus::setServerNospcFatal(bool b)
{
IScopedLock lock(mutex);
server_nospc_fatal=b;
}
int ServerStatus::getServerNospcStalled(void)
{
IScopedLock lock(mutex);
return server_nospc_stalled;
}
bool ServerStatus::getServerNospcFatal(void)
{
IScopedLock lock(mutex);
return server_nospc_fatal;
}
void ServerStatus::setClientVersionString(const std::wstring &clientname, const std::string& client_version_string)
{
IScopedLock lock(mutex);
SStatus *s=&status[clientname];
s->client_version_string=client_version_string;
}
void ServerStatus::setOSVersionString(const std::wstring &clientname, const std::string& os_version_string)
{
IScopedLock lock(mutex);
SStatus *s=&status[clientname];
s->os_version_string=os_version_string;
}
ACTION_IMPL(server_status)
{
#ifndef _DEBUG
Server->Write(tid, "Forbidden");
#else
ITemplate *tmpl=Server->createTemplate("urbackup/status.htm");
std::vector status=ServerStatus::getStatus();
tmpl->getTable(L"CLIENTS");
for(size_t i=0;igetTable(L"CLIENTS."+convert(i));
tab->addString(L"NAME", status[i].client );
if(status[i].has_status)
{
tab->addString(L"PCDONE", convert(status[i].pcdone) );
tab->addString(L"HASHQUEUESIZE", convert(status[i].hashqueuesize) );
tab->addString(L"HASHQUEUE_PREPARE", convert(status[i].prepare_hashqueuesize) );
tab->addString(L"ONLINE", convert(status[i].online) );
time_t tt=(time_t)(time(0)-((Server->getTimeMS()-status[i].starttime)/1000));
#ifdef _WIN32
struct tm ttm;
localtime_s(&ttm, &tt);
char buf[1000];
strftime(buf, 1000, "%d.%m.%Y %H:%M", &ttm);
#else
struct tm *ttm=localtime(&tt);
char buf[1000];
strftime(buf, 1000, "%d.%m.%Y %H:%M", ttm);
#endif
tab->addString(L"STARTTIME", widen((std::string)buf) );
tab->addString(L"DONE", convert(status[i].done) );
std::wstring action;
switch(status[i].statusaction)
{
case sa_none:
action=L"none"; break;
case sa_incr_file:
action=L"incr_file"; break;
case sa_full_file:
action=L"full_file"; break;
case sa_incr_image:
action=L"incr_image"; break;
case sa_full_image:
action=L"full_image"; break;
case sa_resume_full_file:
action=L"resume_full_file"; break;
case sa_resume_incr_file:
action=L"resume_incr_file"; break;
}
tab->addString(L"ACTION", action);
}
else
{
tab->addString(L"PCDONE", L" ");
tab->addString(L"HASHQUEUESIZE", L" ");
tab->addString(L"HASHQUEUE_PREPARE", L" ");
tab->addString(L"ONLINE", convert(status[i].online));
tab->addString(L"STARTTIME", L" ");
tab->addString(L"DONE", L" ");
tab->addString(L"ACTION", L" ");
}
}
}
Server->Write(tid, tmpl->getData());
Server->destroy(tmpl);
#endif
}
#endif //CLIENT_ONLY