00001
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef __FileEventWatcher_h
00030 #define __FileEventWatcher_h
00031
00032 #if HAVE_CONFIG_H
00033 #include "config.h"
00034 #endif
00035
00036 #include <cstdlib>
00037 #include <string>
00038 #include <list>
00039 #include <vector>
00040 #include <boost/shared_ptr.hpp>
00041 #include <sys/poll.h>
00042 #include <map>
00043 #include "DynamicBuffer.hpp"
00044 #include <boost/archive/text_oarchive.hpp>
00045 #include <boost/archive/text_iarchive.hpp>
00046
00048
00050
00051 namespace H {
00052
00054
00056
00065 typedef enum {
00066 WATCH_INVALID = -1,
00067 WATCH_IN,
00068 WATCH_OUT,
00069 WATCH_INOUT
00070 } FileWatchType;
00071
00079 typedef enum {
00080 WATCH_POLL,
00081 WATCH_INOTIFY
00082 } FileDeviceType;
00083
00085
00087
00092 class DeviceInfo {
00093 public:
00094
00095 int DeviceIDBusType;
00096 int DeviceIDProduct;
00097 int DeviceIDVendor;
00098 int DeviceIDVersion;
00099 std::string DeviceName;
00100 int FileDescriptor;
00101 std::string FileName;
00102
00103
00104 DeviceInfo();
00105 DeviceInfo(const DeviceInfo & DeviceInformation);
00106 DeviceInfo(std::string DeviceName, std::string FileName, int DeviceIDBusType, int DeviceIDVendor, int DeviceIDProduct, int DeviceIDVersion, int FileDescriptor);
00107 virtual ~DeviceInfo();
00108
00109 private:
00110
00111 friend class boost::serialization::access;
00112 template<class Archive>
00113 void serialize(Archive & ar, const unsigned int version) {
00114 ar & DeviceIDBusType;
00115 ar & DeviceIDProduct;
00116 ar & DeviceIDVendor;
00117 ar & DeviceIDVersion;
00118 ar & DeviceName;
00119 ar & FileDescriptor;
00120 ar & FileName;
00121 }
00122 };
00123
00125
00127
00132 class FileWatchee : public DeviceInfo {
00133 public:
00134
00135 FileDeviceType DeviceType;
00136 short Events;
00137 int fd;
00138 FileWatchType WatchType;
00139 int wd;
00140
00141
00142 FileWatchee();
00143 FileWatchee(std::string fileName, FileWatchType watchType, short events, int fileDescriptor, int watchDescriptor, std::string deviceName, int deviceIDBusType, int deviceIDVendor, int deviceIDProduct, int deviceIDVersion);
00144 virtual ~FileWatchee();
00145 };
00146
00148
00150
00165 class FileEventWatcher {
00166 public:
00167
00168 boost::shared_ptr<FileWatchee> addFileToWatch(std::string FileName, FileWatchType WatchType, std::string DefaultDeviceName = "Unknown");
00169 boost::shared_ptr<FileWatchee> addUnixSocketToWatch(std::string FileName, std::string DeviceName);
00170 boost::shared_ptr<FileWatchee> getWatcheeByFileDescriptor(int fd);
00171 boost::shared_ptr<FileWatchee> getWatcheeByPath(std::string Path);
00172 boost::shared_ptr<FileWatchee> getWatcheeByWatchDescriptor(int wd);
00173 virtual void onFileEventCreate(boost::shared_ptr<FileWatchee> pWatchee, std::string FullPath, std::string FileName);
00174 virtual void onFileEventDelete(boost::shared_ptr<FileWatchee> pWatchee, std::string FullPath, std::string FileName);
00175 virtual void onFileEventDisconnect(boost::shared_ptr<FileWatchee> pWatchee);
00176 virtual void onFileEventRead(boost::shared_ptr<FileWatchee> pWatchee, DynamicBuffer<char> const & ReadBuffer);
00177 virtual void onFileEventRegister(boost::shared_ptr<FileWatchee> pWatchee);
00178 virtual void onFileEventWatchBegin();
00179 virtual void onFileEventWatchEnd();
00180 void removeWatchee(boost::shared_ptr<FileWatchee> pWatchee);
00181 void shutdown();
00182 void watchForFileEvents();
00183
00184
00185 FileEventWatcher();
00186 virtual ~FileEventWatcher();
00187
00188 private:
00189
00190 void buildPollFDArrayFromWatchees();
00191 void buildPollFDArrayFunctor(std::pair< int, boost::shared_ptr<FileWatchee> > WatcheePair);
00192 FileWatchType getType(int Index);
00193 void handleEventsOnFile(struct pollfd & item);
00194 void readFromFile(int fd, DynamicBuffer<char> & Buffer);
00195 void removeAllWatchDescriptors();
00196 void removeWatchDescriptor(int wd);
00197
00198
00199 int mInotifyFD;
00200 std::vector<int> mInotifyWDs;
00201 std::vector<struct pollfd> mPollFDs;
00202 bool mPolling;
00203 std::map< int, boost::shared_ptr<FileWatchee> > mWatchees;
00204 };
00205
00207
00209
00211 class DeviceDisconnectException : public std::exception {
00212 };
00213
00215 class WatcheeNotFoundException : public std::exception {
00216 };
00217
00219
00220 }
00221
00222 #endif // __FileEventWatcher_h