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 "recplayer.h"
33 #include "mvpreceiver.h"
38 pthread_mutex_t threadClientMutex;
39 int VompClient::nr_clients = 0;
42 VompClient::VompClient(Config* cfgBase, char* tconfigDir, int tsocket)
43 : rrproc(*this), tcp(tsocket), i18n(tconfigDir)
45 #ifndef VOMPSTANDALONE
48 recordingManager = NULL;
50 log = Log::getInstance();
52 configDir = tconfigDir;
53 log->log("Client", Log::DEBUG, "Config dir: %s", configDir);
56 media=new MediaPlayer();
57 mediaprovider=new ServerMediaFile(cfgBase,media);
59 charcoding=1; //latin1 is default
62 setCharset(charcoding);
67 VompClient::~VompClient()
69 log->log("Client", Log::DEBUG, "Vomp client destructor");
70 #ifndef VOMPSTANDALONE
73 lp->detachMVPReceiver();
82 delete recordingManager;
84 recordingManager = NULL;
87 //if (loggedIn) cleanConfig();
93 if (charconvsys) delete charconvsys;
94 if (charconvutf8) delete charconvutf8;
104 void VompClient::setCharset(int charset)
107 cCharSetConv *oldcharconvsys=charconvsys;
108 cCharSetConv *oldcharconvutf8=charconvutf8;
109 switch (charcoding) {
111 charconvsys=new cCharSetConv(NULL,"UTF-8");
112 charconvutf8=new cCharSetConv("UTF-8","UTF-8");
116 charconvsys=new cCharSetConv(NULL,"ISO-8859-1");
117 charconvutf8=new cCharSetConv("UTF-8","ISO-8859-1");
120 if (oldcharconvsys) delete oldcharconvsys;
121 if (oldcharconvutf8) delete oldcharconvutf8;
125 void VompClient::incClients()
127 pthread_mutex_lock(&threadClientMutex);
128 VompClient::nr_clients++;
129 pthread_mutex_unlock(&threadClientMutex);
132 void VompClient::decClients()
134 pthread_mutex_lock(&threadClientMutex);
135 VompClient::nr_clients--;
136 pthread_mutex_unlock(&threadClientMutex);
139 int VompClient::getNrClients()
142 pthread_mutex_lock(&threadClientMutex);
143 nrClients = VompClient::nr_clients;
144 pthread_mutex_unlock(&threadClientMutex);
149 void VompClient::cleanConfig()
151 log->log("Client", Log::DEBUG, "Clean config");
153 #ifndef VOMPSTANDALONE
155 cRecordings Recordings;
160 char* resumes = config.getSectionKeyNames("ResumeData", numReturns, length);
161 char* position = resumes;
162 for(int k = 0; k < numReturns; k++)
164 log->log("Client", Log::DEBUG, "EXAMINING: %i %i %p %s", k, numReturns, position, position);
166 cRecording* recording = Recordings.GetByName(position);
169 // doesn't exist anymore
170 log->log("Client", Log::DEBUG, "Found a recording that doesn't exist anymore");
171 config.deleteValue("ResumeData", position);
175 log->log("Client", Log::DEBUG, "This recording still exists");
178 position += strlen(position) + 1;
185 void VompClient::netLog()
187 // Hook, called from rrproc login after client has logged in.
188 // See if this MVP config says to do network logging, if so open a log
189 // The config object will be usable now since it's set up in login
191 char* doNetLogging = config.getValueString("Advanced", "Network logging");
194 if (!strcasecmp(doNetLogging, "on"))
196 char* netLogFileName = config.getValueString("Advanced", "Network logging file");
199 netLogFile = fopen(netLogFileName, "a");
200 if (netLogFile) log->log("Client", Log::DEBUG, "Client network logging started");
202 delete[] netLogFileName;
206 delete[] doNetLogging;
210 void VompClientStartThread(void* arg)
212 VompClient* m = (VompClient*)arg;
214 // Nothing external to this class has a reference to it
215 // This is the end of the thread.. so delete m
220 int VompClient::run()
222 if (pthread_create(&runThread, NULL, (void*(*)(void*))VompClientStartThread, (void *)this) == -1) return 0;
223 log->log("Client", Log::DEBUG, "VompClient run success");
227 void VompClient::run2()
232 pthread_sigmask(SIG_BLOCK, &sigset, NULL);
233 pthread_detach(runThread); // Detach
235 // tcp.disableReadTimeout();
237 // tcp.setSoKeepTime(3);
238 tcp.setNonBlocking();
243 ULONG extraDataLength;
251 log->log("Client", Log::DEBUG, "Waiting");
253 if (!tcp.readData((UCHAR*)&channelID, sizeof(ULONG)))
255 // If this read fails then the client just hasn't sent anything.
256 // If any of the lower reads fail, then break, the connection is probably dead
258 // check connection is ok
259 // if (tcp.isConnected()) continue;
261 log->log("Client", Log::DEBUG, "Disconnection detected");
265 channelID = ntohl(channelID);
268 if (!tcp.readData((UCHAR*)&requestID, sizeof(ULONG))) break;
269 requestID = ntohl(requestID);
271 if (!tcp.readData((UCHAR*)&opcode, sizeof(ULONG))) break;
272 opcode = ntohl(opcode);
274 if (!tcp.readData((UCHAR*)&extraDataLength, sizeof(ULONG))) break;
275 extraDataLength = ntohl(extraDataLength);
276 if (extraDataLength > 200000) // a random sanity limit
278 log->log("Client", Log::ERR, "ExtraDataLength > 200000!");
284 data = (UCHAR*)malloc(extraDataLength);
287 log->log("Client", Log::ERR, "Extra data buffer malloc error");
291 if (!tcp.readData(data, extraDataLength))
293 log->log("Client", Log::ERR, "Could not read extradata");
303 log->log("Client", Log::DEBUG, "Received chan=%lu, ser=%lu, op=%lu, edl=%lu", channelID, requestID, opcode, extraDataLength);
305 if (!loggedIn && (opcode != 1))
307 log->log("Client", Log::ERR, "Not logged in and opcode != 1");
308 if (data) free(data);
312 RequestPacket* req = new RequestPacket(requestID, opcode, data, extraDataLength);
313 rrproc.recvRequest(req);
315 else if (channelID == 3)
317 if (!tcp.readData((UCHAR*)&kaTimeStamp, sizeof(ULONG))) break;
318 kaTimeStamp = ntohl(kaTimeStamp);
320 log->log("Client", Log::DEBUG, "Received chan=%lu kats=%lu", channelID, kaTimeStamp);
324 p = (ULONG*)&buffer[0]; *p = htonl(3); // KA CHANNEL
325 p = (ULONG*)&buffer[4]; *p = htonl(kaTimeStamp);
326 if (!tcp.sendPacket(buffer, 8))
328 log->log("Client", Log::ERR, "Could not send back KA reply");
332 else if (channelID == 4)
334 if (!tcp.readData((UCHAR*)&logStringLen, sizeof(ULONG))) break;
335 logStringLen = ntohl(logStringLen);
337 log->log("Client", Log::DEBUG, "Received chan=%lu loglen=%lu", channelID, logStringLen);
339 UCHAR buffer[logStringLen + 1];
340 if (!tcp.readData((UCHAR*)&buffer, logStringLen)) break;
341 buffer[logStringLen] = '\0';
343 // log->log("Client", Log::INFO, "Client said: '%s'", buffer);
346 if (fputs((const char*)buffer, netLogFile) == EOF)
356 log->log("Client", Log::ERR, "Incoming channel number unknown");
362 ULLONG VompClient::ntohll(ULLONG a)
367 ULLONG VompClient::htonll(ULLONG a)
369 #if BYTE_ORDER == BIG_ENDIAN
374 b = ((a << 56) & 0xFF00000000000000ULL)
375 | ((a << 40) & 0x00FF000000000000ULL)
376 | ((a << 24) & 0x0000FF0000000000ULL)
377 | ((a << 8) & 0x000000FF00000000ULL)
378 | ((a >> 8) & 0x00000000FF000000ULL)
379 | ((a >> 24) & 0x0000000000FF0000ULL)
380 | ((a >> 40) & 0x000000000000FF00ULL)
381 | ((a >> 56) & 0x00000000000000FFULL) ;
387 #ifndef VOMPSTANDALONE
389 cChannel* VompClient::channelFromNumber(ULONG channelNumber)
391 cChannel* channel = NULL;
393 for (channel = Channels.First(); channel; channel = Channels.Next(channel))
395 if (!channel->GroupSep())
397 log->log("Client", Log::DEBUG, "Looking for channel %lu::: number: %i name: '%s'", channelNumber, channel->Number(), channel->Name());
399 if (channel->Number() == (int)channelNumber)
401 int vpid = channel->Vpid();
402 #if VDRVERSNUM < 10300
403 int apid1 = channel->Apid1();
405 int apid1 = channel->Apid(0);
407 log->log("Client", Log::DEBUG, "Found channel number %lu, vpid = %i, apid1 = %i", channelNumber, vpid, apid1);
415 log->log("Client", Log::DEBUG, "Channel not found");
421 void VompClient::writeResumeData()
423 /*config.setValueLong("ResumeData",
424 (char*)recplayer->getCurrentRecording()->FileName(),
425 recplayer->frameNumberFromPosition(recplayer->getLastPosition()) );*/
427 /* write to vdr resume file */
428 int resume = recplayer->frameNumberFromPosition(recplayer->getLastPosition());
429 char* ResumeIdC = config.getValueString("General", "ResumeId");
432 ResumeId = atoi(ResumeIdC);
437 cCondWait::SleepMs(100);
439 int OldSetupResumeID = Setup.ResumeID;
440 Setup.ResumeID = ResumeId; //UGLY: quickly change resumeid
441 #if VDRVERSNUM < 10703
442 cResumeFile ResumeFile((char*)recplayer->getCurrentRecording()->FileName()); //get corresponding resume file
444 cResumeFile ResumeFile((char*)recplayer->getCurrentRecording()->FileName(),(char*)recplayer->getCurrentRecording()->IsPesRecording()); //get corresponding resume file
446 Setup.ResumeID = OldSetupResumeID; //and restore it back
447 ResumeIDLock = false;
448 ResumeFile.Save(resume);
449 //isyslog("VOMPDEBUG: Saving resume = %i, ResumeId = %i",resume, ResumeId);