File: thread.c

package info (click to toggle)
vdr-plugin-epgsync 1.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 160 kB
  • sloc: ansic: 522; makefile: 60; sh: 9
file content (274 lines) | stat: -rw-r--r-- 6,950 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
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
274
#include <vdr/plugin.h>
#include <vdr/epg.h>
#include <vdr/channels.h>
#include <vdr/skins.h>
#include <vdr/i18n.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include "setup.h"
#include "thread.h"

#define EPGSYNC_SLEEPMS 30

bool IsType(const cChannel* Channel, eChannelTypes Type)
{
	if (!Channel)
		return false;

	if (Type == ctAll)
		return true;

	cString source = cSource::ToString(Channel->Source());
	char c = ((const char*) source)[0];
	switch (c) {
		case 'C':
			// analogtv-, pvrinput-, pvrusb2-plugin
			if (Channel->Ca() >= 0xa0 && Channel->Ca() <= 0xa2)
				return Type == ctAnalog;
			else
				return Type == ctDVB_C;
		case 'I':
			return Type == ctIptv;
		case 'S':
			return Type == ctDVB_S;
		case 'T':
			return Type == ctDVB_T;
		case 'V':
			return Type == ctAnalog;
		default:
			return false;
	}
}

#if APIVERSNUM < 20301
cChannel *GetChannelByName(const char* Name, const cChannel *IgnoreChannel = NULL, eChannelTypes Type = ctAll)
#else
const cChannel *GetChannelByName(const char* Name, const cChannel *IgnoreChannel = NULL, eChannelTypes Type = ctAll)
#endif
{
#if APIVERSNUM < 20301
	for (cChannel *channel = Channels.First(); channel; channel = Channels.Next(channel)) {
#else
	LOCK_CHANNELS_READ;
	for (const cChannel *channel = Channels->First(); channel; channel = Channels->Next(channel)) {
#endif
		if (strcasecmp(Name, channel->Name()) == 0 || strcasecmp(Name, channel->ShortName()) == 0) {
			if (IsType(channel, Type) && channel != IgnoreChannel)
				return channel;
		}
	}
	return NULL;
}

void cEpgSyncThread::Action() {
	SetPriority(15);

	plugin = cPluginManager::GetPlugin("svdrpservice");
	if (!plugin) {
		esyslog("EpgSync: Plugin svdrpservice not available");
		return;
	}

	svdrp.handle = -1;

	for (int i = 0; svdrp.handle < 0 && i < EpgSyncSetup.connectAttempts && Running(); i++) {
		if (i > 0)
			cCondWait::SleepMs((1 << i) * 1000);
		svdrp.serverIp = EpgSyncSetup.serverIp;
		svdrp.serverPort = EpgSyncSetup.serverPort;
		svdrp.shared = EpgSyncSetup.channelByChannel;
		plugin->Service("SvdrpConnection-v1.0", &svdrp);
	}

	if (svdrp.handle < 0) {
		Skins.QueueMessage(mtError, tr("EpgSync: Unable to connect to Server"));
		return;
	}

	FILE *f = tmpfile();
	if (!f) {
		esyslog("EpgSync: Unable to open temporary file");
		plugin->Service("SvdrpConnection-v1.0", &svdrp);
		return;
	}

	// Get now and next
	if (EpgSyncSetup.nowNext && CmdLSTE(f, "now") && CmdLSTE(f, "next")) {
		AddSchedule(f);
	}

	if (EpgSyncSetup.channelByChannel) {
		// Get channel by channel
		if (EpgSyncSetup.redirectChannels == rcmId) {
			// Direct import, no mapping:
			// loop through local channels, get channels by ID
#if APIVERSNUM < 20301
			cSchedulesLock *lock = NULL;
			for (cChannel *channel = Channels.First(); channel && Running();
					channel = Channels.Next(channel)) {
				if (!lock)
					lock = new cSchedulesLock();
				if (cSchedules::Schedules(*lock)->GetSchedule(channel)) {
					DELETENULL(lock);
#else
			LOCK_CHANNELS_READ;
			for (const cChannel *channel = Channels->First(); channel && Running();
					channel = Channels->Next(channel)) {
				LOCK_SCHEDULES_READ;
				if (Schedules->GetSchedule(channel)) {
#endif
					if (CmdLSTE(f, *channel->GetChannelID().ToString())) {
						AddSchedule(f);
					}
					cCondWait::SleepMs(EPGSYNC_SLEEPMS);
				}
			}
#if APIVERSNUM < 20301
			DELETENULL(lock);
#endif
		}
		else {
			// Map channels by name:
			// loop through remote channel list, get channels by number
			SvdrpCommand_v1_0 cmd;
			cmd.command = "LSTC\r\n";
			cmd.handle = svdrp.handle;

			plugin->Service("SvdrpCommand-v1.0", &cmd);

			if (cmd.responseCode == 250) {
				for (cLine *line = cmd.reply.First(); line && Running();
						line = cmd.reply.Next(line)) {
					const char* s = line->Text();
					const char* p = strchr(s, ' ');
					if (p && p > s) {
						if (CmdLSTE(f, cString::sprintf("%.*s", (int)(p - s), s))) {
							AddSchedule(f);
						}
						cCondWait::SleepMs(EPGSYNC_SLEEPMS);
					}
					else {
	        				esyslog("EpgSync: LSTC returned channel without number: %s", line->Text());
					}
				}
			}
			else {
				cLine *line = cmd.reply.First();
	        		esyslog("EpgSync: LSTC error %hu %s", cmd.responseCode,
					line ? line->Text() : "");
			}
		}
	}
	else {
		// Get complete epg
		if (CmdLSTE(f))
			AddSchedule(f);
	}
	fclose(f);

	plugin->Service("SvdrpConnection-v1.0", &svdrp);
	cSchedules::Cleanup(true);
	last = time(NULL);
}

bool cEpgSyncThread::CmdLSTE(FILE *f, const char *Arg) {
	SvdrpCommand_v1_0 cmd;
	cmd.command = cString::sprintf("LSTE %s\r\n", Arg ? Arg : "");
	cmd.handle = svdrp.handle;

	if (!Running())
		return false;

	plugin->Service("SvdrpCommand-v1.0", &cmd);
	cLine *line = cmd.reply.First();
	if (cmd.responseCode == 550) {
		// "Channel not defined" or "No schedule found"
		return false;
	}
	else if (cmd.responseCode != 215 || !line) {
	        esyslog("EpgSync: LSTE error %hu %s", cmd.responseCode,
				line ? line->Text() : "");
		return false;
	}

	const cChannel* targetChannel = NULL;
	while (cmd.reply.Next(line)) {
		const char* s = line->Text();
		if (*s == 'C') {
			// new channel begins
			targetChannel = NULL;

			const char* p = skipspace(s + 1);
#if APIVERSNUM < 20301
			cChannel *c = Channels.GetByChannelID(tChannelID::FromString(p));
#else
			LOCK_CHANNELS_READ;
			const cChannel *c = Channels->GetByChannelID(tChannelID::FromString(p));
#endif
			bool cOk = IsType(c, (eChannelTypes) EpgSyncSetup.channelTypes);

			if (cOk && EpgSyncSetup.redirectChannels != rcmNameId) {
				// got acceptable channel by ID
				targetChannel = c;
			}
			else if (EpgSyncSetup.redirectChannels != rcmId) {
				// get channel by name
				p = strchr(p, ' ');
				if (p) {
					// got channel name
					p = skipspace(p);
					targetChannel = GetChannelByName(p, c, (eChannelTypes) EpgSyncSetup.channelTypes);
				}
				// fallback to channel with original ID
				if (!targetChannel && cOk)
					targetChannel = c;
			}

			if (targetChannel) {
				// generate channel header
				if (fputs("C ", f) < 0 ||
						fputs(targetChannel->GetChannelID().ToString(), f) < 0 ||
						fputs(" ", f) < 0 ||
						fputs(targetChannel->Name(), f) < 0 ||
						fputs("\n", f) < 0) {
					LOG_ERROR;
					return false;
				}
			}
		}
		else if (targetChannel) {
			// copy channel data
			if (fputs(s, f) < 0 || fputs("\n", f) < 0) {
				LOG_ERROR;
				return false;
			}
		}
		line = cmd.reply.Next(line);
	}
	return Running();
}

void cEpgSyncThread::AddSchedule(FILE *f) {
	rewind(f);

	if (!cSchedules::Read(f))
		esyslog("EpgSync: Error parsing EPG data");

	rewind(f);
	if (ftruncate(fileno(f), 0) < 0) {
		LOG_ERROR;
	}
}

cEpgSyncThread::cEpgSyncThread(): cThread("epgsync") {
	plugin = NULL;
	// initialized to "now", so no scheduled sync right after VDR start
	// use syncOnStart option instead
	last = time(NULL);
}

cEpgSyncThread::~cEpgSyncThread() {
	Cancel(5);
}