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);
62 VompClient::~VompClient()
64 log->log("Client", Log::DEBUG, "Vomp client destructor");
65 #ifndef VOMPSTANDALONE
76 delete recordingManager;
78 recordingManager = NULL;
81 if (loggedIn) cleanConfig();
85 void VompClient::incClients()
87 pthread_mutex_lock(&threadClientMutex);
88 VompClient::nr_clients++;
89 pthread_mutex_unlock(&threadClientMutex);
92 void VompClient::decClients()
94 pthread_mutex_lock(&threadClientMutex);
95 VompClient::nr_clients--;
96 pthread_mutex_unlock(&threadClientMutex);
99 int VompClient::getNrClients()
102 pthread_mutex_lock(&threadClientMutex);
103 nrClients = VompClient::nr_clients;
104 pthread_mutex_unlock(&threadClientMutex);
109 void VompClient::cleanConfig()
111 log->log("Client", Log::DEBUG, "Clean config");
113 #ifndef VOMPSTANDALONE
115 cRecordings Recordings;
120 char* resumes = config.getSectionKeyNames("ResumeData", numReturns, length);
121 char* position = resumes;
122 for(int k = 0; k < numReturns; k++)
124 log->log("Client", Log::DEBUG, "EXAMINING: %i %i %p %s", k, numReturns, position, position);
126 cRecording* recording = Recordings.GetByName(position);
129 // doesn't exist anymore
130 log->log("Client", Log::DEBUG, "Found a recording that doesn't exist anymore");
131 config.deleteValue("ResumeData", position);
135 log->log("Client", Log::DEBUG, "This recording still exists");
138 position += strlen(position) + 1;
145 void VompClientStartThread(void* arg)
147 VompClient* m = (VompClient*)arg;
149 // Nothing external to this class has a reference to it
150 // This is the end of the thread.. so delete m
155 int VompClient::run()
157 if (pthread_create(&runThread, NULL, (void*(*)(void*))VompClientStartThread, (void *)this) == -1) return 0;
158 log->log("Client", Log::DEBUG, "VompClient run success");
162 void VompClient::run2()
167 pthread_sigmask(SIG_BLOCK, &sigset, NULL);
168 pthread_detach(runThread); // Detach
170 // tcp.disableReadTimeout();
172 // tcp.setSoKeepTime(3);
173 tcp.setNonBlocking();
178 ULONG extraDataLength;
185 log->log("Client", Log::DEBUG, "Waiting");
187 if (!tcp.readData((UCHAR*)&channelID, sizeof(ULONG)))
189 // If this read fails then the client just hasn't sent anything.
190 // If any of the lower reads fail, then break, the connection is probably dead
192 // check connection is ok
193 // if (tcp.isConnected()) continue;
195 log->log("Client", Log::DEBUG, "Disconnection detected");
199 channelID = ntohl(channelID);
202 if (!tcp.readData((UCHAR*)&requestID, sizeof(ULONG))) break;
203 requestID = ntohl(requestID);
205 if (!tcp.readData((UCHAR*)&opcode, sizeof(ULONG))) break;
206 opcode = ntohl(opcode);
208 if (!tcp.readData((UCHAR*)&extraDataLength, sizeof(ULONG))) break;
209 extraDataLength = ntohl(extraDataLength);
210 if (extraDataLength > 200000) // a random sanity limit
212 log->log("Client", Log::ERR, "ExtraDataLength > 200000!");
218 data = (UCHAR*)malloc(extraDataLength);
221 log->log("Client", Log::ERR, "Extra data buffer malloc error");
225 if (!tcp.readData(data, extraDataLength))
227 log->log("Client", Log::ERR, "Could not read extradata");
237 log->log("Client", Log::DEBUG, "Received chan=%lu, ser=%lu, op=%lu, edl=%lu", channelID, requestID, opcode, extraDataLength);
239 if (!loggedIn && (opcode != 1))
241 log->log("Client", Log::ERR, "Not logged in and opcode != 1");
242 if (data) free(data);
246 RequestPacket* req = new RequestPacket(requestID, opcode, data, extraDataLength);
247 rrproc.recvRequest(req);
249 else if (channelID == 3)
251 if (!tcp.readData((UCHAR*)&kaTimeStamp, sizeof(ULONG))) break;
252 kaTimeStamp = ntohl(kaTimeStamp);
254 log->log("Client", Log::DEBUG, "Received chan=%lu kats=%lu", channelID, kaTimeStamp);
257 *(ULONG*)&buffer[0] = htonl(3); // KA CHANNEL
258 *(ULONG*)&buffer[4] = htonl(kaTimeStamp);
259 if (!tcp.sendPacket(buffer, 8))
261 log->log("Client", Log::ERR, "Could not send back KA reply");
267 log->log("Client", Log::ERR, "Incoming channel number unknown");
273 ULLONG VompClient::ntohll(ULLONG a)
278 ULLONG VompClient::htonll(ULLONG a)
280 #if BYTE_ORDER == BIG_ENDIAN
285 b = ((a << 56) & 0xFF00000000000000ULL)
286 | ((a << 40) & 0x00FF000000000000ULL)
287 | ((a << 24) & 0x0000FF0000000000ULL)
288 | ((a << 8) & 0x000000FF00000000ULL)
289 | ((a >> 8) & 0x00000000FF000000ULL)
290 | ((a >> 24) & 0x0000000000FF0000ULL)
291 | ((a >> 40) & 0x000000000000FF00ULL)
292 | ((a >> 56) & 0x00000000000000FFULL) ;
298 #ifndef VOMPSTANDALONE
300 cChannel* VompClient::channelFromNumber(ULONG channelNumber)
302 cChannel* channel = NULL;
304 for (channel = Channels.First(); channel; channel = Channels.Next(channel))
306 if (!channel->GroupSep())
308 log->log("Client", Log::DEBUG, "Looking for channel %lu::: number: %i name: '%s'", channelNumber, channel->Number(), channel->Name());
310 if (channel->Number() == (int)channelNumber)
312 int vpid = channel->Vpid();
313 #if VDRVERSNUM < 10300
314 int apid1 = channel->Apid1();
316 int apid1 = channel->Apid(0);
318 log->log("Client", Log::DEBUG, "Found channel number %lu, vpid = %i, apid1 = %i", channelNumber, vpid, apid1);
326 log->log("Client", Log::DEBUG, "Channel not found");
332 void VompClient::writeResumeData()
334 config.setValueLong("ResumeData",
335 (char*)recplayer->getCurrentRecording()->FileName(),
336 recplayer->frameNumberFromPosition(recplayer->getLastPosition()) );