BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
AbstractSeeker.h
1 #pragma once
2 
3 #include <Poco/Clock.h>
4 #include <Poco/Mutex.h>
5 #include <Poco/RunnableAdapter.h>
6 #include <Poco/SharedPtr.h>
7 #include <Poco/Timespan.h>
8 #include <Poco/Thread.h>
9 
10 #include "util/AsyncWork.h"
11 #include "util/Joiner.h"
12 #include "util/Loggable.h"
13 #include "loop/StopControl.h"
14 
15 namespace BeeeOn {
16 
25 class AbstractSeeker : public AsyncWork<>, protected Loggable {
26 public:
27  typedef Poco::SharedPtr<AbstractSeeker> Ptr;
28 
29  AbstractSeeker(const Poco::Timespan &duration);
30 
34  Poco::Timespan duration() const;
35 
39  Poco::Timespan elapsed() const;
40 
48  Poco::Timespan remaining() const;
49 
54  void start();
55 
59  bool tryJoin(const Poco::Timespan &timeout) override;
60 
64  void cancel() override;
65 
66 protected:
67  void seek();
68  virtual void seekLoop(StopControl &control) = 0;
69 
70 private:
71  Poco::Timespan m_duration;
72  Poco::RunnableAdapter<AbstractSeeker> m_runnable;
73  Poco::Thread m_thread;
74  mutable Poco::FastMutex m_lock;
75  Poco::Clock m_started;
76  bool m_hasStarted;
77  StopControl m_stopControl;
78  Joiner m_joiner;
79 };
80 
81 }
AsyncWork is an interface allowing to wait until an asynchronous operation finishes. It is also possible to force-stop it by calling cancel().
Definition: AsyncWork.h:21
AbstractSeeker represents an asynchronous process that seeks for new devices in a certain network...
Definition: AbstractSeeker.h:25
Joiner implements join() on a thread that can be called multiple times from different threads...
Definition: Joiner.h:21
void cancel() override
Cancel seeking and wait for the thread to finish.
Definition: AbstractSeeker.cpp:67
StopControl implements the stop mechanism generically.
Definition: StopControl.h:12
Poco::Timespan duration() const
Definition: AbstractSeeker.cpp:17
Definition: Loggable.h:19
Poco::Timespan elapsed() const
Definition: AbstractSeeker.cpp:22
void start()
Start the seeking thread.
Definition: AbstractSeeker.cpp:49
Poco::Timespan remaining() const
Compute time that is remaining to finish the seeking process. If the seeking has finished, it returns 0.
Definition: AbstractSeeker.cpp:32
bool tryJoin(const Poco::Timespan &timeout) override
Join the seeking thread via Joiner.
Definition: AbstractSeeker.cpp:62