]> git.vomp.tv Git - vompserver.git/blob - mvpclient.c
Channel schedules
[vompserver.git] / mvpclient.c
1 /*
2     Copyright 2004-2005 Chris Tallon
3
4     This file is part of VOMP.
5
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.
10
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.
15
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
19 */
20
21 #include "mvpclient.h"
22
23 MVPClient::MVPClient(int tsocket)
24  : tcp(tsocket)
25 {
26   lp = NULL;
27   rp = NULL;
28   recordingManager = NULL;
29   log = Log::getInstance();
30
31   // Get IP address of client for config module
32
33   char ipa[20];
34   struct sockaddr_in peer;
35   socklen_t salen = sizeof(struct sockaddr);
36   if(getpeername(tsocket, (struct sockaddr*)&peer, &salen) == 0)
37   {
38     strcpy(ipa, inet_ntoa(peer.sin_addr));
39   }
40   else
41   {
42     ipa[0] = '\0';
43     log->log("Client", Log::DEBUG, "Cannot get peer name!");
44   }
45
46   const char* configDir = cPlugin::ConfigDirectory();
47   if (!configDir)
48   {
49     log->log("Client", Log::DEBUG, "No config dir!");
50     return;
51   }
52
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);
56
57   log->log("Client", Log::DEBUG, "Config file name: %s", configFileName);
58
59
60 //test(14);
61
62 }
63
64 MVPClient::~MVPClient()
65 {
66   log->log("Client", Log::DEBUG, "MVP client destructor");
67   if (lp)
68   {
69     delete lp;
70     lp = NULL;
71   }
72   else if (rp)
73   {
74     writeResumeData();
75
76     delete rp;
77     delete recordingManager;
78     rp = NULL;
79     recordingManager = NULL;
80   }
81
82   cleanConfig();
83 }
84
85 cChannel* MVPClient::channelFromNumber(ULONG channelNumber)
86 {
87   cChannel* channel = NULL;
88
89   for (channel = Channels.First(); channel; channel = Channels.Next(channel))
90   {
91     if (!channel->GroupSep())
92     {
93       log->log("Client", Log::DEBUG, "Looking for channel %lu::: number: %i name: '%s'", channelNumber, channel->Number(), channel->Name());
94
95       if (channel->Number() == (int)channelNumber)
96       {
97         int vpid = channel->Vpid();
98 #if VDRVERSNUM < 10300
99         int apid1 = channel->Apid1();
100 #else
101         int apid1 = channel->Apid(0);
102 #endif
103         log->log("Client", Log::DEBUG, "Found channel number %lu, vpid = %i, apid1 = %i", channelNumber, vpid, apid1);
104         return channel;
105       }
106     }
107   }
108
109   if (!channel)
110   {
111     log->log("Client", Log::DEBUG, "Channel not found");
112   }
113
114   return channel;
115 }
116
117
118 void MVPClient::writeResumeData()
119 {
120   config.setValueLongLong("ResumeData", (char*)rp->getCurrentRecording()->FileName(), rp->getLastPosition());
121 }
122
123 void MVPClient::sendULONG(ULONG ul)
124 {
125   UCHAR sendBuffer[8];
126   *(ULONG*)&sendBuffer[0] = htonl(4);
127   *(ULONG*)&sendBuffer[4] = htonl(ul);
128
129   tcp.sendPacket(sendBuffer, 8);
130   log->log("Client", Log::DEBUG, "written ULONG %lu", ul);
131 }
132
133 void MVPClientStartThread(void* arg)
134 {
135   MVPClient* m = (MVPClient*)arg;
136   m->run2();
137   // Nothing external to this class has a reference to it
138   // This is the end of the thread.. so delete m
139   delete m;
140   pthread_exit(NULL);
141 }
142
143 int MVPClient::run()
144 {
145   if (pthread_create(&runThread, NULL, (void*(*)(void*))MVPClientStartThread, (void *)this) == -1) return 0;
146   log->log("Client", Log::DEBUG, "MVPClient run success");
147   return 1;
148 }
149
150 void MVPClient::run2()
151 {
152   // Thread stuff
153   sigset_t sigset;
154   sigfillset(&sigset);
155   pthread_sigmask(SIG_BLOCK, &sigset, NULL);
156   pthread_detach(runThread);  // Detach
157
158   tcp.disableReadTimeout();
159
160   tcp.setSoKeepTime(3);
161   tcp.setNonBlocking();
162
163   UCHAR* buffer;
164   UCHAR* data;
165   int packetLength;
166   ULONG opcode;
167
168   while(1)
169   {
170     log->log("Client", Log::DEBUG, "Waiting");
171     buffer = (UCHAR*)tcp.receivePacket();
172     log->log("Client", Log::DEBUG, "Received packet, length = %u", tcp.getDataLength());
173     if (buffer == NULL)
174     {
175       log->log("Client", Log::DEBUG, "Detected connection closed");
176       break;
177     }
178
179     packetLength = tcp.getDataLength() - 4;
180     opcode = ntohl(*(ULONG*)buffer);
181     data = buffer + 4;
182
183
184     switch(opcode)
185     {
186       case 1:
187         processLogin(data, packetLength);
188         break;
189       case 2:
190         processGetRecordingsList(data, packetLength);
191         break;
192       case 3:
193         processDeleteRecording(data, packetLength);
194         break;
195       case 4:
196         processGetSummary(data, packetLength);
197         break;
198       case 5:
199         processGetChannelsList(data, packetLength);
200         break;
201       case 6:
202         processStartStreamingChannel(data, packetLength);
203         break;
204       case 7:
205         processGetBlock(data, packetLength);
206         break;
207       case 8:
208         processStopStreaming(data, packetLength);
209         break;
210       case 9:
211         processStartStreamingRecording(data, packetLength);
212         break;
213       case 10:
214         processGetChannelSchedule(data, packetLength);
215         break;
216       case 11:
217         processConfigSave(data, packetLength);
218         break;
219       case 12:
220         processConfigLoad(data, packetLength);
221         break;
222       case 13:
223         processReScanRecording(data, packetLength);
224         break;
225     }
226
227     free(buffer);
228   }
229 }
230
231 void MVPClient::processLogin(UCHAR* buffer, int length)
232 {
233   time_t timeNow = time(NULL);
234   struct tm* timeStruct = localtime(&timeNow);
235   int timeOffset = timeStruct->tm_gmtoff;
236
237   UCHAR sendBuffer[12];
238   *(ULONG*)&sendBuffer[0] = htonl(8);
239   *(ULONG*)&sendBuffer[4] = htonl(timeNow);
240   *(signed int*)&sendBuffer[8] = htonl(timeOffset);
241
242   tcp.sendPacket(sendBuffer, 12);
243   log->log("Client", Log::DEBUG, "written login reply");
244 }
245
246 void MVPClient::processGetRecordingsList(UCHAR* data, int length)
247 {
248   UCHAR* sendBuffer = new UCHAR[50000]; // hope this is enough
249   int count = 4; // leave space for the packet length
250   char* point;
251
252
253   int FreeMB;
254   int Percent = VideoDiskSpace(&FreeMB);
255   int Total = (FreeMB / (100 - Percent)) * 100;
256
257   *(ULONG*)&sendBuffer[count] = htonl(Total);
258   count += sizeof(ULONG);
259   *(ULONG*)&sendBuffer[count] = htonl(FreeMB);
260   count += sizeof(ULONG);
261   *(ULONG*)&sendBuffer[count] = htonl(Percent);
262   count += sizeof(ULONG);
263
264
265   cRecordings Recordings;
266   Recordings.Load();
267
268   for (cRecording *recording = Recordings.First(); recording; recording = Recordings.Next(recording))
269   {
270     if (count > 49000) break; // just how big is that hard disk?!
271     *(ULONG*)&sendBuffer[count] = htonl(recording->start);// + timeOffset);
272     count += 4;
273
274     point = (char*)recording->Name();
275     strcpy((char*)&sendBuffer[count], point);
276     count += strlen(point) + 1;
277
278     point = (char*)recording->FileName();
279     strcpy((char*)&sendBuffer[count], point);
280     count += strlen(point) + 1;
281   }
282
283   *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 :  take off the size field
284
285   log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
286
287   tcp.sendPacket(sendBuffer, count);
288   delete[] sendBuffer;
289   log->log("Client", Log::DEBUG, "Written list");
290 }
291
292 void MVPClient::processDeleteRecording(UCHAR* data, int length)
293 {
294   // data is a pointer to the fileName string
295
296   cRecordings Recordings;
297   Recordings.Load(); // probably have to do this
298
299   cRecording* recording = Recordings.GetByName((char*)data);
300
301   log->log("Client", Log::DEBUG, "recording pointer %p", recording);
302
303   if (recording)
304   {
305     log->log("Client", Log::DEBUG, "deleting recording: %s", recording->Name());
306     recording->Delete();
307     sendULONG(1);
308   }
309   else
310   {
311     sendULONG(0);
312   }
313 }
314
315 void MVPClient::processGetSummary(UCHAR* data, int length)
316 {
317   // data is a pointer to the fileName string
318
319   cRecordings Recordings;
320   Recordings.Load(); // probably have to do this
321
322   cRecording *recording = Recordings.GetByName((char*)data);
323
324   log->log("Client", Log::DEBUG, "recording pointer %p", recording);
325
326   if (recording)
327   {
328     UCHAR* sendBuffer = new UCHAR[50000]; // hope this is enough
329     int count = 4; // leave space for the packet length
330
331     char* point;
332 #if VDRVERSNUM < 10300
333     point = (char*)recording->Summary();
334 #else
335     const cRecordingInfo *Info = recording->Info();
336     point = (char*)Info->ShortText();
337     log->log("Client", Log::DEBUG, "info pointer %p summary pointer %p", Info, point);
338     if (isempty(point))
339     {
340       point = (char*)Info->Description();
341       log->log("Client", Log::DEBUG, "description pointer %p", point);
342     }
343 #endif
344     strcpy((char*)&sendBuffer[count], point);
345     count += strlen(point) + 1;
346     *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 :  take off the size field
347
348     log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
349
350     tcp.sendPacket(sendBuffer, count);
351     delete[] sendBuffer;
352     log->log("Client", Log::DEBUG, "Written summary");
353
354
355   }
356   else
357   {
358     sendULONG(0);
359   }
360 }
361
362 void MVPClient::processGetChannelsList(UCHAR* data, int length)
363 {
364   UCHAR* sendBuffer = new UCHAR[50000]; // FIXME hope this is enough
365   int count = 4; // leave space for the packet length
366   char* point;
367   ULONG type;
368
369   for (cChannel *channel = Channels.First(); channel; channel = Channels.Next(channel))
370   {
371
372 // Re-enable later with some client side option
373 //#if VDRVERSNUM < 10300
374 //    if (!channel->GroupSep() && !channel->Ca())
375 //#else
376 //    if (!channel->GroupSep() && !channel->Ca(0))
377 //#endif
378 //    {
379
380     if (!channel->GroupSep())
381     {
382       log->log("Client", Log::DEBUG, "name: '%s'", channel->Name());
383
384       if (channel->Vpid()) type = 1;
385 #if VDRVERSNUM < 10300
386       else type = 2;
387 #else
388       else if (channel->Apid(0)) type = 2;
389       else continue;
390 #endif
391
392       if (count > 49000) break;
393       *(ULONG*)&sendBuffer[count] = htonl(channel->Number());
394       count += 4;
395
396       *(ULONG*)&sendBuffer[count] = htonl(type);
397       count += 4;
398
399       point = (char*)channel->Name();
400       strcpy((char*)&sendBuffer[count], point);
401       count += strlen(point) + 1;
402     }
403   }
404
405   *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 :  take off the size field
406
407   log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
408
409   tcp.sendPacket(sendBuffer, count);
410   delete[] sendBuffer;
411   log->log("Client", Log::DEBUG, "Written channels list");
412 }
413
414 void MVPClient::processStartStreamingChannel(UCHAR* data, int length)
415 {
416   log->log("Client", Log::DEBUG, "length = %i", length);
417   ULONG channelNumber = ntohl(*(ULONG*)data);
418
419   cChannel* channel = channelFromNumber(channelNumber);
420   if (!channel)
421   {
422     sendULONG(0);
423     return;
424   }
425
426   lp = MVPReceiver::create(channel);
427
428   if (!lp)
429   {
430     sendULONG(0);
431     return;
432   }
433
434   if (!lp->init())
435   {
436     delete lp;
437     lp = NULL;
438     sendULONG(0);
439     return;
440   }
441
442   sendULONG(1);
443 }
444
445 void MVPClient::processStopStreaming(UCHAR* data, int length)
446 {
447   log->log("Client", Log::DEBUG, "STOP STREAMING RECEIVED");
448   if (lp)
449   {
450     delete lp;
451     lp = NULL;
452   }
453   else if (rp)
454   {
455     writeResumeData();
456
457     delete rp;
458     delete recordingManager;
459     rp = NULL;
460     recordingManager = NULL;
461   }
462
463   sendULONG(1);
464 }
465
466 void MVPClient::processGetBlock(UCHAR* data, int length)
467 {
468   if (!lp && !rp)
469   {
470     log->log("Client", Log::DEBUG, "Get block called when no streaming happening!");
471     return;
472   }
473
474   ULLONG position = ntohll(*(ULLONG*)data);
475   data += sizeof(ULLONG);
476   ULONG amount = ntohl(*(ULONG*)data);
477
478   log->log("Client", Log::DEBUG, "getblock pos = %llu length = %lu", position, amount);
479
480   UCHAR sendBuffer[amount + 4];
481   ULONG amountReceived = 0; // compiler moan.
482   if (lp)
483   {
484     log->log("Client", Log::DEBUG, "getting from live");
485     amountReceived = lp->getBlock(&sendBuffer[4], amount);
486
487     if (!amountReceived)
488     {
489       // vdr has possibly disconnected the receiver
490       log->log("Client", Log::DEBUG, "VDR has disconnected the live receiver");
491       delete lp;
492       lp = NULL;
493     }
494   }
495   else if (rp)
496   {
497     log->log("Client", Log::DEBUG, "getting from recording");
498     amountReceived = rp->getBlock(&sendBuffer[4], position, amount);
499   }
500
501   *(ULONG*)&sendBuffer[0] = htonl(amountReceived);
502   tcp.sendPacket(sendBuffer, amountReceived + 4);
503   log->log("Client", Log::DEBUG, "written ok %lu", amountReceived);
504 }
505
506 void MVPClient::processStartStreamingRecording(UCHAR* data, int length)
507 {
508   // data is a pointer to the fileName string
509
510   recordingManager = new cRecordings;
511   recordingManager->Load();
512
513   cRecording* recording = recordingManager->GetByName((char*)data);
514
515   log->log("Client", Log::DEBUG, "recording pointer %p", recording);
516
517   if (recording)
518   {
519     rp = new RecPlayer(recording);
520
521     UCHAR sendBuffer[12];
522     *(ULONG*)&sendBuffer[0] = htonl(8);
523     *(ULLONG*)&sendBuffer[4] = htonll(rp->getTotalLength());
524
525     tcp.sendPacket(sendBuffer, 12);
526     log->log("Client", Log::DEBUG, "written totalLength");
527   }
528   else
529   {
530     delete recordingManager;
531     recordingManager = NULL;
532   }
533 }
534
535 void MVPClient::processReScanRecording(UCHAR* data, int length)
536 {
537   ULLONG retval = 0;
538
539   if (!rp)
540   {
541     log->log("Client", Log::DEBUG, "Rescan recording called when no recording being played!");
542   }
543   else
544   {
545     rp->scan();
546     retval = rp->getTotalLength();
547   }
548
549   UCHAR sendBuffer[12];
550   *(ULONG*)&sendBuffer[0] = htonl(8);
551   *(ULLONG*)&sendBuffer[4] = htonll(retval);
552
553   tcp.sendPacket(sendBuffer, 12);
554   log->log("Client", Log::DEBUG, "Rescan recording, wrote new length to client");
555 }
556
557 void MVPClient::processGetChannelSchedule(UCHAR* data, int length)
558 {
559   ULONG channelNumber = ntohl(*(ULLONG*)data);
560   log->log("Client", Log::DEBUG, "get schedule called for channel %lu", channelNumber);
561
562   cChannel* channel = channelFromNumber(channelNumber);
563   if (!channel)
564   {
565     UCHAR sendBuffer[4];
566     *(ULONG*)&sendBuffer[0] = htonl(4);
567     *(ULONG*)&sendBuffer[4] = htonl(0);
568     tcp.sendPacket(sendBuffer, 8);
569     log->log("Client", Log::DEBUG, "written 0 because channel = NULL");
570     return;
571   }
572
573   log->log("Client", Log::DEBUG, "Got channel");
574
575 #if VDRVERSNUM < 10300
576   cMutexLock MutexLock;
577   const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
578 #else
579   cSchedulesLock MutexLock;
580   const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
581 #endif
582   if (!Schedules)
583   {
584     UCHAR sendBuffer[8];
585     *(ULONG*)&sendBuffer[0] = htonl(4);
586     *(ULONG*)&sendBuffer[4] = htonl(0);
587     tcp.sendPacket(sendBuffer, 8);
588     log->log("Client", Log::DEBUG, "written 0 because Schedule!s! = NULL");
589     return;
590   }
591
592   log->log("Client", Log::DEBUG, "Got schedule!s! object");
593
594   const cSchedule *Schedule = Schedules->GetSchedule(channel->GetChannelID());
595   if (!Schedule)
596   {
597     UCHAR sendBuffer[8];
598     *(ULONG*)&sendBuffer[0] = htonl(4);
599     *(ULONG*)&sendBuffer[4] = htonl(0);
600     tcp.sendPacket(sendBuffer, 8);
601     log->log("Client", Log::DEBUG, "written 0 because Schedule = NULL");
602     return;
603   }
604
605   log->log("Client", Log::DEBUG, "Got schedule object");
606
607   UCHAR* sendBuffer = (UCHAR*)malloc(100000);
608   ULONG sendBufferLength = 100000;
609   ULONG sendBufferUsed = sizeof(ULONG); // leave a hole for the entire packet length
610
611   char* empty = "";
612
613   // assign all the event info to temp vars then we know exactly what size they are
614   ULONG thisEventID;
615   ULONG thisEventTime;
616   ULONG thisEventDuration;
617   const char* thisEventTitle;
618   const char* thisEventSubTitle;
619   const char* thisEventDescription;
620
621   ULONG constEventLength = sizeof(thisEventID) + sizeof(thisEventTime) + sizeof(thisEventDuration);
622   ULONG thisEventLength;
623
624 #if VDRVERSNUM < 10300
625
626   const cEventInfo *event;
627   for (int eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
628   {
629     event = Schedule->GetEventNumber(eventNumber);
630
631     thisEventID = event->GetEventID();
632     thisEventTime = event->GetTime();
633     thisEventDuration = event->GetDuration();
634     thisEventTitle = event->GetTitle();
635     thisEventSubTitle = event->GetSubtitle();
636     thisEventDescription = event->GetExtendedDescription();
637
638 #else
639
640   for (const cEvent* event = Schedule->Events()->First(); event; event = Schedule->Events()->Next(event))
641   {
642     thisEventID = event->EventID();
643     thisEventTime = event->StartTime();
644     thisEventDuration = event->Duration();
645     thisEventTitle = event->Title();
646     thisEventSubTitle = NULL;
647     thisEventDescription = event->Description();
648
649 #endif
650
651     log->log("Client", Log::DEBUG, "Got an event object %p", event);
652
653     //in the past filter
654     if ((thisEventTime + thisEventDuration) < (ULONG)time(NULL)) continue;
655
656     //24 hour filter
657     if (thisEventTime > ((ULONG)time(NULL) + 86400)) continue;
658
659     if (!thisEventTitle) thisEventTitle = empty;
660     if (!thisEventSubTitle) thisEventSubTitle = empty;
661     if (!thisEventDescription) thisEventDescription = empty;
662
663     thisEventLength = constEventLength + strlen(thisEventTitle) + 1 + strlen(thisEventSubTitle) + 1 + strlen(thisEventDescription) + 1;
664
665     log->log("Client", Log::DEBUG, "Done s1");
666
667     // now extend the buffer if necessary
668     if ((sendBufferUsed + thisEventLength) > sendBufferLength)
669     {
670       log->log("Client", Log::DEBUG, "Extending buffer");
671       sendBufferLength += 100000;
672       UCHAR* temp = (UCHAR*)realloc(sendBuffer, sendBufferLength);
673       if (temp == NULL)
674       {
675         free(sendBuffer);
676         UCHAR sendBuffer2[8];
677         *(ULONG*)&sendBuffer2[0] = htonl(4);
678         *(ULONG*)&sendBuffer2[4] = htonl(0);
679         tcp.sendPacket(sendBuffer2, 8);
680         log->log("Client", Log::DEBUG, "written 0 because failed to realloc packet");
681         return;
682       }
683       sendBuffer = temp;
684     }
685
686     log->log("Client", Log::DEBUG, "Done s2");
687
688     *(ULONG*)&sendBuffer[sendBufferUsed] = htonl(thisEventID);       sendBufferUsed += sizeof(ULONG);
689     *(ULONG*)&sendBuffer[sendBufferUsed] = htonl(thisEventTime);     sendBufferUsed += sizeof(ULONG);
690     *(ULONG*)&sendBuffer[sendBufferUsed] = htonl(thisEventDuration); sendBufferUsed += sizeof(ULONG);
691
692     strcpy((char*)&sendBuffer[sendBufferUsed], thisEventTitle);       sendBufferUsed += strlen(thisEventTitle) + 1;
693     strcpy((char*)&sendBuffer[sendBufferUsed], thisEventSubTitle);    sendBufferUsed += strlen(thisEventSubTitle) + 1;
694     strcpy((char*)&sendBuffer[sendBufferUsed], thisEventDescription); sendBufferUsed += strlen(thisEventDescription) + 1;
695
696     log->log("Client", Log::DEBUG, "Done s3 %lu", sendBufferUsed);
697   }
698
699   log->log("Client", Log::DEBUG, "Got all event data");
700
701   // Write the length into the first 4 bytes. It's sendBufferUsed - 4 because of the hole!
702   *(ULONG*)&sendBuffer[0] = htonl(sendBufferUsed - sizeof(ULONG));
703
704   tcp.sendPacket(sendBuffer, sendBufferUsed);
705   log->log("Client", Log::DEBUG, "written %lu schedules packet", sendBufferUsed);
706
707   free(sendBuffer);
708
709   return;
710 }
711
712 void MVPClient::processConfigSave(UCHAR* buffer, int length)
713 {
714   char* section = (char*)buffer;
715   char* key = NULL;
716   char* value = NULL;
717
718   for (int k = 0; k < length; k++)
719   {
720     if (buffer[k] == '\0')
721     {
722       if (!key)
723       {
724         key = (char*)&buffer[k+1];
725       }
726       else
727       {
728         value = (char*)&buffer[k+1];
729         break;
730       }
731     }
732   }
733
734   // if the last string (value) doesnt have null terminator, give up
735   if (buffer[length - 1] != '\0') return;
736
737   log->log("Client", Log::DEBUG, "Config save: %s %s %s", section, key, value);
738   if (config.setValueString(section, key, value))
739   {
740     sendULONG(1);
741   }
742   else
743   {
744     sendULONG(0);
745   }
746 }
747
748 void MVPClient::processConfigLoad(UCHAR* buffer, int length)
749 {
750   char* section = (char*)buffer;
751   char* key = NULL;
752
753   for (int k = 0; k < length; k++)
754   {
755     if (buffer[k] == '\0')
756     {
757       key = (char*)&buffer[k+1];
758       break;
759     }
760   }
761
762   char* value = config.getValueString(section, key);
763
764   if (value)
765   {
766     UCHAR sendBuffer[4 + strlen(value) + 1];
767     *(ULONG*)&sendBuffer[0] = htonl(strlen(value) + 1);
768     strcpy((char*)&sendBuffer[4], value);
769     tcp.sendPacket(sendBuffer, 4 + strlen(value) + 1);
770
771     log->log("Client", Log::DEBUG, "Written config load packet");
772     delete[] value;
773   }
774   else
775   {
776     UCHAR sendBuffer[8];
777     *(ULONG*)&sendBuffer[0] = htonl(4);
778     *(ULONG*)&sendBuffer[4] = htonl(0);
779     tcp.sendPacket(sendBuffer, 8);
780
781     log->log("Client", Log::DEBUG, "Written config load failed packet");
782   }
783 }
784
785 void MVPClient::cleanConfig()
786 {
787   log->log("Client", Log::DEBUG, "Clean config");
788
789   cRecordings Recordings;
790   Recordings.Load();
791
792   int numReturns;
793   int length;
794   char* resumes = config.getSectionKeyNames("ResumeData", numReturns, length);
795   char* position = resumes;
796   for(int k = 0; k < numReturns; k++)
797   {
798     log->log("Client", Log::DEBUG, "EXAMINING: %i %i %p %s", k, numReturns, position, position);
799
800     cRecording* recording = Recordings.GetByName(position);
801     if (!recording)
802     {
803       // doesn't exist anymore
804       log->log("Client", Log::DEBUG, "Found a recording that doesn't exist anymore");
805       config.deleteValue("ResumeData", position);
806     }
807     else
808     {
809       log->log("Client", Log::DEBUG, "This recording still exists");
810     }
811
812     position += strlen(position) + 1;
813   }
814
815   delete[] resumes;
816 }
817
818
819
820
821
822
823 /*
824     event = Schedule->GetPresentEvent();
825
826     fprintf(f, "\n\nCurrent event\n\n");
827
828     fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", 0, event->GetEventID(), event->GetTime(), event->GetDuration());
829     fprintf(f, "Event %i title = %s subtitle = %s\n", 0, event->GetTitle(), event->GetSubtitle());
830     fprintf(f, "Event %i extendeddescription = %s\n", 0, event->GetExtendedDescription());
831     fprintf(f, "Event %i isFollowing = %i, isPresent = %i\n", 0, event->IsFollowing(), event->IsPresent());
832
833     event = Schedule->GetFollowingEvent();
834
835     fprintf(f, "\n\nFollowing event\n\n");
836
837     fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", 0, event->GetEventID(), event->GetTime(), event->GetDuration());
838     fprintf(f, "Event %i title = %s subtitle = %s\n", 0, event->GetTitle(), event->GetSubtitle());
839     fprintf(f, "Event %i extendeddescription = %s\n", 0, event->GetExtendedDescription());
840     fprintf(f, "Event %i isFollowing = %i, isPresent = %i\n", 0, event->IsFollowing(), event->IsPresent());
841
842     fprintf(f, "\n\n");
843 */
844
845 /*
846     fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", eventNumber, event->GetEventID(), event->GetTime(), event->GetDuration());
847     fprintf(f, "Event %i title = %s subtitle = %s\n", eventNumber, event->GetTitle(), event->GetSubtitle());
848     fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
849     fprintf(f, "Event %i isFollowing = %i, isPresent = %i\n", eventNumber, event->IsFollowing(), event->IsPresent());
850
851     fprintf(f, "\n\n");
852 */
853
854 /*
855
856
857 void MVPClient::test2()
858 {
859   FILE* f = fopen("/tmp/s.txt", "w");
860
861 #if VDRVERSNUM < 10300
862   cMutexLock MutexLock;
863   const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
864 #else
865   cSchedulesLock MutexLock;
866   const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
867 #endif
868
869   if (!Schedules)
870   {
871     fprintf(f, "Schedules = NULL\n");
872     fclose(f);
873     return;
874   }
875
876   fprintf(f, "Schedules dump:\n");
877   Schedules->Dump(f);
878
879
880   const cSchedule *Schedule;
881   int scheduleNumber = 0;
882
883   tChannelID tchid;
884   cChannel *thisChannel;
885
886 #if VDRVERSNUM < 10300
887   const cEventInfo *event;
888   int eventNumber = 0;
889 #else
890   const cEvent *event;
891 #endif
892
893 //    Schedule = Schedules->GetSchedule(channel->GetChannelID());
894 //    Schedule = Schedules->GetSchedule();
895   Schedule = Schedules->First();
896   if (!Schedule)
897   {
898     fprintf(f, "First Schedule = NULL\n");
899     fclose(f);
900     return;
901   }
902
903   while (Schedule)
904   {
905     fprintf(f, "Schedule #%i\n", scheduleNumber);
906     fprintf(f, "-------------\n\n");
907
908 #if VDRVERSNUM < 10300
909     tchid = Schedule->GetChannelID();
910 #else
911     tchid = Schedule->ChannelID();
912 #endif
913
914 #if VDRVERSNUM < 10300
915     fprintf(f, "ChannelID.ToString() = %s\n", tchid.ToString());
916     fprintf(f, "NumEvents() = %i\n", Schedule->NumEvents());
917 #else
918 //  put the count at the end.
919 #endif
920
921     thisChannel = Channels.GetByChannelID(tchid, true);
922     if (thisChannel)
923     {
924       fprintf(f, "Channel Number: %p %i\n", thisChannel, thisChannel->Number());
925     }
926     else
927     {
928       fprintf(f, "thisChannel = NULL for tchid\n");
929     }
930
931 #if VDRVERSNUM < 10300
932     for (eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
933     {
934       event = Schedule->GetEventNumber(eventNumber);
935       fprintf(f, "Event %i tableid = %i timestring = %s endtimestring = %s\n", eventNumber, event->GetTableID(), event->GetTimeString(), event->GetEndTimeString());
936       fprintf(f, "Event %i date = %s isfollowing = %i ispresent = %i\n", eventNumber, event->GetDate(), event->IsFollowing(), event->IsPresent());
937       fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
938       fprintf(f, "Event %i subtitle = %s title = %s\n", eventNumber, event->GetSubtitle(), event->GetTitle());
939       fprintf(f, "Event %i eventid = %u duration = %li time = %lu channelnumber = %i\n", eventNumber, event->GetEventID(), event->GetDuration(), event->GetTime(), event->GetChannelNumber());
940       fprintf(f, "Event %u dump:\n", eventNumber);
941       event->Dump(f);
942       fprintf(f, "\n\n");
943     }
944 #else
945 //  This whole section needs rewriting to walk the list.
946     event = Schedule->Events()->First();
947     while (event) {
948       event = Schedule->Events()->Next(event);
949     }
950 #endif
951
952
953     fprintf(f, "\nDump from object:\n");
954     Schedule->Dump(f);
955     fprintf(f, "\nEND\n");
956
957
958
959
960
961
962
963
964
965     fprintf(f, "End of current Schedule\n\n\n");
966
967     Schedule = (const cSchedule *)Schedules->Next(Schedule);
968     scheduleNumber++;
969   }
970
971   fclose(f);
972 }
973
974
975
976 */
977
978
979
980 /*
981   const cEventInfo *GetPresentEvent(void) const;
982   const cEventInfo *GetFollowingEvent(void) const;
983   const cEventInfo *GetEvent(unsigned short uEventID, time_t tTime = 0) const;
984   const cEventInfo *GetEventAround(time_t tTime) const;
985   const cEventInfo *GetEventNumber(int n) const { return Events.Get(n); }
986
987
988   const unsigned char GetTableID(void) const;
989   const char *GetTimeString(void) const;
990   const char *GetEndTimeString(void) const;
991   const char *GetDate(void) const;
992   bool IsFollowing(void) const;
993   bool IsPresent(void) const;
994   const char *GetExtendedDescription(void) const;
995   const char *GetSubtitle(void) const;
996   const char *GetTitle(void) const;
997   unsigned short GetEventID(void) const;
998   long GetDuration(void) const;
999   time_t GetTime(void) const;
1000   tChannelID GetChannelID(void) const;
1001   int GetChannelNumber(void) const { return nChannelNumber; }
1002   void SetChannelNumber(int ChannelNumber) const { ((cEventInfo *)this)->nChannelNumber = ChannelNumber; } // doesn't modify the EIT data, so it's ok to make it 'const'
1003   void Dump(FILE *f, const char *Prefix = "") const;
1004
1005 */
1006
1007
1008 /*
1009 void MVPClient::test(int channelNumber)
1010 {
1011   FILE* f = fopen("/tmp/test.txt", "w");
1012
1013   cMutexLock MutexLock;
1014   const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
1015
1016   if (!Schedules)
1017   {
1018     fprintf(f, "Schedules = NULL\n");
1019     fclose(f);
1020     return;
1021   }
1022
1023   fprintf(f, "Schedules dump:\n");
1024 //  Schedules->Dump(f);
1025
1026   const cSchedule *Schedule;
1027   cChannel *thisChannel;
1028   const cEventInfo *event;
1029
1030   thisChannel = channelFromNumber(channelNumber);
1031   if (!thisChannel)
1032   {
1033     fprintf(f, "thisChannel = NULL\n");
1034     fclose(f);
1035     return;
1036   }
1037
1038   Schedule = Schedules->GetSchedule(thisChannel->GetChannelID());
1039 //    Schedule = Schedules->GetSchedule();
1040 //  Schedule = Schedules->First();
1041   if (!Schedule)
1042   {
1043     fprintf(f, "First Schedule = NULL\n");
1044     fclose(f);
1045     return;
1046   }
1047
1048   fprintf(f, "NumEvents() = %i\n\n", Schedule->NumEvents());
1049
1050   // For some channels VDR seems to pick a random point in time to
1051   // start dishing out events, but they are in order
1052   // at some point in the list the time snaps to the current event
1053
1054
1055
1056
1057   for (int eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
1058   {
1059     event = Schedule->GetEventNumber(eventNumber);
1060     fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", eventNumber, event->GetEventID(), event->GetTime(), event->GetDuration());
1061     fprintf(f, "Event %i title = %s subtitle = %s\n", eventNumber, event->GetTitle(), event->GetSubtitle());
1062     fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
1063     fprintf(f, "\n\n");
1064   }
1065
1066   fprintf(f, "\nEND\n");
1067
1068   fclose(f);
1069 }
1070
1071 */
1072
1073
1074
1075 /*
1076
1077
1078 Right, so
1079
1080 Schedules = the collection of all the Schedule objects
1081 Schedule  = One schedule, contants all the events for a channel
1082 Event     = One programme
1083
1084
1085 Want:
1086
1087 Event ID
1088 Time
1089 Duration
1090 Title
1091 Subtitle (used for "Programmes resume at ...")
1092 Description
1093
1094 IsPresent ? easy to work out tho. Oh it doesn't always work
1095
1096 */