ifw-ccf  2.1.0-pre1
application.hpp
Go to the documentation of this file.
1 
5 #ifndef CCF_CONTROL_APPLICATION_HPP_
6 #define CCF_CONTROL_APPLICATION_HPP_
7 
8 #include <mal/Mal.hpp>
9 
10 #include <rad/mal/replier.hpp>
11 #include <rad/dbAdapterRedis.hpp>
12 
13 #include <Recif.hpp>
14 
15 #include <ccf/common/base.hpp>
19 #include <ccf/common/appBase.hpp>
20 #include <ccf/control/config.hpp>
33 
34 namespace ccf::control {
35 
37  // @{
38  const std::string MON_THR_TAG = "MonThr";
39  const std::string ACQ_THR_TAG = "AcqThr";
40  //const std::string PROC_THR_TAG = "ProcThr::{}";
41  //const std::string PUB_THR_TAG = "PubThr::{}::{}";
42  const std::string INPUT_Q_TAG = "InputQueue";
43  const std::string OUTPUT_Q_TAG = "OutputQueue::{}";
44  // @}
45 
47  // @{
48  const std::string THREAD_SIG_TERM = "SigTerm";
49  const std::string THREAD_SIG_SETUP = "SigSetup";
50  const std::string THREAD_SIG_NEW_DATA = "SigNewData";
51  const std::string THREAD_SIG_PUBLISH = "SigPublish";
52  const std::string THREAD_SIG_START = "SigStart";
53  // @}
54 
55  //const std::string THREAD_SYNC_ACK_OK = "SigAckOk";
56  //const std::string THREAD_SYNC_ACK_ERROR = "SigAckError";
57  //const std::string THREAD_SYNC_UNKNOWN = "SigUnknown";
58 
60  // @{
61  const std::string ACTIVE_REC_MUTEX = "ActiveRecMutex";
62  const std::string REC_HISTORY_MUTEX = "RecHistoryMutex";
63  const std::string MUTEX_REC_WAIT_REQUESTS = "RecWaitRequests";
64  // @}
65 
67  template <typename T>
68  void CcfRequestRejectHandler(const rad::AnyEvent& event, const std::string& state) {
69  LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
70  auto req = rad::GetPayloadNothrow<T>(event);
71  if (req == nullptr) {
72  LOG4CPLUS_ERROR(Logger(), event.GetId() << " has no associated request!");
73  return;
74  }
75  req->SetException(elt::mal::MalException("Request rejected in state " + state));
76  }
77 
83  public:
84 
87 
89  static Application& Instance();
90 
91  virtual ~Application();
92 
94  elt::mal::Mal& GetMal();
95 
97  int Execute(int argc, char* argv[]);
98 
101  void CreateThreads();
102 
104  void LoadInitSetup();
105 
107  void HandleSetup();
108 
111  void Dismantle();
112 
114  bool GetAcquisitionActive();
115 
117  void StartAcquistion();
118 
121  void StopAcquistion();
122 
124  void CheckStopAcquistion();
125 
127  void IncrementFrameCounter();
128 
130  std::string GetState();
131 
133  void SetRequestStatus(const std::string& request,
134  const std::string& status);
135 
137  std::string GetRequestStatus(const std::string& request) const;
138 
140  std::string ListAdapters();
141 
143  template <class COM_ADPT_TYPE, class SIM_COM_ADPT_TYPE>
144  void RegisterComAdapters(const COM_ADPT_TYPE& com_adapter_factory_obj,
145  const SIM_COM_ADPT_TYPE& sim_com_adapter_factory_obj) {
146  LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
147  // if (m_com_adapter != nullptr) {
148  // delete m_com_adapter;
149  // }
150  // if (m_sim_com_adapter != nullptr) {
151  // delete m_sim_com_adapter;
152  // }
153  m_com_adapter = std::make_unique<COM_ADPT_TYPE>();
154  ccf::AssertPtr(m_com_adapter.get(), "Communication Adapter", CCFLOC);
155  m_sim_com_adapter = std::make_unique<SIM_COM_ADPT_TYPE>();
156  ccf::AssertPtr(m_sim_com_adapter.get(), "Simulation Communication Adapter", CCFLOC);
157  }
158 
162 
164  void ConnectDevice();
165 
167  void DisconnectDevice();
168 
170  const std::vector<std::string>& GetProcThreadNames() const;
171 
173  const std::vector<std::string>& GetPubThreadNames(const std::string& proc_thread_name) const;
174 
176  const std::list<PubThread*>& GetPubThreads() const;
177 
179  bool IsProcThread(const std::string& thread_name);
180 
182  bool IsPubThread(const std::string& thread_name);
183 
184  Application(const Application&) = delete;
185  Application& operator=(const Application&) = delete;
186 
188  // Recording handling
190 
192  void UpdateRecordingStatus();
193 
195  void SetActiveRecStatusObj(std::shared_ptr<recif::RecStatus>& active_rec);
196 
198  void UpdateActiveRecStatusObj(const ccf::common::PublisherStatus& sum_rec_pub_status);
199 
201  void UpdateRecStatusInDb();
202 
204  void GetActiveRecStatus(std::shared_ptr<recif::RecStatus>& active_rec) const;
205 
208  std::shared_ptr<recif::RecStatus>& active_rec);
209 
211  void SetRecordingStatus(const ccf::PubStatus status);
212 
214  void GetRecStatus(const std::string& rec_id,
215  std::shared_ptr<recif::RecStatus>& active_rec) const;
216 
218  void GetMostRecentRecStatus(std::shared_ptr<recif::RecStatus>& active_rec) const;
219 
221  void AddRecWaitRequest(std::shared_ptr<RecWaitRequest>& req_wait_req);
222 
224  void CheckRecWaitRequests(const bool recording_completed=false);
225 
227  void CleanUpRecWaitRequests();
229 
230  private:
231  Application();
232 
233  int8_t m_simulation;
234  std::unique_ptr<rad::cii::Replier> m_mal_replier;
235  std::shared_ptr<elt::mal::Mal> m_mal;
236  std::unique_ptr<DataContext> m_data_ctx;
237  ActionMgr m_action_mgr;
238  //StdActions* m_actions_std;
239  bool m_dismantle_invoked;
240 
241  std::map<std::string, std::string> m_request_status_map;
242 
243  std::unique_ptr<ccf::common::ComAdptBase> m_com_adapter;
244  std::unique_ptr<ccf::common::ComAdptBase> m_sim_com_adapter;
245 
246  std::map<std::string, std::shared_ptr<ccf::common::FrameQueue>> m_frame_queues;
247 
248  std::vector<std::string> m_proc_thread_names;
249  std::map<std::string, std::vector<std::string>> m_proc_to_pub_thread_names;
250 
251  std::map<std::string, bool> m_pub_thread_names;
252 
253  // For easy access. The threads are handled by the MPTK.
254  std::list<PubThread*> m_pub_threads;
255 
256  // Acquisition control.
257  ccf::ExpoMode m_acq_setup_expo_mode;
258  int64_t m_acq_setup_nb_of_frames;
259  int64_t m_acq_status_frame_count;
260 
261  // Recording session control.
262  std::shared_ptr<recif::RecStatus> m_active_recording;
263  int64_t m_exp_nb_of_frames;
264  int32_t m_max_sz_rec_history;
265  int32_t m_rec_history_exp;
266  // Maps "<rec id>" into the set of associated Recording Sessions.
267  std::map<std::string, std::shared_ptr<recif::RecStatus>> m_rec_history;
268  std::vector<std::string> m_rec_id_history;
269 
270  std::map<std::string, std::string> m_file_stored_to_pub_id;
271 
272  // Pending ReqWait Requests.
273  std::map<std::string, std::shared_ptr<RecWaitRequest>> m_rec_wait_requests;
274 };
275 
276 inline Application& App() {
277  return Application::Instance();
278 }
279 
280 } // namespace ccf::control
281 
282 #endif // CCFCONTROL_APPLICATION_HPP_
dataContext.hpp
ccf::control::Application
Implements the core CCF Control Application.
Definition: application.hpp:82
stdCmdsImpl.hpp
frameQueue.hpp
ccf::control::ACQ_THR_TAG
const std::string ACQ_THR_TAG
Definition: application.hpp:39
ccf::control::Application::SetActiveRecStatusObj
void SetActiveRecStatusObj(std::shared_ptr< recif::RecStatus > &active_rec)
Set the Active Recording Status Object.
Definition: application.cpp:819
ccf::control::Application::SetRecordingStatus
void SetRecordingStatus(const ccf::PubStatus status)
Set the status of the an active Recording Session (Active Recording Object).
Definition: application.cpp:800
ccf::AssertPtr
void AssertPtr(const void *ptr, const std::string &object, const std::string &location)
Check that pointer is not nullptr and raise rad::exception in case it is.
Definition: base.cpp:53
ccf::control::ACTIVE_REC_MUTEX
const std::string ACTIVE_REC_MUTEX
Definition: application.hpp:61
ccf::control::Application::Dismantle
void Dismantle()
Definition: application.cpp:233
ccf::control::THREAD_SIG_PUBLISH
const std::string THREAD_SIG_PUBLISH
Definition: application.hpp:51
ccf::control::INPUT_Q_TAG
const std::string INPUT_Q_TAG
Definition: application.hpp:42
ccf::common::AppBase
Class to be used as parent for CCF application type of classes.
Definition: appBase.hpp:25
monitorThread.hpp
ccf::control::ActionMgr
Responsible for the life-cycle management of actions and activities.
Definition: actionMgr.hpp:19
ccf::control::Application::GetComAdapter
ccf::common::ComAdptBase & GetComAdapter()
Definition: application.cpp:502
ccf::control::Application::GetPubThreads
const std::list< PubThread * > & GetPubThreads() const
Get the IDs of the Publisher Threads running.
Definition: application.cpp:579
ccf::control::CcfRequestRejectHandler
void CcfRequestRejectHandler(const rad::AnyEvent &event, const std::string &state)
Template function RAD request rejection check/handler.
Definition: application.hpp:68
ccf::control::Application::IsProcThread
bool IsProcThread(const std::string &thread_name)
Returns bool if the thread is a Processing Thread.
Definition: application.cpp:939
recCmdsImpl.hpp
ccf::control::Application::GetRecStatus
void GetRecStatus(const std::string &rec_id, std::shared_ptr< recif::RecStatus > &active_rec) const
Check if a Recording with the given ID is found and returns its status.
Definition: application.cpp:756
ccf::control::Application::GetMal
elt::mal::Mal & GetMal()
Return reference to the MAL (Singleton) instance.
Definition: application.cpp:60
ccf::control::Application::GetState
std::string GetState()
Return the current state (status) of the SCXML state machine.
Definition: application.cpp:584
ccf::control::REC_HISTORY_MUTEX
const std::string REC_HISTORY_MUTEX
Definition: application.hpp:62
procThread.hpp
ccf::common::PublisherStatus
Class used by a Publisher to handle its own publisher status.
Definition: pubBase.hpp:32
acqThread.hpp
CCFLOC
#define CCFLOC
Macro generating a location identifier: "<file>:<line>:<function>:<thread>".
Definition: base.hpp:453
config.hpp
ccf::control::Application::CreateThreads
void CreateThreads()
Definition: application.cpp:366
ccf::control::Application::Application
Application(const Application &)=delete
ccf::control::Application::CheckRecWaitRequests
void CheckRecWaitRequests(const bool recording_completed=false)
Check if there are pending requests from RecWait Requests and send response if needed.
Definition: application.cpp:864
ccf::PubStatus
PubStatus
Defines the various possible states of a Data Publisher.
Definition: base.hpp:284
ccf::control::Application::Execute
int Execute(int argc, char *argv[])
Initialise and execute the application.
Definition: application.cpp:66
ccf::control::Application::HandleSetup
void HandleSetup()
Handle the setup of the application.
Definition: application.cpp:523
ccf::control::Application::IsPubThread
bool IsPubThread(const std::string &thread_name)
Returns bool if the thread is a Publisher Thread.
Definition: application.cpp:944
ccf::control::Application::IncrementFrameCounter
void IncrementFrameCounter()
Increment frame counter, counting the number of complete frames received.
Definition: application.cpp:743
ccf::control::Application::GetProcThreadNames
const std::vector< std::string > & GetProcThreadNames() const
Get the names of the Processing Threads running.
Definition: application.cpp:549
stdActions.hpp
pubThread.hpp
ccf::control::Application::MoveActiveRecStatusToHistory
void MoveActiveRecStatusToHistory(const ccf::PubStatus status, std::shared_ptr< recif::RecStatus > &active_rec)
Move the Active Rec Status object to the Rec Status History.
Definition: application.cpp:828
ccf::control::Application::UpdateActiveRecStatusObj
void UpdateActiveRecStatusObj(const ccf::common::PublisherStatus &sum_rec_pub_status)
Update the Active Recording Status Object in memory and in the OLDB.
Definition: application.cpp:628
ccf::control::Application::StartAcquistion
void StartAcquistion()
Instruct the system to start an acquisition (finite or infnite).
Definition: application.cpp:682
ccf::control::OUTPUT_Q_TAG
const std::string OUTPUT_Q_TAG
Definition: application.hpp:43
ccf::control::THREAD_SIG_SETUP
const std::string THREAD_SIG_SETUP
Definition: application.hpp:49
ccf::control::App
Application & App()
Definition: application.hpp:276
ccf::control::Application::ConnectDevice
void ConnectDevice()
Connect to the associated device.
Definition: application.cpp:316
ccf::control::Application::RegisterComAdapters
void RegisterComAdapters(const COM_ADPT_TYPE &com_adapter_factory_obj, const SIM_COM_ADPT_TYPE &sim_com_adapter_factory_obj)
Method to allocate and register the Communication Adapters.
Definition: application.hpp:144
ccf::control::Application::~Application
virtual ~Application()
Definition: application.cpp:55
ccf::control::Application::GetRequestStatus
std::string GetRequestStatus(const std::string &request) const
Get the status of the given request.
Definition: application.cpp:929
ccf::control::Application::GetPubThreadNames
const std::vector< std::string > & GetPubThreadNames(const std::string &proc_thread_name) const
Get the names of the Publisher Threads running for a specific Processing Thread.
Definition: application.cpp:568
ccf::control::Application::UpdateRecStatusInDb
void UpdateRecStatusInDb()
Update the status of the current Recording Session in the DB.
Definition: application.cpp:655
base.hpp
ccf::control::Application::GetMostRecentRecStatus
void GetMostRecentRecStatus(std::shared_ptr< recif::RecStatus > &active_rec) const
Get the information about the most recent Recording (active or in the history).
Definition: application.cpp:795
comAdptBase.hpp
ccf::control::Application::GetAcquisitionActive
bool GetAcquisitionActive()
Returns true if image acquisition is active.
Definition: application.cpp:539
ccf::control::Application::SetRequestStatus
void SetRequestStatus(const std::string &request, const std::string &status)
Set status of the given request.
Definition: application.cpp:923
ccf::control::THREAD_SIG_TERM
const std::string THREAD_SIG_TERM
Definition: application.hpp:48
ccf::control::Application::CheckStopAcquistion
void CheckStopAcquistion()
Check if the conditions are met to stop an ongoing acqusition session.
Definition: application.cpp:711
ccf::control::Application::LoadInitSetup
void LoadInitSetup()
Load the Initialisation Setup specified in the configuration and install it.
Definition: application.cpp:515
appBase.hpp
ccf::control::Application::GetActiveRecStatus
void GetActiveRecStatus(std::shared_ptr< recif::RecStatus > &active_rec) const
Get the currently active Recording status.
Definition: application.cpp:748
ccf::ExpoMode
ExpoMode
Exposure modes.
Definition: base.hpp:312
ccf::control::THREAD_SIG_NEW_DATA
const std::string THREAD_SIG_NEW_DATA
Definition: application.hpp:50
ccf::control::Application::DisconnectDevice
void DisconnectDevice()
Disconnect from the associated device.
Definition: application.cpp:334
ccf::control::Application::ListAdapters
std::string ListAdapters()
List the available adapters registered in this executable.
Definition: application.cpp:350
ccf::control::THREAD_SIG_START
const std::string THREAD_SIG_START
Definition: application.hpp:52
ccf::control::Application::Instance
static Application & Instance()
Return reference to unique instance of the application class.
Definition: application.cpp:34
comAdptSim.hpp
ccf::control::MON_THR_TAG
const std::string MON_THR_TAG
Definition: application.hpp:38
recWaitRequest.hpp
recipeBase.hpp
ccf::control::Application::StopAcquistion
void StopAcquistion()
Definition: application.cpp:728
ccf::control
Definition: acqThread.cpp:10
ccf::control::Application::AddRecWaitRequest
void AddRecWaitRequest(std::shared_ptr< RecWaitRequest > &req_wait_req)
Add a ReqWaitRequest in the internal registry.
Definition: application.cpp:856
ccf::control::Application::operator=
Application & operator=(const Application &)=delete
Disable copy constructor.
ccf::control::Application::s_app_instance
static Application * s_app_instance
Singleton instance.
Definition: application.hpp:86
dcsCmdsImpl.hpp
ccf::control::Application::CleanUpRecWaitRequests
void CleanUpRecWaitRequests()
Check if there are RecWait Requests that can be removed from the queue.
Definition: application.cpp:902
ccf::control::Application::UpdateRecordingStatus
void UpdateRecordingStatus()
Disable assignment operator.
Definition: application.cpp:589
actionMgr.hpp
ccf::Logger
log4cplus::Logger & Logger()
Definition: base.cpp:9
ccf::control::MUTEX_REC_WAIT_REQUESTS
const std::string MUTEX_REC_WAIT_REQUESTS
Definition: application.hpp:63
ccf::common::ComAdptBase
Class to be used as parent for CCF Communication Adapters.
Definition: comAdptBase.hpp:31