BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
Answer.h
1 #pragma once
2 
3 #include <Poco/AutoPtr.h>
4 #include <Poco/Event.h>
5 #include <Poco/Mutex.h>
6 #include <Poco/RefCountedObject.h>
7 #include <Poco/SynchronizedObject.h>
8 #include <Poco/Task.h>
9 #include <Poco/TaskManager.h>
10 
11 #include "core/Result.h"
12 
13 namespace BeeeOn {
14 
15 class AnswerQueue;
16 class CommandDispatcher;
17 class CommandHandler;
18 
19 /*
20  * During the Answer creation the queue is set. The queue is notified using
21  * the event() in the case of status change of the dirty.
22  * The change of the status in Result causes this notification.
23  *
24  * The Answer and the Result share the common mutex. The operations that
25  * change the status in the Answer and in the Result MUST be locked.
26  *
27  * Be aware, only a single thread is allowed to wait for notification (e.g.: via waitNotPending()).
28  * Otherwise, a race condition can occur.
29  */
30 class Answer : public Poco::RefCountedObject, public Poco::SynchronizedObject {
31 public:
32  typedef Poco::AutoPtr<Answer> Ptr;
33 
34  Answer(AnswerQueue &answerQueue, const bool autoDispose = false);
35 
36  /*
37  * All reference counted objects should have a protected destructor,
38  * to forbid explicit use of delete.
39  */
40  Answer(const Answer&) = delete;
41 
42  /*
43  * The status that informs about the change of a Result.
44  */
45  void setDirty(bool dirty);
46  bool isDirty() const;
47 
48  /*
49  * The check if the Result are in the terminal state (SUCCESS/ERROR).
50  */
51  bool isPending() const;
52 
53  Poco::Event &event();
54 
55  /*
56  * True if the list of commands is empty.
57  */
58  bool isEmpty() const;
59 
60  unsigned long resultsCount() const;
61 
62  int handlersCount() const;
63  void setHandlersCount(unsigned long counter);
64 
65  void addResult(Result *result);
66 
67  /*
68  * Notifies the waiting queue that this Answer isEmpty().
69  * The call sets Answer::setDirty(true).
70  */
71  void notifyUpdated();
72 
81  void waitNotPending(const Poco::Timespan &timeout);
82 
83  Result::Ptr at(size_t position);
84 
85  std::vector<Result::Ptr>::iterator begin();
86  std::vector<Result::Ptr>::iterator end();
87 
88 private:
89  AnswerQueue &m_answerQueue;
90  Poco::Event m_notifyEvent;
91  Poco::AtomicCounter m_dirty;
92  std::vector<Result::Ptr> m_resultList;
93  unsigned long m_handlers;
94  const bool m_autoDispose;
95 };
96 
97 }
Definition: Answer.h:30
Definition: AnswerQueue.h:22
void waitNotPending(const Poco::Timespan &timeout)
Definition: Answer.cpp:124
Definition: Result.h:29