Stefan Schuermans commited on 2011-12-11 00:04:04
Showing 3 changed files, with 166 additions and 146 deletions.
... | ... |
@@ -73,152 +73,6 @@ protected: |
73 | 73 |
ModuleList m_moduleList; ///< module list |
74 | 74 |
}; // class ModuleMgr |
75 | 75 |
|
76 |
-/* ############# |
|
77 |
- # ModuleMgr # |
|
78 |
- ############# */ |
|
79 |
- |
|
80 |
-/** |
|
81 |
- * @brief constructor |
|
82 |
- * @param[in] callMgr callback manager |
|
83 |
- * @param[in] streamMgr stream manager |
|
84 |
- * @param[in] dirBase base directory |
|
85 |
- */ |
|
86 |
-template<typename MODULE> |
|
87 |
-ModuleMgr<MODULE>::ModuleMgr(CallMgr &callMgr, StreamMgr &streamMgr, |
|
88 |
- const Directory &dirBase): |
|
89 |
- m_callMgr(callMgr), |
|
90 |
- m_streamMgr(streamMgr), |
|
91 |
- m_dirBase(dirBase) |
|
92 |
-{ |
|
93 |
- updateModuleListFull(); |
|
94 |
- |
|
95 |
- // request call in 1s |
|
96 |
- m_callMgr.requestTimeCall(this, Time::now() + Time(1)); |
|
97 |
-} |
|
98 |
- |
|
99 |
-/// destructor |
|
100 |
-template<typename MODULE> |
|
101 |
-ModuleMgr<MODULE>::~ModuleMgr() |
|
102 |
-{ |
|
103 |
- // free all modules |
|
104 |
- while (!m_moduleList.empty()) { |
|
105 |
- m_moduleList.back().destroyModule(); |
|
106 |
- m_moduleList.pop_back(); |
|
107 |
- } |
|
108 |
-} |
|
109 |
- |
|
110 |
-/// callback when requsted time reached |
|
111 |
-template<typename MODULE> |
|
112 |
-void ModuleMgr<MODULE>::timeCall() |
|
113 |
-{ |
|
114 |
- updateConfig(); |
|
115 |
- |
|
116 |
- // request next call in 1s |
|
117 |
- m_callMgr.requestTimeCall(this, Time::now() + Time(1)); |
|
118 |
-} |
|
119 |
- |
|
120 |
-/// check for update of configuration |
|
121 |
-template<typename MODULE> |
|
122 |
-void ModuleMgr<MODULE>::updateConfig() |
|
123 |
-{ |
|
124 |
- // module list update (base directory modified -> full, otherwise -> light) |
|
125 |
- if (m_dirBase.checkModified()) |
|
126 |
- updateModuleListFull(); |
|
127 |
- else |
|
128 |
- updateModuleListLight(); |
|
129 |
-} |
|
130 |
- |
|
131 |
-/// light update of module list, i.e. call all modules in current list |
|
132 |
-template<typename MODULE> |
|
133 |
-void ModuleMgr<MODULE>::updateModuleListLight() |
|
134 |
-{ |
|
135 |
- typename ModuleList::iterator itEntry; |
|
136 |
- for (itEntry = m_moduleList.begin(); itEntry != m_moduleList.end(); |
|
137 |
- ++itEntry) { |
|
138 |
- |
|
139 |
- // call module to check for changes |
|
140 |
- itEntry->m_pModule->updateConfig(); |
|
141 |
- |
|
142 |
- } // for itEntry |
|
143 |
-} |
|
144 |
- |
|
145 |
-/// full update of module list, i.e. scan all subdirs in base directory |
|
146 |
-template<typename MODULE> |
|
147 |
-void ModuleMgr<MODULE>::updateModuleListFull() |
|
148 |
-{ |
|
149 |
- // get list of subdirectories in base directory |
|
150 |
- typedef std::list<std::string> Subdirlist; |
|
151 |
- Subdirlist curSubdirs; |
|
152 |
- m_dirBase.getEntries(Directory::TypeSubdir, curSubdirs); |
|
153 |
- |
|
154 |
- // walk through current module list and subdir list simultaneously |
|
155 |
- Subdirlist::const_iterator itSubdir = curSubdirs.begin(); |
|
156 |
- typename ModuleList::iterator itEntry = m_moduleList.begin(); |
|
157 |
- while (itSubdir != curSubdirs.end() || itEntry != m_moduleList.end()) { |
|
158 |
- |
|
159 |
- // new module inserted |
|
160 |
- if (itEntry == m_moduleList.end() || |
|
161 |
- (itSubdir != curSubdirs.end() && *itSubdir < itEntry->m_name)) { |
|
162 |
- // create module |
|
163 |
- Entry entry(*itSubdir); |
|
164 |
- entry.createModule(*this); |
|
165 |
- // insert module list entry |
|
166 |
- m_moduleList.insert(itEntry, entry); |
|
167 |
- // advance to next subdir |
|
168 |
- ++itSubdir; |
|
169 |
- } |
|
170 |
- |
|
171 |
- // module removed |
|
172 |
- else if (itSubdir == curSubdirs.end() || *itSubdir > itEntry->m_name) { |
|
173 |
- // remove entry |
|
174 |
- itEntry->destroyModule(); |
|
175 |
- itEntry = m_moduleList.erase(itEntry); |
|
176 |
- // do not advance to next subdir |
|
177 |
- } |
|
178 |
- |
|
179 |
- // module stayed in list |
|
180 |
- else { |
|
181 |
- // call module to check for changes |
|
182 |
- itEntry->m_pModule->updateConfig(); |
|
183 |
- // advance to next subdir and next entry |
|
184 |
- ++itSubdir; |
|
185 |
- ++itEntry; |
|
186 |
- } |
|
187 |
- |
|
188 |
- } // while itSubdir itEntry |
|
189 |
-} |
|
190 |
- |
|
191 |
-/* #################### |
|
192 |
- # ModuleMgr::Entry # |
|
193 |
- #################### */ |
|
194 |
- |
|
195 |
-/// constructor |
|
196 |
-template<typename MODULE> |
|
197 |
-ModuleMgr<MODULE>::Entry::Entry(const std::string &name): |
|
198 |
- m_name(name), |
|
199 |
- m_pModule(NULL) |
|
200 |
-{ |
|
201 |
-} |
|
202 |
- |
|
203 |
-/// create module |
|
204 |
-template<typename MODULE> |
|
205 |
-void ModuleMgr<MODULE>::Entry::createModule(ModuleMgr &mgr) |
|
206 |
-{ |
|
207 |
- destroyModule(); |
|
208 |
- m_pModule = new MODULE(mgr.m_callMgr, mgr.m_streamMgr, |
|
209 |
- mgr.m_dirBase.getSubdir(m_name)); |
|
210 |
-} |
|
211 |
- |
|
212 |
-/// destroy module |
|
213 |
-template<typename MODULE> |
|
214 |
-void ModuleMgr<MODULE>::Entry::destroyModule() |
|
215 |
-{ |
|
216 |
- if (m_pModule) { |
|
217 |
- delete m_pModule; |
|
218 |
- m_pModule = NULL; |
|
219 |
- } |
|
220 |
-} |
|
221 |
- |
|
222 | 76 |
} // namespace Blinker |
223 | 77 |
|
224 | 78 |
#endif // #ifndef MODULEMGR_H |
... | ... |
@@ -0,0 +1,165 @@ |
1 |
+/* Blinker |
|
2 |
+ Copyright 2011 Stefan Schuermans <stefan@blinkenarea.org> |
|
3 |
+ Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html |
|
4 |
+ a blinkenarea.org project */ |
|
5 |
+ |
|
6 |
+#ifndef MODULEMGR_IMPL_H |
|
7 |
+#define MODULEMGR_IMPL_H |
|
8 |
+ |
|
9 |
+#include <list> |
|
10 |
+ |
|
11 |
+#include "CallMgr.h" |
|
12 |
+#include "Directory.h" |
|
13 |
+#include "Module.h" |
|
14 |
+#include "ModuleMgr.h" |
|
15 |
+#include "StreamMgr.h" |
|
16 |
+#include "TimeCallee.h" |
|
17 |
+ |
|
18 |
+namespace Blinker { |
|
19 |
+ |
|
20 |
+/** |
|
21 |
+ * @brief constructor |
|
22 |
+ * @param[in] callMgr callback manager |
|
23 |
+ * @param[in] streamMgr stream manager |
|
24 |
+ * @param[in] dirBase base directory |
|
25 |
+ */ |
|
26 |
+template<typename MODULE> |
|
27 |
+ModuleMgr<MODULE>::ModuleMgr(CallMgr &callMgr, StreamMgr &streamMgr, |
|
28 |
+ const Directory &dirBase): |
|
29 |
+ m_callMgr(callMgr), |
|
30 |
+ m_streamMgr(streamMgr), |
|
31 |
+ m_dirBase(dirBase) |
|
32 |
+{ |
|
33 |
+ updateModuleListFull(); |
|
34 |
+ |
|
35 |
+ // request call in 1s |
|
36 |
+ m_callMgr.requestTimeCall(this, Time::now() + Time(1)); |
|
37 |
+} |
|
38 |
+ |
|
39 |
+/// destructor |
|
40 |
+template<typename MODULE> |
|
41 |
+ModuleMgr<MODULE>::~ModuleMgr() |
|
42 |
+{ |
|
43 |
+ // free all modules |
|
44 |
+ while (!m_moduleList.empty()) { |
|
45 |
+ m_moduleList.back().destroyModule(); |
|
46 |
+ m_moduleList.pop_back(); |
|
47 |
+ } |
|
48 |
+} |
|
49 |
+ |
|
50 |
+/// callback when requsted time reached |
|
51 |
+template<typename MODULE> |
|
52 |
+void ModuleMgr<MODULE>::timeCall() |
|
53 |
+{ |
|
54 |
+ updateConfig(); |
|
55 |
+ |
|
56 |
+ // request next call in 1s |
|
57 |
+ m_callMgr.requestTimeCall(this, Time::now() + Time(1)); |
|
58 |
+} |
|
59 |
+ |
|
60 |
+/// check for update of configuration |
|
61 |
+template<typename MODULE> |
|
62 |
+void ModuleMgr<MODULE>::updateConfig() |
|
63 |
+{ |
|
64 |
+ // module list update (base directory modified -> full, otherwise -> light) |
|
65 |
+ if (m_dirBase.checkModified()) |
|
66 |
+ updateModuleListFull(); |
|
67 |
+ else |
|
68 |
+ updateModuleListLight(); |
|
69 |
+} |
|
70 |
+ |
|
71 |
+/// light update of module list, i.e. call all modules in current list |
|
72 |
+template<typename MODULE> |
|
73 |
+void ModuleMgr<MODULE>::updateModuleListLight() |
|
74 |
+{ |
|
75 |
+ typename ModuleList::iterator itEntry; |
|
76 |
+ for (itEntry = m_moduleList.begin(); itEntry != m_moduleList.end(); |
|
77 |
+ ++itEntry) { |
|
78 |
+ |
|
79 |
+ // call module to check for changes |
|
80 |
+ itEntry->m_pModule->updateConfig(); |
|
81 |
+ |
|
82 |
+ } // for itEntry |
|
83 |
+} |
|
84 |
+ |
|
85 |
+/// full update of module list, i.e. scan all subdirs in base directory |
|
86 |
+template<typename MODULE> |
|
87 |
+void ModuleMgr<MODULE>::updateModuleListFull() |
|
88 |
+{ |
|
89 |
+ // get list of subdirectories in base directory |
|
90 |
+ typedef std::list<std::string> Subdirlist; |
|
91 |
+ Subdirlist curSubdirs; |
|
92 |
+ m_dirBase.getEntries(Directory::TypeSubdir, curSubdirs); |
|
93 |
+ |
|
94 |
+ // walk through current module list and subdir list simultaneously |
|
95 |
+ Subdirlist::const_iterator itSubdir = curSubdirs.begin(); |
|
96 |
+ typename ModuleList::iterator itEntry = m_moduleList.begin(); |
|
97 |
+ while (itSubdir != curSubdirs.end() || itEntry != m_moduleList.end()) { |
|
98 |
+ |
|
99 |
+ // new module inserted |
|
100 |
+ if (itEntry == m_moduleList.end() || |
|
101 |
+ (itSubdir != curSubdirs.end() && *itSubdir < itEntry->m_name)) { |
|
102 |
+ // create module |
|
103 |
+ Entry entry(*itSubdir); |
|
104 |
+ entry.createModule(*this); |
|
105 |
+ // insert module list entry |
|
106 |
+ m_moduleList.insert(itEntry, entry); |
|
107 |
+ // advance to next subdir |
|
108 |
+ ++itSubdir; |
|
109 |
+ } |
|
110 |
+ |
|
111 |
+ // module removed |
|
112 |
+ else if (itSubdir == curSubdirs.end() || *itSubdir > itEntry->m_name) { |
|
113 |
+ // remove entry |
|
114 |
+ itEntry->destroyModule(); |
|
115 |
+ itEntry = m_moduleList.erase(itEntry); |
|
116 |
+ // do not advance to next subdir |
|
117 |
+ } |
|
118 |
+ |
|
119 |
+ // module stayed in list |
|
120 |
+ else { |
|
121 |
+ // call module to check for changes |
|
122 |
+ itEntry->m_pModule->updateConfig(); |
|
123 |
+ // advance to next subdir and next entry |
|
124 |
+ ++itSubdir; |
|
125 |
+ ++itEntry; |
|
126 |
+ } |
|
127 |
+ |
|
128 |
+ } // while itSubdir itEntry |
|
129 |
+} |
|
130 |
+ |
|
131 |
+/* #################### |
|
132 |
+ # ModuleMgr::Entry # |
|
133 |
+ #################### */ |
|
134 |
+ |
|
135 |
+/// constructor |
|
136 |
+template<typename MODULE> |
|
137 |
+ModuleMgr<MODULE>::Entry::Entry(const std::string &name): |
|
138 |
+ m_name(name), |
|
139 |
+ m_pModule(NULL) |
|
140 |
+{ |
|
141 |
+} |
|
142 |
+ |
|
143 |
+/// create module |
|
144 |
+template<typename MODULE> |
|
145 |
+void ModuleMgr<MODULE>::Entry::createModule(ModuleMgr &mgr) |
|
146 |
+{ |
|
147 |
+ destroyModule(); |
|
148 |
+ m_pModule = new MODULE(mgr.m_callMgr, mgr.m_streamMgr, |
|
149 |
+ mgr.m_dirBase.getSubdir(m_name)); |
|
150 |
+} |
|
151 |
+ |
|
152 |
+/// destroy module |
|
153 |
+template<typename MODULE> |
|
154 |
+void ModuleMgr<MODULE>::Entry::destroyModule() |
|
155 |
+{ |
|
156 |
+ if (m_pModule) { |
|
157 |
+ delete m_pModule; |
|
158 |
+ m_pModule = NULL; |
|
159 |
+ } |
|
160 |
+} |
|
161 |
+ |
|
162 |
+} // namespace Blinker |
|
163 |
+ |
|
164 |
+#endif // #ifndef MODULEMGR_IMPL_H |
|
165 |
+ |