FileSystemCommPort.h
Go to the documentation of this file.00001
00002 #ifndef INCLUDED_FileSystemCommPort_h_
00003 #define INCLUDED_FileSystemCommPort_h_
00004
00005 #include "local/CommPort.h"
00006 #include "Shared/plist.h"
00007 #include <Wireless/netstream.h>
00008 #include <ios>
00009
00010
00011
00012 class FileSystemCommPort : public CommPort, public virtual plist::PrimitiveListener {
00013 public:
00014
00015 explicit FileSystemCommPort(const std::string& name)
00016 : CommPort(autoRegisterFileSystemCommPort,name),
00017 path(), mode(std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::binary),
00018 rbuf(), wbuf(), curloc(), curmode(), openedCnt(0)
00019 {
00020 addEntry("Path",path,"Path of file system object being accessed");
00021 addEntry("Mode",mode,"Mode bitmask to pass to the open() call, defaults to 'w+b': in|out|trunc|binary (see std::ios_base::openmode)");
00022 }
00023
00024
00025 virtual ~FileSystemCommPort() {
00026 lock.lock();
00027 if(openedCnt>0)
00028 connectionError("File descriptor still open in FileSystemCommPort destructor",true,NULL,false);
00029 }
00030
00031 virtual std::string getClassName() const { return autoRegisterFileSystemCommPort; }
00032
00033 virtual basic_netbuf<std::ios::char_type>& getReadStreambuf() { return rbuf; }
00034 virtual basic_netbuf<std::ios::char_type>& getWriteStreambuf() { return wbuf; }
00035 virtual bool isReadable() { return rbuf.is_open(); }
00036 virtual bool isWriteable() { return wbuf.is_open(); }
00037
00038
00039 virtual bool open();
00040
00041
00042 virtual bool close();
00043
00044
00045 virtual void plistValueChanged(const plist::PrimitiveBase& pl);
00046
00047 plist::Primitive<std::string> path;
00048 plist::Primitive<int> mode;
00049
00050 protected:
00051 explicit FileSystemCommPort(const std::string& classname, const std::string& instancename)
00052 : CommPort(classname,instancename),
00053 path(), mode(std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::binary),
00054 rbuf(), wbuf(), curloc(), curmode(), openedCnt(0)
00055 {
00056 addEntry("Path",path,"Path of file system object being accessed");
00057 addEntry("Mode",mode,"Mode bitmask to pass to the open() call, defaults to 'w+b': in|out|trunc|binary (see std::ios_base::openmode)");
00058 }
00059
00060
00061
00062
00063 virtual void connectionError(const std::string& msg, bool fatal, const char* sysmsg, bool useClassName=true) {
00064 if(useClassName && getClassName()!=instanceName)
00065 std::cerr << getClassName() << " \"" << instanceName << "\": ";
00066 else
00067 std::cerr << instanceName << ": ";
00068 std::cerr << msg;
00069 if(sysmsg!=NULL && sysmsg[0]!='\0')
00070 std::cerr << ": " << sysmsg;
00071 std::cerr << std::endl;
00072 if(fatal && (rbuf.is_open() || wbuf.is_open())) {
00073 openedCnt=1;
00074 close();
00075 }
00076 }
00077
00078 basic_netbuf<std::ios::char_type> rbuf;
00079 basic_netbuf<std::ios::char_type> wbuf;
00080 std::string curloc;
00081 std::ios_base::openmode curmode;
00082 unsigned int openedCnt;
00083
00084
00085 static const std::string autoRegisterFileSystemCommPort;
00086 };
00087
00088
00089
00090
00091
00092
00093 #endif