summaryrefslogtreecommitdiff
path: root/tasks.h
blob: 841ee43370ff398a9163ca0d3fec849ee0b06098 (plain)
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
#ifndef VDR_LIVE_TASKS_H
#define VDR_LIVE_TASKS_H

#include <memory>
#include <string>
#include <vector>
#include <vdr/channels.h>
#include <vdr/thread.h>

namespace vdrlive {

class Task;

class TaskManager: public cMutex
{
	friend TaskManager& LiveTaskManager();
	friend class StickyTask;

	typedef std::vector< Task* > TaskList;

public:
	bool Execute( Task& task );

	// may only be called from Plugin::MainThreadHook
	void DoScheduledTasks();

private:
	TaskManager();
	TaskManager( TaskManager const& );

	void AddStickyTask( Task& task );
	void RemoveStickyTask( Task& task );

	TaskList m_taskQueue;
	TaskList m_stickyTasks;
	cCondVar m_scheduleWait;
};

class Task
{
	friend void TaskManager::DoScheduledTasks();

public:
	virtual ~Task() {}

	bool Result() const { return m_result; }
	std::string const& Error() const { return m_error; }

protected:
	explicit Task()
		: m_result( true )
		{}
	Task( Task const& );

	void SetError( std::string const& error ) { m_result = false; m_error = error; }

private:
	bool m_result;
	std::string m_error;

	virtual void Action() = 0;
};

class StickyTask: public Task
{
protected:
	explicit StickyTask();
	virtual ~StickyTask();
};

class SwitchChannelTask: public Task
{
public:
	explicit SwitchChannelTask( tChannelID channel ): m_channel( channel ) {}
	
private:
	tChannelID m_channel;

	virtual void Action();
};

class ReplayRecordingTask: public Task
{
public:
	explicit ReplayRecordingTask( std::string const& recording ): m_recording( recording ) {}

private:
	std::string m_recording;
	
	virtual void Action();
};


TaskManager& LiveTaskManager();

} // namespace vdrlive

#endif // VDR_LIVE_TASKS_H