UniSet @VERSION@
SharedMemory.h
1/*
2 * Copyright (c) 2015 Pavel Vainerman.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation, version 2.1.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Lesser Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16// -----------------------------------------------------------------------------
17#ifndef SharedMemory_H_
18#define SharedMemory_H_
19// -----------------------------------------------------------------------------
20#include <unordered_map>
21#include <string>
22#include <memory>
23#include <deque>
24#include <time.h>
25#include "IONotifyController.h"
26#include "Mutex.h"
27#include "PassiveTimer.h"
28#include "WDTInterface.h"
29#include "LogServer.h"
30#include "DebugStream.h"
31#include "LogAgregator.h"
32#include "VMonitor.h"
33#include "IOConfig_XML.h"
34#include "USingleProcess.h"
35// -----------------------------------------------------------------------------
36#ifndef vmonit
37#define vmonit( var ) vmon.add( #var, var )
38#endif
39// --------------------------------------------------------------------------
40namespace uniset
41{
42 // -----------------------------------------------------------------------------
45 private USingleProcess,
47 {
48 public:
49
50 // конструктор с конфигурированием через xml
52 xmlNode* cnode,
53 const std::shared_ptr<IOConfig_XML>& ioconf );
54
55 virtual ~SharedMemory();
56
58 static std::shared_ptr<SharedMemory> init_smemory( int argc, const char* const* argv );
59
61 static void help_print( int argc, const char* const* argv );
62
63 // функция определяет "готовность" SM к работе.
64 // должна использоваться другими процессами, для того,
65 // чтобы понять, когда можно получать от SM данные.
66 virtual CORBA::Boolean exist() override;
67
68 virtual uniset::SimpleInfo* getInfo( const char* userparam = 0 ) override;
69
70 void addReadItem( IOConfig_XML::ReaderSlot sl );
71
72 // ------------ HISTORY --------------------
73 typedef std::deque<long> HBuffer;
74
76 {
77 explicit HistoryItem( size_t bufsize = 0 ): id(uniset::DefaultObjectId), buf(bufsize) {}
78 HistoryItem( const uniset::ObjectId _id, const size_t bufsize, const long val ): id(_id), buf(bufsize, val) {}
79
80 inline void init( size_t size, long val )
81 {
82 if( size > 0 )
83 buf.assign(size, val);
84 }
85
87 HBuffer buf;
88
89 IOStateList::iterator ioit;
90
91 void add( long val, size_t size )
92 {
93 // т.к. буфер у нас уже заданного размера
94 // то просто удаляем очередную точку в начале
95 // и добавляем в конце
96 buf.pop_front();
97 buf.push_back(val);
98 }
99 };
100
101 typedef std::list<HistoryItem> HistoryList;
102
104 {
106 {
107 ::clock_gettime(CLOCK_REALTIME, &fuse_tm);
108 }
109
110 long id = { 0 }; // ID
111 HistoryList hlst; // history list
112 size_t size = { 0 };
113 std::string filter = { "" }; // filter field
114 uniset::ObjectId fuse_id = { uniset::DefaultObjectId }; // fuse sensor
115 bool fuse_invert = { false };
116 bool fuse_use_val = { false };
117 long fuse_val = { 0 };
118 timespec fuse_tm = { 0, 0 }; // timestamp
119 };
120
121 friend std::ostream& operator<<( std::ostream& os, const HistoryInfo& h );
122
123 typedef std::list<HistoryInfo> History;
124
125 // т.к. могут быть одинаковые "детонаторы" для разных "историй" то,
126 // вводим не просто map, а "map списка историй".
127 // точнее итераторов-историй.
128 typedef std::list<History::iterator> HistoryItList;
129 typedef std::unordered_map<uniset::ObjectId, HistoryItList> HistoryFuseMap;
130
131 typedef sigc::signal<void, const HistoryInfo&> HistorySlot;
132 HistorySlot signal_history();
134 inline int getHistoryStep() const
135 {
136 return histSaveTime;
137 }
138
139 // -------------------------------------------------------------------------------
140
141 inline std::shared_ptr<LogAgregator> logAgregator()
142 {
143 return loga;
144 }
145 inline std::shared_ptr<DebugStream> log()
146 {
147 return smlog;
148 }
149
150 protected:
151 typedef std::list<IOConfig_XML::ReaderSlot> ReadSlotList;
152 ReadSlotList lstRSlot;
153
154 virtual void sysCommand( const uniset::SystemMessage* sm ) override;
155 virtual void timerInfo( const uniset::TimerMessage* tm ) override;
156 virtual std::string getTimerName(int id) const override;
157
158 void sendEvent( uniset::SystemMessage& sm );
159 void initFromReserv();
160 bool initFromSM( uniset::ObjectId sm_id, uniset::ObjectId sm_node );
161 void reloadConfig();
162
163 virtual bool activateObject() override;
164 virtual bool deactivateObject() override;
165 bool readItem( const std::shared_ptr<UniXML>& xml, UniXML::iterator& it, xmlNode* sec );
166
167 void buildEventList( xmlNode* cnode );
168 void readEventList( const std::string& oname );
169
170 std::mutex mutexStart;
171
173 {
174 public:
179 timer_running(false),
181 {}
182
183 uniset::ObjectId a_sid; // аналоговый счётчик
184 uniset::ObjectId d_sid; // дискретный датчик состояния процесса
185 IOStateList::iterator a_it;
186 IOStateList::iterator d_it;
187
188 timeout_t reboot_msec;
193 bool timer_running;
194 PassiveTimer ptReboot;
195 };
196
197 enum Timers
198 {
199 tmHeartBeatCheck,
200 tmEvent,
201 tmHistory,
202 tmPulsar,
203 tmLastOfTimerID
204 };
205
206 int heartbeatCheckTime;
207 std::string heartbeat_node;
208 int histSaveTime;
209
210 void checkHeartBeat();
211
212 typedef std::list<HeartBeatInfo> HeartBeatList;
213 HeartBeatList hblist; // список датчиков "сердцебиения"
214 std::shared_ptr<WDTInterface> wdt;
215 std::atomic_bool activated = { false };
216 std::atomic_bool workready = { false };
217 std::atomic_bool cancelled = { false };
218
219 typedef std::list<uniset::ObjectId> EventList;
220 EventList elst;
221 std::string e_filter;
222 int evntPause;
223 int activateTimeout;
224
225 virtual void logging( uniset::SensorMessage& sm ) override;
226
227 bool dblogging = { false };
228
231 // оптимизация с использованием userdata (IOController::USensorInfo::userdata) нужна
232 // чтобы не использовать поиск в HistoryFuseMap (см. checkFuse)
233 // т.к. 0,1 - использует IONotifyController (см. IONotifyController::UserDataID)
234 // то используем не занятый "2" - в качестве элемента userdata
235 static const size_t udataHistory = 2;
236
237 History hist;
238 HistoryFuseMap histmap;
240 virtual void checkFuse( std::shared_ptr<IOController::USensorInfo>& usi, IOController* );
241 virtual void saveToHistory();
242
243 void buildHistoryList( xmlNode* cnode );
244 void checkHistoryFilter( UniXML::iterator& it );
245
246 IOStateList::iterator itPulsar;
247 uniset::ObjectId sidPulsar;
248 int msecPulsar;
249
250 xmlNode* confnode;
251
252 std::shared_ptr<LogAgregator> loga;
253 std::shared_ptr<DebugStream> smlog;
254 std::shared_ptr<LogServer> logserv;
255 std::string logserv_host = {""};
256 int logserv_port = {0};
257
258 VMonitor vmon;
259
260#ifndef DISABLE_REST_API
261 Poco::JSON::Object::Ptr buildLogServerInfo();
262 virtual Poco::JSON::Object::Ptr httpRequest( const UHttp::HttpRequestContext& ctx ) override;
263 virtual Poco::JSON::Object::Ptr httpHelp( const Poco::URI::QueryParameters& p ) override;
264 virtual Poco::JSON::Object::Ptr httpGetMyInfo( Poco::JSON::Object::Ptr root ) override;
265#endif
266
267 private:
268 HistorySlot m_historySignal;
269 };
270 // --------------------------------------------------------------------------
271} // end of namespace uniset
272// -----------------------------------------------------------------------------
273#endif // SharedMemory_H_
274// -----------------------------------------------------------------------------
sigc::slot< bool, const std::shared_ptr< UniXML > &, UniXML::iterator &, xmlNode * > ReaderSlot
Definition IOConfig_XML.h:71
Definition IOController.h:52
Definition IONotifyController.h:135
Пассивный таймер
Definition PassiveTimer.h:94
Definition MessageType.h:127
Definition SharedMemory.h:173
timeout_t reboot_msec
Definition SharedMemory.h:188
Definition SharedMemory.h:47
virtual void logging(uniset::SensorMessage &sm) override
сохранение информации об изменении состояния датчика
Definition SharedMemory.cc:720
HistoryFuseMap histmap
Definition SharedMemory.h:238
HistorySlot signal_history()
Definition SharedMemory.cc:842
virtual std::string getTimerName(int id) const override
Definition SharedMemory.cc:272
virtual bool deactivateObject() override
Definition SharedMemory.cc:357
static std::shared_ptr< SharedMemory > init_smemory(int argc, const char *const *argv)
Definition SharedMemory.cc:600
int getHistoryStep() const
Definition SharedMemory.h:134
static void help_print(int argc, const char *const *argv)
Definition SharedMemory.cc:32
virtual bool activateObject() override
Definition SharedMemory.cc:371
static const size_t udataHistory
Definition SharedMemory.h:235
Definition MessageType.h:171
Definition MessageType.h:214
Definition USingleProcess.h:28
static const timeout_t WaitUpTime
Definition PassiveTimer.h:69
Definition UniXML.h:44
Definition AccessConfig.h:30
const ObjectId DefaultObjectId
Definition UniSetTypes.h:71
long ObjectId
Definition UniSetTypes_i.idl:30
Definition SharedMemory.h:104
Definition SharedMemory.h:76
Definition UniSetTypes_i.idl:65