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 "vompclient.h"
23 #include "responsepacket.h"
25 #ifndef VOMPSTANDALONE
26 #include <vdr/channels.h>
27 #include <vdr/recording.h>
28 #include "recplayer.h"
29 #include "mvpreceiver.h"
34 pthread_mutex_t threadClientMutex;
35 int VompClient::nr_clients = 0;
38 VompClient::VompClient(Config* cfgBase, char* tconfigDir, int tsocket)
39 : rrproc(*this), tcp(tsocket), i18n(tconfigDir)
41 #ifndef VOMPSTANDALONE
44 recordingManager = NULL;
47 log = Log::getInstance();
49 configDir = tconfigDir;
50 log->log("Client", Log::DEBUG, "Config dir: %s", configDir);
57 VompClient::~VompClient()
59 log->log("Client", Log::DEBUG, "Vomp client destructor");
60 #ifndef VOMPSTANDALONE
71 delete recordingManager;
73 recordingManager = NULL;
76 if (loggedIn) cleanConfig();
80 void VompClient::incClients()
82 pthread_mutex_lock(&threadClientMutex);
83 VompClient::nr_clients++;
84 pthread_mutex_unlock(&threadClientMutex);
87 void VompClient::decClients()
89 pthread_mutex_lock(&threadClientMutex);
90 VompClient::nr_clients--;
91 pthread_mutex_unlock(&threadClientMutex);
94 int VompClient::getNrClients()
97 pthread_mutex_lock(&threadClientMutex);
98 nrClients = VompClient::nr_clients;
99 pthread_mutex_unlock(&threadClientMutex);
104 void VompClient::cleanConfig()
106 log->log("Client", Log::DEBUG, "Clean config");
108 #ifndef VOMPSTANDALONE
110 cRecordings Recordings;
115 char* resumes = config.getSectionKeyNames("ResumeData", numReturns, length);
116 char* position = resumes;
117 for(int k = 0; k < numReturns; k++)
119 log->log("Client", Log::DEBUG, "EXAMINING: %i %i %p %s", k, numReturns, position, position);
121 cRecording* recording = Recordings.GetByName(position);
124 // doesn't exist anymore
125 log->log("Client", Log::DEBUG, "Found a recording that doesn't exist anymore");
126 config.deleteValue("ResumeData", position);
130 log->log("Client", Log::DEBUG, "This recording still exists");
133 position += strlen(position) + 1;
140 void VompClientStartThread(void* arg)
142 VompClient* m = (VompClient*)arg;
144 // Nothing external to this class has a reference to it
145 // This is the end of the thread.. so delete m
150 int VompClient::run()
152 if (pthread_create(&runThread, NULL, (void*(*)(void*))VompClientStartThread, (void *)this) == -1) return 0;
153 log->log("Client", Log::DEBUG, "VompClient run success");
157 void VompClient::run2()
162 pthread_sigmask(SIG_BLOCK, &sigset, NULL);
163 pthread_detach(runThread); // Detach
165 tcp.disableReadTimeout();
167 tcp.setSoKeepTime(3);
168 tcp.setNonBlocking();
173 ULONG extraDataLength;
180 log->log("Client", Log::DEBUG, "Waiting");
182 if (!tcp.readData((UCHAR*)&channelID, sizeof(ULONG))) break;
183 channelID = ntohl(channelID);
186 if (!tcp.readData((UCHAR*)&requestID, sizeof(ULONG))) break;
187 requestID = ntohl(requestID);
189 if (!tcp.readData((UCHAR*)&opcode, sizeof(ULONG))) break;
190 opcode = ntohl(opcode);
192 if (!tcp.readData((UCHAR*)&extraDataLength, sizeof(ULONG))) break;
193 extraDataLength = ntohl(extraDataLength);
194 if (extraDataLength > 200000) // a random sanity limit
196 log->log("Client", Log::ERR, "ExtraDataLength > 200000!");
202 data = (UCHAR*)malloc(extraDataLength);
205 log->log("Client", Log::ERR, "Extra data buffer malloc error");
209 if (!tcp.readData(data, extraDataLength))
211 log->log("Client", Log::ERR, "Could not read extradata");
221 log->log("Client", Log::DEBUG, "Received chan=%lu, ser=%lu, op=%lu, edl=%lu", channelID, requestID, opcode, extraDataLength);
223 if (!loggedIn && (opcode != 1))
225 log->log("Client", Log::ERR, "Not logged in and opcode != 1");
226 if (data) free(data);
230 RequestPacket* req = new RequestPacket(requestID, opcode, data, extraDataLength);
231 rrproc.recvRequest(req);
233 else if (channelID == 3)
235 if (!tcp.readData((UCHAR*)&kaTimeStamp, sizeof(ULONG))) break;
236 kaTimeStamp = ntohl(kaTimeStamp);
238 log->log("Client", Log::DEBUG, "Received chan=%lu kats=%lu", channelID, kaTimeStamp);
241 *(ULONG*)&buffer[0] = htonl(3); // KA CHANNEL
242 *(ULONG*)&buffer[4] = htonl(kaTimeStamp);
243 if (!tcp.sendPacket(buffer, 8))
245 log->log("Client", Log::ERR, "Could not send back KA reply");
251 log->log("Client", Log::ERR, "Incoming channel number unknown");
257 ULLONG VompClient::ntohll(ULLONG a)
262 ULLONG VompClient::htonll(ULLONG a)
264 #if BYTE_ORDER == BIG_ENDIAN
269 b = ((a << 56) & 0xFF00000000000000ULL)
270 | ((a << 40) & 0x00FF000000000000ULL)
271 | ((a << 24) & 0x0000FF0000000000ULL)
272 | ((a << 8) & 0x000000FF00000000ULL)
273 | ((a >> 8) & 0x00000000FF000000ULL)
274 | ((a >> 24) & 0x0000000000FF0000ULL)
275 | ((a >> 40) & 0x000000000000FF00ULL)
276 | ((a >> 56) & 0x00000000000000FFULL) ;
282 #ifndef VOMPSTANDALONE
284 cChannel* VompClient::channelFromNumber(ULONG channelNumber)
286 cChannel* channel = NULL;
288 for (channel = Channels.First(); channel; channel = Channels.Next(channel))
290 if (!channel->GroupSep())
292 log->log("Client", Log::DEBUG, "Looking for channel %lu::: number: %i name: '%s'", channelNumber, channel->Number(), channel->Name());
294 if (channel->Number() == (int)channelNumber)
296 int vpid = channel->Vpid();
297 #if VDRVERSNUM < 10300
298 int apid1 = channel->Apid1();
300 int apid1 = channel->Apid(0);
302 log->log("Client", Log::DEBUG, "Found channel number %lu, vpid = %i, apid1 = %i", channelNumber, vpid, apid1);
310 log->log("Client", Log::DEBUG, "Channel not found");
316 void VompClient::writeResumeData()
318 config.setValueLong("ResumeData",
319 (char*)recplayer->getCurrentRecording()->FileName(),
320 recplayer->frameNumberFromPosition(recplayer->getLastPosition()) );