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;
44 VompClient::VompClient(Config* cfgBase, char* tconfigDir, int tsocket)
45 : rrproc(*this), tcp(tsocket), i18n(tconfigDir)
47 #ifndef VOMPSTANDALONE
50 recordingManager = NULL;
51 if (!scraper) scraper = cPluginManager::GetPlugin("scraper2vdr");
52 pict = new PictureReader(this);
54 log = Log::getInstance();
56 configDir = tconfigDir;
57 log->log("Client", Log::DEBUG, "Config dir: %s", configDir);
60 media=new MediaPlayer();
61 mediaprovider=new ServerMediaFile(cfgBase,media);
63 charcoding=1; //latin1 is default
66 setCharset(charcoding);
71 VompClient::~VompClient()
73 log->log("Client", Log::DEBUG, "Vomp client destructor");
74 #ifndef VOMPSTANDALONE
77 lp->detachMVPReceiver();
86 delete recordingManager;
88 recordingManager = NULL;
91 //if (loggedIn) cleanConfig();
99 if (charconvsys) delete charconvsys;
100 if (charconvutf8) delete charconvutf8;
110 void VompClient::setCharset(int charset)
113 cCharSetConv *oldcharconvsys=charconvsys;
114 cCharSetConv *oldcharconvutf8=charconvutf8;
115 switch (charcoding) {
117 charconvsys=new cCharSetConv(NULL,"UTF-8");
118 charconvutf8=new cCharSetConv("UTF-8","UTF-8");
122 charconvsys=new cCharSetConv(NULL,"ISO-8859-1");
123 charconvutf8=new cCharSetConv("UTF-8","ISO-8859-1");
126 if (oldcharconvsys) delete oldcharconvsys;
127 if (oldcharconvutf8) delete oldcharconvutf8;
131 void VompClient::incClients()
133 pthread_mutex_lock(&threadClientMutex);
134 VompClient::nr_clients++;
135 pthread_mutex_unlock(&threadClientMutex);
138 void VompClient::decClients()
140 pthread_mutex_lock(&threadClientMutex);
141 VompClient::nr_clients--;
142 pthread_mutex_unlock(&threadClientMutex);
145 int VompClient::getNrClients()
148 pthread_mutex_lock(&threadClientMutex);
149 nrClients = VompClient::nr_clients;
150 pthread_mutex_unlock(&threadClientMutex);
155 void VompClient::cleanConfig()
157 log->log("Client", Log::DEBUG, "Clean config");
159 #ifndef VOMPSTANDALONE
161 cRecordings Recordings;
166 char* resumes = config.getSectionKeyNames("ResumeData", numReturns, length);
167 char* position = resumes;
168 for(int k = 0; k < numReturns; k++)
170 log->log("Client", Log::DEBUG, "EXAMINING: %i %i %p %s", k, numReturns, position, position);
172 cRecording* recording = Recordings.GetByName(position);
175 // doesn't exist anymore
176 log->log("Client", Log::DEBUG, "Found a recording that doesn't exist anymore");
177 config.deleteValue("ResumeData", position);
181 log->log("Client", Log::DEBUG, "This recording still exists");
184 position += strlen(position) + 1;
191 void VompClient::netLog()
193 // Hook, called from rrproc login after client has logged in.
194 // See if this MVP config says to do network logging, if so open a log
195 // The config object will be usable now since it's set up in login
197 char* doNetLogging = config.getValueString("Advanced", "Network logging");
200 if (!strcasecmp(doNetLogging, "on"))
202 char* netLogFileName = config.getValueString("Advanced", "Network logging file");
205 netLogFile = fopen(netLogFileName, "a");
206 if (netLogFile) log->log("Client", Log::DEBUG, "Client network logging started");
208 delete[] netLogFileName;
212 delete[] doNetLogging;
216 void VompClientStartThread(void* arg)
218 VompClient* m = (VompClient*)arg;
220 // Nothing external to this class has a reference to it
221 // This is the end of the thread.. so delete m
226 int VompClient::run()
228 if (pthread_create(&runThread, NULL, (void*(*)(void*))VompClientStartThread, (void *)this) == -1) return 0;
229 log->log("Client", Log::DEBUG, "VompClient run success");
233 void VompClient::run2()
238 pthread_sigmask(SIG_BLOCK, &sigset, NULL);
239 pthread_detach(runThread); // Detach
241 // tcp.disableReadTimeout();
243 // tcp.setSoKeepTime(3);
244 tcp.setNonBlocking();
250 ULONG extraDataLength;
258 log->log("Client", Log::DEBUG, "Waiting");
260 if (!tcp.readData((UCHAR*)&channelID, sizeof(ULONG)))
262 // If this read fails then the client just hasn't sent anything.
263 // If any of the lower reads fail, then break, the connection is probably dead
265 // check connection is ok
266 // if (tcp.isConnected()) continue;
268 log->log("Client", Log::DEBUG, "Disconnection detected");
272 channelID = ntohl(channelID);
275 if (!tcp.readData((UCHAR*)&requestID, sizeof(ULONG))) break;
276 requestID = ntohl(requestID);
278 if (!tcp.readData((UCHAR*)&opcode, sizeof(ULONG))) break;
279 opcode = ntohl(opcode);
281 if (!tcp.readData((UCHAR*)&extraDataLength, sizeof(ULONG))) break;
282 extraDataLength = ntohl(extraDataLength);
283 if (extraDataLength > 200000) // a random sanity limit
285 log->log("Client", Log::ERR, "ExtraDataLength > 200000!");
291 data = (UCHAR*)malloc(extraDataLength);
294 log->log("Client", Log::ERR, "Extra data buffer malloc error");
298 if (!tcp.readData(data, extraDataLength))
300 log->log("Client", Log::ERR, "Could not read extradata");
310 log->log("Client", Log::DEBUG, "Received chan=%lu, ser=%lu, op=%lu, edl=%lu", channelID, requestID, opcode, extraDataLength);
312 if (!loggedIn && (opcode != 1))
314 log->log("Client", Log::ERR, "Not logged in and opcode != 1");
315 if (data) free(data);
319 RequestPacket* req = new RequestPacket(requestID, opcode, data, extraDataLength);
320 rrproc.recvRequest(req);
322 else if (channelID == 3)
324 if (!tcp.readData((UCHAR*)&kaTimeStamp, sizeof(ULONG))) break;
325 kaTimeStamp = ntohl(kaTimeStamp);
327 log->log("Client", Log::DEBUG, "Received chan=%lu kats=%lu", channelID, kaTimeStamp);
331 p = (ULONG*)&buffer[0]; *p = htonl(3); // KA CHANNEL
332 p = (ULONG*)&buffer[4]; *p = htonl(kaTimeStamp);
333 if (!tcp.sendPacket(buffer, 8))
335 log->log("Client", Log::ERR, "Could not send back KA reply");
339 else if (channelID == 4)
341 if (!tcp.readData((UCHAR*)&logStringLen, sizeof(ULONG))) break;
342 logStringLen = ntohl(logStringLen);
344 log->log("Client", Log::DEBUG, "Received chan=%lu loglen=%lu", channelID, logStringLen);
346 UCHAR buffer[logStringLen + 1];
347 if (!tcp.readData((UCHAR*)&buffer, logStringLen)) break;
348 buffer[logStringLen] = '\0';
350 // log->log("Client", Log::INFO, "Client said: '%s'", buffer);
353 if (fputs((const char*)buffer, netLogFile) == EOF)
363 log->log("Client", Log::ERR, "Incoming channel number unknown");
369 ULLONG VompClient::ntohll(ULLONG a)
374 ULLONG VompClient::htonll(ULLONG a)
376 #if BYTE_ORDER == BIG_ENDIAN
381 b = ((a << 56) & 0xFF00000000000000ULL)
382 | ((a << 40) & 0x00FF000000000000ULL)
383 | ((a << 24) & 0x0000FF0000000000ULL)
384 | ((a << 8) & 0x000000FF00000000ULL)
385 | ((a >> 8) & 0x00000000FF000000ULL)
386 | ((a >> 24) & 0x0000000000FF0000ULL)
387 | ((a >> 40) & 0x000000000000FF00ULL)
388 | ((a >> 56) & 0x00000000000000FFULL) ;
394 #ifndef VOMPSTANDALONE
396 cChannel* VompClient::channelFromNumber(ULONG channelNumber)
398 cChannel* channel = NULL;
400 for (channel = Channels.First(); channel; channel = Channels.Next(channel))
402 if (!channel->GroupSep())
404 log->log("Client", Log::DEBUG, "Looking for channel %lu::: number: %i name: '%s'", channelNumber, channel->Number(), channel->Name());
406 if (channel->Number() == (int)channelNumber)
408 int vpid = channel->Vpid();
409 #if VDRVERSNUM < 10300
410 int apid1 = channel->Apid1();
412 int apid1 = channel->Apid(0);
414 log->log("Client", Log::DEBUG, "Found channel number %lu, vpid = %i, apid1 = %i", channelNumber, vpid, apid1);
422 log->log("Client", Log::DEBUG, "Channel not found");
428 void VompClient::writeResumeData()
430 /*config.setValueLong("ResumeData",
431 (char*)recplayer->getCurrentRecording()->FileName(),
432 recplayer->frameNumberFromPosition(recplayer->getLastPosition()) );*/
434 /* write to vdr resume file */
435 int resume = recplayer->frameNumberFromPosition(recplayer->getLastPosition());
436 char* ResumeIdC = config.getValueString("General", "ResumeId");
439 ResumeId = atoi(ResumeIdC);
444 cCondWait::SleepMs(100);
446 int OldSetupResumeID = Setup.ResumeID;
447 Setup.ResumeID = ResumeId; //UGLY: quickly change resumeid
448 #if VDRVERSNUM < 10703
449 cResumeFile ResumeFile((char*)recplayer->getCurrentRecording()->FileName()); //get corresponding resume file
451 cResumeFile ResumeFile((char*)recplayer->getCurrentRecording()->FileName(),(char*)recplayer->getCurrentRecording()->IsPesRecording()); //get corresponding resume file
453 Setup.ResumeID = OldSetupResumeID; //and restore it back
454 ResumeIDLock = false;
455 ResumeFile.Save(resume);
456 //isyslog("VOMPDEBUG: Saving resume = %i, ResumeId = %i",resume, ResumeId);