2 Copyright 2004-2008 Chris Tallon
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "mediaplayer.h"
23 #include "servermediafile.h"
24 #include "vdrcommand.h"
25 #include "vompclient.h"
27 #include "responsepacket.h"
29 #ifndef VOMPSTANDALONE
30 #include <vdr/channels.h>
31 #include <vdr/recording.h>
32 #include <vdr/plugin.h>
33 #include "recplayer.h"
34 #include "mvpreceiver.h"
35 #include "picturereader.h"
40 pthread_mutex_t threadClientMutex;
41 int VompClient::nr_clients = 0;
42 cPlugin *VompClient::scraper = NULL;
43 time_t VompClient::lastScrapQuery = 0;
45 VompClient::VompClient(Config* cfgBase, char* tconfigDir, char* tlogoDir,
46 char *tresourceDir, int tsocket)
47 : rrproc(*this), tcp(tsocket), i18n(tconfigDir)
49 #ifndef VOMPSTANDALONE
52 recordingManager = NULL;
53 pict = new PictureReader(this);
54 if (!scraper) scrapQuery();
56 resourceDir = tresourceDir;
58 log = Log::getInstance();
60 configDir = tconfigDir;
61 log->log("Client", Log::DEBUG, "Config dir: %s", configDir);
64 media=new MediaPlayer();
65 mediaprovider=new ServerMediaFile(cfgBase,media);
67 charcoding=1; //latin1 is default
70 setCharset(charcoding);
75 VompClient::~VompClient()
77 log->log("Client", Log::DEBUG, "Vomp client destructor");
78 #ifndef VOMPSTANDALONE
81 lp->detachMVPReceiver();
90 delete recordingManager;
92 recordingManager = NULL;
95 //if (loggedIn) cleanConfig();
101 delete mediaprovider;
103 if (charconvsys) delete charconvsys;
104 if (charconvutf8) delete charconvutf8;
114 cPlugin *VompClient::scrapQuery()
116 if (scraper) return scraper;
117 if ((time(NULL)-lastScrapQuery) > 5*60) {
118 lastScrapQuery = time(NULL);
119 if (!scraper) scraper = cPluginManager::GetPlugin("scraper2vdr");
124 void VompClient::setCharset(int charset)
127 cCharSetConv *oldcharconvsys=charconvsys;
128 cCharSetConv *oldcharconvutf8=charconvutf8;
129 switch (charcoding) {
131 charconvsys=new cCharSetConv(NULL,"UTF-8");
132 charconvutf8=new cCharSetConv("UTF-8","UTF-8");
136 charconvsys=new cCharSetConv(NULL,"ISO-8859-1");
137 charconvutf8=new cCharSetConv("UTF-8","ISO-8859-1");
140 if (oldcharconvsys) delete oldcharconvsys;
141 if (oldcharconvutf8) delete oldcharconvutf8;
145 void VompClient::incClients()
147 pthread_mutex_lock(&threadClientMutex);
148 VompClient::nr_clients++;
149 pthread_mutex_unlock(&threadClientMutex);
152 void VompClient::decClients()
154 pthread_mutex_lock(&threadClientMutex);
155 VompClient::nr_clients--;
156 pthread_mutex_unlock(&threadClientMutex);
159 int VompClient::getNrClients()
162 pthread_mutex_lock(&threadClientMutex);
163 nrClients = VompClient::nr_clients;
164 pthread_mutex_unlock(&threadClientMutex);
169 void VompClient::cleanConfig()
171 log->log("Client", Log::DEBUG, "Clean config");
173 #ifndef VOMPSTANDALONE
175 cRecordings Recordings;
180 char* resumes = config.getSectionKeyNames("ResumeData", numReturns, length);
181 char* position = resumes;
182 for(int k = 0; k < numReturns; k++)
184 log->log("Client", Log::DEBUG, "EXAMINING: %i %i %p %s", k, numReturns, position, position);
186 cRecording* recording = Recordings.GetByName(position);
189 // doesn't exist anymore
190 log->log("Client", Log::DEBUG, "Found a recording that doesn't exist anymore");
191 config.deleteValue("ResumeData", position);
195 log->log("Client", Log::DEBUG, "This recording still exists");
198 position += strlen(position) + 1;
205 void VompClient::netLog()
207 // Hook, called from rrproc login after client has logged in.
208 // See if this MVP config says to do network logging, if so open a log
209 // The config object will be usable now since it's set up in login
211 char* doNetLogging = config.getValueString("Advanced", "Network logging");
214 if (!strcasecmp(doNetLogging, "on"))
216 char* netLogFileName = config.getValueString("Advanced", "Network logging file");
219 netLogFile = fopen(netLogFileName, "a");
220 if (netLogFile) log->log("Client", Log::DEBUG, "Client network logging started");
222 delete[] netLogFileName;
226 delete[] doNetLogging;
230 void VompClientStartThread(void* arg)
232 VompClient* m = (VompClient*)arg;
234 // Nothing external to this class has a reference to it
235 // This is the end of the thread.. so delete m
240 int VompClient::run()
242 if (pthread_create(&runThread, NULL, (void*(*)(void*))VompClientStartThread, (void *)this) == -1) return 0;
243 log->log("Client", Log::DEBUG, "VompClient run success");
247 void VompClient::run2()
252 pthread_sigmask(SIG_BLOCK, &sigset, NULL);
253 pthread_detach(runThread); // Detach
255 // tcp.disableReadTimeout();
257 // tcp.setSoKeepTime(3);
258 tcp.setNonBlocking();
264 ULONG extraDataLength;
272 log->log("Client", Log::DEBUG, "Waiting");
274 if (!tcp.readData((UCHAR*)&channelID, sizeof(ULONG)))
276 // If this read fails then the client just hasn't sent anything.
277 // If any of the lower reads fail, then break, the connection is probably dead
279 // check connection is ok
280 // if (tcp.isConnected()) continue;
282 log->log("Client", Log::DEBUG, "Disconnection detected");
286 channelID = ntohl(channelID);
289 if (!tcp.readData((UCHAR*)&requestID, sizeof(ULONG))) break;
290 requestID = ntohl(requestID);
292 if (!tcp.readData((UCHAR*)&opcode, sizeof(ULONG))) break;
293 opcode = ntohl(opcode);
295 if (!tcp.readData((UCHAR*)&extraDataLength, sizeof(ULONG))) break;
296 extraDataLength = ntohl(extraDataLength);
297 if (extraDataLength > 200000) // a random sanity limit
299 log->log("Client", Log::ERR, "ExtraDataLength > 200000!");
305 data = (UCHAR*)malloc(extraDataLength);
308 log->log("Client", Log::ERR, "Extra data buffer malloc error");
312 if (!tcp.readData(data, extraDataLength))
314 log->log("Client", Log::ERR, "Could not read extradata");
324 log->log("Client", Log::DEBUG, "Received chan=%lu, ser=%lu, op=%lu, edl=%lu", channelID, requestID, opcode, extraDataLength);
326 if (!loggedIn && (opcode != 1))
328 log->log("Client", Log::ERR, "Not logged in and opcode != 1");
329 if (data) free(data);
333 RequestPacket* req = new RequestPacket(requestID, opcode, data, extraDataLength);
334 rrproc.recvRequest(req);
336 else if (channelID == 3)
338 if (!tcp.readData((UCHAR*)&kaTimeStamp, sizeof(ULONG))) break;
339 kaTimeStamp = ntohl(kaTimeStamp);
341 log->log("Client", Log::DEBUG, "Received chan=%lu kats=%lu", channelID, kaTimeStamp);
345 p = (ULONG*)&buffer[0]; *p = htonl(3); // KA CHANNEL
346 p = (ULONG*)&buffer[4]; *p = htonl(kaTimeStamp);
347 if (!tcp.sendPacket(buffer, 8))
349 log->log("Client", Log::ERR, "Could not send back KA reply");
353 else if (channelID == 4)
355 if (!tcp.readData((UCHAR*)&logStringLen, sizeof(ULONG))) break;
356 logStringLen = ntohl(logStringLen);
358 log->log("Client", Log::DEBUG, "Received chan=%lu loglen=%lu", channelID, logStringLen);
360 UCHAR buffer[logStringLen + 1];
361 if (!tcp.readData((UCHAR*)&buffer, logStringLen)) break;
362 buffer[logStringLen] = '\0';
364 // log->log("Client", Log::INFO, "Client said: '%s'", buffer);
367 if (fputs((const char*)buffer, netLogFile) == EOF)
377 log->log("Client", Log::ERR, "Incoming channel number unknown");
383 ULLONG VompClient::ntohll(ULLONG a)
388 ULLONG VompClient::htonll(ULLONG a)
390 #if BYTE_ORDER == BIG_ENDIAN
395 b = ((a << 56) & 0xFF00000000000000ULL)
396 | ((a << 40) & 0x00FF000000000000ULL)
397 | ((a << 24) & 0x0000FF0000000000ULL)
398 | ((a << 8) & 0x000000FF00000000ULL)
399 | ((a >> 8) & 0x00000000FF000000ULL)
400 | ((a >> 24) & 0x0000000000FF0000ULL)
401 | ((a >> 40) & 0x000000000000FF00ULL)
402 | ((a >> 56) & 0x00000000000000FFULL) ;
408 #ifndef VOMPSTANDALONE
410 cChannel* VompClient::channelFromNumber(ULONG channelNumber)
412 cChannel* channel = NULL;
414 for (channel = Channels.First(); channel; channel = Channels.Next(channel))
416 if (!channel->GroupSep())
418 // log->log("Client", Log::DEBUG, "Looking for channel %lu::: number: %i name: '%s'", channelNumber, channel->Number(), channel->Name());
420 if (channel->Number() == (int)channelNumber)
422 int vpid = channel->Vpid();
423 #if VDRVERSNUM < 10300
424 int apid1 = channel->Apid1();
426 int apid1 = channel->Apid(0);
428 // log->log("Client", Log::DEBUG, "Found channel number %lu, vpid = %i, apid1 = %i", channelNumber, vpid, apid1);
436 log->log("Client", Log::DEBUG, "Channel not found");
442 void VompClient::writeResumeData()
444 /*config.setValueLong("ResumeData",
445 (char*)recplayer->getCurrentRecording()->FileName(),
446 recplayer->frameNumberFromPosition(recplayer->getLastPosition()) );*/
448 /* write to vdr resume file */
449 int resume = recplayer->frameNumberFromPosition(recplayer->getLastPosition());
450 char* ResumeIdC = config.getValueString("General", "ResumeId");
453 ResumeId = atoi(ResumeIdC);
458 cCondWait::SleepMs(100);
460 int OldSetupResumeID = Setup.ResumeID;
461 Setup.ResumeID = ResumeId; //UGLY: quickly change resumeid
462 #if VDRVERSNUM < 10703
463 cResumeFile ResumeFile((char*)recplayer->getCurrentRecording()->FileName()); //get corresponding resume file
465 cResumeFile ResumeFile((char*)recplayer->getCurrentRecording()->FileName(),(char*)recplayer->getCurrentRecording()->IsPesRecording()); //get corresponding resume file
467 Setup.ResumeID = OldSetupResumeID; //and restore it back
468 ResumeIDLock = false;
469 ResumeFile.Save(resume);
470 //isyslog("VOMPDEBUG: Saving resume = %i, ResumeId = %i",resume, ResumeId);