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;
30 // Get IP address of client for config module
33 struct sockaddr_in peer;
34 socklen_t salen = sizeof(struct sockaddr);
35 if(getpeername(tsocket, (struct sockaddr*)&peer, &salen) == 0)
37 strcpy(ipa, inet_ntoa(peer.sin_addr));
42 printf("Cannot get peer name!\n");
45 const char* configDir = cPlugin::ConfigDirectory();
48 printf("No config dir!\n");
52 char configFileName[PATH_MAX];
53 snprintf(configFileName, PATH_MAX - strlen(configDir) - strlen(ipa) - 20, "%s/vomp-%s.conf", configDir, ipa);
54 config.init(configFileName);
56 printf("Config file name: %s\n", configFileName);
58 // processGetChannelSchedule(NULL, 0);
65 MVPClient::~MVPClient()
67 printf("MVP client destructor\n");
79 delete recordingManager;
81 recordingManager = NULL;
87 cChannel* MVPClient::channelFromNumber(unsigned long channelNumber)
89 cChannel* channel = NULL;
91 for (channel = Channels.First(); channel; channel = Channels.Next(channel))
93 if (!channel->GroupSep())
95 printf("Looking for channel %lu::: number: %i name: '%s'\n", channelNumber, channel->Number(), channel->Name());
97 if (channel->Number() == (int)channelNumber)
99 int vpid = channel->Vpid();
100 #if VDRVERSNUM < 10300
101 int apid1 = channel->Apid1();
103 int apid1 = channel->Apid(0);
105 printf("Found channel number %lu, vpid = %i, apid1 = %i\n", channelNumber, vpid, apid1);
113 printf("Channel not found\n");
120 void MVPClient::writeResumeData()
122 config.setValueLongLong("ResumeData", (char*)rp->getCurrentRecording()->FileName(), rp->getLastPosition());
125 void MVPClient::sendULONG(ULONG ul)
127 unsigned char sendBuffer[8];
128 *(unsigned long*)&sendBuffer[0] = htonl(4);
129 *(unsigned long*)&sendBuffer[4] = htonl(ul);
131 tcp.sendPacket(sendBuffer, 8);
132 printf("written ULONG %lu\n", ul);
135 void MVPClientStartThread(void* arg)
137 MVPClient* m = (MVPClient*)arg;
139 // Nothing external to this class has a reference to it
140 // This is the end of the thread.. so delete m
147 if (pthread_create(&runThread, NULL, (void*(*)(void*))MVPClientStartThread, (void *)this) == -1) return 0;
148 printf("MVPClient run success\n");
152 void MVPClient::run2()
157 pthread_sigmask(SIG_BLOCK, &sigset, NULL);
158 pthread_detach(runThread); // Detach
160 tcp.disableReadTimeout();
162 tcp.setSoKeepTime(3);
163 tcp.setNonBlocking();
165 unsigned char* buffer;
168 unsigned long opcode;
172 printf("starting wait\n");
173 buffer = (unsigned char*)tcp.receivePacket();
174 printf("back from wait\n");
177 printf("Detected connection closed\n");
181 packetLength = tcp.getDataLength() - 4;
182 opcode = ntohl(*(unsigned long*)buffer);
189 processLogin(data, packetLength);
192 processGetRecordingsList(data, packetLength);
195 processDeleteRecording(data, packetLength);
198 processGetSummary(data, packetLength);
201 processGetChannelsList(data, packetLength);
204 processStartStreamingChannel(data, packetLength);
207 processGetBlock(data, packetLength);
210 processStopStreaming(data, packetLength);
213 processStartStreamingRecording(data, packetLength);
216 processGetChannelSchedule(data, packetLength);
219 processConfigSave(data, packetLength);
222 processConfigLoad(data, packetLength);
230 void MVPClient::processLogin(unsigned char* buffer, int length)
232 time_t timeNow = time(NULL);
233 struct tm* timeStruct = localtime(&timeNow);
234 int timeOffset = timeStruct->tm_gmtoff;
236 unsigned char sendBuffer[12];
237 *(unsigned long*)&sendBuffer[0] = htonl(8);
238 *(unsigned long*)&sendBuffer[4] = htonl(timeNow);
239 *(signed int*)&sendBuffer[8] = htonl(timeOffset);
241 tcp.sendPacket(sendBuffer, 12);
242 printf("written login reply\n");
245 void MVPClient::processGetRecordingsList(unsigned char* data, int length)
247 unsigned char* sendBuffer = new unsigned char[50000]; // hope this is enough
248 int count = 4; // leave space for the packet length
253 int Percent = VideoDiskSpace(&FreeMB);
254 int Total = (FreeMB / (100 - Percent)) * 100;
256 *(unsigned long*)&sendBuffer[count] = htonl(Total);
257 count += sizeof(unsigned long);
258 *(unsigned long*)&sendBuffer[count] = htonl(FreeMB);
259 count += sizeof(unsigned long);
260 *(unsigned long*)&sendBuffer[count] = htonl(Percent);
261 count += sizeof(unsigned long);
264 cRecordings Recordings;
267 for (cRecording *recording = Recordings.First(); recording; recording = Recordings.Next(recording))
269 if (count > 49000) break; // just how big is that hard disk?!
270 *(unsigned long*)&sendBuffer[count] = htonl(recording->start);// + timeOffset);
273 point = (char*)recording->Name();
274 strcpy((char*)&sendBuffer[count], point);
275 count += strlen(point) + 1;
277 point = (char*)recording->FileName();
278 strcpy((char*)&sendBuffer[count], point);
279 count += strlen(point) + 1;
282 *(unsigned long*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
284 printf("recorded size as %u\n", ntohl(*(unsigned long*)&sendBuffer[0]));
286 tcp.sendPacket(sendBuffer, count);
288 printf("Written list\n");
291 void MVPClient::processDeleteRecording(unsigned char* data, int length)
293 // data is a pointer to the fileName string
295 cRecordings Recordings;
296 Recordings.Load(); // probably have to do this
298 cRecording* recording = Recordings.GetByName((char*)data);
300 printf("recording pointer %p\n", recording);
304 printf("deleting recording: %s\n", recording->Name());
314 void MVPClient::processGetSummary(unsigned char* data, int length)
316 // data is a pointer to the fileName string
318 cRecordings Recordings;
319 Recordings.Load(); // probably have to do this
321 cRecording *recording = Recordings.GetByName((char*)data);
323 printf("recording pointer %p\n", recording);
327 unsigned char* sendBuffer = new unsigned char[50000]; // hope this is enough
328 int count = 4; // leave space for the packet length
331 #if VDRVERSNUM < 10300
332 point = (char*)recording->Summary();
334 point = (char*)recording->Info()->ShortText();
336 strcpy((char*)&sendBuffer[count], point);
337 count += strlen(point) + 1;
338 *(unsigned long*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
340 printf("recorded size as %u\n", ntohl(*(unsigned long*)&sendBuffer[0]));
342 tcp.sendPacket(sendBuffer, count);
344 printf("Written summary\n");
354 void MVPClient::processGetChannelsList(unsigned char* data, int length)
356 unsigned char* sendBuffer = new unsigned char[50000]; // FIXME hope this is enough
357 int count = 4; // leave space for the packet length
361 for (cChannel *channel = Channels.First(); channel; channel = Channels.Next(channel))
363 if (!channel->GroupSep())
365 printf("name: '%s'\n", channel->Name());
367 if (count > 49000) break;
368 *(unsigned long*)&sendBuffer[count] = htonl(channel->Number());
371 if (channel->Vpid()) type = 1;
374 *(unsigned long*)&sendBuffer[count] = htonl(type);
377 point = (char*)channel->Name();
378 strcpy((char*)&sendBuffer[count], point);
379 count += strlen(point) + 1;
383 *(unsigned long*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
385 printf("recorded size as %u\n", ntohl(*(unsigned long*)&sendBuffer[0]));
387 tcp.sendPacket(sendBuffer, count);
389 printf("Written channels list\n");
392 void MVPClient::processStartStreamingChannel(unsigned char* data, int length)
394 printf("length = %i\n", length);
395 unsigned long channelNumber = ntohl(*(unsigned long*)data);
397 cChannel* channel = channelFromNumber(channelNumber);
404 // MVPReceiver* m = new MVPReceiver(channel->Vpid(), channel->Apid1());
405 cm = new cMediamvpTransceiver(channel, 0, 0, cDevice::ActualDevice());
406 cDevice::ActualDevice()->AttachReceiver(cm);
407 //cDevice::ActualDevice()->SwitchChannel(channel, false);
412 void MVPClient::processStopStreaming(unsigned char* data, int length)
414 printf("STOP STREAMING RECEIVED\n");
425 delete recordingManager;
427 recordingManager = NULL;
433 void MVPClient::processGetBlock(unsigned char* data, int length)
437 printf("Get block called when no streaming happening!\n");
441 ULLONG position = ntohll(*(ULLONG*)data);
442 printf("getblock called for position = %llu\n", position);
444 data += sizeof(ULLONG);
446 unsigned long amount = ntohl(*(unsigned long*)data);
447 printf("getblock called for length = %lu\n", amount);
449 unsigned char sendBuffer[amount + 4];
450 unsigned long amountReceived = 0; // compiler moan.
453 printf("getting from live\n");
454 amountReceived = cm->getBlock(&sendBuffer[4], amount);
458 printf("getting from recording\n");
459 amountReceived = rp->getBlock(&sendBuffer[4], position, amount);
462 *(unsigned long*)&sendBuffer[0] = htonl(amountReceived);
463 printf("sendpacket go\n");
464 tcp.sendPacket(sendBuffer, amountReceived + 4);
465 printf("written ok %lu\n", amountReceived);
468 void MVPClient::processStartStreamingRecording(unsigned char* data, int length)
470 // data is a pointer to the fileName string
472 recordingManager = new cRecordings;
473 recordingManager->Load();
475 cRecording* recording = recordingManager->GetByName((char*)data);
477 printf("recording pointer %p\n", recording);
481 rp = new RecPlayer(recording);
483 unsigned char sendBuffer[12];
484 *(unsigned long*)&sendBuffer[0] = htonl(8);
485 *(ULLONG*)&sendBuffer[4] = htonll(rp->getTotalLength());
487 tcp.sendPacket(sendBuffer, 12);
488 printf("written totalLength\n");
492 delete recordingManager;
493 recordingManager = NULL;
497 void MVPClient::processGetChannelSchedule(unsigned char* data, int length)
499 ULONG channelNumber = ntohl(*(ULLONG*)data);
500 printf("get schedule called for channel %lu\n", channelNumber);
502 cChannel* channel = channelFromNumber(channelNumber);
505 unsigned char sendBuffer[4];
506 *(unsigned long*)&sendBuffer[0] = htonl(0);
507 tcp.sendPacket(sendBuffer, 4);
508 printf("written null\n");
512 #if VDRVERSNUM < 10300
513 cMutexLock MutexLock;
514 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
516 cSchedulesLock MutexLock;
517 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
521 unsigned char sendBuffer[8];
522 *(unsigned long*)&sendBuffer[0] = htonl(4);
523 *(unsigned long*)&sendBuffer[4] = htonl(0);
524 tcp.sendPacket(sendBuffer, 8);
525 printf("written 0\n");
529 unsigned char sendBuffer[8];
530 *(unsigned long*)&sendBuffer[0] = htonl(4);
531 *(unsigned long*)&sendBuffer[4] = htonl(1);
532 tcp.sendPacket(sendBuffer, 8);
533 printf("written 1\n");
538 void MVPClient::testChannelSchedule(unsigned char* data, int length)
540 FILE* f = fopen("/tmp/s.txt", "w");
542 #if VDRVERSNUM < 10300
543 cMutexLock MutexLock;
544 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
546 cSchedulesLock MutexLock;
547 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
551 fprintf(f, "Schedules = NULL\n");
556 fprintf(f, "Schedules dump:\n");
560 const cSchedule *Schedule;
561 int scheduleNumber = 0;
564 cChannel *thisChannel;
566 #if VDRVERSNUM < 10300
567 const cEventInfo *event;
573 // Schedule = Schedules->GetSchedule(channel->GetChannelID());
574 // Schedule = Schedules->GetSchedule();
575 Schedule = Schedules->First();
578 fprintf(f, "First Schedule = NULL\n");
585 fprintf(f, "Schedule #%i\n", scheduleNumber);
586 fprintf(f, "-------------\n\n");
588 #if VDRVERSNUM < 10300
589 tchid = Schedule->GetChannelID();
591 tchid = Schedule->ChannelID();
593 #if VDRVERSNUM < 10300
594 fprintf(f, "ChannelID.ToString() = %s\n", tchid.ToString());
595 fprintf(f, "NumEvents() = %i\n", Schedule->NumEvents());
597 // put the count at the end.
599 thisChannel = Channels.GetByChannelID(tchid, true);
602 fprintf(f, "Channel Number: %p %i\n", thisChannel, thisChannel->Number());
606 fprintf(f, "thisChannel = NULL for tchid\n");
609 #if VDRVERSNUM < 10300
610 for (eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
612 event = Schedule->GetEventNumber(eventNumber);
613 fprintf(f, "Event %i tableid = %i timestring = %s endtimestring = %s\n", eventNumber, event->GetTableID(), event->GetTimeString(), event->GetEndTimeString());
614 fprintf(f, "Event %i date = %s isfollowing = %i ispresent = %i\n", eventNumber, event->GetDate(), event->IsFollowing(), event->IsPresent());
615 fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
616 fprintf(f, "Event %i subtitle = %s title = %s\n", eventNumber, event->GetSubtitle(), event->GetTitle());
617 fprintf(f, "Event %i eventid = %u duration = %li time = %lu channelnumber = %i\n", eventNumber, event->GetEventID(), event->GetDuration(), event->GetTime(), event->GetChannelNumber());
618 fprintf(f, "Event %u dump:\n", eventNumber);
623 // This whole section needs rewriting to walk the list.
624 event = Schedule->Events()->First();
626 event = Schedule->Events()->Next(event);
631 fprintf(f, "\nDump from object:\n");
633 fprintf(f, "\nEND\n");
639 const cEventInfo *GetPresentEvent(void) const;
640 const cEventInfo *GetFollowingEvent(void) const;
641 const cEventInfo *GetEvent(unsigned short uEventID, time_t tTime = 0) const;
642 const cEventInfo *GetEventAround(time_t tTime) const;
643 const cEventInfo *GetEventNumber(int n) const { return Events.Get(n); }
646 const unsigned char GetTableID(void) const;
647 const char *GetTimeString(void) const;
648 const char *GetEndTimeString(void) const;
649 const char *GetDate(void) const;
650 bool IsFollowing(void) const;
651 bool IsPresent(void) const;
652 const char *GetExtendedDescription(void) const;
653 const char *GetSubtitle(void) const;
654 const char *GetTitle(void) const;
655 unsigned short GetEventID(void) const;
656 long GetDuration(void) const;
657 time_t GetTime(void) const;
658 tChannelID GetChannelID(void) const;
659 int GetChannelNumber(void) const { return nChannelNumber; }
660 void SetChannelNumber(int ChannelNumber) const { ((cEventInfo *)this)->nChannelNumber = ChannelNumber; } // doesn't modify the EIT data, so it's ok to make it 'const'
661 void Dump(FILE *f, const char *Prefix = "") const;
669 fprintf(f, "End of current Schedule\n\n\n");
671 Schedule = (const cSchedule *)Schedules->Next(Schedule);
678 void MVPClient::processConfigSave(unsigned char* buffer, int length)
680 char* section = (char*)buffer;
684 for (int k = 0; k < length; k++)
686 if (buffer[k] == '\0')
690 key = (char*)&buffer[k+1];
694 value = (char*)&buffer[k+1];
700 // if the last string (value) doesnt have null terminator, give up
701 if (buffer[length - 1] != '\0') return;
703 printf("Config save:\n%s\n%s\n%s\n", section, key, value);
704 if (config.setValueString(section, key, value))
714 void MVPClient::processConfigLoad(unsigned char* buffer, int length)
716 char* section = (char*)buffer;
719 for (int k = 0; k < length; k++)
721 if (buffer[k] == '\0')
723 key = (char*)&buffer[k+1];
728 char* value = config.getValueString(section, key);
732 unsigned char sendBuffer[4 + strlen(value) + 1];
733 *(unsigned long*)&sendBuffer[0] = htonl(strlen(value) + 1);
734 strcpy((char*)&sendBuffer[4], value);
735 tcp.sendPacket(sendBuffer, 4 + strlen(value) + 1);
737 printf("Written config load packet\n");
742 unsigned char sendBuffer[8];
743 *(unsigned long*)&sendBuffer[0] = htonl(4);
744 *(unsigned long*)&sendBuffer[4] = htonl(0);
745 tcp.sendPacket(sendBuffer, 8);
747 printf("Written config load failed packet\n");
751 void MVPClient::cleanConfig()
753 printf("Clean config\n");
755 cRecordings Recordings;
760 char* resumes = config.getSectionKeyNames("ResumeData", numReturns, length);
761 char* position = resumes;
762 for(int k = 0; k < numReturns; k++)
764 printf("EXAMINING: %i %i %p %s\n", k, numReturns, position, position);
766 cRecording* recording = Recordings.GetByName(position);
769 // doesn't exist anymore
770 printf("Found a recording that doesn't exist anymore\n");
771 config.deleteValue("ResumeData", position);
775 printf("This recording still exists\n");
778 position += strlen(position) + 1;