Wittyshare  0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
WsFsTreeUpdater.cpp
Go to the documentation of this file.
1 
14 #include "WsFsTreeUpdater.h"
15 #include "WsMonitor.h"
16 #include <Include/WsGlobalConfig.h>
17 
18 WsFsTreeUpdater::WsFsTreeUpdater(const string& p, unsigned int delay, bool m)
19  : m_rootPath(p),
20  m_isMonitor(m),
21  m_delay(delay),
22  m_monitor(0),
23  m_isLoaded(false)
24 {
25 }
26 
28 {
29  if (m_monitor != 0)
30  delete m_monitor;
31  LOG(DEBUG) << "WsFsTreeUpdater::~WsFsTreeUpdater() : Freeing memory completed";
32 }
33 
35 {
36  LOG(DEBUG) << "WsFsTreeUpdater::getLastTree() : Getting LastTree";
37  boost::mutex::scoped_lock lock(m_lMutex);
38  {
39  /* No trees yet, create one */
40  if ( m_fsTree.get() == 0)
41  update();
42  }
43  return m_fsTree;
44 }
45 
47 {
48  if (m_updateMutex.try_lock()) {
49  LOG(DEBUG) << "WsFileSystemTree::update() : Updating FsTree";
50  /* If there is already active trees, build the new one in a thread
51  * to avoid server block
52  */
53  if (m_fsTree.get()) {
54  new boost::thread(boost::bind(&WsFsTreeUpdater::threadUpdate, this));
55  m_updateMutex.unlock();
56  return ErrorCode::Success;
57  }
58  /* Create the new Tree */
60  if (newTree->start() == ErrorCode::Failure) {
61  m_updateMutex.unlock();
62  return ErrorCode::Failure;
63  }
64  /* Start the monitor is activated */
65  if (m_isMonitor && m_monitor == 0) {
66  LOG(INFO) << "WsFsTreeUpdater::update() : Launching monitor";
67  m_monitor = new WsMonitor(this, newTree->getMonitorPaths(), m_delay);
68  boost::thread* t = m_monitor->start();
69  }
70  /* No monitor activated, so we update the tree every x seconds */
71  if (!m_isMonitor && !m_isLoaded) {
72  boost::thread* updater = new boost::thread(boost::bind(&WsFsTreeUpdater::startRegularUpdate, this));
73  m_isLoaded = true;
74  }
75  /* Put it on the top of the stack */
76  m_fsTree = FileSystemTreePtr(newTree);
77  m_updateMutex.unlock();
78  return ErrorCode::Success;
79  } else
80  LOG(DEBUG) << "WsFsTreeUpdater::update() : Update already in progress";
81  return ErrorCode::Failure;
82 }
83 
85 {
86  return m_fsTree.get() == fs.get();
87 }
88 
90 {
91  /* Create new tree */
93  if (newTree->start() == ErrorCode::Failure)
94  return ErrorCode::Failure;
95  m_fsTree = FileSystemTreePtr(newTree);
96  return ErrorCode::Success;
97 }
98 
100 {
101  if (m_delay == 0) {
102  return ErrorCode::Success;
103  }
104  LOG(INFO) << "WsFsTreeUpdater::startRegularUpdate() : Monitoring is on. Will update the tree every " << m_delay << " s.";
105  /* Endless loop that will update the tree every m_delay seconds */
106  while (1) {
107  string rootpath = WsGlobalProperties::instance()->get("global", "root_path", ".");
108  string temp = rootpath + GlobalConfig::PathToLockFile;
109  temp = boost::filesystem::path(temp).string();
110  ifstream fconf(temp.c_str());
111  if (fconf.good()) {
112  continue;
113  }
114  sleep(m_delay);
115  this->update();
116  }
117 }
Represents the file structure on disk.
boost::mutex m_updateMutex
#define DEBUG
Definition: WsLogger.h:27
int start()
crawls the diretory and sorts the children. It is called by WsDirectoryCrawler::start() ...
bool m_isMonitor
Use Gamin or no.
const int Failure
Monitors the filesystem and call the WsFsTreeUpdater::update() method accordignly.
Definition: WsMonitor.h:31
int update()
update the fsTree
std::string m_rootPath
RootPath.
FileSystemTreePtr m_fsTree
FileSystemTreePtr getLastTree()
returns the last created tree
#define LOG
Definition: WsLogger.h:22
boost::mutex m_lMutex
int startRegularUpdate()
Starts the regular update of the tree.
#define INFO
Definition: WsLogger.h:32
boost::shared_ptr< WsFileSystemTree > FileSystemTreePtr
int threadUpdate()
Updates the tree in a thread.
WsAbstractMonitor * m_monitor
Pointer to the file monitoring class used.
std::string get(const std::string &section, const std::string &id, const std::string &def)
unsigned int m_delay
Delay after which the FileSystemTree will be updated if changed occured.
const std::string PathToLockFile
WsFsTreeUpdater(const std::string &p, unsigned int delay, bool m=false)
constrictor
static WsGlobalProperties * instance()
virtual boost::thread * start()=0
starts the monitoring process
Updates the tree and takes care of unconsistencies with the user.
Monitors the FileSystem for any modification.
bool isLastVersion(FileSystemTreePtr fs)
checks whether the fs is the last version
const int Success
std::vector< boost::filesystem::path > & getMonitorPaths()
returns the std::vector containing the boost::filesystem::path that should be monitored by the WsUpda...