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, char * timageDir, char * tcacheDir, 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;
60 log = Log::getInstance();
62 configDir = tconfigDir;
63 log->log("Client", Log::DEBUG, "Config dir: %s", configDir);
66 media=new MediaPlayer();
67 mediaprovider=new ServerMediaFile(cfgBase,media);
69 charcoding=1; //latin1 is default
72 setCharset(charcoding);
77 VompClient::~VompClient()
79 log->log("Client", Log::DEBUG, "Vomp client destructor");
80 #ifndef VOMPSTANDALONE
83 lp->detachMVPReceiver();
92 delete recordingManager;
94 recordingManager = NULL;
97 //if (loggedIn) cleanConfig();
103 delete mediaprovider;
105 if (charconvsys) delete charconvsys;
106 if (charconvutf8) delete charconvutf8;
116 cPlugin *VompClient::scrapQuery()
118 if (scraper) return scraper;
119 if ((time(NULL)-lastScrapQuery) > 5*60) {
120 lastScrapQuery = time(NULL);
121 if (!scraper) scraper = cPluginManager::GetPlugin("scraper2vdr");
126 void VompClient::setCharset(int charset)
129 cCharSetConv *oldcharconvsys=charconvsys;
130 cCharSetConv *oldcharconvutf8=charconvutf8;
131 switch (charcoding) {
133 charconvsys=new cCharSetConv(NULL,"UTF-8");
134 charconvutf8=new cCharSetConv("UTF-8","UTF-8");
138 charconvsys=new cCharSetConv(NULL,"ISO-8859-1");
139 charconvutf8=new cCharSetConv("UTF-8","ISO-8859-1");
142 if (oldcharconvsys) delete oldcharconvsys;
143 if (oldcharconvutf8) delete oldcharconvutf8;
147 void VompClient::incClients()
149 pthread_mutex_lock(&threadClientMutex);
150 VompClient::nr_clients++;
151 pthread_mutex_unlock(&threadClientMutex);
154 void VompClient::decClients()
156 pthread_mutex_lock(&threadClientMutex);
157 VompClient::nr_clients--;
158 pthread_mutex_unlock(&threadClientMutex);
161 int VompClient::getNrClients()
164 pthread_mutex_lock(&threadClientMutex);
165 nrClients = VompClient::nr_clients;
166 pthread_mutex_unlock(&threadClientMutex);
171 void VompClient::cleanConfig()
173 log->log("Client", Log::DEBUG, "Clean config");
175 #ifndef VOMPSTANDALONE
177 cRecordings Recordings;
182 char* resumes = config.getSectionKeyNames("ResumeData", numReturns, length);
183 char* position = resumes;
184 for(int k = 0; k < numReturns; k++)
186 log->log("Client", Log::DEBUG, "EXAMINING: %i %i %p %s", k, numReturns, position, position);
188 cRecording* recording = Recordings.GetByName(position);
191 // doesn't exist anymore
192 log->log("Client", Log::DEBUG, "Found a recording that doesn't exist anymore");
193 config.deleteValue("ResumeData", position);
197 log->log("Client", Log::DEBUG, "This recording still exists");
200 position += strlen(position) + 1;
207 void VompClient::netLog()
209 // Hook, called from rrproc login after client has logged in.
210 // See if this MVP config says to do network logging, if so open a log
211 // The config object will be usable now since it's set up in login
213 char* doNetLogging = config.getValueString("Advanced", "Network logging");
216 if (!strcasecmp(doNetLogging, "on"))
218 char* netLogFileName = config.getValueString("Advanced", "Network logging file");
221 netLogFile = fopen(netLogFileName, "a");
222 if (netLogFile) log->log("Client", Log::DEBUG, "Client network logging started");
224 delete[] netLogFileName;
228 delete[] doNetLogging;
232 void VompClientStartThread(void* arg)
234 VompClient* m = (VompClient*)arg;
236 // Nothing external to this class has a reference to it
237 // This is the end of the thread.. so delete m
242 int VompClient::run()
244 if (pthread_create(&runThread, NULL, (void*(*)(void*))VompClientStartThread, (void *)this) == -1) return 0;
245 log->log("Client", Log::DEBUG, "VompClient run success");
249 void VompClient::run2()
254 pthread_sigmask(SIG_BLOCK, &sigset, NULL);
255 pthread_detach(runThread); // Detach
257 // tcp.disableReadTimeout();
259 // tcp.setSoKeepTime(3);
260 tcp.setNonBlocking();
266 ULONG extraDataLength;
274 log->log("Client", Log::DEBUG, "Waiting");
276 if (!tcp.readData((UCHAR*)&channelID, sizeof(ULONG)))
278 // If this read fails then the client just hasn't sent anything.
279 // If any of the lower reads fail, then break, the connection is probably dead
281 // check connection is ok
282 // if (tcp.isConnected()) continue;
284 log->log("Client", Log::DEBUG, "Disconnection detected");
288 channelID = ntohl(channelID);
291 if (!tcp.readData((UCHAR*)&requestID, sizeof(ULONG))) break;
292 requestID = ntohl(requestID);
294 if (!tcp.readData((UCHAR*)&opcode, sizeof(ULONG))) break;
295 opcode = ntohl(opcode);
297 if (!tcp.readData((UCHAR*)&extraDataLength, sizeof(ULONG))) break;
298 extraDataLength = ntohl(extraDataLength);
299 if (extraDataLength > 200000) // a random sanity limit
301 log->log("Client", Log::ERR, "ExtraDataLength > 200000!");
307 data = (UCHAR*)malloc(extraDataLength);
310 log->log("Client", Log::ERR, "Extra data buffer malloc error");
314 if (!tcp.readData(data, extraDataLength))
316 log->log("Client", Log::ERR, "Could not read extradata");
326 log->log("Client", Log::DEBUG, "Received chan=%lu, ser=%lu, op=%lu, edl=%lu", channelID, requestID, opcode, extraDataLength);
328 if (!loggedIn && (opcode != 1))
330 log->log("Client", Log::ERR, "Not logged in and opcode != 1");
331 if (data) free(data);
335 RequestPacket* req = new RequestPacket(requestID, opcode, data, extraDataLength);
336 rrproc.recvRequest(req);
338 else if (channelID == 3)
340 if (!tcp.readData((UCHAR*)&kaTimeStamp, sizeof(ULONG))) break;
341 kaTimeStamp = ntohl(kaTimeStamp);
343 log->log("Client", Log::DEBUG, "Received chan=%lu kats=%lu", channelID, kaTimeStamp);
347 p = (ULONG*)&buffer[0]; *p = htonl(3); // KA CHANNEL
348 p = (ULONG*)&buffer[4]; *p = htonl(kaTimeStamp);
349 if (!tcp.sendPacket(buffer, 8))
351 log->log("Client", Log::ERR, "Could not send back KA reply");
355 else if (channelID == 4)
357 if (!tcp.readData((UCHAR*)&logStringLen, sizeof(ULONG))) break;
358 logStringLen = ntohl(logStringLen);
360 log->log("Client", Log::DEBUG, "Received chan=%lu loglen=%lu", channelID, logStringLen);
362 UCHAR buffer[logStringLen + 1];
363 if (!tcp.readData((UCHAR*)&buffer, logStringLen)) break;
364 buffer[logStringLen] = '\0';
366 // log->log("Client", Log::INFO, "Client said: '%s'", buffer);
369 if (fputs((const char*)buffer, netLogFile) == EOF)
379 log->log("Client", Log::ERR, "Incoming channel number unknown");
385 ULLONG VompClient::ntohll(ULLONG a)
390 ULLONG VompClient::htonll(ULLONG a)
392 #if BYTE_ORDER == BIG_ENDIAN
397 b = ((a << 56) & 0xFF00000000000000ULL)
398 | ((a << 40) & 0x00FF000000000000ULL)
399 | ((a << 24) & 0x0000FF0000000000ULL)
400 | ((a << 8) & 0x000000FF00000000ULL)
401 | ((a >> 8) & 0x00000000FF000000ULL)
402 | ((a >> 24) & 0x0000000000FF0000ULL)
403 | ((a >> 40) & 0x000000000000FF00ULL)
404 | ((a >> 56) & 0x00000000000000FFULL) ;
410 #ifndef VOMPSTANDALONE
412 void VompClient::writeResumeData()
414 /*config.setValueLong("ResumeData",
415 (char*)recplayer->getCurrentRecording()->FileName(),
416 recplayer->frameNumberFromPosition(recplayer->getLastPosition()) );*/
418 /* write to vdr resume file */
419 int resume = recplayer->frameNumberFromPosition(recplayer->getLastPosition());
420 char* ResumeIdC = config.getValueString("General", "ResumeId");
423 ResumeId = atoi(ResumeIdC);
428 cCondWait::SleepMs(100);
430 int OldSetupResumeID = Setup.ResumeID;
431 Setup.ResumeID = ResumeId; //UGLY: quickly change resumeid
432 #if VDRVERSNUM < 10703
433 cResumeFile ResumeFile((char*)recplayer->getCurrentRecording()->FileName()); //get corresponding resume file
435 cResumeFile ResumeFile((char*)recplayer->getCurrentRecording()->FileName(),(char*)recplayer->getCurrentRecording()->IsPesRecording()); //get corresponding resume file
437 Setup.ResumeID = OldSetupResumeID; //and restore it back
438 ResumeIDLock = false;
439 ResumeFile.Save(resume);
440 //isyslog("VOMPDEBUG: Saving resume = %i, ResumeId = %i",resume, ResumeId);