Skip to content

Commit 071b826

Browse files
committed
new SVDRP command CHKU
1 parent eeef9bf commit 071b826

File tree

6 files changed

+87
-29
lines changed

6 files changed

+87
-29
lines changed

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ OBJS = $(PLUGIN).o common.o config.o device.o pidscanner.o \
8282
protocolcurl.o protocolext.o protocolfile.o protocolhttp.o \
8383
protocoludp.o sectionfilter.o setup.o sidscanner.o socket.o \
8484
protocolm3u.o protocolradio.o protocolstream.o \
85-
m3u8handler.o process.o process_unix.o ffmpeghandler.o streambasehandler.o \
86-
vlchandler.o source.o statistics.o streamer.o radioimage.o
85+
m3u8handler.o process.o process_unix.o streambasehandler.o \
86+
source.o statistics.o streamer.o radioimage.o checkurl.o
8787

8888
### The main target:
8989

checkurl.cpp

+30-14
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,41 @@ CheckURL::CheckURL() {
1717
}
1818

1919
CheckURL::~CheckURL() {
20-
abort();
20+
stop();
2121
}
2222

2323
void CheckURL::start() {
2424
if (isRunning) {
2525
return;
2626
}
2727

28+
countChannels = 0;
29+
checkedChannels = 0;
30+
currentChannelId = "";
31+
currentChannelUrl = "";
32+
2833
isRunning = true;
2934
checkThread = std::thread(&CheckURL::executeChecks, this);
3035
}
3136

32-
void CheckURL::abort() {
37+
void CheckURL::stop() {
38+
if (!isRunning) {
39+
return;
40+
}
41+
3342
isRunning = false;
34-
checkThread.join();
43+
if (checkThread.joinable()) {
44+
checkThread.join();
45+
}
3546
}
3647

48+
cString CheckURL::status() {
49+
if (!isRunning) {
50+
return { "Check is not running or finished" };
51+
}
52+
53+
return cString::sprintf("%d / %d finished, Currently testing %s, %s", getCheckedChannels(), getCountChannels(), *getCurrentChannelId(), *getCurrentChannelUrl());
54+
}
3755

3856
static size_t check_header_callback(char *buffer, size_t size, size_t nitems, void *userdata)
3957
{
@@ -91,6 +109,8 @@ void CheckURL::executeChecks() {
91109
}
92110
}
93111

112+
countChannels = channels.size();
113+
94114
// iterate over all channels and checks the URL
95115
CURLcode ret;
96116
CURL *hnd = curl_easy_init();
@@ -108,6 +128,11 @@ void CheckURL::executeChecks() {
108128
if (!isRunning) {
109129
break;
110130
}
131+
132+
checkedChannels++;
133+
currentChannelId = a.first.c_str();
134+
currentChannelUrl = a.second.c_str();
135+
111136
fullsizeRead = 0;
112137
curl_easy_setopt(hnd, CURLOPT_URL, a.second.c_str());
113138

@@ -120,17 +145,8 @@ void CheckURL::executeChecks() {
120145
curl_easy_getinfo(hnd, CURLINFO_RESPONSE_CODE, &response_code);
121146

122147
if (!((ret == CURLE_OK || fullsizeRead >= READLIMIT_BYTES) && (response_code < 400))) {
123-
printf("Error: %ld: %s -> %s\n", response_code, a.first.c_str(), a.second.c_str());
124-
brokenChannels.emplace(a.first.c_str());
125-
126-
/*
127-
{
128-
LOCK_CHANNELS_READ;
129-
tChannelID testChannelID = tChannelID::FromString(a.first.c_str());
130-
const cChannel *testChannel = Channels->GetByChannelID(testChannelID);
131-
cDevice::PrimaryDevice()->SwitchChannel(testChannel, true);
132-
}
133-
*/
148+
dsyslog("[iptv] Check url failed: %ld: %s -> %s, bytes read %ld\n", response_code, a.first.c_str(), a.second.c_str(), fullsizeRead);
149+
mark404Channel(a.first.c_str());
134150
}
135151
}
136152

checkurl.h

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <thread>
44
#include <map>
5+
#include <set>
56

67
class CheckURL {
78
private:
@@ -13,11 +14,22 @@ class CheckURL {
1314
private:
1415
void executeChecks();
1516

17+
int countChannels;
18+
int checkedChannels;
19+
cString currentChannelId;
20+
cString currentChannelUrl;
21+
1622
public:
1723
CheckURL();
1824
~CheckURL();
1925

2026
void start();
21-
void abort();
27+
cString status();
28+
void stop();
29+
30+
int getCountChannels() const { return countChannels; };
31+
int getCheckedChannels() const { return checkedChannels; };
32+
cString getCurrentChannelId() { return currentChannelId; };
33+
cString getCurrentChannelUrl() { return currentChannelUrl; };
2234
};
2335

common.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,18 @@ void printBacktrace() {
117117
}
118118

119119
void mark404Channel(int channelId) {
120-
/*
121120
std::lock_guard<std::mutex> guard(all404ChannelMutex);
122121
all404Channels.emplace(channelId);
123-
*/
122+
}
123+
124+
void mark404Channel(const char* channelId) {
125+
std::lock_guard<std::mutex> guard(all404ChannelMutex);
126+
LOCK_CHANNELS_READ;
127+
const cChannel *channel = Channels->GetByChannelID(tChannelID::FromString(channelId));
128+
all404Channels.emplace(channel->Number());
124129
}
125130

126131
void rename404Channels() {
127-
/*
128132
if (all404Channels.empty()) {
129133
return;
130134
}
@@ -145,6 +149,5 @@ void rename404Channels() {
145149
}
146150
}
147151
}
148-
*/
149152
}
150153

common.h

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ void printBacktrace();
8383
#define CHANNELMARK404 "- 404"
8484
extern std::set<int> all404Channels;
8585
void mark404Channel(int channelId);
86+
void mark404Channel(const char* channelId);
8687
void rename404Channels();
8788

8889
struct section_filter_table_type {

iptv.cpp

+34-8
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ bool thread404Running;
3333

3434
void run404() {
3535
while(thread404Running) {
36-
std::this_thread::sleep_for(std::chrono::milliseconds(200));
36+
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
3737
rename404Channels();
3838
}
3939
}
@@ -48,6 +48,10 @@ cPluginIptv::cPluginIptv() : deviceCountM(1) {
4848
cPluginIptv::~cPluginIptv() {
4949
debug16("%s", __PRETTY_FUNCTION__);
5050
// Clean up after yourself!
51+
if (thread404.joinable()) {
52+
thread404Running = false;
53+
thread404.join();
54+
}
5155
}
5256

5357
const char *cPluginIptv::CommandLineHelp() {
@@ -133,8 +137,8 @@ bool cPluginIptv::Start() {
133137
info("%s", *info);
134138
}
135139

136-
// thread404Running = true;
137-
// thread404 = std::thread(run404);
140+
thread404Running = true;
141+
thread404 = std::thread(run404);
138142

139143
return true;
140144
}
@@ -264,7 +268,7 @@ const char **cPluginIptv::SVDRPHelpPages() {
264268
" adds a STREAM URL to channels. PIDs and frequency will be automatically set\n",
265269
"ADDR <name>#<URL>\n"
266270
" adds a RADIO URL to channels. PIDs and frequency will be automatically set\n",
267-
"CHKU\n"
271+
"CHKU START, STATUS, STOP\n"
268272
" checks all configured URL and checks if a connect is possible and a valid result is returned.\n"
269273
" If this is not the case the channel will be marked with ' - 404'\n",
270274
nullptr
@@ -570,11 +574,33 @@ cString cPluginIptv::SVDRPCommand(const char *commandP, const char *optionP, int
570574

571575
return replyMessage;
572576
}
573-
} else if (strcasecmp(commandP, "CHKS") == 0) {
574-
chk.start();
577+
} else if (strcasecmp(commandP, "CHKU") == 0) {
578+
if (*optionP) {
579+
char buf[strlen(optionP) + 1];
580+
char *p = strcpy(buf, optionP);
575581

576-
replyCodeP = 250;
577-
return { "URL check started" };
582+
cString cmd = cString(p);
583+
cmd = ChangeCase(cmd, true);
584+
585+
if (strcmp(cmd, "START") == 0) {
586+
chk.start();
587+
replyCodeP = 250;
588+
return { "URL check started" };
589+
} else if (strcmp(cmd, "STATUS") == 0) {
590+
replyCodeP = 250;
591+
return { chk.status() };
592+
} else if (strcmp(cmd, "STOP") == 0) {
593+
chk.stop();
594+
replyCodeP = 250;
595+
return { "URL check stopped" };
596+
} else {
597+
replyCodeP = 501;
598+
return { "CHKU command is unknown" };
599+
}
600+
} else {
601+
replyCodeP = 501;
602+
return { "CHKU missing command START, STATUS or STOP" };
603+
}
578604
}
579605

580606
return nullptr;

0 commit comments

Comments
 (0)