File: Service.cpp

package info (click to toggle)
nzbget 21.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,128 kB
  • sloc: cpp: 62,884; sh: 5,311; python: 1,381; makefile: 491
file content (116 lines) | stat: -rw-r--r-- 2,866 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 *  This file is part of nzbget. See <http://nzbget.net>.
 *
 *  Copyright (C) 2015-2019 Andrey Prygunkov <hugbug@users.sourceforge.net>
 *
 *  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 2 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 <http://www.gnu.org/licenses/>.
 */


#include "nzbget.h"
#include "Service.h"
#include "DownloadInfo.h"
#include "Options.h"
#include "Log.h"
#include "Util.h"

Service::Service()
{
	g_ServiceCoordinator->RegisterService(this);
}

void Service::WakeUp()
{
	g_ServiceCoordinator->WakeUp();
};


ServiceCoordinator::ServiceCoordinator()
{
	debug("Creating ServiceCoordinator");

}

ServiceCoordinator::~ServiceCoordinator()
{
	debug("Destroying ServiceCoordinator");
}

void ServiceCoordinator::Run()
{
	debug("Entering ServiceCoordinator-loop");

	while (!IsStopped())
	{
		// perform service work
		for (Service* service : m_services)
		{
			int serviceInterval = service->ServiceInterval();
			if (serviceInterval >= Service::Now &&
				abs(Util::CurrentTime() - service->m_lastWork) >= serviceInterval)
			{
				service->ServiceWork();
				service->m_lastWork = Util::CurrentTime();
			}
		}

		// calculate wait interval
		int waitInterval = 60 * 60 * 24; // something very large
		time_t curTime = Util::CurrentTime();
		for (Service* service : m_services)
		{
			int serviceInterval = service->ServiceInterval();
			int remaining = Service::Sleep;
			if (serviceInterval >= Service::Now)
			{
				remaining = serviceInterval - (curTime - service->m_lastWork);
				waitInterval = std::min(waitInterval, remaining);
			}
			debug("serviceInterval: %i, remaining: %i", serviceInterval, remaining);
		}

		debug("Waiting in ServiceCoordinator: %i", waitInterval);
		if (waitInterval > 0)
		{
			Guard guard(m_waitMutex);
			m_waitCond.WaitFor(m_waitMutex, waitInterval * 1000, [&] { return m_workenUp || IsStopped(); });
			m_workenUp = false;
		}
	}

	debug("Exiting ServiceCoordinator-loop");
}

void ServiceCoordinator::Stop()
{
	Thread::Stop();
	
	// Resume Run() to exit it
	Guard guard(m_waitMutex);
	m_waitCond.NotifyAll();
}

void ServiceCoordinator::WakeUp()
{
	debug("Waking up ServiceCoordinator");
	// Resume Run()
	Guard guard(m_waitMutex);
	m_workenUp = true;
	m_waitCond.NotifyAll();
}

void ServiceCoordinator::RegisterService(Service* service)
{
	m_services.push_back(service);
}