From 2331befb330683d0e5459f9ca2d522f2f06c3d7e Mon Sep 17 00:00:00 2001 From: geronimo Date: Mon, 30 Jul 2012 09:12:30 +0200 Subject: worked out commandreader --- libs/IO/IO.cbp | 4 ++ libs/IO/IO.layout | 20 +++--- libs/IO/include/CommandReader.h | 49 ++++++++++++++ libs/IO/include/ConfigReader.h | 3 +- libs/IO/include/FileReader.h | 11 ++-- libs/IO/include/FileRepresentation.h | 12 ++-- libs/IO/include/LineReader.h | 10 ++- libs/IO/include/Reader.h | 41 ++++++++++++ libs/IO/nbproject/Makefile-Debug.mk | 12 ++++ libs/IO/nbproject/Makefile-Release.mk | 12 ++++ libs/IO/nbproject/configurations.xml | 4 ++ libs/IO/src/CommandReader.cc | 120 ++++++++++++++++++++++++++++++++++ libs/IO/src/File.cc | 12 ++-- libs/IO/src/FileReader.cc | 18 +++-- libs/IO/src/FileRepresentation.cc | 12 ++-- libs/IO/src/LineReader.cc | 4 +- libs/IO/src/Reader.cc | 44 +++++++++++++ 17 files changed, 335 insertions(+), 53 deletions(-) create mode 100644 libs/IO/include/CommandReader.h create mode 100644 libs/IO/include/Reader.h create mode 100644 libs/IO/src/CommandReader.cc create mode 100644 libs/IO/src/Reader.cc (limited to 'libs/IO') diff --git a/libs/IO/IO.cbp b/libs/IO/IO.cbp index 36f087a..e1377fc 100644 --- a/libs/IO/IO.cbp +++ b/libs/IO/IO.cbp @@ -48,18 +48,22 @@ + + + + diff --git a/libs/IO/IO.layout b/libs/IO/IO.layout index 0e6cd97..4af9097 100644 --- a/libs/IO/IO.layout +++ b/libs/IO/IO.layout @@ -1,24 +1,24 @@ - + - + - + - + - + - + - + - + @@ -31,9 +31,9 @@ - + - + diff --git a/libs/IO/include/CommandReader.h b/libs/IO/include/CommandReader.h new file mode 100644 index 0000000..e54a64c --- /dev/null +++ b/libs/IO/include/CommandReader.h @@ -0,0 +1,49 @@ +/** + * ======================== legal notice ====================== + * + * File: CommandReader.h + * Created: 30. Juli 2012, 06:42 + * Author: Geronimo + * Project: libIO: classes for files, filesystem and input/output + * + * 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 + * + * -------------------------------------------------------------- + */ +#ifndef COMMANDREADER_H +#define COMMANDREADER_H + +#include +#include +#include + +class cCommandReader : public cReader { +public: + cCommandReader(const char *cmd); + virtual ~cCommandReader(); + + void AddCommandParameter(const char *Param); + +protected: + virtual bool Open(void); + virtual void Close(void); + +private: + pid_t pid; + std::vector args; + }; + +#endif /* COMMANDREADER_H */ + diff --git a/libs/IO/include/ConfigReader.h b/libs/IO/include/ConfigReader.h index f3aa5eb..6d0cd30 100644 --- a/libs/IO/include/ConfigReader.h +++ b/libs/IO/include/ConfigReader.h @@ -29,8 +29,7 @@ #include class cLineReader; -class cConfigReader -{ +class cConfigReader { public: typedef std::tuple ConfigEntry; diff --git a/libs/IO/include/FileReader.h b/libs/IO/include/FileReader.h index 31cb7d6..816b140 100644 --- a/libs/IO/include/FileReader.h +++ b/libs/IO/include/FileReader.h @@ -25,18 +25,19 @@ #ifndef FILEREADER_H #define FILEREADER_H +#include + class cFile; -class cFileReader -{ +class cFileReader : public cReader { public: cFileReader(cFile *Input); virtual ~cFileReader(); - int Read(char *buf, int bufSize); - void Close(); +protected: + virtual bool Open(void); + virtual void Close(void); private: - int fd; cFile *file; }; diff --git a/libs/IO/include/FileRepresentation.h b/libs/IO/include/FileRepresentation.h index 8bc9d9b..2c2db5a 100644 --- a/libs/IO/include/FileRepresentation.h +++ b/libs/IO/include/FileRepresentation.h @@ -1,25 +1,25 @@ /** * ======================== legal notice ====================== - * + * * File: FileRepresentation.h * Created: 21. Juli 2012, 12 * Author: Geronimo * Project: libIO: classes for files, filesystem and input/output - * + * * 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 - * + * * -------------------------------------------------------------- */ #ifndef FILEREPRESENTATION_H diff --git a/libs/IO/include/LineReader.h b/libs/IO/include/LineReader.h index 8b8b754..3de64dc 100644 --- a/libs/IO/include/LineReader.h +++ b/libs/IO/include/LineReader.h @@ -25,19 +25,17 @@ #ifndef LINEREADER_H #define LINEREADER_H - -class cFileReader; -class cLineReader -{ +class cReader; +class cLineReader { public: - cLineReader(cFileReader *FileReader); + cLineReader(cReader *Reader); virtual ~cLineReader(); const char *ReadLine(void); void Close(void); private: - cFileReader *reader; + cReader *reader; int bytesRead; int off; int bufSize; diff --git a/libs/IO/include/Reader.h b/libs/IO/include/Reader.h new file mode 100644 index 0000000..bc346a6 --- /dev/null +++ b/libs/IO/include/Reader.h @@ -0,0 +1,41 @@ +/** + * ======================== legal notice ====================== + * + * File: Reader.h + * Created: 30. Juli 2012, 06:44 + * Author: Geronimo + * Project: libIO: classes for files, filesystem and input/output + * + * 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 + * + * -------------------------------------------------------------- + */ +#ifndef READER_H +#define READER_H + +class cReader { +public: + cReader(); + virtual ~cReader(); + virtual int Read(char *buf, int bufSize); + +protected: + virtual bool Open(void) = 0; + virtual void Close(void) = 0; + int fd; + }; + +#endif /* READER_H */ + diff --git a/libs/IO/nbproject/Makefile-Debug.mk b/libs/IO/nbproject/Makefile-Debug.mk index 0fa73d7..36615b9 100644 --- a/libs/IO/nbproject/Makefile-Debug.mk +++ b/libs/IO/nbproject/Makefile-Debug.mk @@ -35,9 +35,11 @@ OBJECTDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM} # Object Files OBJECTFILES= \ + ${OBJECTDIR}/src/CommandReader.o \ ${OBJECTDIR}/src/FileRepresentation.o \ ${OBJECTDIR}/src/LineReader.o \ ${OBJECTDIR}/src/File.o \ + ${OBJECTDIR}/src/Reader.o \ ${OBJECTDIR}/src/ConfigReader.o \ ${OBJECTDIR}/src/FileReader.o \ ${OBJECTDIR}/src/FileSystem.o @@ -69,6 +71,11 @@ ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/libio.a: ${OBJECTFILES} ${AR} -rv ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/libio.a ${OBJECTFILES} $(RANLIB) ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/libio.a +${OBJECTDIR}/src/CommandReader.o: src/CommandReader.cc + ${MKDIR} -p ${OBJECTDIR}/src + ${RM} $@.d + $(COMPILE.cc) -g -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE=1 -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE -D_REENTRANT -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -Iinclude -I../util/include -I../vdr/include -MMD -MP -MF $@.d -o ${OBJECTDIR}/src/CommandReader.o src/CommandReader.cc + ${OBJECTDIR}/src/FileRepresentation.o: src/FileRepresentation.cc ${MKDIR} -p ${OBJECTDIR}/src ${RM} $@.d @@ -84,6 +91,11 @@ ${OBJECTDIR}/src/File.o: src/File.cc ${RM} $@.d $(COMPILE.cc) -g -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE=1 -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE -D_REENTRANT -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -Iinclude -I../util/include -I../vdr/include -MMD -MP -MF $@.d -o ${OBJECTDIR}/src/File.o src/File.cc +${OBJECTDIR}/src/Reader.o: src/Reader.cc + ${MKDIR} -p ${OBJECTDIR}/src + ${RM} $@.d + $(COMPILE.cc) -g -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE=1 -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE -D_REENTRANT -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -Iinclude -I../util/include -I../vdr/include -MMD -MP -MF $@.d -o ${OBJECTDIR}/src/Reader.o src/Reader.cc + ${OBJECTDIR}/src/ConfigReader.o: src/ConfigReader.cc ${MKDIR} -p ${OBJECTDIR}/src ${RM} $@.d diff --git a/libs/IO/nbproject/Makefile-Release.mk b/libs/IO/nbproject/Makefile-Release.mk index 853a053..29ba2ae 100644 --- a/libs/IO/nbproject/Makefile-Release.mk +++ b/libs/IO/nbproject/Makefile-Release.mk @@ -35,9 +35,11 @@ OBJECTDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM} # Object Files OBJECTFILES= \ + ${OBJECTDIR}/src/CommandReader.o \ ${OBJECTDIR}/src/FileRepresentation.o \ ${OBJECTDIR}/src/LineReader.o \ ${OBJECTDIR}/src/File.o \ + ${OBJECTDIR}/src/Reader.o \ ${OBJECTDIR}/src/ConfigReader.o \ ${OBJECTDIR}/src/FileReader.o \ ${OBJECTDIR}/src/FileSystem.o @@ -69,6 +71,11 @@ ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/libio.a: ${OBJECTFILES} ${AR} -rv ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/libio.a ${OBJECTFILES} $(RANLIB) ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/libio.a +${OBJECTDIR}/src/CommandReader.o: src/CommandReader.cc + ${MKDIR} -p ${OBJECTDIR}/src + ${RM} $@.d + $(COMPILE.cc) -O2 -MMD -MP -MF $@.d -o ${OBJECTDIR}/src/CommandReader.o src/CommandReader.cc + ${OBJECTDIR}/src/FileRepresentation.o: src/FileRepresentation.cc ${MKDIR} -p ${OBJECTDIR}/src ${RM} $@.d @@ -84,6 +91,11 @@ ${OBJECTDIR}/src/File.o: src/File.cc ${RM} $@.d $(COMPILE.cc) -O2 -MMD -MP -MF $@.d -o ${OBJECTDIR}/src/File.o src/File.cc +${OBJECTDIR}/src/Reader.o: src/Reader.cc + ${MKDIR} -p ${OBJECTDIR}/src + ${RM} $@.d + $(COMPILE.cc) -O2 -MMD -MP -MF $@.d -o ${OBJECTDIR}/src/Reader.o src/Reader.cc + ${OBJECTDIR}/src/ConfigReader.o: src/ConfigReader.cc ${MKDIR} -p ${OBJECTDIR}/src ${RM} $@.d diff --git a/libs/IO/nbproject/configurations.xml b/libs/IO/nbproject/configurations.xml index 7cc1823..ebf4175 100644 --- a/libs/IO/nbproject/configurations.xml +++ b/libs/IO/nbproject/configurations.xml @@ -4,12 +4,14 @@ + include/CommandReader.h include/ConfigReader.h include/File.h include/FileReader.h include/FileRepresentation.h include/FileSystem.h include/LineReader.h + include/Reader.h + src/CommandReader.cc src/ConfigReader.cc src/File.cc src/FileReader.cc src/FileRepresentation.cc src/FileSystem.cc src/LineReader.cc + src/Reader.cc Geronimo + * Project: libIO: classes for files, filesystem and input/output + * + * 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 + +cCommandReader::cCommandReader(const char *cmd) + : pid(-1) +{ + args.push_back(cmd); +} + +cCommandReader::~cCommandReader() +{ + Close(); +} + +void cCommandReader::AddCommandParameter(const char* Param) +{ + args.push_back(Param); +} + +void cCommandReader::Close(void) +{ + int status; + + if (pid != waitpid(pid, &status, 0)) { + esyslog("ERROR: failed to wait for child #%d - error #%d", pid, errno); + } + else { + isyslog("child exit status: %d", WEXITSTATUS(status)); + } + pid = fd = -1; +} + +bool cCommandReader::Open(void) +{ + enum { FDRead, FDWrite }; + int parent2Child[2]; + int child2Parent[2]; + + if (pipe(parent2Child)) { + esyslog("ERROR: failed to create parent2Child-pipe #%d", errno); + return false; + } + if (pipe(child2Parent)) { + esyslog("ERROR: failed to create child2Parent-pipe #%d", errno); + return false; + } + switch ((pid = fork())) { + case -1: + esyslog("fork failed"); + return false; + + case 0: { /* child */ + const char *cmdArgs[args.size() + 1]; + + for (size_t i=0; i < args.size(); ++i) { + cmdArgs[i] = args[i].c_str(); + } + cmdArgs[args.size()] = 0; + if (dup2(parent2Child[FDRead], STDIN_FILENO) < 0) { + esyslog("ERROR: failed to create parents stdin #%d", errno); + exit(-1); + } + if (dup2(child2Parent[FDWrite], STDOUT_FILENO) < 0) { + esyslog("ERROR: failed to create childs stdout #%d", errno); + exit(-1); + } + if (dup2(child2Parent[FDWrite], STDERR_FILENO) < 0) { + esyslog("ERROR: failed to create childs stderr #%d", errno); + exit(-1); + } + if (close(parent2Child[FDWrite])) { + esyslog("ERROR: failed to close parents write-end of the pipe #%d", errno); + exit(-1); + } + if (close(child2Parent[FDRead])) { + esyslog("ERROR: failed to close childs read-end of the pipe #%d", errno); + exit(-1); + } + execv(args[0].c_str(), (char *const*) cmdArgs); + + esyslog("should never be reached!"); + exit(-1); + } break; + + default: { /* parent */ + isyslog("child process #%d started ...", pid); + fd = child2Parent[FDRead]; + + return true; + } + } + return false; +} diff --git a/libs/IO/src/File.cc b/libs/IO/src/File.cc index 9b92b55..910d24d 100644 --- a/libs/IO/src/File.cc +++ b/libs/IO/src/File.cc @@ -1,25 +1,25 @@ /** * ======================== legal notice ====================== - * + * * File: File.cc * Created: 21. Juli 2012, 12 * Author: Geronimo * Project: libIO: classes for files, filesystem and input/output - * + * * 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 diff --git a/libs/IO/src/FileReader.cc b/libs/IO/src/FileReader.cc index 519c749..fd692ca 100644 --- a/libs/IO/src/FileReader.cc +++ b/libs/IO/src/FileReader.cc @@ -29,13 +29,18 @@ #include cFileReader::cFileReader(cFile *Input) - : fd(-1) - , file(Input) + : file(Input) +{ + Open(); +} + +bool cFileReader::Open() { if (file && file->Exists() && file->CanRead()) { fd = open(file->AbsolutePath(), O_RDONLY); if (fd < 0) esyslog("ERROR: could not open %s", file->AbsolutePath()); } + return fd > 0; } cFileReader::~cFileReader() @@ -45,7 +50,7 @@ cFileReader::~cFileReader() void cFileReader::Close() { - if (fd >= 0) { + if (fd > 0) { close(fd); fd = -1; } @@ -54,10 +59,3 @@ void cFileReader::Close() file = NULL; } } - -int cFileReader::Read(char* buf, int bufSize) -{ - if (fd >= 0) return read(fd, buf, bufSize); - esyslog("ERROR: not an open file! %s", file->AbsolutePath()); - return 0; -} diff --git a/libs/IO/src/FileRepresentation.cc b/libs/IO/src/FileRepresentation.cc index 3366157..f74d2c8 100644 --- a/libs/IO/src/FileRepresentation.cc +++ b/libs/IO/src/FileRepresentation.cc @@ -1,25 +1,25 @@ /** * ======================== legal notice ====================== - * + * * File: FileRepresentation.cc * Created: 21. Juli 2012, 12 * Author: Geronimo * Project: libIO: classes for files, filesystem and input/output - * + * * 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 diff --git a/libs/IO/src/LineReader.cc b/libs/IO/src/LineReader.cc index bf4bcc2..0d06628 100644 --- a/libs/IO/src/LineReader.cc +++ b/libs/IO/src/LineReader.cc @@ -23,14 +23,14 @@ * -------------------------------------------------------------- */ #include -#include +#include #include #include #include #include #include -cLineReader::cLineReader(cFileReader *FileReader) +cLineReader::cLineReader(cReader *FileReader) : reader(FileReader) , bytesRead(0) , off(0) diff --git a/libs/IO/src/Reader.cc b/libs/IO/src/Reader.cc new file mode 100644 index 0000000..cbead31 --- /dev/null +++ b/libs/IO/src/Reader.cc @@ -0,0 +1,44 @@ +/** + * ======================== legal notice ====================== + * + * File: Reader.cc + * Created: 30. Juli 2012, 08:11 + * Author: Geronimo + * Project: libIO: classes for files, filesystem and input/output + * + * 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 + +cReader::cReader() + : fd(-1) +{ +} + +cReader::~cReader() +{ +} + +int cReader::Read(char* buf, int bufSize) +{ + if (fd < 0) Open(); + if (fd > 0) return read(fd, buf, bufSize); + esyslog("ERROR: no valid filehandle to read from!"); + return 0; +} -- cgit v1.2.3