2 Copyright 2004-2005 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "mvpclient.h"
23 MVPClient::MVPClient(int tsocket)
28 recordingManager = NULL;
29 log = Log::getInstance();
31 // Get IP address of client for config module
34 struct sockaddr_in peer;
35 socklen_t salen = sizeof(struct sockaddr);
36 if(getpeername(tsocket, (struct sockaddr*)&peer, &salen) == 0)
38 strcpy(ipa, inet_ntoa(peer.sin_addr));
43 log->log("Client", Log::DEBUG, "Cannot get peer name!");
46 const char* configDir = cPlugin::ConfigDirectory();
49 log->log("Client", Log::DEBUG, "No config dir!");
53 char configFileName[PATH_MAX];
54 snprintf(configFileName, PATH_MAX - strlen(configDir) - strlen(ipa) - 20, "%s/vomp-%s.conf", configDir, ipa);
55 config.init(configFileName);
57 log->log("Client", Log::DEBUG, "Config file name: %s", configFileName);
59 // processGetChannelSchedule(NULL, 0);
65 MVPClient::~MVPClient()
67 log->log("Client", Log::DEBUG, "MVP client destructor");
78 delete recordingManager;
80 recordingManager = NULL;
86 cChannel* MVPClient::channelFromNumber(unsigned long channelNumber)
88 cChannel* channel = NULL;
90 for (channel = Channels.First(); channel; channel = Channels.Next(channel))
92 if (!channel->GroupSep())
94 log->log("Client", Log::DEBUG, "Looking for channel %lu::: number: %i name: '%s'", channelNumber, channel->Number(), channel->Name());
96 if (channel->Number() == (int)channelNumber)
98 int vpid = channel->Vpid();
99 #if VDRVERSNUM < 10300
100 int apid1 = channel->Apid1();
102 int apid1 = channel->Apid(0);
104 log->log("Client", Log::DEBUG, "Found channel number %lu, vpid = %i, apid1 = %i", channelNumber, vpid, apid1);
112 log->log("Client", Log::DEBUG, "Channel not found");
119 void MVPClient::writeResumeData()
121 config.setValueLongLong("ResumeData", (char*)rp->getCurrentRecording()->FileName(), rp->getLastPosition());
124 void MVPClient::sendULONG(ULONG ul)
126 unsigned char sendBuffer[8];
127 *(unsigned long*)&sendBuffer[0] = htonl(4);
128 *(unsigned long*)&sendBuffer[4] = htonl(ul);
130 tcp.sendPacket(sendBuffer, 8);
131 log->log("Client", Log::DEBUG, "written ULONG %lu", ul);
134 void MVPClientStartThread(void* arg)
136 MVPClient* m = (MVPClient*)arg;
138 // Nothing external to this class has a reference to it
139 // This is the end of the thread.. so delete m
146 if (pthread_create(&runThread, NULL, (void*(*)(void*))MVPClientStartThread, (void *)this) == -1) return 0;
147 log->log("Client", Log::DEBUG, "MVPClient run success");
151 void MVPClient::run2()
156 pthread_sigmask(SIG_BLOCK, &sigset, NULL);
157 pthread_detach(runThread); // Detach
159 tcp.disableReadTimeout();
161 tcp.setSoKeepTime(3);
162 tcp.setNonBlocking();
164 unsigned char* buffer;
167 unsigned long opcode;
171 log->log("Client", Log::DEBUG, "Waiting");
172 buffer = (unsigned char*)tcp.receivePacket();
173 log->log("Client", Log::DEBUG, "Received packet, length = %u", tcp.getDataLength());
176 log->log("Client", Log::DEBUG, "Detected connection closed");
180 packetLength = tcp.getDataLength() - 4;
181 opcode = ntohl(*(unsigned long*)buffer);
188 processLogin(data, packetLength);
191 processGetRecordingsList(data, packetLength);
194 processDeleteRecording(data, packetLength);
197 processGetSummary(data, packetLength);
200 processGetChannelsList(data, packetLength);
203 processStartStreamingChannel(data, packetLength);
206 processGetBlock(data, packetLength);
209 processStopStreaming(data, packetLength);
212 processStartStreamingRecording(data, packetLength);
215 processGetChannelSchedule(data, packetLength);
218 processConfigSave(data, packetLength);
221 processConfigLoad(data, packetLength);
229 void MVPClient::processLogin(unsigned char* buffer, int length)
231 time_t timeNow = time(NULL);
232 struct tm* timeStruct = localtime(&timeNow);
233 int timeOffset = timeStruct->tm_gmtoff;
235 unsigned char sendBuffer[12];
236 *(unsigned long*)&sendBuffer[0] = htonl(8);
237 *(unsigned long*)&sendBuffer[4] = htonl(timeNow);
238 *(signed int*)&sendBuffer[8] = htonl(timeOffset);
240 tcp.sendPacket(sendBuffer, 12);
241 log->log("Client", Log::DEBUG, "written login reply");
244 void MVPClient::processGetRecordingsList(unsigned char* data, int length)
246 unsigned char* sendBuffer = new unsigned char[50000]; // hope this is enough
247 int count = 4; // leave space for the packet length
252 int Percent = VideoDiskSpace(&FreeMB);
253 int Total = (FreeMB / (100 - Percent)) * 100;
255 *(unsigned long*)&sendBuffer[count] = htonl(Total);
256 count += sizeof(unsigned long);
257 *(unsigned long*)&sendBuffer[count] = htonl(FreeMB);
258 count += sizeof(unsigned long);
259 *(unsigned long*)&sendBuffer[count] = htonl(Percent);
260 count += sizeof(unsigned long);
263 cRecordings Recordings;
266 for (cRecording *recording = Recordings.First(); recording; recording = Recordings.Next(recording))
268 if (count > 49000) break; // just how big is that hard disk?!
269 *(unsigned long*)&sendBuffer[count] = htonl(recording->start);// + timeOffset);
272 point = (char*)recording->Name();
273 strcpy((char*)&sendBuffer[count], point);
274 count += strlen(point) + 1;
276 point = (char*)recording->FileName();
277 strcpy((char*)&sendBuffer[count], point);
278 count += strlen(point) + 1;
281 *(unsigned long*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
283 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(unsigned long*)&sendBuffer[0]));
285 tcp.sendPacket(sendBuffer, count);
287 log->log("Client", Log::DEBUG, "Written list");
290 void MVPClient::processDeleteRecording(unsigned char* data, int length)
292 // data is a pointer to the fileName string
294 cRecordings Recordings;
295 Recordings.Load(); // probably have to do this
297 cRecording* recording = Recordings.GetByName((char*)data);
299 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
303 log->log("Client", Log::DEBUG, "deleting recording: %s", recording->Name());
313 void MVPClient::processGetSummary(unsigned char* data, int length)
315 // data is a pointer to the fileName string
317 cRecordings Recordings;
318 Recordings.Load(); // probably have to do this
320 cRecording *recording = Recordings.GetByName((char*)data);
322 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
326 unsigned char* sendBuffer = new unsigned char[50000]; // hope this is enough
327 int count = 4; // leave space for the packet length
330 #if VDRVERSNUM < 10300
331 point = (char*)recording->Summary();
333 const cRecordingInfo *Info = recording->Info();
334 point = (char*)Info->ShortText();
335 log->log("Client", Log::DEBUG, "info pointer %p summary pointer %p", Info, point);
338 point = (char*)Info->Description();
339 log->log("Client", Log::DEBUG, "description pointer %p", point);
342 strcpy((char*)&sendBuffer[count], point);
343 count += strlen(point) + 1;
344 *(unsigned long*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
346 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(unsigned long*)&sendBuffer[0]));
348 tcp.sendPacket(sendBuffer, count);
350 log->log("Client", Log::DEBUG, "Written summary");
360 void MVPClient::processGetChannelsList(unsigned char* data, int length)
362 unsigned char* sendBuffer = new unsigned char[50000]; // FIXME hope this is enough
363 int count = 4; // leave space for the packet length
367 for (cChannel *channel = Channels.First(); channel; channel = Channels.Next(channel))
369 #if VDRVERSNUM < 10300
370 if (!channel->GroupSep() && !channel->Ca())
372 if (!channel->GroupSep() && !channel->Ca(0))
375 log->log("Client", Log::DEBUG, "name: '%s'", channel->Name());
377 if (channel->Vpid()) type = 1;
378 #if VDRVERSNUM < 10300
381 else if (channel->Apid(0)) type = 2;
385 if (count > 49000) break;
386 *(unsigned long*)&sendBuffer[count] = htonl(channel->Number());
389 *(unsigned long*)&sendBuffer[count] = htonl(type);
392 point = (char*)channel->Name();
393 strcpy((char*)&sendBuffer[count], point);
394 count += strlen(point) + 1;
398 *(unsigned long*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
400 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(unsigned long*)&sendBuffer[0]));
402 tcp.sendPacket(sendBuffer, count);
404 log->log("Client", Log::DEBUG, "Written channels list");
407 void MVPClient::processStartStreamingChannel(unsigned char* data, int length)
409 log->log("Client", Log::DEBUG, "length = %i", length);
410 unsigned long channelNumber = ntohl(*(unsigned long*)data);
412 cChannel* channel = channelFromNumber(channelNumber);
419 lp = MVPReceiver::create(channel);
438 void MVPClient::processStopStreaming(unsigned char* data, int length)
440 log->log("Client", Log::DEBUG, "STOP STREAMING RECEIVED");
451 delete recordingManager;
453 recordingManager = NULL;
459 void MVPClient::processGetBlock(unsigned char* data, int length)
463 log->log("Client", Log::DEBUG, "Get block called when no streaming happening!");
467 ULLONG position = ntohll(*(ULLONG*)data);
468 data += sizeof(ULLONG);
469 unsigned long amount = ntohl(*(unsigned long*)data);
471 log->log("Client", Log::DEBUG, "getblock pos = %llu length = %lu", position, amount);
473 unsigned char sendBuffer[amount + 4];
474 unsigned long amountReceived = 0; // compiler moan.
477 log->log("Client", Log::DEBUG, "getting from live");
478 amountReceived = lp->getBlock(&sendBuffer[4], amount);
482 // vdr has possibly disconnected the receiver
483 log->log("Client", Log::DEBUG, "VDR has disconnected the live receiver");
490 log->log("Client", Log::DEBUG, "getting from recording");
491 amountReceived = rp->getBlock(&sendBuffer[4], position, amount);
494 *(unsigned long*)&sendBuffer[0] = htonl(amountReceived);
495 tcp.sendPacket(sendBuffer, amountReceived + 4);
496 log->log("Client", Log::DEBUG, "written ok %lu", amountReceived);
499 void MVPClient::processStartStreamingRecording(unsigned char* data, int length)
501 // data is a pointer to the fileName string
503 recordingManager = new cRecordings;
504 recordingManager->Load();
506 cRecording* recording = recordingManager->GetByName((char*)data);
508 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
512 rp = new RecPlayer(recording);
514 unsigned char sendBuffer[12];
515 *(unsigned long*)&sendBuffer[0] = htonl(8);
516 *(ULLONG*)&sendBuffer[4] = htonll(rp->getTotalLength());
518 tcp.sendPacket(sendBuffer, 12);
519 log->log("Client", Log::DEBUG, "written totalLength");
523 delete recordingManager;
524 recordingManager = NULL;
528 void MVPClient::processGetChannelSchedule(unsigned char* data, int length)
530 ULONG channelNumber = ntohl(*(ULLONG*)data);
531 log->log("Client", Log::DEBUG, "get schedule called for channel %lu", channelNumber);
533 cChannel* channel = channelFromNumber(channelNumber);
536 unsigned char sendBuffer[4];
537 *(unsigned long*)&sendBuffer[0] = htonl(0);
538 tcp.sendPacket(sendBuffer, 4);
539 log->log("Client", Log::DEBUG, "written null");
543 #if VDRVERSNUM < 10300
544 cMutexLock MutexLock;
545 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
547 cSchedulesLock MutexLock;
548 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
552 unsigned char sendBuffer[8];
553 *(unsigned long*)&sendBuffer[0] = htonl(4);
554 *(unsigned long*)&sendBuffer[4] = htonl(0);
555 tcp.sendPacket(sendBuffer, 8);
556 log->log("Client", Log::DEBUG, "written 0");
560 unsigned char sendBuffer[8];
561 *(unsigned long*)&sendBuffer[0] = htonl(4);
562 *(unsigned long*)&sendBuffer[4] = htonl(1);
563 tcp.sendPacket(sendBuffer, 8);
564 log->log("Client", Log::DEBUG, "written 1");
569 void MVPClient::testChannelSchedule(unsigned char* data, int length)
571 FILE* f = fopen("/tmp/s.txt", "w");
573 #if VDRVERSNUM < 10300
574 cMutexLock MutexLock;
575 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
577 cSchedulesLock MutexLock;
578 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
582 fprintf(f, "Schedules = NULL\n");
587 fprintf(f, "Schedules dump:\n");
591 const cSchedule *Schedule;
592 int scheduleNumber = 0;
595 cChannel *thisChannel;
597 #if VDRVERSNUM < 10300
598 const cEventInfo *event;
604 // Schedule = Schedules->GetSchedule(channel->GetChannelID());
605 // Schedule = Schedules->GetSchedule();
606 Schedule = Schedules->First();
609 fprintf(f, "First Schedule = NULL\n");
616 fprintf(f, "Schedule #%i\n", scheduleNumber);
617 fprintf(f, "-------------\n\n");
619 #if VDRVERSNUM < 10300
620 tchid = Schedule->GetChannelID();
622 tchid = Schedule->ChannelID();
624 #if VDRVERSNUM < 10300
625 fprintf(f, "ChannelID.ToString() = %s\n", tchid.ToString());
626 fprintf(f, "NumEvents() = %i\n", Schedule->NumEvents());
628 // put the count at the end.
630 thisChannel = Channels.GetByChannelID(tchid, true);
633 fprintf(f, "Channel Number: %p %i\n", thisChannel, thisChannel->Number());
637 fprintf(f, "thisChannel = NULL for tchid\n");
640 #if VDRVERSNUM < 10300
641 for (eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
643 event = Schedule->GetEventNumber(eventNumber);
644 fprintf(f, "Event %i tableid = %i timestring = %s endtimestring = %s\n", eventNumber, event->GetTableID(), event->GetTimeString(), event->GetEndTimeString());
645 fprintf(f, "Event %i date = %s isfollowing = %i ispresent = %i\n", eventNumber, event->GetDate(), event->IsFollowing(), event->IsPresent());
646 fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
647 fprintf(f, "Event %i subtitle = %s title = %s\n", eventNumber, event->GetSubtitle(), event->GetTitle());
648 fprintf(f, "Event %i eventid = %u duration = %li time = %lu channelnumber = %i\n", eventNumber, event->GetEventID(), event->GetDuration(), event->GetTime(), event->GetChannelNumber());
649 fprintf(f, "Event %u dump:\n", eventNumber);
654 // This whole section needs rewriting to walk the list.
655 event = Schedule->Events()->First();
657 event = Schedule->Events()->Next(event);
662 fprintf(f, "\nDump from object:\n");
664 fprintf(f, "\nEND\n");
670 const cEventInfo *GetPresentEvent(void) const;
671 const cEventInfo *GetFollowingEvent(void) const;
672 const cEventInfo *GetEvent(unsigned short uEventID, time_t tTime = 0) const;
673 const cEventInfo *GetEventAround(time_t tTime) const;
674 const cEventInfo *GetEventNumber(int n) const { return Events.Get(n); }
677 const unsigned char GetTableID(void) const;
678 const char *GetTimeString(void) const;
679 const char *GetEndTimeString(void) const;
680 const char *GetDate(void) const;
681 bool IsFollowing(void) const;
682 bool IsPresent(void) const;
683 const char *GetExtendedDescription(void) const;
684 const char *GetSubtitle(void) const;
685 const char *GetTitle(void) const;
686 unsigned short GetEventID(void) const;
687 long GetDuration(void) const;
688 time_t GetTime(void) const;
689 tChannelID GetChannelID(void) const;
690 int GetChannelNumber(void) const { return nChannelNumber; }
691 void SetChannelNumber(int ChannelNumber) const { ((cEventInfo *)this)->nChannelNumber = ChannelNumber; } // doesn't modify the EIT data, so it's ok to make it 'const'
692 void Dump(FILE *f, const char *Prefix = "") const;
700 fprintf(f, "End of current Schedule\n\n\n");
702 Schedule = (const cSchedule *)Schedules->Next(Schedule);
709 void MVPClient::processConfigSave(unsigned char* buffer, int length)
711 char* section = (char*)buffer;
715 for (int k = 0; k < length; k++)
717 if (buffer[k] == '\0')
721 key = (char*)&buffer[k+1];
725 value = (char*)&buffer[k+1];
731 // if the last string (value) doesnt have null terminator, give up
732 if (buffer[length - 1] != '\0') return;
734 log->log("Client", Log::DEBUG, "Config save: %s %s %s", section, key, value);
735 if (config.setValueString(section, key, value))
745 void MVPClient::processConfigLoad(unsigned char* buffer, int length)
747 char* section = (char*)buffer;
750 for (int k = 0; k < length; k++)
752 if (buffer[k] == '\0')
754 key = (char*)&buffer[k+1];
759 char* value = config.getValueString(section, key);
763 unsigned char sendBuffer[4 + strlen(value) + 1];
764 *(unsigned long*)&sendBuffer[0] = htonl(strlen(value) + 1);
765 strcpy((char*)&sendBuffer[4], value);
766 tcp.sendPacket(sendBuffer, 4 + strlen(value) + 1);
768 log->log("Client", Log::DEBUG, "Written config load packet");
773 unsigned char sendBuffer[8];
774 *(unsigned long*)&sendBuffer[0] = htonl(4);
775 *(unsigned long*)&sendBuffer[4] = htonl(0);
776 tcp.sendPacket(sendBuffer, 8);
778 log->log("Client", Log::DEBUG, "Written config load failed packet");
782 void MVPClient::cleanConfig()
784 log->log("Client", Log::DEBUG, "Clean config");
786 cRecordings Recordings;
791 char* resumes = config.getSectionKeyNames("ResumeData", numReturns, length);
792 char* position = resumes;
793 for(int k = 0; k < numReturns; k++)
795 log->log("Client", Log::DEBUG, "EXAMINING: %i %i %p %s", k, numReturns, position, position);
797 cRecording* recording = Recordings.GetByName(position);
800 // doesn't exist anymore
801 log->log("Client", Log::DEBUG, "Found a recording that doesn't exist anymore");
802 config.deleteValue("ResumeData", position);
806 log->log("Client", Log::DEBUG, "This recording still exists");
809 position += strlen(position) + 1;