NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
watch_files.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, United States Government, as represented by the
2  * Administrator of the National Aeronautics and Space Administration.
3  *
4  * All rights reserved.
5  *
6  * The Astrobee platform is licensed under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations
16  * under the License.
17  */
18 
19 // Code taken from www.cs.cmu.edu/~coral/projects/localization/source.html
20 // Brian Coltin is in the team and said this code could be used for the project
21 
22 #ifndef CONFIG_READER_WATCH_FILES_H_
23 #define CONFIG_READER_WATCH_FILES_H_
24 
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <sys/inotify.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <unistd.h>
32 
33 #include <fstream>
34 #include <string>
35 #include <vector>
36 
37 namespace config_reader {
38 
39 class ActiveFile{
40  public:
41  std::string name;
42  protected:
43  time_t mod_time;
44  protected:
45  time_t getFileModTime();
46  public:
48  {mod_time = 0;}
49 
50  void init(const char *_name)
51  {name = _name; mod_time = 0;}
52  const char *operator()()
53  {return(name.c_str());}
54 
55  void invalidate()
56  {mod_time = 0;}
57  bool isModified()
58  {return(mod_time != getFileModTime());}
59  void markAsRead()
61 };
62 
63 class WatchFiles{
64  protected:
65  static const uint32_t ModEvents = IN_CLOSE_WRITE|IN_DELETE_SELF;
66 
67  public:
68  class Watch{
69  protected:
71  int wd; // watch descriptor
72  public:
74  {parent = NULL; wd = -1;}
75  Watch(const Watch &w)
76  {parent = w.parent; wd = w.wd;}
77 
78  bool valid()
79  {return(parent != NULL && wd >= 0);}
80  bool watch(WatchFiles *_parent, const char *filename)
81  {return(_parent && _parent->addWatch(this, filename));}
82  bool rewatch(const char *filename)
83  {return(parent && parent->addWatch(this, filename));}
84  bool remove()
85  {return(parent && parent->removeWatch(this));}
86 
88  {return(parent && (parent->calcEventMask(this) & ModEvents));}
89 
90  friend class WatchFiles;
91  };
92 
93  protected:
94  std::vector<inotify_event> events;
97 
98  public:
100  {inotify_fd = -1; num_watches = 0;}
102  {reset();}
103 
104  bool init();
105  void reset();
106  bool isInited()
107  {return(inotify_fd >= 0);}
108 
109  bool addWatch(Watch *w, const char *filename);
110  bool removeWatch(Watch *w);
111  uint32_t calcEventMask(Watch *w);
112 
113  int getEvents();
115  {return(events.size());}
116  void clearEvents()
117  {events.clear();}
118 };
119 
120 time_t FileModTime(const char *filename);
121 void SetNonBlocking(int fd);
122 
123 } // namespace config_reader
124 
125 #endif // CONFIG_READER_WATCH_FILES_H_
config_reader
Definition: config_reader.h:46
config_reader::WatchFiles::addWatch
bool addWatch(Watch *w, const char *filename)
Definition: watch_files.cc:75
config_reader::ActiveFile::mod_time
time_t mod_time
Definition: watch_files.h:43
config_reader::SetNonBlocking
void SetNonBlocking(int fd)
Definition: watch_files.cc:42
config_reader::WatchFiles::~WatchFiles
~WatchFiles()
Definition: watch_files.h:101
config_reader::WatchFiles::calcEventMask
uint32_t calcEventMask(Watch *w)
Definition: watch_files.cc:102
config_reader::WatchFiles::Watch::parent
WatchFiles * parent
Definition: watch_files.h:70
config_reader::ActiveFile::getFileModTime
time_t getFileModTime()
Definition: watch_files.cc:50
config_reader::WatchFiles::Watch::rewatch
bool rewatch(const char *filename)
Definition: watch_files.h:82
config_reader::ActiveFile::name
std::string name
Definition: watch_files.h:41
config_reader::WatchFiles::WatchFiles
WatchFiles()
Definition: watch_files.h:99
config_reader::ActiveFile::operator()
const char * operator()()
Definition: watch_files.h:52
config_reader::WatchFiles
Definition: watch_files.h:63
config_reader::WatchFiles::inotify_fd
int inotify_fd
Definition: watch_files.h:95
config_reader::ActiveFile::init
void init(const char *_name)
Definition: watch_files.h:50
config_reader::WatchFiles::Watch::valid
bool valid()
Definition: watch_files.h:78
config_reader::WatchFiles::reset
void reset()
Definition: watch_files.cc:66
config_reader::FileModTime
time_t FileModTime(const char *filename)
Definition: watch_files.cc:35
config_reader::WatchFiles::ModEvents
static const uint32_t ModEvents
Definition: watch_files.h:65
config_reader::WatchFiles::clearEvents
void clearEvents()
Definition: watch_files.h:116
config_reader::WatchFiles::Watch::Watch
Watch(const Watch &w)
Definition: watch_files.h:75
config_reader::WatchFiles::num_watches
int num_watches
Definition: watch_files.h:96
config_reader::WatchFiles::Watch::watch
bool watch(WatchFiles *_parent, const char *filename)
Definition: watch_files.h:80
config_reader::WatchFiles::Watch
Definition: watch_files.h:68
config_reader::WatchFiles::init
bool init()
Definition: watch_files.cc:56
config_reader::WatchFiles::Watch::isFileModified
bool isFileModified()
Definition: watch_files.h:87
config_reader::WatchFiles::getEvents
int getEvents()
Definition: watch_files.cc:110
config_reader::WatchFiles::isInited
bool isInited()
Definition: watch_files.h:106
config_reader::WatchFiles::getNumEvents
int getNumEvents()
Definition: watch_files.h:114
config_reader::WatchFiles::events
std::vector< inotify_event > events
Definition: watch_files.h:94
config_reader::ActiveFile
Definition: watch_files.h:39
config_reader::WatchFiles::Watch::Watch
Watch()
Definition: watch_files.h:73
config_reader::ActiveFile::ActiveFile
ActiveFile()
Definition: watch_files.h:47
config_reader::ActiveFile::markAsRead
void markAsRead()
Definition: watch_files.h:59
config_reader::WatchFiles::removeWatch
bool removeWatch(Watch *w)
Definition: watch_files.cc:91
config_reader::WatchFiles::Watch::wd
int wd
Definition: watch_files.h:71
config_reader::ActiveFile::isModified
bool isModified()
Definition: watch_files.h:57
config_reader::WatchFiles::Watch::remove
bool remove()
Definition: watch_files.h:84
config_reader::ActiveFile::invalidate
void invalidate()
Definition: watch_files.h:55