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
81 delete recordingManager;
83 recordingManager = NULL;
86 //if (loggedIn) cleanConfig();
92 if (charconvsys) delete charconvsys;
93 if (charconvutf8) delete charconvutf8;
103 void VompClient::setCharset(int charset)
106 cCharSetConv *oldcharconvsys=charconvsys;
107 cCharSetConv *oldcharconvutf8=charconvutf8;
108 switch (charcoding) {
110 charconvsys=new cCharSetConv(NULL,"UTF-8");
111 charconvutf8=new cCharSetConv("UTF-8","UTF-8");
115 charconvsys=new cCharSetConv(NULL,"ISO-8859-1");
116 charconvutf8=new cCharSetConv("UTF-8","ISO-8859-1");
119 if (oldcharconvsys) delete oldcharconvsys;
120 if (oldcharconvutf8) delete oldcharconvutf8;
124 void VompClient::incClients()
126 pthread_mutex_lock(&threadClientMutex);
127 VompClient::nr_clients++;
128 pthread_mutex_unlock(&threadClientMutex);
131 void VompClient::decClients()
133 pthread_mutex_lock(&threadClientMutex);
134 VompClient::nr_clients--;
135 pthread_mutex_unlock(&threadClientMutex);
138 int VompClient::getNrClients()
141 pthread_mutex_lock(&threadClientMutex);
142 nrClients = VompClient::nr_clients;
143 pthread_mutex_unlock(&threadClientMutex);
148 void VompClient::cleanConfig()
150 log->log("Client", Log::DEBUG, "Clean config");
152 #ifndef VOMPSTANDALONE
154 cRecordings Recordings;
159 char* resumes = config.getSectionKeyNames("ResumeData", numReturns, length);
160 char* position = resumes;
161 for(int k = 0; k < numReturns; k++)
163 log->log("Client", Log::DEBUG, "EXAMINING: %i %i %p %s", k, numReturns, position, position);
165 cRecording* recording = Recordings.GetByName(position);
168 // doesn't exist anymore
169 log->log("Client", Log::DEBUG, "Found a recording that doesn't exist anymore");
170 config.deleteValue("ResumeData", position);
174 log->log("Client", Log::DEBUG, "This recording still exists");
177 position += strlen(position) + 1;
184 void VompClient::netLog()
186 // Hook, called from rrproc login after client has logged in.
187 // See if this MVP config says to do network logging, if so open a log
188 // The config object will be usable now since it's set up in login
190 char* doNetLogging = config.getValueString("Advanced", "Network logging");
193 if (!strcasecmp(doNetLogging, "on"))
195 char* netLogFileName = config.getValueString("Advanced", "Network logging file");
198 netLogFile = fopen(netLogFileName, "a");
199 if (netLogFile) log->log("Client", Log::DEBUG, "Client network logging started");
201 delete[] netLogFileName;
205 delete[] doNetLogging;
209 void VompClientStartThread(void* arg)
211 VompClient* m = (VompClient*)arg;
213 // Nothing external to this class has a reference to it
214 // This is the end of the thread.. so delete m
219 int VompClient::run()
221 if (pthread_create(&runThread, NULL, (void*(*)(void*))VompClientStartThread, (void *)this) == -1) return 0;
222 log->log("Client", Log::DEBUG, "VompClient run success");
226 void VompClient::run2()
231 pthread_sigmask(SIG_BLOCK, &sigset, NULL);
232 pthread_detach(runThread); // Detach
234 // tcp.disableReadTimeout();
236 // tcp.setSoKeepTime(3);
237 tcp.setNonBlocking();
242 ULONG extraDataLength;
250 log->log("Client", Log::DEBUG, "Waiting");
252 if (!tcp.readData((UCHAR*)&channelID, sizeof(ULONG)))
254 // If this read fails then the client just hasn't sent anything.
255 // If any of the lower reads fail, then break, the connection is probably dead
257 // check connection is ok
258 // if (tcp.isConnected()) continue;
260 log->log("Client", Log::DEBUG, "Disconnection detected");
264 channelID = ntohl(channelID);
267 if (!tcp.readData((UCHAR*)&requestID, sizeof(ULONG))) break;
268 requestID = ntohl(requestID);
270 if (!tcp.readData((UCHAR*)&opcode, sizeof(ULONG))) break;
271 opcode = ntohl(opcode);
273 if (!tcp.readData((UCHAR*)&extraDataLength, sizeof(ULONG))) break;
274 extraDataLength = ntohl(extraDataLength);
275 if (extraDataLength > 200000) // a random sanity limit
277 log->log("Client", Log::ERR, "ExtraDataLength > 200000!");
283 data = (UCHAR*)malloc(extraDataLength);
286 log->log("Client", Log::ERR, "Extra data buffer malloc error");
290 if (!tcp.readData(data, extraDataLength))
292 log->log("Client", Log::ERR, "Could not read extradata");
302 log->log("Client", Log::DEBUG, "Received chan=%lu, ser=%lu, op=%lu, edl=%lu", channelID, requestID, opcode, extraDataLength);
304 if (!loggedIn && (opcode != 1))
306 log->log("Client", Log::ERR, "Not logged in and opcode != 1");
307 if (data) free(data);
311 RequestPacket* req = new RequestPacket(requestID, opcode, data, extraDataLength);
312 rrproc.recvRequest(req);
314 else if (channelID == 3)
316 if (!tcp.readData((UCHAR*)&kaTimeStamp, sizeof(ULONG))) break;
317 kaTimeStamp = ntohl(kaTimeStamp);
319 log->log("Client", Log::DEBUG, "Received chan=%lu kats=%lu", channelID, kaTimeStamp);
323 p = (ULONG*)&buffer[0]; *p = htonl(3); // KA CHANNEL
324 p = (ULONG*)&buffer[4]; *p = htonl(kaTimeStamp);
325 if (!tcp.sendPacket(buffer, 8))
327 log->log("Client", Log::ERR, "Could not send back KA reply");
331 else if (channelID == 4)
333 if (!tcp.readData((UCHAR*)&logStringLen, sizeof(ULONG))) break;
334 logStringLen = ntohl(logStringLen);
336 log->log("Client", Log::DEBUG, "Received chan=%lu loglen=%lu", channelID, logStringLen);
338 UCHAR buffer[logStringLen + 1];
339 if (!tcp.readData((UCHAR*)&buffer, logStringLen)) break;
340 buffer[logStringLen] = '\0';
342 // log->log("Client", Log::INFO, "Client said: '%s'", buffer);
345 if (fputs((const char*)buffer, netLogFile) == EOF)
355 log->log("Client", Log::ERR, "Incoming channel number unknown");
361 ULLONG VompClient::ntohll(ULLONG a)
366 ULLONG VompClient::htonll(ULLONG a)
368 #if BYTE_ORDER == BIG_ENDIAN
373 b = ((a << 56) & 0xFF00000000000000ULL)
374 | ((a << 40) & 0x00FF000000000000ULL)
375 | ((a << 24) & 0x0000FF0000000000ULL)
376 | ((a << 8) & 0x000000FF00000000ULL)
377 | ((a >> 8) & 0x00000000FF000000ULL)
378 | ((a >> 24) & 0x0000000000FF0000ULL)
379 | ((a >> 40) & 0x000000000000FF00ULL)
380 | ((a >> 56) & 0x00000000000000FFULL) ;
386 #ifndef VOMPSTANDALONE
388 cChannel* VompClient::channelFromNumber(ULONG channelNumber)
390 cChannel* channel = NULL;
392 for (channel = Channels.First(); channel; channel = Channels.Next(channel))
394 if (!channel->GroupSep())
396 log->log("Client", Log::DEBUG, "Looking for channel %lu::: number: %i name: '%s'", channelNumber, channel->Number(), channel->Name());
398 if (channel->Number() == (int)channelNumber)
400 int vpid = channel->Vpid();
401 #if VDRVERSNUM < 10300
402 int apid1 = channel->Apid1();
404 int apid1 = channel->Apid(0);
406 log->log("Client", Log::DEBUG, "Found channel number %lu, vpid = %i, apid1 = %i", channelNumber, vpid, apid1);
414 log->log("Client", Log::DEBUG, "Channel not found");
420 void VompClient::writeResumeData()
422 /*config.setValueLong("ResumeData",
423 (char*)recplayer->getCurrentRecording()->FileName(),
424 recplayer->frameNumberFromPosition(recplayer->getLastPosition()) );*/
426 /* write to vdr resume file */
427 int resume = recplayer->frameNumberFromPosition(recplayer->getLastPosition());
428 char* ResumeIdC = config.getValueString("General", "ResumeId");
431 ResumeId = atoi(ResumeIdC);
436 cCondWait::SleepMs(100);
438 int OldSetupResumeID = Setup.ResumeID;
439 Setup.ResumeID = ResumeId; //UGLY: quickly change resumeid
440 #if VDRVERSNUM < 10703
441 cResumeFile ResumeFile((char*)recplayer->getCurrentRecording()->FileName()); //get corresponding resume file
443 cResumeFile ResumeFile((char*)recplayer->getCurrentRecording()->FileName(),(char*)recplayer->getCurrentRecording()->IsPesRecording()); //get corresponding resume file
445 Setup.ResumeID = OldSetupResumeID; //and restore it back
446 ResumeIDLock = false;
447 ResumeFile.Save(resume);
448 //isyslog("VOMPDEBUG: Saving resume = %i, ResumeId = %i",resume, ResumeId);