-
Notifications
You must be signed in to change notification settings - Fork 0
/
taskman_svdrp.c
273 lines (263 loc) · 6.7 KB
/
taskman_svdrp.c
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
Copyright (C) 2007 Christian Wieninger
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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Or, point your browser to http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
The author can be reached at cwieninger@gmx.de
The project's page is at http://winni.vdr-developer.org/taskman
*/
/*
* taskman_svdrp.c: A plugin for the Video Disk Recorder
*
* See the README file for copyright information and how to reach the author.
*
* $Id$
*/
#include <string>
#include <vdr/plugin.h>
#include "scheduler.h"
#include "task.h"
#include "workerthread.h"
#include "taskmanager_thread.h"
#include "log.h"
using namespace std;
const char **cPluginScheduler::SVDRPHelpPages(void)
{
static const char *HelpPages[] = {
"LSTT [ Id ]\n"
" List all tasks or the one with the given Id",
"NEWT <tasksettings>\n"
" Adds a new task with <tasksettings> as in tasks.conf",
"DELT <taskId>\n"
" Delete the task with given Id",
"EDIT <tasksettings>\n"
" Modify a task with the given new settings",
"RUNC <cmd>\n"
" Starts the given command and returns\n"
" 'task started with id: <taskId>' on successful startup",
"RUNT <taskname>\n"
" Starts an existing task with the given name and returns\n"
" 'task started with id: <taskId>' on successful startup",
"STAT <taskId>\n"
" Returns the execution state of a task given by <taskId>",
NULL
};
return HelpPages;
}
cString cPluginScheduler::SVDRPCommand(const char *Command, const char *Option, int &ReplyCode)
{
if (strcasecmp(Command, "LSTT") == 0)
{
if (*Option)
{
if (isnumber(Option))
{
cTask *Task = Tasks.TaskFromId(atol(Option));
if (Task)
return cString::sprintf("%s", Task->ToText());
else
{
ReplyCode = 901;
return cString::sprintf("task id %s not defined", Option);
}
}
else
{
ReplyCode = 901;
return cString::sprintf("Error in task ID \"%s\"", Option);
}
}
else if (Tasks.Count()>0)
{
string sBuffer;
cMutexLock TasksLock(&Tasks);
for (int i = 0; i < Tasks.Count(); i++)
{
cTask* Task = Tasks.Get(i);
if (Task)
sBuffer += string(Task->ToText()) + string((i<Tasks.Count()-1)?"\n":"");
}
return sBuffer.c_str();
}
else
{
ReplyCode = 901;
return cString("no tasks defined");
}
}
else if (strcasecmp(Command, "NEWT") == 0)
{
if (*Option)
{
cTask* Task = new cTask;;
if (Task->Parse(Option))
{
Task->SetId(Tasks.GetNewId());
LogFile.Log(1,"added task '%s' (%ld) via SVDRP", Task->Name(), Task->Id());
cMutexLock TasksLock(&Tasks);
Tasks.Add(Task);
Tasks.Save();
return cString::sprintf("task '%s' (with new ID %ld) added", Task->Name(), Task->Id());
}
else
{
ReplyCode = 901;
delete Task;
return cString("Error in task settings");
}
}
else
{
ReplyCode = 901;
return cString("missing task settings");
}
}
else if (strcasecmp(Command, "EDIT") == 0)
{
if (*Option)
{
cTask Task;
if (Task.Parse(Option))
{
cTask *taskTemp = Tasks.TaskFromId(Task.Id());
if (taskTemp)
{
taskTemp->Parse(Option);
LogFile.Log(1,"modified task '%s' (%ld) via SVDRP", taskTemp->Name(), taskTemp->Id());
Tasks.Save();
return cString::sprintf("task '%s' with %ld modified", taskTemp->Name(), taskTemp->Id());
}
else
{
ReplyCode = 901;
return cString::sprintf("task id %ld does not exists", Task.Id());
}
}
else
{
ReplyCode = 901;
return cString("Error in task settings");
}
}
else
{
ReplyCode = 901;
return cString("missing task settings");
}
}
else if (strcasecmp(Command, "RUNC") == 0)
{
if (*Option)
{
cTask* tempTask = TempTasks.AddTemp(Option);
if (tempTask && tempTask->Run())
return cString::sprintf("task started with id: %ld", tempTask->Id());
else
{
ReplyCode = 901;
return cString::sprintf("failed to start task '%s'", Option);
}
}
else
{
ReplyCode = 901;
return cString("no task name given");
}
}
else if (strcasecmp(Command, "DELT") == 0)
{
if (*Option)
{
if (isnumber(Option))
{
long taskId = atol(Option);
cTask *Task = Tasks.TaskFromId(atol(Option));
if (Task)
{
if (Task->Active())
{
ReplyCode = 901;
return cString("task is currently executed");
}
LogFile.Log(1,"task '%s' deleted via SVDRP", Task->Name());
cMutexLock TasksLock(&Tasks);
Tasks.Del(Task);
Tasks.Save();
return cString::sprintf("task id %ld deleted", taskId);
}
else
{
ReplyCode = 901;
return cString::sprintf("task id %ld not defined", taskId);
}
}
else
{
ReplyCode = 901;
return cString::sprintf("Error in task ID \"%s\"", Option);
}
}
else
{
ReplyCode = 901;
return cString("missing task ID");
}
}
else if (strcasecmp(Command, "RUNT") == 0)
{
if (*Option)
{
cTask* Task = Tasks.TaskFromName(Option);
if (!Task)
{
ReplyCode = 901;
return cString("task not found");
}
if (Task->Run())
return cString::sprintf("task started with id: %ld", Task->Id());
else
{
ReplyCode = 901;
return cString::sprintf("failed to start task '%s'", Option);
}
}
else
{
ReplyCode = 901;
return cString("no task name given");
}
}
else if (strcasecmp(Command, "STAT") == 0)
{
if (*Option)
{
long Id = atol(Option);
cTask* Task = cTasks::TaskFromId(Id);
cWorkerThread* workerThread = cTaskManagerThread::ThreadById(Id);
if (!Task)
{
ReplyCode = 901;
return cString("task not found");
}
if (workerThread && workerThread->Active())
return cString("task running");
else
return cString("task not running");
}
else
{
ReplyCode = 901;
return cString("no task name given");
}
}
return NULL;
}