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 const cRecordingInfo *Info = recording->Info();
335 point = (char*)Info->ShortText();
336 printf("info pointer %p\nsummary pointer %p\n", Info, point);
339 point = (char*)Info->Description();
340 printf("description pointer %p\n", point);
343 strcpy((char*)&sendBuffer[count], point);
344 count += strlen(point) + 1;
345 *(unsigned long*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
347 printf("recorded size as %u\n", ntohl(*(unsigned long*)&sendBuffer[0]));
349 tcp.sendPacket(sendBuffer, count);
351 printf("Written summary\n");
361 void MVPClient::processGetChannelsList(unsigned char* data, int length)
363 unsigned char* sendBuffer = new unsigned char[50000]; // FIXME hope this is enough
364 int count = 4; // leave space for the packet length
368 for (cChannel *channel = Channels.First(); channel; channel = Channels.Next(channel))
370 #if VDRVERSNUM < 10300
371 if (!channel->GroupSep() && !channel->Ca())
373 if (!channel->GroupSep() && !channel->Ca(0))
376 printf("name: '%s'\n", channel->Name());
378 if (channel->Vpid()) type = 1;
379 #if VDRVERSNUM < 10300
382 else if (channel->Apid(0)) type = 2;
386 if (count > 49000) break;
387 *(unsigned long*)&sendBuffer[count] = htonl(channel->Number());
390 *(unsigned long*)&sendBuffer[count] = htonl(type);
393 point = (char*)channel->Name();
394 strcpy((char*)&sendBuffer[count], point);
395 count += strlen(point) + 1;
399 *(unsigned long*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
401 printf("recorded size as %u\n", ntohl(*(unsigned long*)&sendBuffer[0]));
403 tcp.sendPacket(sendBuffer, count);
405 printf("Written channels list\n");
408 void MVPClient::processStartStreamingChannel(unsigned char* data, int length)
410 printf("length = %i\n", length);
411 unsigned long channelNumber = ntohl(*(unsigned long*)data);
413 cChannel* channel = channelFromNumber(channelNumber);
420 // MVPReceiver* m = new MVPReceiver(channel->Vpid(), channel->Apid1());
421 cm = new cMediamvpTransceiver(channel, 0, 0, cDevice::ActualDevice());
422 cDevice::ActualDevice()->AttachReceiver(cm);
423 //cDevice::ActualDevice()->SwitchChannel(channel, false);
428 void MVPClient::processStopStreaming(unsigned char* data, int length)
430 printf("STOP STREAMING RECEIVED\n");
441 delete recordingManager;
443 recordingManager = NULL;
449 void MVPClient::processGetBlock(unsigned char* data, int length)
453 printf("Get block called when no streaming happening!\n");
457 ULLONG position = ntohll(*(ULLONG*)data);
458 printf("getblock called for position = %llu\n", position);
460 data += sizeof(ULLONG);
462 unsigned long amount = ntohl(*(unsigned long*)data);
463 printf("getblock called for length = %lu\n", amount);
465 unsigned char sendBuffer[amount + 4];
466 unsigned long amountReceived = 0; // compiler moan.
469 printf("getting from live\n");
470 amountReceived = cm->getBlock(&sendBuffer[4], amount);
474 printf("getting from recording\n");
475 amountReceived = rp->getBlock(&sendBuffer[4], position, amount);
478 *(unsigned long*)&sendBuffer[0] = htonl(amountReceived);
479 printf("sendpacket go\n");
480 tcp.sendPacket(sendBuffer, amountReceived + 4);
481 printf("written ok %lu\n", amountReceived);
484 void MVPClient::processStartStreamingRecording(unsigned char* data, int length)
486 // data is a pointer to the fileName string
488 recordingManager = new cRecordings;
489 recordingManager->Load();
491 cRecording* recording = recordingManager->GetByName((char*)data);
493 printf("recording pointer %p\n", recording);
497 rp = new RecPlayer(recording);
499 unsigned char sendBuffer[12];
500 *(unsigned long*)&sendBuffer[0] = htonl(8);
501 *(ULLONG*)&sendBuffer[4] = htonll(rp->getTotalLength());
503 tcp.sendPacket(sendBuffer, 12);
504 printf("written totalLength\n");
508 delete recordingManager;
509 recordingManager = NULL;
513 void MVPClient::processGetChannelSchedule(unsigned char* data, int length)
515 ULONG channelNumber = ntohl(*(ULLONG*)data);
516 printf("get schedule called for channel %lu\n", channelNumber);
518 cChannel* channel = channelFromNumber(channelNumber);
521 unsigned char sendBuffer[4];
522 *(unsigned long*)&sendBuffer[0] = htonl(0);
523 tcp.sendPacket(sendBuffer, 4);
524 printf("written null\n");
528 #if VDRVERSNUM < 10300
529 cMutexLock MutexLock;
530 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
532 cSchedulesLock MutexLock;
533 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
537 unsigned char sendBuffer[8];
538 *(unsigned long*)&sendBuffer[0] = htonl(4);
539 *(unsigned long*)&sendBuffer[4] = htonl(0);
540 tcp.sendPacket(sendBuffer, 8);
541 printf("written 0\n");
545 unsigned char sendBuffer[8];
546 *(unsigned long*)&sendBuffer[0] = htonl(4);
547 *(unsigned long*)&sendBuffer[4] = htonl(1);
548 tcp.sendPacket(sendBuffer, 8);
549 printf("written 1\n");
554 void MVPClient::testChannelSchedule(unsigned char* data, int length)
556 FILE* f = fopen("/tmp/s.txt", "w");
558 #if VDRVERSNUM < 10300
559 cMutexLock MutexLock;
560 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
562 cSchedulesLock MutexLock;
563 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
567 fprintf(f, "Schedules = NULL\n");
572 fprintf(f, "Schedules dump:\n");
576 const cSchedule *Schedule;
577 int scheduleNumber = 0;
580 cChannel *thisChannel;
582 #if VDRVERSNUM < 10300
583 const cEventInfo *event;
589 // Schedule = Schedules->GetSchedule(channel->GetChannelID());
590 // Schedule = Schedules->GetSchedule();
591 Schedule = Schedules->First();
594 fprintf(f, "First Schedule = NULL\n");
601 fprintf(f, "Schedule #%i\n", scheduleNumber);
602 fprintf(f, "-------------\n\n");
604 #if VDRVERSNUM < 10300
605 tchid = Schedule->GetChannelID();
607 tchid = Schedule->ChannelID();
609 #if VDRVERSNUM < 10300
610 fprintf(f, "ChannelID.ToString() = %s\n", tchid.ToString());
611 fprintf(f, "NumEvents() = %i\n", Schedule->NumEvents());
613 // put the count at the end.
615 thisChannel = Channels.GetByChannelID(tchid, true);
618 fprintf(f, "Channel Number: %p %i\n", thisChannel, thisChannel->Number());
622 fprintf(f, "thisChannel = NULL for tchid\n");
625 #if VDRVERSNUM < 10300
626 for (eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
628 event = Schedule->GetEventNumber(eventNumber);
629 fprintf(f, "Event %i tableid = %i timestring = %s endtimestring = %s\n", eventNumber, event->GetTableID(), event->GetTimeString(), event->GetEndTimeString());
630 fprintf(f, "Event %i date = %s isfollowing = %i ispresent = %i\n", eventNumber, event->GetDate(), event->IsFollowing(), event->IsPresent());
631 fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
632 fprintf(f, "Event %i subtitle = %s title = %s\n", eventNumber, event->GetSubtitle(), event->GetTitle());
633 fprintf(f, "Event %i eventid = %u duration = %li time = %lu channelnumber = %i\n", eventNumber, event->GetEventID(), event->GetDuration(), event->GetTime(), event->GetChannelNumber());
634 fprintf(f, "Event %u dump:\n", eventNumber);
639 // This whole section needs rewriting to walk the list.
640 event = Schedule->Events()->First();
642 event = Schedule->Events()->Next(event);
647 fprintf(f, "\nDump from object:\n");
649 fprintf(f, "\nEND\n");
655 const cEventInfo *GetPresentEvent(void) const;
656 const cEventInfo *GetFollowingEvent(void) const;
657 const cEventInfo *GetEvent(unsigned short uEventID, time_t tTime = 0) const;
658 const cEventInfo *GetEventAround(time_t tTime) const;
659 const cEventInfo *GetEventNumber(int n) const { return Events.Get(n); }
662 const unsigned char GetTableID(void) const;
663 const char *GetTimeString(void) const;
664 const char *GetEndTimeString(void) const;
665 const char *GetDate(void) const;
666 bool IsFollowing(void) const;
667 bool IsPresent(void) const;
668 const char *GetExtendedDescription(void) const;
669 const char *GetSubtitle(void) const;
670 const char *GetTitle(void) const;
671 unsigned short GetEventID(void) const;
672 long GetDuration(void) const;
673 time_t GetTime(void) const;
674 tChannelID GetChannelID(void) const;
675 int GetChannelNumber(void) const { return nChannelNumber; }
676 void SetChannelNumber(int ChannelNumber) const { ((cEventInfo *)this)->nChannelNumber = ChannelNumber; } // doesn't modify the EIT data, so it's ok to make it 'const'
677 void Dump(FILE *f, const char *Prefix = "") const;
685 fprintf(f, "End of current Schedule\n\n\n");
687 Schedule = (const cSchedule *)Schedules->Next(Schedule);
694 void MVPClient::processConfigSave(unsigned char* buffer, int length)
696 char* section = (char*)buffer;
700 for (int k = 0; k < length; k++)
702 if (buffer[k] == '\0')
706 key = (char*)&buffer[k+1];
710 value = (char*)&buffer[k+1];
716 // if the last string (value) doesnt have null terminator, give up
717 if (buffer[length - 1] != '\0') return;
719 printf("Config save:\n%s\n%s\n%s\n", section, key, value);
720 if (config.setValueString(section, key, value))
730 void MVPClient::processConfigLoad(unsigned char* buffer, int length)
732 char* section = (char*)buffer;
735 for (int k = 0; k < length; k++)
737 if (buffer[k] == '\0')
739 key = (char*)&buffer[k+1];
744 char* value = config.getValueString(section, key);
748 unsigned char sendBuffer[4 + strlen(value) + 1];
749 *(unsigned long*)&sendBuffer[0] = htonl(strlen(value) + 1);
750 strcpy((char*)&sendBuffer[4], value);
751 tcp.sendPacket(sendBuffer, 4 + strlen(value) + 1);
753 printf("Written config load packet\n");
758 unsigned char sendBuffer[8];
759 *(unsigned long*)&sendBuffer[0] = htonl(4);
760 *(unsigned long*)&sendBuffer[4] = htonl(0);
761 tcp.sendPacket(sendBuffer, 8);
763 printf("Written config load failed packet\n");
767 void MVPClient::cleanConfig()
769 printf("Clean config\n");
771 cRecordings Recordings;
776 char* resumes = config.getSectionKeyNames("ResumeData", numReturns, length);
777 char* position = resumes;
778 for(int k = 0; k < numReturns; k++)
780 printf("EXAMINING: %i %i %p %s\n", k, numReturns, position, position);
782 cRecording* recording = Recordings.GetByName(position);
785 // doesn't exist anymore
786 printf("Found a recording that doesn't exist anymore\n");
787 config.deleteValue("ResumeData", position);
791 printf("This recording still exists\n");
794 position += strlen(position) + 1;