I'm writing a monitoring-file program based on the source code: https://github.com/kvikas/file-monitor-service/blob/master/
My program uses boost::asio::stream_descriptor::async_read_some() for asynchronous reading from the inotify descriptor http://linux.die.net/man/7/inotify
My code is as following:
Constructor:
void init(){
int fd = inotify_init1(IN_NONBLOCK);
int wd = inotify_add_watch(fd_, "./test.txt", IN_ALL_EVENTS);
stream_.reset(new boost::asio::posix::stream_descriptor(io_service_, fd_)));
}
The asynchronous reading:
template<typename Monitor_Handler>
void async_monitor(Monitor_Handler handler) {
stream_->async_read_some(boost::asio::buffer(buffer_),
boost::bind(&monitor::handle_monitor<Monitor_Handler>,
shared_from_this(), boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred, handler));
}
The handler:
template<typename Monitor_Handler>
void handle_monitor(const boost::system::error_code &ec,
std::size_t bytes_transferred, Monitor_Handler handler) {
//process buffer
async_monitor(handler);
}
The error is that at first handle_monitor is invoked several times (multiple events such as MODIFY, ACCESS, OPEN...) for the first change in the monitored file. After that async_read_some method is called again, but I got no signal anymore (the handle_monitor is not called anymore)
However, when I tried to reset the inotify description, and readd the monitored files again ==> It worked, the handle_monitor is called for new changes in such monitored files.
Modified Code:
template<typename Monitor_Handler>
void handle_monitor(const boost::system::error_code &ec,
std::size_t bytes_transferred, Monitor_Handler handler) {
//process buffer
async_monitor(handler);
init();//for resetting the inotify desciptor
}
Could you guys help me explain this???? I'm dying for your answer.....