From 2d48ae784ea6828e8626c32c848f64232d8f35c0 Mon Sep 17 00:00:00 2001 From: geronimo Date: Fri, 13 Jul 2012 04:26:40 +0200 Subject: initial import --- cmps/serverlib/src/AbstractMediaRequestHandler.cc | 46 ++++++ cmps/serverlib/src/CommandHandler.cc | 94 ++++++++++++ cmps/serverlib/src/HTMLListAssembler.cc | 171 ++++++++++++++++++++++ cmps/serverlib/src/HTTPMediaResponse.cc | 50 +++++++ cmps/serverlib/src/JSonListAssembler.cc | 73 +++++++++ cmps/serverlib/src/MediaFileHandler.cc | 62 ++++++++ cmps/serverlib/src/MediaListHandler.cc | 101 +++++++++++++ 7 files changed, 597 insertions(+) create mode 100644 cmps/serverlib/src/AbstractMediaRequestHandler.cc create mode 100644 cmps/serverlib/src/CommandHandler.cc create mode 100644 cmps/serverlib/src/HTMLListAssembler.cc create mode 100644 cmps/serverlib/src/HTTPMediaResponse.cc create mode 100644 cmps/serverlib/src/JSonListAssembler.cc create mode 100644 cmps/serverlib/src/MediaFileHandler.cc create mode 100644 cmps/serverlib/src/MediaListHandler.cc (limited to 'cmps/serverlib/src') diff --git a/cmps/serverlib/src/AbstractMediaRequestHandler.cc b/cmps/serverlib/src/AbstractMediaRequestHandler.cc new file mode 100644 index 0000000..b65ad13 --- /dev/null +++ b/cmps/serverlib/src/AbstractMediaRequestHandler.cc @@ -0,0 +1,46 @@ +/** + * ======================== legal notice ====================== + * + * File: AbstractMediaRequestHandler.cc + * Created: 5. Juli 2012, 07:58 + * Author: Geronimo + * Project: cmps - the backend (server) part of compound media player + * + * CMP - compound media player + * + * is a client/server mediaplayer intended to play any media from any workstation + * without the need to export or mount shares. cmps is an easy to use backend + * with a (ready to use) HTML-interface. Additionally the backend supports + * authentication via HTTP-digest authorization. + * cmpc is a client with vdr-like osd-menues. + * + * Copyright (c) 2012 Reinhard Mantey, some rights reserved! + * published under Creative Commons by-sa + * For details see http://creativecommons.org/licenses/by-sa/3.0/ + * + * The cmp project's homepage is at http://projects.vdr-developer.org/projects/cmp + * + * -------------------------------------------------------------- + */ +#include +#include + +static cFilesystemScanner *scanner = NULL; + +cAbstractMediaRequestHandler::cAbstractMediaRequestHandler() +{ +} + +cAbstractMediaRequestHandler::~cAbstractMediaRequestHandler() +{ +} + +cFilesystemScanner *cAbstractMediaRequestHandler::FileSystemScanner(void) +{ + return scanner; +} + +void cAbstractMediaRequestHandler::SetFilesystemScanner(cFilesystemScanner* Scanner) +{ + scanner = Scanner; +} \ No newline at end of file diff --git a/cmps/serverlib/src/CommandHandler.cc b/cmps/serverlib/src/CommandHandler.cc new file mode 100644 index 0000000..b1a17b8 --- /dev/null +++ b/cmps/serverlib/src/CommandHandler.cc @@ -0,0 +1,94 @@ +/** + * ======================== legal notice ====================== + * + * File: CommandHandler.cc + * Created: 5. Juli 2012, 16:06 + * Author: Geronimo + * Project: cmps - the backend (server) part of compound media player + * + * CMP - compound media player + * + * is a client/server mediaplayer intended to play any media from any workstation + * without the need to export or mount shares. cmps is an easy to use backend + * with a (ready to use) HTML-interface. Additionally the backend supports + * authentication via HTTP-digest authorization. + * cmpc is a client with vdr-like osd-menues. + * + * Copyright (c) 2012 Reinhard Mantey, some rights reserved! + * published under Creative Commons by-sa + * For details see http://creativecommons.org/licenses/by-sa/3.0/ + * + * The cmp project's homepage is at http://projects.vdr-developer.org/projects/cmp + * + * -------------------------------------------------------------- + */ +#include +#include +#include +#include +#include +#include + +typedef struct { + CommandCallback callback; + void *opaque; +} CallbackInfo; +std::tr1::unordered_map supportedCommands; + + +cCommandHandler::cCommandHandler() +{ +} + +cCommandHandler::~cCommandHandler() +{ +} + +void cCommandHandler::Cleanup() +{ + std::tr1::unordered_map::iterator it = supportedCommands.begin(); + + while (it != supportedCommands.end()) { + free(it->second); + ++it; + } +} + +cHTTPResponse *cCommandHandler::ProcessRequest(cHTTPRequest& Request) +{ + const char *command = Request.GetHeader("what"); + CallbackInfo *info; + + isyslog("cCommandHandler::ProcessRequest ..."); + if (!command) { + esyslog("ERROR: no command in request"); + return new cHTTPResponse(HTTP_BadRequest); + } + info = supportedCommands[command]; + if (!info) { + esyslog("ERROR: command not registered/supported!"); + return new cHTTPResponse(HTTP_BadRequest); + } + if ((*info->callback)(info->opaque, Request)) { + esyslog("ERROR: failed to execute command!"); + return new cHTTPResponse(HTTP_InternalServerError); + } + isyslog("OK - all went well?!?"); + + return new cHTTPResponse(HTTP_NoContent); +} + +void cCommandHandler::Usage(cStringBuilder &sb) +{ + +} + +void cCommandHandler::RegisterCallback(const char* CommandID, CommandCallback cb, void* opaque) +{ + CallbackInfo *info = (CallbackInfo *)malloc(sizeof(CallbackInfo)); + + info->callback = cb; + info->opaque = opaque; + supportedCommands[CommandID] = info; +} + diff --git a/cmps/serverlib/src/HTMLListAssembler.cc b/cmps/serverlib/src/HTMLListAssembler.cc new file mode 100644 index 0000000..8ef3261 --- /dev/null +++ b/cmps/serverlib/src/HTMLListAssembler.cc @@ -0,0 +1,171 @@ +/** + * ======================== legal notice ====================== + * + * File: HTMLListAssembler.cc + * Created: 6. Juli 2012, 09:55 + * Author: Geronimo + * Project: cmps - the backend (server) part of compound media player + * + * CMP - compound media player + * + * is a client/server mediaplayer intended to play any media from any workstation + * without the need to export or mount shares. cmps is an easy to use backend + * with a (ready to use) HTML-interface. Additionally the backend supports + * authentication via HTTP-digest authorization. + * cmpc is a client with vdr-like osd-menues. + * + * Copyright (c) 2012 Reinhard Mantey, some rights reserved! + * published under Creative Commons by-sa + * For details see http://creativecommons.org/licenses/by-sa/3.0/ + * + * The cmp project's homepage is at http://projects.vdr-developer.org/projects/cmp + * + * -------------------------------------------------------------- + */ +#include +#include +#include +#include + +cHTMLListAssembler::cHTMLListAssembler() +{ +} + +cHTMLListAssembler::~cHTMLListAssembler() +{ +} + +bool cHTMLListAssembler::OpenList(cStringBuilder &sb, std::map &Categories, size_t total, size_t start, uint delta) +{ + genHTMLPrefix(sb, "Medien-Liste"); + + genPreviousLink(sb, !start ? -1 : max(0, start - delta)); + genNextLink(sb, start + delta >= total ? -1 : min(start + delta, total)); + genDelim(sb); + genTypeMenu(sb, Categories); + genDelim(sb); + + return true; +} + +bool cHTMLListAssembler::AddElement(cStringBuilder &sb, void *ListElement, bool odd) +{ + genMediaLink(sb, ListElement, odd); + + return true; +} + +bool cHTMLListAssembler::CloseList(cStringBuilder &sb, size_t total, size_t start, uint delta) +{ + genDelim(sb); + genPreviousLink(sb, !start ? -1 : max(0, start - delta)); + genNextLink(sb, start + delta >= total ? -1 : min(start + delta, total)); + + return true; +} + +cStringBuilder &cHTMLListAssembler::genHTMLPrefix(cStringBuilder& sb, const char *Title) +{ + sb.Append("").Append(Title).Append(""); + + sb.Append(""); + sb.Append(""); + sb.Append("

").Append(Title).Append("

"); + + return sb; +} + +const char *cHTMLListAssembler::MediaType() const +{ + return "text/html"; +} + +cStringBuilder &cHTMLListAssembler::genDelim(cStringBuilder &sb) +{ + return sb.Append("

"); +} + +cStringBuilder &cHTMLListAssembler::genMediaLink(cStringBuilder &sb, void *Element, bool odd) +{ + cAbstractMedia *m = (cAbstractMedia *)Element; + + sb.Append("LogicalPath()); + sb.Append("\">"); + switch (m->MediaType()) { + case cAbstractMedia::Audio: sb.Append("[A] "); break; + case cAbstractMedia::Movie: sb.Append("[M] "); break; + case cAbstractMedia::DVDImage: sb.Append("[D] "); break; + case cAbstractMedia::LegacyVdrRecording: sb.Append("[L] "); break; + case cAbstractMedia::VdrRecording: sb.Append("[V] "); break; + case cAbstractMedia::Picture: sb.Append("[P] "); break; + default: ; + } + sb.Append(m->Name()); + sb.Append(""); + + return sb; +} + +cStringBuilder &cHTMLListAssembler::genNextLink(cStringBuilder &sb, int n) +{ + sb.Append("
"); + if (n < 0) + sb.Append("next"); + else { + sb.Append(""); + sb.Append("next"); + sb.Append(""); + } + sb.Append("
"); + + return sb; +} + +cStringBuilder &cHTMLListAssembler::genPreviousLink(cStringBuilder &sb, int n) +{ + sb.Append("
"); + if (n < 0) + sb.Append("previous"); + else { + sb.Append(""); + sb.Append("previous"); + sb.Append(""); + } + sb.Append("
"); + + return sb; +} + +cStringBuilder &cHTMLListAssembler::genTypeMenu(cStringBuilder &sb, std::map &Categories) +{ + sb.Append("
"); + std::map::iterator it = Categories.begin(); + + while (it != Categories.end()) { + sb.Append("second); + sb.Append("\">"); + sb.Append(cAbstractMedia::MediaType2Text(it->first)); + sb.Append(""); + ++it; + } + sb.Append("
"); + + return sb; +} \ No newline at end of file diff --git a/cmps/serverlib/src/HTTPMediaResponse.cc b/cmps/serverlib/src/HTTPMediaResponse.cc new file mode 100644 index 0000000..667d114 --- /dev/null +++ b/cmps/serverlib/src/HTTPMediaResponse.cc @@ -0,0 +1,50 @@ +/** + * ======================== legal notice ====================== + * + * File: HTTPMediaResponse.cc + * Created: 6. Juli 2012, 07:44 + * Author: Geronimo + * Project: cmps - the backend (server) part of compound media player + * + * CMP - compound media player + * + * is a client/server mediaplayer intended to play any media from any workstation + * without the need to export or mount shares. cmps is an easy to use backend + * with a (ready to use) HTML-interface. Additionally the backend supports + * authentication via HTTP-digest authorization. + * cmpc is a client with vdr-like osd-menues. + * + * Copyright (c) 2012 Reinhard Mantey, some rights reserved! + * published under Creative Commons by-sa + * For details see http://creativecommons.org/licenses/by-sa/3.0/ + * + * The cmp project's homepage is at http://projects.vdr-developer.org/projects/cmp + * + * -------------------------------------------------------------- + */ +#include +#include + +cHTTPMediaResponse::cHTTPMediaResponse(cAbstractMedia *Media) + : cHTTPFileResponse() + , media(Media) +{ + if (media) { + char *lm = FormatTime(media->LastModified()); + + SetHeader("LastModified", lm); + free(lm); + SetContentSize(media->Size()); + SetContentType(media->MimeType()); + } +} + +cHTTPMediaResponse::~cHTTPMediaResponse() +{ +} + +size_t cHTTPMediaResponse::ReadContentChunk(char* Buf, size_t bufSize) +{ + if (!media) return 0; + return media->ReadChunk(Buf, bufSize); +} \ No newline at end of file diff --git a/cmps/serverlib/src/JSonListAssembler.cc b/cmps/serverlib/src/JSonListAssembler.cc new file mode 100644 index 0000000..c5126a3 --- /dev/null +++ b/cmps/serverlib/src/JSonListAssembler.cc @@ -0,0 +1,73 @@ +/** + * ======================== legal notice ====================== + * + * File: JSonListAssembler.cc + * Created: 6. Juli 2012, 09:53 + * Author: Geronimo + * Project: cmps - the backend (server) part of compound media player + * + * CMP - compound media player + * + * is a client/server mediaplayer intended to play any media from any workstation + * without the need to export or mount shares. cmps is an easy to use backend + * with a (ready to use) HTML-interface. Additionally the backend supports + * authentication via HTTP-digest authorization. + * cmpc is a client with vdr-like osd-menues. + * + * Copyright (c) 2012 Reinhard Mantey, some rights reserved! + * published under Creative Commons by-sa + * For details see http://creativecommons.org/licenses/by-sa/3.0/ + * + * The cmp project's homepage is at http://projects.vdr-developer.org/projects/cmp + * + * -------------------------------------------------------------- + */ +#include +#include +#include + +cJSonListAssembler::cJSonListAssembler() +{ +} + +cJSonListAssembler::~cJSonListAssembler() +{ +} + +bool cJSonListAssembler::OpenList(cStringBuilder &sb, std::map &Categories, size_t total, size_t start, uint delta) +{ + cJSonWriter writer(sb); + + writer.Object().Key("total").Value(total); + writer.Key("results").Array(); + + return true; +} + +bool cJSonListAssembler::AddElement(cStringBuilder &sb, void *ListElement, bool odd) +{ + cJSonWriter writer(sb); + cAbstractMedia *m = (cAbstractMedia *)ListElement; + + writer.Object(); + writer.Key("name").Value(m->Name()); + writer.Key("type").Value(m->MediaType()); + writer.Key("path").Value(m->LogicalPath()); + writer.EndObject(); + + return true; +} + +bool cJSonListAssembler::CloseList(cStringBuilder &sb, size_t total, size_t start, uint delta) +{ + cJSonWriter writer(sb); + + writer.EndArray().EndObject(); + + return true; +} + +const char *cJSonListAssembler::MediaType(void) const +{ + return "application/json"; +} \ No newline at end of file diff --git a/cmps/serverlib/src/MediaFileHandler.cc b/cmps/serverlib/src/MediaFileHandler.cc new file mode 100644 index 0000000..d16a033 --- /dev/null +++ b/cmps/serverlib/src/MediaFileHandler.cc @@ -0,0 +1,62 @@ +/** + * ======================== legal notice ====================== + * + * File: MediaFileHandler.cc + * Created: 5. Juli 2012, 08:06 + * Author: Geronimo + * Project: cmps - the backend (server) part of compound media player + * + * CMP - compound media player + * + * is a client/server mediaplayer intended to play any media from any workstation + * without the need to export or mount shares. cmps is an easy to use backend + * with a (ready to use) HTML-interface. Additionally the backend supports + * authentication via HTTP-digest authorization. + * cmpc is a client with vdr-like osd-menues. + * + * Copyright (c) 2012 Reinhard Mantey, some rights reserved! + * published under Creative Commons by-sa + * For details see http://creativecommons.org/licenses/by-sa/3.0/ + * + * The cmp project's homepage is at http://projects.vdr-developer.org/projects/cmp + * + * -------------------------------------------------------------- + */ +#include +#include +#include +#include +#include +#include +#include + +cMediaFileHandler::cMediaFileHandler() +{ +} + +cMediaFileHandler::~cMediaFileHandler() +{ +} + +cHTTPResponse *cMediaFileHandler::ProcessRequest(cHTTPRequest& Request) +{ + cAbstractMedia *media = FileSystemScanner()->FindMedia(Request.Url().Path()); + + isyslog("cMediaFileHandler::ProcessRequest ... %0X", media); + + if (!media) return NULL; + media->Refresh(); + + return new cHTTPMediaResponse(media); +} + +void cMediaFileHandler::Usage(cStringBuilder& sb) +{ + isyslog("start of cMediaFileHandler::Usage() ..."); + sb.Append("
"); + sb.Append("/any/path/to/media/file"); + sb.Append("
"); + sb.Append("returns the media-file ready to be played by client media-players."); + sb.Append("
"); + isyslog("end of cMediaFileHandler::Usage() ..."); +} \ No newline at end of file diff --git a/cmps/serverlib/src/MediaListHandler.cc b/cmps/serverlib/src/MediaListHandler.cc new file mode 100644 index 0000000..029c137 --- /dev/null +++ b/cmps/serverlib/src/MediaListHandler.cc @@ -0,0 +1,101 @@ +/** + * ======================== legal notice ====================== + * + * File: MediaListHandler.cc + * Created: 5. Juli 2012, 08:06 + * Author: Geronimo + * Project: cmps - the backend (server) part of compound media player + * + * CMP - compound media player + * + * is a client/server mediaplayer intended to play any media from any workstation + * without the need to export or mount shares. cmps is an easy to use backend + * with a (ready to use) HTML-interface. Additionally the backend supports + * authentication via HTTP-digest authorization. + * cmpc is a client with vdr-like osd-menues. + * + * Copyright (c) 2012 Reinhard Mantey, some rights reserved! + * published under Creative Commons by-sa + * For details see http://creativecommons.org/licenses/by-sa/3.0/ + * + * The cmp project's homepage is at http://projects.vdr-developer.org/projects/cmp + * + * -------------------------------------------------------------- + */ +#include +#include +#include +#include +#include +#include +#include + +cMediaListHandler::cMediaListHandler() +{ +} + +cMediaListHandler::~cMediaListHandler() +{ + AssemblerList::iterator it = listAssemblers.begin(); + + while (it != listAssemblers.end()) { + delete it->second; + ++it; + } + if (defaultListAssembler) delete defaultListAssembler; +} + +cAbstractListAssembler *cMediaListHandler::ListAssembler(const char* ID) +{ + if (!ID) return defaultListAssembler; + cAbstractListAssembler *rv = listAssemblers[ID]; + + if (rv) return rv; + return defaultListAssembler; +} + +cHTTPResponse *cMediaListHandler::ProcessRequest(cHTTPRequest& Request) +{ + isyslog("cMediaListHandler::ProcessRequest ..."); + cHTTPResponse *res = NULL; + cAbstractListAssembler *la = ListAssembler(Request.Url().Parameter("format")); + const char *hdStart = Request.Url().Parameter("start"); + const char *hdDelta = Request.Url().Parameter("limit"); + int start = 0; + int delta = 40; + + if (hdStart) start = atol(hdStart); + if (hdDelta) delta = atol(hdDelta); + if (!la) return new cHTTPResponse(HTTP_NotFound); + res = new cHTTPResponse(HTTP_OK); + res->SetContentType(la->MediaType()); + la->AssembleList(res->StringBuilder(), FileSystemScanner()->MediaPool(), FileSystemScanner()->Categories(), start, delta); + + return res; +} + +void cMediaListHandler::Usage(cStringBuilder& sb) +{ + isyslog("start of cMediaListHandler::Usage() ..."); + sb.Append("returns a list of known/supported media on this machine. To access any media from the list, "); + sb.Append("use the path as uri (using the http protocol).
"); + sb.Append("supported parameters:
    "); + sb.Append("
    start=#
    "); + sb.Append("
    used to step list by chunks, where # stands for the index of the first element to retrieve
    "); + sb.Append("
    limit=#
    "); + sb.Append("
    gives the chunksize (default is 40), where # stands for the number of elements to retrieve
    "); + sb.Append("
    format=json
    "); + sb.Append("
    default format is html, so if you like to get the list in json format, use this parameter
    "); + sb.Append("
"); + isyslog("end of cMediaListHandler::Usage() ..."); +} + +void cMediaListHandler::SetDefaultListAssembler(cAbstractListAssembler* assembler) +{ + defaultListAssembler = assembler; +} + +void cMediaListHandler::SetListAssembler(const char* ID, cAbstractListAssembler* assembler) +{ + listAssemblers[ID] = assembler; +} -- cgit v1.2.3