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 int apid1 = channel->Apid1();
102 printf("Found channel number %lu, vpid = %i, apid1 = %i\n", channelNumber, vpid, apid1);
110 printf("Channel not found\n");
117 void MVPClient::writeResumeData()
119 config.setValueLongLong("ResumeData", (char*)rp->getCurrentRecording()->FileName(), rp->getLastPosition());
122 void MVPClient::sendULONG(ULONG ul)
124 unsigned char sendBuffer[8];
125 *(unsigned long*)&sendBuffer[0] = htonl(4);
126 *(unsigned long*)&sendBuffer[4] = htonl(ul);
128 tcp.sendPacket(sendBuffer, 8);
129 printf("written ULONG %lu\n", ul);
132 void MVPClientStartThread(void* arg)
134 MVPClient* m = (MVPClient*)arg;
136 // Nothing external to this class has a reference to it
137 // This is the end of the thread.. so delete m
144 if (pthread_create(&runThread, NULL, (void*(*)(void*))MVPClientStartThread, (void *)this) == -1) return 0;
145 printf("MVPClient run success\n");
149 void MVPClient::run2()
154 pthread_sigmask(SIG_BLOCK, &sigset, NULL);
155 pthread_detach(runThread); // Detach
157 tcp.disableReadTimeout();
159 tcp.setSoKeepTime(3);
160 tcp.setNonBlocking();
162 unsigned char* buffer;
165 unsigned long opcode;
169 printf("starting wait\n");
170 buffer = (unsigned char*)tcp.receivePacket();
171 printf("back from wait\n");
174 printf("Detected connection closed\n");
178 packetLength = tcp.getDataLength() - 4;
179 opcode = ntohl(*(unsigned long*)buffer);
186 processLogin(data, packetLength);
189 processGetRecordingsList(data, packetLength);
192 processDeleteRecording(data, packetLength);
195 processGetSummary(data, packetLength);
198 processGetChannelsList(data, packetLength);
201 processStartStreamingChannel(data, packetLength);
204 processGetBlock(data, packetLength);
207 processStopStreaming(data, packetLength);
210 processStartStreamingRecording(data, packetLength);
213 processGetChannelSchedule(data, packetLength);
216 processConfigSave(data, packetLength);
219 processConfigLoad(data, packetLength);
227 void MVPClient::processLogin(unsigned char* buffer, int length)
229 time_t timeNow = time(NULL);
230 struct tm* timeStruct = localtime(&timeNow);
231 timeOffset = timeStruct->tm_gmtoff;
233 // seems dhcp is sending timezone out to mvp
234 // so just supply utc timestamp
237 sendULONG(timeNow + timeOffset);
238 printf("written time\n");
241 void MVPClient::processGetRecordingsList(unsigned char* data, int length)
243 unsigned char* sendBuffer = new unsigned char[50000]; // hope this is enough
244 int count = 4; // leave space for the packet length
249 int Percent = VideoDiskSpace(&FreeMB);
250 int Total = (FreeMB / (100 - Percent)) * 100;
252 *(unsigned long*)&sendBuffer[count] = htonl(Total);
253 count += sizeof(unsigned long);
254 *(unsigned long*)&sendBuffer[count] = htonl(FreeMB);
255 count += sizeof(unsigned long);
256 *(unsigned long*)&sendBuffer[count] = htonl(Percent);
257 count += sizeof(unsigned long);
260 cRecordings Recordings;
263 for (cRecording *recording = Recordings.First(); recording; recording = Recordings.Next(recording))
265 if (count > 49000) break; // just how big is that hard disk?!
266 *(unsigned long*)&sendBuffer[count] = htonl(recording->start + timeOffset);
269 point = (char*)recording->Name();
270 strcpy((char*)&sendBuffer[count], point);
271 count += strlen(point) + 1;
273 point = (char*)recording->FileName();
274 strcpy((char*)&sendBuffer[count], point);
275 count += strlen(point) + 1;
278 *(unsigned long*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
280 printf("recorded size as %u\n", ntohl(*(unsigned long*)&sendBuffer[0]));
282 tcp.sendPacket(sendBuffer, count);
284 printf("Written list\n");
287 void MVPClient::processDeleteRecording(unsigned char* data, int length)
289 // data is a pointer to the fileName string
291 cRecordings Recordings;
292 Recordings.Load(); // probably have to do this
294 cRecording* recording = Recordings.GetByName((char*)data);
296 printf("recording pointer %p\n", recording);
300 printf("deleting recording: %s\n", recording->Name());
310 void MVPClient::processGetSummary(unsigned char* data, int length)
312 // data is a pointer to the fileName string
314 cRecordings Recordings;
315 Recordings.Load(); // probably have to do this
317 cRecording* recording = Recordings.GetByName((char*)data);
319 printf("recording pointer %p\n", recording);
323 unsigned char* sendBuffer = new unsigned char[50000]; // hope this is enough
324 int count = 4; // leave space for the packet length
328 point = (char*)recording->Summary();
329 strcpy((char*)&sendBuffer[count], point);
330 count += strlen(point) + 1;
331 *(unsigned long*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
333 printf("recorded size as %u\n", ntohl(*(unsigned long*)&sendBuffer[0]));
335 tcp.sendPacket(sendBuffer, count);
337 printf("Written summary\n");
347 void MVPClient::processGetChannelsList(unsigned char* data, int length)
349 unsigned char* sendBuffer = new unsigned char[50000]; // FIXME hope this is enough
350 int count = 4; // leave space for the packet length
354 for (cChannel *channel = Channels.First(); channel; channel = Channels.Next(channel))
356 if (!channel->GroupSep())
358 printf("name: '%s'\n", channel->Name());
360 if (count > 49000) break;
361 *(unsigned long*)&sendBuffer[count] = htonl(channel->Number());
364 if (channel->Vpid()) type = 1;
367 *(unsigned long*)&sendBuffer[count] = htonl(type);
370 point = (char*)channel->Name();
371 strcpy((char*)&sendBuffer[count], point);
372 count += strlen(point) + 1;
376 *(unsigned long*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
378 printf("recorded size as %u\n", ntohl(*(unsigned long*)&sendBuffer[0]));
380 tcp.sendPacket(sendBuffer, count);
382 printf("Written channels list\n");
385 void MVPClient::processStartStreamingChannel(unsigned char* data, int length)
387 printf("length = %i\n", length);
388 unsigned long channelNumber = ntohl(*(unsigned long*)data);
390 cChannel* channel = channelFromNumber(channelNumber);
397 // MVPReceiver* m = new MVPReceiver(channel->Vpid(), channel->Apid1());
398 cm = new cMediamvpTransceiver(channel, 0, 0, cDevice::ActualDevice());
399 cDevice::ActualDevice()->AttachReceiver(cm);
400 //cDevice::ActualDevice()->SwitchChannel(channel, false);
405 void MVPClient::processStopStreaming(unsigned char* data, int length)
407 printf("STOP STREAMING RECEIVED\n");
418 delete recordingManager;
420 recordingManager = NULL;
426 void MVPClient::processGetBlock(unsigned char* data, int length)
430 printf("Get block called when no streaming happening!\n");
434 ULLONG position = ntohll(*(ULLONG*)data);
435 printf("getblock called for position = %llu\n", position);
437 data += sizeof(ULLONG);
439 unsigned long amount = ntohl(*(unsigned long*)data);
440 printf("getblock called for length = %lu\n", amount);
442 unsigned char sendBuffer[amount + 4];
443 unsigned long amountReceived = 0; // compiler moan.
446 printf("getting from live\n");
447 amountReceived = cm->getBlock(&sendBuffer[4], amount);
451 printf("getting from recording\n");
452 amountReceived = rp->getBlock(&sendBuffer[4], position, amount);
455 *(unsigned long*)&sendBuffer[0] = htonl(amountReceived);
456 printf("sendpacket go\n");
457 tcp.sendPacket(sendBuffer, amountReceived + 4);
458 printf("written ok %lu\n", amountReceived);
461 void MVPClient::processStartStreamingRecording(unsigned char* data, int length)
463 // data is a pointer to the fileName string
465 recordingManager = new cRecordings;
466 recordingManager->Load();
468 cRecording* recording = recordingManager->GetByName((char*)data);
470 printf("recording pointer %p\n", recording);
474 rp = new RecPlayer(recording);
476 unsigned char sendBuffer[12];
477 *(unsigned long*)&sendBuffer[0] = htonl(8);
478 *(ULLONG*)&sendBuffer[4] = htonll(rp->getTotalLength());
480 tcp.sendPacket(sendBuffer, 12);
481 printf("written totalLength\n");
485 delete recordingManager;
486 recordingManager = NULL;
490 void MVPClient::processGetChannelSchedule(unsigned char* data, int length)
492 ULONG channelNumber = ntohl(*(ULLONG*)data);
493 printf("get schedule called for channel %lu\n", channelNumber);
495 cChannel* channel = channelFromNumber(channelNumber);
498 unsigned char sendBuffer[4];
499 *(unsigned long*)&sendBuffer[0] = htonl(0);
500 tcp.sendPacket(sendBuffer, 4);
501 printf("written null\n");
505 cMutexLock MutexLock;
506 const cSchedules* Schedules = cSIProcessor::Schedules(MutexLock);
507 // const cSchedules* Schedules = cSchedules::Schedules(MutexLock);
510 unsigned char sendBuffer[8];
511 *(unsigned long*)&sendBuffer[0] = htonl(4);
512 *(unsigned long*)&sendBuffer[4] = htonl(0);
513 tcp.sendPacket(sendBuffer, 8);
514 printf("written 0\n");
518 unsigned char sendBuffer[8];
519 *(unsigned long*)&sendBuffer[0] = htonl(4);
520 *(unsigned long*)&sendBuffer[4] = htonl(1);
521 tcp.sendPacket(sendBuffer, 8);
522 printf("written 1\n");
527 void MVPClient::testChannelSchedule(unsigned char* data, int length)
529 FILE* f = fopen("/tmp/s.txt", "w");
531 cMutexLock MutexLock;
532 const cSchedules* Schedules = cSIProcessor::Schedules(MutexLock);
533 // const cSchedules* Schedules = cSchedules::Schedules(MutexLock);
536 fprintf(f, "Schedules = NULL\n");
541 fprintf(f, "Schedules dump:\n");
545 const cSchedule *Schedule;
546 int scheduleNumber = 0;
549 cChannel *thisChannel;
551 const cEventInfo* event;
554 // Schedule = Schedules->GetSchedule(channel->GetChannelID());
555 // Schedule = Schedules->GetSchedule();
556 Schedule = Schedules->First();
559 fprintf(f, "First Schedule = NULL\n");
566 fprintf(f, "Schedule #%i\n", scheduleNumber);
567 fprintf(f, "-------------\n\n");
569 tchid = Schedule->GetChannelID();
570 fprintf(f, "ChannelID.ToString() = %s\n", tchid.ToString());
571 fprintf(f, "NumEvents() = %i\n", Schedule->NumEvents());
572 thisChannel = Channels.GetByChannelID(tchid, true);
575 fprintf(f, "Channel Number: %p %i\n", thisChannel, thisChannel->Number());
579 fprintf(f, "thisChannel = NULL for tchid\n");
582 for (eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
584 event = Schedule->GetEventNumber(eventNumber);
585 fprintf(f, "Event %i tableid = %i timestring = %s endtimestring = %s\n", eventNumber, event->GetTableID(), event->GetTimeString(), event->GetEndTimeString());
586 fprintf(f, "Event %i date = %s isfollowing = %i ispresent = %i\n", eventNumber, event->GetDate(), event->IsFollowing(), event->IsPresent());
587 fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
588 fprintf(f, "Event %i subtitle = %s title = %s\n", eventNumber, event->GetSubtitle(), event->GetTitle());
589 fprintf(f, "Event %i eventid = %u duration = %li time = %lu channelnumber = %i\n", eventNumber, event->GetEventID(), event->GetDuration(), event->GetTime(), event->GetChannelNumber());
590 fprintf(f, "Event %u dump:\n", eventNumber);
597 fprintf(f, "\nDump from object:\n");
599 fprintf(f, "\nEND\n");
605 const cEventInfo *GetPresentEvent(void) const;
606 const cEventInfo *GetFollowingEvent(void) const;
607 const cEventInfo *GetEvent(unsigned short uEventID, time_t tTime = 0) const;
608 const cEventInfo *GetEventAround(time_t tTime) const;
609 const cEventInfo *GetEventNumber(int n) const { return Events.Get(n); }
612 const unsigned char GetTableID(void) const;
613 const char *GetTimeString(void) const;
614 const char *GetEndTimeString(void) const;
615 const char *GetDate(void) const;
616 bool IsFollowing(void) const;
617 bool IsPresent(void) const;
618 const char *GetExtendedDescription(void) const;
619 const char *GetSubtitle(void) const;
620 const char *GetTitle(void) const;
621 unsigned short GetEventID(void) const;
622 long GetDuration(void) const;
623 time_t GetTime(void) const;
624 tChannelID GetChannelID(void) const;
625 int GetChannelNumber(void) const { return nChannelNumber; }
626 void SetChannelNumber(int ChannelNumber) const { ((cEventInfo *)this)->nChannelNumber = ChannelNumber; } // doesn't modify the EIT data, so it's ok to make it 'const'
627 void Dump(FILE *f, const char *Prefix = "") const;
635 fprintf(f, "End of current Schedule\n\n\n");
637 Schedule = (const cSchedule *)Schedules->Next(Schedule);
644 void MVPClient::processConfigSave(unsigned char* buffer, int length)
646 char* section = (char*)buffer;
650 for (int k = 0; k < length; k++)
652 if (buffer[k] == '\0')
656 key = (char*)&buffer[k+1];
660 value = (char*)&buffer[k+1];
666 // if the last string (value) doesnt have null terminator, give up
667 if (buffer[length - 1] != '\0') return;
669 printf("Config save:\n%s\n%s\n%s\n", section, key, value);
670 if (config.setValueString(section, key, value))
680 void MVPClient::processConfigLoad(unsigned char* buffer, int length)
682 char* section = (char*)buffer;
685 for (int k = 0; k < length; k++)
687 if (buffer[k] == '\0')
689 key = (char*)&buffer[k+1];
694 char* value = config.getValueString(section, key);
698 unsigned char sendBuffer[4 + strlen(value) + 1];
699 *(unsigned long*)&sendBuffer[0] = htonl(strlen(value) + 1);
700 strcpy((char*)&sendBuffer[4], value);
701 tcp.sendPacket(sendBuffer, 4 + strlen(value) + 1);
703 printf("Written config load packet\n");
708 unsigned char sendBuffer[8];
709 *(unsigned long*)&sendBuffer[0] = htonl(0);
710 *(unsigned long*)&sendBuffer[4] = htonl(0);
711 tcp.sendPacket(sendBuffer, 8);
713 printf("Written config load failed packet\n");
717 void MVPClient::cleanConfig()
719 printf("Clean config\n");
721 cRecordings Recordings;
726 char* resumes = config.getSectionKeyNames("ResumeData", numReturns, length);
727 char* position = resumes;
728 for(int k = 0; k < numReturns; k++)
730 printf("EXAMINING: %i %i %p %s\n", k, numReturns, position, position);
732 cRecording* recording = Recordings.GetByName(position);
735 // doesn't exist anymore
736 printf("Found a recording that doesn't exist anymore\n");
737 config.deleteValue("ResumeData", position);
741 printf("This recording still exists\n");
744 position += strlen(position) + 1;