Wittyshare  0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
WsMonitor.cpp
Go to the documentation of this file.
1 
14 #include "WsFsTreeUpdater.h"
15 #include "WsMonitor.h"
16 #include <Include/WsGlobalConfig.h>
17 
18 WsMonitor::WsMonitor(WsFsTreeUpdater* u, vector<path> v, unsigned int delay):
19  m_updater(u),
20  m_paths(v),
21  m_updated(false),
22  m_delay(delay)
23 {
24 }
25 
27 {
28  LOG(DEBUG) << "WsMonitor::~WsMonitor() : Deleting this";
29  if (FAMCancelMonitor(m_fc, m_frp) < 0)
30  LOG(ERROR) << "WsMonitor::~WsMonitor() : Error while stopping Fam monitor ";
31  else
32  LOG(INFO) << "WsMonitor::~WsMonitor() : Fam stopped successfully";
33  if (FAMCancelMonitor(m_fc2, m_frp2) < 0)
34  LOG(ERROR) << "WsMonitor::~WsMonitor() : Error while stopping Fam monitor ";
35  else
36  LOG(INFO) << "WsMonitor::~WsMonitor() : Fam stopped successfully";
37  delete m_frp;
38  m_frp = 0;
39  delete m_fc;
40  m_fc = 0;
41  delete m_frp2;
42  m_frp2 = 0;
43  delete m_fc2;
44  m_fc2 = 0;
45 }
46 
47 const char* WsMonitor::eventName(const int& code)
48 {
49  static const char* famevent[] = {
50  "",
51  "FAMChanged",
52  "FAMDeleted",
53  "FAMStartExecuting",
54  "FAMStopExecuting",
55  "FAMCreated",
56  "FAMMoved",
57  "FAMAcknowledge",
58  "FAMExists",
59  "FAMEndExist"
60  };
61  static char unknown_event[10];
62  /* Unknown code */
63  if (code < FAMChanged || code > FAMEndExist) {
64  LOG(ERROR) << "WsMonitor::WsMonitor() : Unknown event code returned by Gamin " << code << endl;
65  return "unknown";
66  }
67  /* Return code */
68  return famevent[code];
69 }
70 
71 boost::thread* WsMonitor::start()
72 {
73  /* Launch the threads that will monitor the files and the thread that will check the m_updater var */
74  boost::thread* updater = new boost::thread(boost::bind(&WsMonitor::startCallBack, this));
75  boost::thread* updaterConf = new boost::thread(boost::bind(&WsMonitor::startCallBackConfOnly, this));
76  boost::thread* checker = new boost::thread(boost::bind(&WsMonitor::startChecker, this));
77  return updater;
78 }
79 
81 {
82  LOG(INFO) << "WsMonitor::startChecker() : Will update FsTree every " << m_delay << "s if changes occurs";
83  while (1) {
84  LOG(DEBUG) << "WsMonitor::startChecker() : Checking updated status : " << m_updated;
85  /* If changed, update the tree */
86  if (m_updated) {
87  LOG(INFO) << "WsMonitor::startChecker() : Updating FsTree";
88  m_updated = false;
89  m_updater->update();
90  }
91  sleep(m_delay);
92  }
93 }
94 
96 {
97  int i, nmon, rc, fam_fd;
98  FAMEvent fe;
99  fd_set readfds;
100  string config;
101  /* Allocate storage for requests */
102  m_frp = (FAMRequest*)malloc(m_paths.size() * sizeof * m_frp);
103  if (!m_frp) {
104  LOG(ERROR) << "WsMonitor::startCallBack() : Malloc failed for Fam";
105  return ErrorCode::Failure;
106  }
107  m_fc = (FAMConnection*)malloc(sizeof(m_fc));
108  /* Open fam connection */
109  if ((FAMOpen(m_fc)) < 0) {
110  LOG(ERROR) << "WsMonitor::startCallBack() : Fam open failed";
111  return ErrorCode::Failure;
112  }
113  /* Request monitoring for each program argument */
114  for (nmon = 0, i = 0; i < m_paths.size(); i++) {
115  rc = FAMMonitorDirectory(m_fc, m_paths[i].string().c_str(), m_frp + i, 0);
116  if (rc < 0) {
117  LOG(ERROR) << "WsMonitor::startCallBack() : FAMMonitor failed" << endl;
118  continue;
119  }
120  nmon++;
121  }
122  /* If nmon == 0, nothing to monitor */
123  if (!nmon) {
124  LOG(ERROR) << "WsMonitor::startCallBack() : Fam nothing to monitor." << endl;
125  return ErrorCode::Failure;
126  }
127  /* Initialize select data structure */
128  fam_fd = FAMCONNECTION_GETFD(m_fc);
129  FD_ZERO(&readfds);
130  FD_SET(fam_fd, &readfds);
131  /* Loop forever. */
132  while (1) {
133  if (select(fam_fd + 1, &readfds,
134  0, 0, 0) < 0) {
135  LOG(ERROR) << "WsMonitor::startCallBack() : Select failed" << endl;
136  return ErrorCode::Failure;
137  }
138  if (FD_ISSET(fam_fd, &readfds)) {
139  if (FAMNextEvent(m_fc, &fe) < 0) {
140  LOG(ERROR) << "WsMonitor::startCallBack() : FAMNextEvent Failed" << endl;
141  exit(1);
142  }
143  signal(fe.filename, fe.code);
144  }
145  }
146 }
147 
149 {
150  int i, nmon, rc, fam_fd;
151  FAMEvent fe;
152  fd_set readfds;
153  string config;
154  /* Allocate storage for requests */
155  m_frp2 = (FAMRequest*)malloc(m_paths.size() * sizeof * m_frp2);
156  if (!m_frp2) {
157  LOG(ERROR) << "WsMonitor::startCallBackConfOnly() : Malloc failed for Fam";
158  return ErrorCode::Failure;
159  }
160  m_fc2 = (FAMConnection*)malloc(sizeof(m_fc2));
161  /* Open fam connection */
162  if ((FAMOpen(m_fc2)) < 0) {
163  LOG(ERROR) << "WsMonitor::startCallBackConfOnly() Fam open failed";
164  return ErrorCode::Failure;
165  }
166  /* Request monitoring for each program argument */
167  for (nmon = 0, i = 0; i < m_paths.size(); i++) {
168  config = m_paths[i].string() + GlobalConfig::PathToNodeProperties;
169  ifstream fconf(config.c_str());
170  if (fconf.good()) {
171  rc = FAMMonitorDirectory(m_fc2, config.c_str(), m_frp2 + i, 0);
172  if (rc < 0) {
173  LOG(ERROR) << "WsMonitor::startCallBackConfOnly() : FAMMonitor failed" << endl;
174  continue;
175  }
176  nmon++;
177  }
178  nmon++;
179  }
180  if (!nmon) {
181  LOG(ERROR) << "WsMonitor::startCallBackConfOnly() : Fam nothing to monitor." << endl;
182  return ErrorCode::Failure;
183  }
184  /* Initialize select data structure */
185  fam_fd = FAMCONNECTION_GETFD(m_fc2);
186  FD_ZERO(&readfds);
187  FD_SET(fam_fd, &readfds);
188  /* Loop forever. */
189  while (1) {
190  if (select(fam_fd + 1, &readfds,
191  0, 0, 0) < 0) {
192  LOG(ERROR) << "WsMonitor::startCallBackConfOnly() : Select failed" << endl;
193  return ErrorCode::Failure;
194  }
195  if (FD_ISSET(fam_fd, &readfds)) {
196  if (FAMNextEvent(m_fc2, &fe) < 0) {
197  LOG(ERROR) << "WsMonitor::startCallBackConfOnly() : FAMNextEvent Failed" << endl;
198  exit(1);
199  }
200  signalConf(fe.filename, fe.code);
201  }
202  }
203 }
204 
205 int WsMonitor::signal(const string& filename, const int& code)
206 {
207  string rootpath = WsGlobalProperties::instance()->get("global", "root_path", ".");
208  string temp = rootpath + GlobalConfig::PathToLockFile;
209  temp = boost::filesystem::path(temp).string();
210  ifstream fconf(temp.c_str());
211  if (fconf.good()) {
212  LOG(DEBUG) << "WsMonitor::signal() : Lock File is present. Not updating";
213  return ErrorCode::Success;
214  }
215  switch (code) {
216  case FAMDeleted:
217  LOG(DEBUG) << "WsMonitor::signal() Change occured on " << filename << " - Code FamDeleted";
218  m_updated = true;
219  break;
220  case FAMCreated:
221  LOG(DEBUG) << "WsMonitor::signal() : Change occured on " << filename << " - Code FamCreated";
222  m_updated = true;
223  break;
224  }
225  return ErrorCode::Success;
226 }
227 
228 int WsMonitor::signalConf(const string& filename, const int& code)
229 {
230  string rootpath = WsGlobalProperties::instance()->get("global", "root_path", ".");
231  string temp = rootpath + GlobalConfig::PathToLockFile;
232  temp = boost::filesystem::path(temp).string();
233  ifstream fconf(temp.c_str());
234  if (fconf.good()) {
235  LOG(DEBUG) << "WsMonitor::signalConf() : Lock File is present. Not updating";
236  return ErrorCode::Success;
237  }
238  switch (code) {
239  case FAMDeleted:
240  LOG(DEBUG) << "WsMonitor::signalConf() : Change occured on conf dir" << filename << " - Code FamDeleted";
241  m_updated = true;
242  break;
243  case FAMCreated:
244  LOG(DEBUG) << "WsMonitor::signalConf() : Change occured on conf dir" << filename << " - Code FamCreated";
245  m_updated = true;
246  break;
247  case FAMChanged:
248  LOG(DEBUG) << "WsMonitor::signalConf() : Change occured on conf dir " << filename << " - Code FamChanged";
249  m_updated = true;
250  break;
251  }
252 }
253 
int startCallBack()
Starts monitoring the Files of the filesystemtree Should be started in a separated thread...
Definition: WsMonitor.cpp:95
FAMConnection * m_fc
Definition: WsMonitor.h:136
#define DEBUG
Definition: WsLogger.h:27
const int Failure
FAMRequest * m_frp
Definition: WsMonitor.h:134
unsigned int m_delay
Definition: WsMonitor.h:132
int update()
update the fsTree
const char * eventName(const int &code)
Matches an event code with it's name.
Definition: WsMonitor.cpp:47
handles the update of the FileSystemTree
const std::string PathToNodeProperties
std::vector< path > m_paths
Definition: WsMonitor.h:120
#define LOG
Definition: WsLogger.h:22
WsFsTreeUpdater * m_updater
pointer to the WsFsTreeUpdater class
Definition: WsMonitor.h:125
WsMonitor(WsFsTreeUpdater *u, std::vector< path > v, unsigned int delay)
Constructor.
Definition: WsMonitor.cpp:18
#define INFO
Definition: WsLogger.h:32
bool m_updated
Changed occured ?
Definition: WsMonitor.h:131
int signal(const std::string &filename, const int &code)
Called when a change occurs on a file monitored.
Definition: WsMonitor.cpp:205
FAMConnection * m_fc2
Definition: WsMonitor.h:137
std::string get(const std::string &section, const std::string &id, const std::string &def)
int signalConf(const std::string &filename, const int &code)
Called when a change occurs on a conf file monitored.
Definition: WsMonitor.cpp:228
~WsMonitor()
Destructor.
Definition: WsMonitor.cpp:26
const std::string PathToLockFile
static WsGlobalProperties * instance()
int startCallBackConfOnly()
Starts monitoring the config Files of the filesystemtree Should be started in a separated thread...
Definition: WsMonitor.cpp:148
Updates the tree and takes care of unconsistencies with the user.
Monitors the FileSystem for any modification.
int startChecker()
Starts loop that will monitor the m_updated variable every m_delay and update the tree if changed occ...
Definition: WsMonitor.cpp:80
const int Success
boost::thread * start()
Starts the monitoring process.
Definition: WsMonitor.cpp:71
#define ERROR
Definition: WsLogger.h:42
FAMRequest * m_frp2
Definition: WsMonitor.h:135