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);
224 processReScanRecording(data, packetLength);
232 void MVPClient::processLogin(unsigned char* buffer, int length)
234 time_t timeNow = time(NULL);
235 struct tm* timeStruct = localtime(&timeNow);
236 int timeOffset = timeStruct->tm_gmtoff;
238 unsigned char sendBuffer[12];
239 *(unsigned long*)&sendBuffer[0] = htonl(8);
240 *(unsigned long*)&sendBuffer[4] = htonl(timeNow);
241 *(signed int*)&sendBuffer[8] = htonl(timeOffset);
243 tcp.sendPacket(sendBuffer, 12);
244 log->log("Client", Log::DEBUG, "written login reply");
247 void MVPClient::processGetRecordingsList(unsigned char* data, int length)
249 unsigned char* sendBuffer = new unsigned char[50000]; // hope this is enough
250 int count = 4; // leave space for the packet length
255 int Percent = VideoDiskSpace(&FreeMB);
256 int Total = (FreeMB / (100 - Percent)) * 100;
258 *(unsigned long*)&sendBuffer[count] = htonl(Total);
259 count += sizeof(unsigned long);
260 *(unsigned long*)&sendBuffer[count] = htonl(FreeMB);
261 count += sizeof(unsigned long);
262 *(unsigned long*)&sendBuffer[count] = htonl(Percent);
263 count += sizeof(unsigned long);
266 cRecordings Recordings;
269 for (cRecording *recording = Recordings.First(); recording; recording = Recordings.Next(recording))
271 if (count > 49000) break; // just how big is that hard disk?!
272 *(unsigned long*)&sendBuffer[count] = htonl(recording->start);// + timeOffset);
275 point = (char*)recording->Name();
276 strcpy((char*)&sendBuffer[count], point);
277 count += strlen(point) + 1;
279 point = (char*)recording->FileName();
280 strcpy((char*)&sendBuffer[count], point);
281 count += strlen(point) + 1;
284 *(unsigned long*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
286 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(unsigned long*)&sendBuffer[0]));
288 tcp.sendPacket(sendBuffer, count);
290 log->log("Client", Log::DEBUG, "Written list");
293 void MVPClient::processDeleteRecording(unsigned char* data, int length)
295 // data is a pointer to the fileName string
297 cRecordings Recordings;
298 Recordings.Load(); // probably have to do this
300 cRecording* recording = Recordings.GetByName((char*)data);
302 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
306 log->log("Client", Log::DEBUG, "deleting recording: %s", recording->Name());
316 void MVPClient::processGetSummary(unsigned char* data, int length)
318 // data is a pointer to the fileName string
320 cRecordings Recordings;
321 Recordings.Load(); // probably have to do this
323 cRecording *recording = Recordings.GetByName((char*)data);
325 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
329 unsigned char* sendBuffer = new unsigned char[50000]; // hope this is enough
330 int count = 4; // leave space for the packet length
333 #if VDRVERSNUM < 10300
334 point = (char*)recording->Summary();
336 const cRecordingInfo *Info = recording->Info();
337 point = (char*)Info->ShortText();
338 log->log("Client", Log::DEBUG, "info pointer %p summary pointer %p", Info, point);
341 point = (char*)Info->Description();
342 log->log("Client", Log::DEBUG, "description pointer %p", point);
345 strcpy((char*)&sendBuffer[count], point);
346 count += strlen(point) + 1;
347 *(unsigned long*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
349 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(unsigned long*)&sendBuffer[0]));
351 tcp.sendPacket(sendBuffer, count);
353 log->log("Client", Log::DEBUG, "Written summary");
363 void MVPClient::processGetChannelsList(unsigned char* data, int length)
365 unsigned char* sendBuffer = new unsigned char[50000]; // FIXME hope this is enough
366 int count = 4; // leave space for the packet length
370 for (cChannel *channel = Channels.First(); channel; channel = Channels.Next(channel))
372 #if VDRVERSNUM < 10300
373 if (!channel->GroupSep() && !channel->Ca())
375 if (!channel->GroupSep() && !channel->Ca(0))
378 log->log("Client", Log::DEBUG, "name: '%s'", channel->Name());
380 if (channel->Vpid()) type = 1;
381 #if VDRVERSNUM < 10300
384 else if (channel->Apid(0)) type = 2;
388 if (count > 49000) break;
389 *(unsigned long*)&sendBuffer[count] = htonl(channel->Number());
392 *(unsigned long*)&sendBuffer[count] = htonl(type);
395 point = (char*)channel->Name();
396 strcpy((char*)&sendBuffer[count], point);
397 count += strlen(point) + 1;
401 *(unsigned long*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
403 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(unsigned long*)&sendBuffer[0]));
405 tcp.sendPacket(sendBuffer, count);
407 log->log("Client", Log::DEBUG, "Written channels list");
410 void MVPClient::processStartStreamingChannel(unsigned char* data, int length)
412 log->log("Client", Log::DEBUG, "length = %i", length);
413 unsigned long channelNumber = ntohl(*(unsigned long*)data);
415 cChannel* channel = channelFromNumber(channelNumber);
422 lp = MVPReceiver::create(channel);
441 void MVPClient::processStopStreaming(unsigned char* data, int length)
443 log->log("Client", Log::DEBUG, "STOP STREAMING RECEIVED");
454 delete recordingManager;
456 recordingManager = NULL;
462 void MVPClient::processGetBlock(unsigned char* data, int length)
466 log->log("Client", Log::DEBUG, "Get block called when no streaming happening!");
470 ULLONG position = ntohll(*(ULLONG*)data);
471 data += sizeof(ULLONG);
472 unsigned long amount = ntohl(*(unsigned long*)data);
474 log->log("Client", Log::DEBUG, "getblock pos = %llu length = %lu", position, amount);
476 unsigned char sendBuffer[amount + 4];
477 unsigned long amountReceived = 0; // compiler moan.
480 log->log("Client", Log::DEBUG, "getting from live");
481 amountReceived = lp->getBlock(&sendBuffer[4], amount);
485 // vdr has possibly disconnected the receiver
486 log->log("Client", Log::DEBUG, "VDR has disconnected the live receiver");
493 log->log("Client", Log::DEBUG, "getting from recording");
494 amountReceived = rp->getBlock(&sendBuffer[4], position, amount);
497 *(unsigned long*)&sendBuffer[0] = htonl(amountReceived);
498 tcp.sendPacket(sendBuffer, amountReceived + 4);
499 log->log("Client", Log::DEBUG, "written ok %lu", amountReceived);
502 void MVPClient::processStartStreamingRecording(unsigned char* data, int length)
504 // data is a pointer to the fileName string
506 recordingManager = new cRecordings;
507 recordingManager->Load();
509 cRecording* recording = recordingManager->GetByName((char*)data);
511 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
515 rp = new RecPlayer(recording);
517 unsigned char sendBuffer[12];
518 *(unsigned long*)&sendBuffer[0] = htonl(8);
519 *(ULLONG*)&sendBuffer[4] = htonll(rp->getTotalLength());
521 tcp.sendPacket(sendBuffer, 12);
522 log->log("Client", Log::DEBUG, "written totalLength");
526 delete recordingManager;
527 recordingManager = NULL;
531 void MVPClient::processReScanRecording(unsigned char* data, int length)
537 log->log("Client", Log::DEBUG, "Rescan recording called when no recording being played!");
542 retval = rp->getTotalLength();
545 unsigned char sendBuffer[12];
546 *(unsigned long*)&sendBuffer[0] = htonl(8);
547 *(ULLONG*)&sendBuffer[4] = htonll(retval);
549 tcp.sendPacket(sendBuffer, 12);
550 log->log("Client", Log::DEBUG, "Rescan recording, wrote new length to client");
553 void MVPClient::processGetChannelSchedule(unsigned char* data, int length)
555 ULONG channelNumber = ntohl(*(ULLONG*)data);
556 log->log("Client", Log::DEBUG, "get schedule called for channel %lu", channelNumber);
558 cChannel* channel = channelFromNumber(channelNumber);
561 unsigned char sendBuffer[4];
562 *(unsigned long*)&sendBuffer[0] = htonl(0);
563 tcp.sendPacket(sendBuffer, 4);
564 log->log("Client", Log::DEBUG, "written null");
568 #if VDRVERSNUM < 10300
569 cMutexLock MutexLock;
570 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
572 cSchedulesLock MutexLock;
573 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
577 unsigned char sendBuffer[8];
578 *(unsigned long*)&sendBuffer[0] = htonl(4);
579 *(unsigned long*)&sendBuffer[4] = htonl(0);
580 tcp.sendPacket(sendBuffer, 8);
581 log->log("Client", Log::DEBUG, "written 0");
585 unsigned char sendBuffer[8];
586 *(unsigned long*)&sendBuffer[0] = htonl(4);
587 *(unsigned long*)&sendBuffer[4] = htonl(1);
588 tcp.sendPacket(sendBuffer, 8);
589 log->log("Client", Log::DEBUG, "written 1");
594 void MVPClient::testChannelSchedule(unsigned char* data, int length)
596 FILE* f = fopen("/tmp/s.txt", "w");
598 #if VDRVERSNUM < 10300
599 cMutexLock MutexLock;
600 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
602 cSchedulesLock MutexLock;
603 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
607 fprintf(f, "Schedules = NULL\n");
612 fprintf(f, "Schedules dump:\n");
616 const cSchedule *Schedule;
617 int scheduleNumber = 0;
620 cChannel *thisChannel;
622 #if VDRVERSNUM < 10300
623 const cEventInfo *event;
629 // Schedule = Schedules->GetSchedule(channel->GetChannelID());
630 // Schedule = Schedules->GetSchedule();
631 Schedule = Schedules->First();
634 fprintf(f, "First Schedule = NULL\n");
641 fprintf(f, "Schedule #%i\n", scheduleNumber);
642 fprintf(f, "-------------\n\n");
644 #if VDRVERSNUM < 10300
645 tchid = Schedule->GetChannelID();
647 tchid = Schedule->ChannelID();
649 #if VDRVERSNUM < 10300
650 fprintf(f, "ChannelID.ToString() = %s\n", tchid.ToString());
651 fprintf(f, "NumEvents() = %i\n", Schedule->NumEvents());
653 // put the count at the end.
655 thisChannel = Channels.GetByChannelID(tchid, true);
658 fprintf(f, "Channel Number: %p %i\n", thisChannel, thisChannel->Number());
662 fprintf(f, "thisChannel = NULL for tchid\n");
665 #if VDRVERSNUM < 10300
666 for (eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
668 event = Schedule->GetEventNumber(eventNumber);
669 fprintf(f, "Event %i tableid = %i timestring = %s endtimestring = %s\n", eventNumber, event->GetTableID(), event->GetTimeString(), event->GetEndTimeString());
670 fprintf(f, "Event %i date = %s isfollowing = %i ispresent = %i\n", eventNumber, event->GetDate(), event->IsFollowing(), event->IsPresent());
671 fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
672 fprintf(f, "Event %i subtitle = %s title = %s\n", eventNumber, event->GetSubtitle(), event->GetTitle());
673 fprintf(f, "Event %i eventid = %u duration = %li time = %lu channelnumber = %i\n", eventNumber, event->GetEventID(), event->GetDuration(), event->GetTime(), event->GetChannelNumber());
674 fprintf(f, "Event %u dump:\n", eventNumber);
679 // This whole section needs rewriting to walk the list.
680 event = Schedule->Events()->First();
682 event = Schedule->Events()->Next(event);
687 fprintf(f, "\nDump from object:\n");
689 fprintf(f, "\nEND\n");
695 const cEventInfo *GetPresentEvent(void) const;
696 const cEventInfo *GetFollowingEvent(void) const;
697 const cEventInfo *GetEvent(unsigned short uEventID, time_t tTime = 0) const;
698 const cEventInfo *GetEventAround(time_t tTime) const;
699 const cEventInfo *GetEventNumber(int n) const { return Events.Get(n); }
702 const unsigned char GetTableID(void) const;
703 const char *GetTimeString(void) const;
704 const char *GetEndTimeString(void) const;
705 const char *GetDate(void) const;
706 bool IsFollowing(void) const;
707 bool IsPresent(void) const;
708 const char *GetExtendedDescription(void) const;
709 const char *GetSubtitle(void) const;
710 const char *GetTitle(void) const;
711 unsigned short GetEventID(void) const;
712 long GetDuration(void) const;
713 time_t GetTime(void) const;
714 tChannelID GetChannelID(void) const;
715 int GetChannelNumber(void) const { return nChannelNumber; }
716 void SetChannelNumber(int ChannelNumber) const { ((cEventInfo *)this)->nChannelNumber = ChannelNumber; } // doesn't modify the EIT data, so it's ok to make it 'const'
717 void Dump(FILE *f, const char *Prefix = "") const;
725 fprintf(f, "End of current Schedule\n\n\n");
727 Schedule = (const cSchedule *)Schedules->Next(Schedule);
734 void MVPClient::processConfigSave(unsigned char* buffer, int length)
736 char* section = (char*)buffer;
740 for (int k = 0; k < length; k++)
742 if (buffer[k] == '\0')
746 key = (char*)&buffer[k+1];
750 value = (char*)&buffer[k+1];
756 // if the last string (value) doesnt have null terminator, give up
757 if (buffer[length - 1] != '\0') return;
759 log->log("Client", Log::DEBUG, "Config save: %s %s %s", section, key, value);
760 if (config.setValueString(section, key, value))
770 void MVPClient::processConfigLoad(unsigned char* buffer, int length)
772 char* section = (char*)buffer;
775 for (int k = 0; k < length; k++)
777 if (buffer[k] == '\0')
779 key = (char*)&buffer[k+1];
784 char* value = config.getValueString(section, key);
788 unsigned char sendBuffer[4 + strlen(value) + 1];
789 *(unsigned long*)&sendBuffer[0] = htonl(strlen(value) + 1);
790 strcpy((char*)&sendBuffer[4], value);
791 tcp.sendPacket(sendBuffer, 4 + strlen(value) + 1);
793 log->log("Client", Log::DEBUG, "Written config load packet");
798 unsigned char sendBuffer[8];
799 *(unsigned long*)&sendBuffer[0] = htonl(4);
800 *(unsigned long*)&sendBuffer[4] = htonl(0);
801 tcp.sendPacket(sendBuffer, 8);
803 log->log("Client", Log::DEBUG, "Written config load failed packet");
807 void MVPClient::cleanConfig()
809 log->log("Client", Log::DEBUG, "Clean config");
811 cRecordings Recordings;
816 char* resumes = config.getSectionKeyNames("ResumeData", numReturns, length);
817 char* position = resumes;
818 for(int k = 0; k < numReturns; k++)
820 log->log("Client", Log::DEBUG, "EXAMINING: %i %i %p %s", k, numReturns, position, position);
822 cRecording* recording = Recordings.GetByName(position);
825 // doesn't exist anymore
826 log->log("Client", Log::DEBUG, "Found a recording that doesn't exist anymore");
827 config.deleteValue("ResumeData", position);
831 log->log("Client", Log::DEBUG, "This recording still exists");
834 position += strlen(position) + 1;