libmoost
|
Asynchronously watches a remote file, then notifies a callback function when a change occurs. More...
#include <remote_watcher.hpp>
Public Types | |
enum | file_action { CREATED = 0, CHANGED = 1, DELETED = 2 } |
The actions that remote_watcher can monitor. More... | |
typedef boost::function< void(file_action action, const std::string &url)> | callback_t |
Public Member Functions | |
remote_watcher (int sleep_ms=10000, int timeout_ms=2000) | |
~remote_watcher () | |
Destructor. | |
void | insert (const std::string &url, const callback_t &callback) |
Adds path to the list of monitored paths. | |
void | clear () |
clear the whole list | |
void | erase (const std::string &url) |
Removes path from the list of monitored paths. | |
void | start () |
Starts the asynchronous monitor thread. | |
void | stop () |
Stops the asynchronous monitor thread. | |
Static Public Member Functions | |
static bool | connect (boost::asio::ip::tcp::iostream &s, std::string url, int timeout_ms, bool skip_header=false) |
Connects to the given url. Will put the connection in the passed stream. | |
Private Member Functions | |
std::time_t | get_time () |
std::string | last_write_time (const std::string &url) |
we cannot assume that if a file exists, it will exist in the following moment that we check its file size | |
void | run () |
Entry point for the asynchronous monitor thread. | |
Private Attributes | |
std::map< std::string, callback_t > | m_file_callback |
The map that associates watched files to the callbacks that should be launched. | |
std::map< std::string, std::string > | m_file_modified |
The map that associates watched files with their last known modification times. | |
boost::mutex | m_file_mutex |
Mutex to make modifications to remote_watcher::m_file_callback and remote_watcher::m_file_modified thread-safe. | |
boost::shared_ptr< boost::thread > | m_pthread |
Shared pointer to the thread that is constantly checking the files for modifications. | |
bool | m_run |
Whether the watcher thread is running. | |
boost::mutex | m_run_mutex |
Mutex that must be locked when we are changing remote_watcher::m_run. | |
boost::condition | m_run_cond |
Condition to notify threads blocking on the remote_watcher watcher thread. | |
int | m_sleep_ms |
int | m_timeout_ms |
Asynchronously watches a remote file, then notifies a callback function when a change occurs.
After a remote_watcher is instantiated, you must call remote_watcher::start() to start up the asynchronous thread.
Definition at line 61 of file remote_watcher.hpp.
typedef boost::function<void(file_action action, const std::string & url)> moost::io::remote_watcher::callback_t |
Definition at line 73 of file remote_watcher.hpp.
The actions that remote_watcher can monitor.
Definition at line 66 of file remote_watcher.hpp.
moost::io::remote_watcher::remote_watcher | ( | int | sleep_ms = 10000 , |
int | timeout_ms = 2000 |
||
) | [inline] |
Constructs an empty remote_watcher that is not running and not watching anything
sleep_ms,: | the number of milliseconds to sleep in between checking files |
Definition at line 186 of file remote_watcher.hpp.
moost::io::remote_watcher::~remote_watcher | ( | ) | [inline] |
Destructor.
This method takes care of properly setting remote_watcher::m_run
to false
using the remote_watcher::m_run_mutex mutex, and it also waits until the watcher thread exits.
Definition at line 197 of file remote_watcher.hpp.
void moost::io::remote_watcher::clear | ( | ) | [inline] |
clear the whole list
Definition at line 221 of file remote_watcher.hpp.
static bool moost::io::remote_watcher::connect | ( | boost::asio::ip::tcp::iostream & | s, |
std::string | url, | ||
int | timeout_ms, | ||
bool | skip_header = false |
||
) | [inline, static] |
Connects to the given url. Will put the connection in the passed stream.
Definition at line 278 of file remote_watcher.hpp.
void moost::io::remote_watcher::erase | ( | const std::string & | url | ) | [inline] |
Removes path from the list of monitored paths.
path | the path to stop monitoring |
Definition at line 233 of file remote_watcher.hpp.
std::time_t moost::io::remote_watcher::get_time | ( | ) | [inline, private] |
Definition at line 116 of file remote_watcher.hpp.
void moost::io::remote_watcher::insert | ( | const std::string & | url, |
const callback_t & | callback | ||
) | [inline] |
Adds path to the list of monitored paths.
url | the url to monitor |
callback | the callback to notify if the file at url changes |
Definition at line 206 of file remote_watcher.hpp.
std::string moost::io::remote_watcher::last_write_time | ( | const std::string & | url | ) | [inline, private] |
we cannot assume that if a file exists, it will exist in the following moment that we check its file size
Definition at line 124 of file remote_watcher.hpp.
void moost::io::remote_watcher::run | ( | ) | [inline, private] |
Entry point for the asynchronous monitor thread.
Definition at line 353 of file remote_watcher.hpp.
void moost::io::remote_watcher::start | ( | ) | [inline] |
Starts the asynchronous monitor thread.
std::invalid_argument | if the thread is already started start will NOT call the callback function straight away!!! |
Definition at line 245 of file remote_watcher.hpp.
void moost::io::remote_watcher::stop | ( | ) | [inline] |
Stops the asynchronous monitor thread.
std::invalid_argument | if the thread is already stopped |
Definition at line 258 of file remote_watcher.hpp.
std::map<std::string, callback_t > moost::io::remote_watcher::m_file_callback [private] |
The map that associates watched files to the callbacks that should be launched.
Definition at line 78 of file remote_watcher.hpp.
std::map<std::string, std::string > moost::io::remote_watcher::m_file_modified [private] |
The map that associates watched files with their last known modification times.
We actually track the last two modification times. Ideally, we want to send a notification only when the file was modified and the modifying process already closed the file, therefore we fire a CHANGED
event only if the previous previous modification time is different from the previous modification time but the previous modification time equals the modification time we read from the file. Otherwise we just "shift" the pair (drop the previous previous modification, move the previous to the first position that is empty and push the file's last modification obtained from the filesystem to the second element of the pair).
Definition at line 90 of file remote_watcher.hpp.
boost::mutex moost::io::remote_watcher::m_file_mutex [private] |
Mutex to make modifications to remote_watcher::m_file_callback and remote_watcher::m_file_modified thread-safe.
Definition at line 94 of file remote_watcher.hpp.
boost::shared_ptr< boost::thread> moost::io::remote_watcher::m_pthread [private] |
Shared pointer to the thread that is constantly checking the files for modifications.
Definition at line 97 of file remote_watcher.hpp.
bool moost::io::remote_watcher::m_run [private] |
Whether the watcher thread is running.
Definition at line 103 of file remote_watcher.hpp.
boost::condition moost::io::remote_watcher::m_run_cond [private] |
Condition to notify threads blocking on the remote_watcher watcher thread.
Definition at line 111 of file remote_watcher.hpp.
boost::mutex moost::io::remote_watcher::m_run_mutex [private] |
Mutex that must be locked when we are changing remote_watcher::m_run.
This is because remote_watcher::m_run can be modified from the watcher thread itself and also from the thread that originally constructed this remote_watcher.
Definition at line 109 of file remote_watcher.hpp.
int moost::io::remote_watcher::m_sleep_ms [private] |
Definition at line 113 of file remote_watcher.hpp.
int moost::io::remote_watcher::m_timeout_ms [private] |
Definition at line 114 of file remote_watcher.hpp.