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
23 VDR* VDR::instance = NULL;
32 pthread_mutex_init(&mutex, NULL);
38 if (initted) shutdown();
41 VDR* VDR::getInstance()
46 int VDR::init(int tport)
48 if (initted) return 0;
51 logger = Log::getInstance();
57 if (!initted) return 0;
63 void VDR::findServers(std::vector<char*>& serverIPs)
66 char* message = "VOMP CLIENT";
67 DatagramSocket ds(port);
69 int haveAtLeastOne = 0;
77 logger->log("Core", Log::NOTICE, "Broadcasting for server");
78 ds.send("255.255.255.255", 3024, message, strlen(message));
80 retval = ds.waitforMessage(waitType);
82 if (retval == 2) // we got a reply
84 if (strcmp(ds.getData(), "VOMP SERVER")) // echo.....
91 strcpy(newIP, ds.getFromIPA());
92 serverIPs.push_back(newIP);
99 if (haveAtLeastOne) break;
105 void VDR::cancelFindingServer()
110 void VDR::setServerIP(char* newIP)
112 strcpy(serverIP, newIP);
119 return tcp->connectTo(serverIP, 3024);
122 void VDR::disconnect()
128 long VDR::getSimpleReply()
130 unsigned char* p = (unsigned char*)tcp->receivePacket();
133 Log::getInstance()->log("VDR", Log::DEBUG, "tcp data length = %i", tcp->getDataLength());
135 if (tcp->getDataLength() != 4)
141 ULONG reply = ntohl(*(ULONG*)p);
143 Log::getInstance()->log("VDR", Log::DEBUG, "VDR said %li", reply);
150 char* VDR::getStringReply()
152 unsigned char* p = (unsigned char*)tcp->receivePacket();
155 int dataLength = tcp->getDataLength();
157 Log::getInstance()->log("VDR", Log::DEBUG, "Data length %u", dataLength);
164 tLength = strlen((char*)&p[count]);
165 returnText = new char[tLength + 1];
166 strcpy(returnText, (char*)&p[count]);
167 count += tLength + 1;
174 /////////////////////////////////////////////////////////////////////////////
180 *(unsigned long*)&buffer[0] = htonl(4);
181 *(unsigned long*)&buffer[4] = htonl(VDR_LOGIN);
183 pthread_mutex_lock(&mutex);
184 int a = tcp->sendPacket(buffer, 8);
187 pthread_mutex_unlock(&mutex);
193 UCHAR* p = tcp->receivePacket();
194 pthread_mutex_unlock(&mutex);
197 unsigned long vdrTime = ntohl(*(unsigned long*)p);
198 Log::getInstance()->log("VDR", Log::DEBUG, "vdrtime = %lu", vdrTime);
200 struct timespec currentTime;
201 currentTime.tv_sec = vdrTime;
202 currentTime.tv_nsec = 0;
203 int b = clock_settime(CLOCK_REALTIME, ¤tTime);
205 Log::getInstance()->log("VDR", Log::DEBUG, "set clock = %u", b);
211 Directory* VDR::getRecordingsList()
215 *(unsigned long*)&buffer[0] = htonl(4);
216 *(unsigned long*)&buffer[4] = htonl(VDR_GETRECORDINGLIST);
218 pthread_mutex_lock(&mutex);
219 int a = tcp->sendPacket(buffer, 8);
222 pthread_mutex_unlock(&mutex);
228 unsigned char* p = (unsigned char*)tcp->receivePacket();
229 pthread_mutex_unlock(&mutex);
232 Directory* recDir = new Directory();
233 recDir->setName("/");
236 int dataLength = tcp->getDataLength();
238 Log::getInstance()->log("VDR", Log::DEBUG, "Data length %u", dataLength);
242 Directory::totalSpace = (*(ULONG*)&p[count]);
243 count += sizeof(ULONG);
244 Directory::freeSpace = (*(ULONG*)&p[count]);
245 count += sizeof(ULONG);
246 Directory::usedPercent = (*(ULONG*)&p[count]);
247 count += sizeof(ULONG);
251 while (count < dataLength)
253 Recording* rec = new Recording();
254 rec->start = ntohl(*(unsigned long*)&p[count]);
257 tLength = strlen((char*)&p[count]);
258 rec->setName((char*)&p[count]);
260 // rec->name = new char[tLength + 1];
261 // strcpy(rec->name, (char*)&p[count]);
262 count += tLength + 1;
264 tLength = strlen((char*)&p[count]);
265 rec->fileName = new char[tLength + 1];
266 strcpy(rec->fileName, (char*)&p[count]);
267 count += tLength + 1;
271 char* dirName = rec->getDirName();
273 Directory* d = recDir->getDirByName(dirName);
278 Log::getInstance()->log("VDR", Log::DEBUG, "Added a new directory = %s", d->name);
279 recDir->dirList->add(d);
282 d->recList->add(rec);
287 recDir->recList->add(rec);
288 recDir->recList->next();
291 Log::getInstance()->log("VDR", Log::DEBUG, "Have added a recording to list. %lu %s", rec->start, rec->getProgName());
299 int VDR::deleteRecording(char* fileName)
301 unsigned long totalLength = 8 + strlen(fileName) + 1;
302 UCHAR buffer[totalLength];
304 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
305 *(unsigned long*)&buffer[4] = htonl(VDR_DELETERECORDING);
306 strcpy((char*)&buffer[8], fileName);
308 pthread_mutex_lock(&mutex);
309 unsigned int a = tcp->sendPacket(buffer, totalLength);
310 if (a != totalLength)
312 pthread_mutex_unlock(&mutex);
316 int toReturn = getSimpleReply();
317 pthread_mutex_unlock(&mutex);
321 char* VDR::getRecordingSummary(char* fileName)
323 unsigned long totalLength = 8 + strlen(fileName) + 1;
324 UCHAR buffer[totalLength];
326 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
327 *(unsigned long*)&buffer[4] = htonl(VDR_GETSUMMARY);
328 strcpy((char*)&buffer[8], fileName);
330 pthread_mutex_lock(&mutex);
331 unsigned int a = tcp->sendPacket(buffer, totalLength);
332 if (a != totalLength)
334 pthread_mutex_unlock(&mutex);
338 char* toReturn = getStringReply();
339 pthread_mutex_unlock(&mutex);
343 List* VDR::getChannelsList(ULONG type)
347 *(unsigned long*)&buffer[0] = htonl(4);
348 *(unsigned long*)&buffer[4] = htonl(VDR_GETCHANNELLIST);
350 pthread_mutex_lock(&mutex);
351 int a = tcp->sendPacket(buffer, 8);
354 pthread_mutex_unlock(&mutex);
360 unsigned char* p = (unsigned char*)tcp->receivePacket();
361 pthread_mutex_unlock(&mutex);
364 List* chanList = new List();
366 int dataLength = tcp->getDataLength();
368 Log::getInstance()->log("VDR", Log::DEBUG, "Data length %u", dataLength);
374 while (count < dataLength)
376 Channel* chan = new Channel();
377 chan->number = ntohl(*(unsigned long*)&p[count]);
379 chan->type = ntohl(*(unsigned long*)&p[count]);
382 tLength = strlen((char*)&p[count]);
383 chan->name = new char[tLength + 1];
384 strcpy(chan->name, (char*)&p[count]);
385 count += tLength + 1;
387 if (chan->type == type)
391 Log::getInstance()->log("VDR", Log::DEBUG, "Have added a channel to list. %lu %lu %s", chan->number, chan->type, chan->name);
404 int VDR::streamChannel(ULONG number)
408 *(unsigned long*)&buffer[0] = htonl(8);
409 *(unsigned long*)&buffer[4] = htonl(VDR_STREAMCHANNEL);
410 *(unsigned long*)&buffer[8] = htonl(number);
412 pthread_mutex_lock(&mutex);
413 int a = tcp->sendPacket(buffer, 12);
417 pthread_mutex_unlock(&mutex);
421 int toReturn = getSimpleReply();
422 pthread_mutex_unlock(&mutex);
426 int VDR::stopStreaming()
430 *(unsigned long*)&buffer[0] = htonl(4);
431 *(unsigned long*)&buffer[4] = htonl(VDR_STOPSTREAMING);
433 pthread_mutex_lock(&mutex);
434 int a = tcp->sendPacket(buffer, 8);
438 pthread_mutex_unlock(&mutex);
442 int toReturn = getSimpleReply();
443 pthread_mutex_unlock(&mutex);
447 int VDR::getBlock(UCHAR* buf, ULLONG position, int amount)
451 *(unsigned long*)&buffer[0] = htonl(16);
452 *(unsigned long*)&buffer[4] = htonl(VDR_GETBLOCK);
453 *(ULLONG*)&buffer[8] = htonll(position);
454 *(unsigned long*)&buffer[16] = htonl(amount);
456 pthread_mutex_lock(&mutex);
457 int a = tcp->sendPacket(buffer, 20);
460 pthread_mutex_unlock(&mutex);
464 unsigned char* p = (unsigned char*)tcp->receivePacket();
465 pthread_mutex_unlock(&mutex);
467 int dataLength = tcp->getDataLength();
469 memcpy(buf, p, dataLength);
474 ULLONG VDR::streamRecording(Recording* rec)
476 unsigned long totalLength = 8 + strlen(rec->fileName) + 1;
477 UCHAR buffer[totalLength];
479 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
480 *(unsigned long*)&buffer[4] = htonl(VDR_STREAMRECORDING);
481 strcpy((char*)&buffer[8], rec->fileName);
483 pthread_mutex_lock(&mutex);
484 unsigned int a = tcp->sendPacket(buffer, totalLength);
485 if (a != totalLength)
487 pthread_mutex_unlock(&mutex);
491 unsigned char* p = (unsigned char*)tcp->receivePacket();
492 pthread_mutex_unlock(&mutex);
495 if (tcp->getDataLength() != 8)
501 ULLONG recordingLength = ntohll(*(ULLONG*)p);
503 Log::getInstance()->log("VDR", Log::DEBUG, "VDR said length is: %llu", recordingLength);
506 return recordingLength;
509 int VDR::getChannelSchedule(ULONG number)
513 *(unsigned long*)&buffer[0] = htonl(8);
514 *(unsigned long*)&buffer[4] = htonl(VDR_GETCHANNELSCHEDULE);
515 *(unsigned long*)&buffer[8] = htonl(number);
517 pthread_mutex_lock(&mutex);
518 int a = tcp->sendPacket(buffer, 12);
522 pthread_mutex_unlock(&mutex);
526 unsigned char* p = (unsigned char*)tcp->receivePacket();
527 pthread_mutex_unlock(&mutex);
530 int dataLength = tcp->getDataLength();
538 ULONG data = ntohl(*(ULONG*)p);
541 Log::getInstance()->log("VDR", Log::DEBUG, "Success got to end of getChannelSchedule %lu", data);
546 ULLONG VDR::getResumePoint(char* fileName)
548 char* resumeString = configLoad("ResumeData", fileName);
549 if (!resumeString) return 0;
551 ULLONG toReturn = strtoull(resumeString, NULL, 10);
552 delete[] resumeString;
556 int VDR::configSave(char* section, char* key, char* value)
558 ULONG totalLength = 8 + strlen(section) + strlen(key) + strlen(value) + 3; // 8 for headers, 3 for nulls
559 UCHAR buffer[totalLength];
561 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
562 *(unsigned long*)&buffer[4] = htonl(VDR_CONFIGSAVE);
565 strcpy((char*)&buffer[position], section);
566 position += strlen(section) + 1;
567 strcpy((char*)&buffer[position], key);
568 position += strlen(key) + 1;
569 strcpy((char*)&buffer[position], value);
571 pthread_mutex_lock(&mutex);
572 unsigned int a = tcp->sendPacket(buffer, totalLength);
573 if (a != totalLength)
575 pthread_mutex_unlock(&mutex);
579 int toReturn = getSimpleReply();
580 pthread_mutex_unlock(&mutex);
584 char* VDR::configLoad(char* section, char* key)
586 ULONG totalLength = 8 + strlen(section) + strlen(key) + 2; // 8 for headers, 2 for nulls
587 UCHAR buffer[totalLength];
589 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
590 *(unsigned long*)&buffer[4] = htonl(VDR_CONFIGLOAD);
593 strcpy((char*)&buffer[position], section);
594 position += strlen(section) + 1;
595 strcpy((char*)&buffer[position], key);
597 pthread_mutex_lock(&mutex);
598 unsigned int a = tcp->sendPacket(buffer, totalLength);
599 if (a != totalLength)
601 pthread_mutex_unlock(&mutex);
605 char* toReturn = getStringReply();
606 pthread_mutex_unlock(&mutex);