Belle II Software  release-05-01-25
ZMQApp.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2019 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Nils Braun *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #pragma once
11 
12 #include <framework/pcore/zmq/utils/ZMQParent.h>
13 #include <framework/pcore/zmq/connections/ZMQSimpleConnection.h>
14 #include <daq/rfarm/event/hltsocket/HLTMainLoop.h>
15 #include <framework/pcore/zmq/messages/ZMQMessageFactory.h>
16 #include <framework/logging/Logger.h>
17 
18 #include <boost/program_options.hpp>
19 #include <iostream>
20 
21 namespace po = boost::program_options;
22 
23 namespace Belle2 {
47  template<class AInputConnection, class AOutputConnection>
48  class ZMQStandardApp {
49  public:
56  void initFromConsole(const std::string& description, int argc, char* argv[]);
57 
63  void main();
64 
65  protected:
67  std::shared_ptr<ZMQParent> m_parent;
69  std::unique_ptr<AInputConnection> m_input;
71  std::unique_ptr<AOutputConnection> m_output;
73  std::unique_ptr<ZMQSimpleConnection> m_monitor;
75  bool m_terminate = false;
77  unsigned int m_timeout = 0;
79  bool m_monitorHasPriority = false;
80 
82  virtual void initialize();
84  virtual void addOptions(po::options_description& desc);
86  virtual void handleExternalSignal(EMessageTypes);
88  virtual void handleInput();
90  virtual void handleOutput();
92  virtual void handleTimeout();
94  virtual void fillMonitoringJSON(std::stringstream& buffer) const;
96  bool terminated() const;
98  void resetTimer();
100  void pollEvent(bool pollOnInput);
101 
102  private:
106  std::string m_monitoringAddress;
108  int m_remainingTime = -1;
110  std::chrono::system_clock::time_point m_start;
111 
113  bool checkTimer();
115  void updateTimer();
117  void handleMonitoring();
118  };
120 }
121 
123 #include <daq/hbasf2/apps/ZMQApp.details.h>
Belle2::ZMQStandardApp::m_start
std::chrono::system_clock::time_point m_start
Start time for the timeout.
Definition: ZMQApp.h:118
Belle2::ZMQStandardApp::m_output
std::unique_ptr< AOutputConnection > m_output
Pointer to the output connection. Should be set in initialize.
Definition: ZMQApp.h:79
Belle2::ZMQStandardApp::pollEvent
void pollEvent(bool pollOnInput)
Poll until a single event is retreived.
Definition: ZMQApp.details.h:78
Belle2::EMessageTypes
EMessageTypes
Type the messages can have.
Definition: ZMQDefinitions.h:26
Belle2::ZMQStandardApp::m_monitorHasPriority
bool m_monitorHasPriority
Flag to break out of the polling loop to check for monitoring messages. Except for the finalcollector...
Definition: ZMQApp.h:87
Belle2::ZMQStandardApp::handleTimeout
virtual void handleTimeout()
Will get called on a timeout. Can be overridden in a derived class. Empty by default.
Definition: ZMQApp.details.h:158
Belle2::ZMQStandardApp::checkTimer
bool checkTimer()
Helper function to check, if the timeout should happen.
Definition: ZMQApp.details.h:169
Belle2::ZMQStandardApp::m_terminate
bool m_terminate
Can be set by functions to terminate the main loop at the next possibility.
Definition: ZMQApp.h:83
Belle2::ZMQStandardApp::main
void main()
Start the main loop polling on the output and monitoring connections and eventually also on the input...
Definition: ZMQApp.details.h:59
Belle2::ZMQStandardApp::handleInput
virtual void handleInput()
Will get called for every message on the input connection. Can be overridden in a derived class....
Definition: ZMQApp.details.h:146
Belle2::ZMQStandardApp::m_parent
std::shared_ptr< ZMQParent > m_parent
Pointer to the ZMQParent to be used as base for all connections.
Definition: ZMQApp.h:75
Belle2::ZMQStandardApp::resetTimer
void resetTimer()
Helper function to reset the start time and the remaining time.
Definition: ZMQApp.details.h:193
Belle2::ZMQStandardApp::handleExternalSignal
virtual void handleExternalSignal(EMessageTypes)
Will get called for every signal message on the monitoring connection. Can be overridden in a derived...
Definition: ZMQApp.details.h:141
Belle2::ZMQStandardApp::addOptions
virtual void addOptions(po::options_description &desc)
Override in a derived class to add the command line arguments. Do not forget to call this base functi...
Definition: ZMQApp.details.h:134
Belle2::ZMQStandardApp::updateTimer
void updateTimer()
Helper function to update the remaining time.
Definition: ZMQApp.details.h:176
Belle2::ZMQStandardApp::terminated
bool terminated() const
Check if the main loop will be exited on next occasion. Can be set via the "m_terminate" flag.
Definition: ZMQApp.details.h:163
Belle2::ZMQStandardApp::m_monitor
std::unique_ptr< ZMQSimpleConnection > m_monitor
Pointer to the monitoring connection. Should be set in initialize.
Definition: ZMQApp.h:81
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::ZMQStandardApp::m_remainingTime
int m_remainingTime
Counter for the remaining time until a timeout happens.
Definition: ZMQApp.h:116
Belle2::HLTMainLoop
Definition: HLTMainLoop.h:28
Belle2::ZMQStandardApp::m_mainLoop
HLTMainLoop m_mainLoop
Internal signal handler.
Definition: ZMQApp.h:112
Belle2::ZMQStandardApp::handleMonitoring
void handleMonitoring()
Handle an incoming message on the monitoring socket by either calling handleExternalSignal() or by pa...
Definition: ZMQApp.details.h:206
Belle2::ZMQStandardApp::initFromConsole
void initFromConsole(const std::string &description, int argc, char *argv[])
Should be called before the main() function to initialize the connections using the paremeters given ...
Definition: ZMQApp.details.h:29
Belle2::ZMQStandardApp::m_monitoringAddress
std::string m_monitoringAddress
Storage for the monitoring address for the cmd arguments.
Definition: ZMQApp.h:114
Belle2::ZMQStandardApp::handleOutput
virtual void handleOutput()
Will get called for every message on the output connection. Can be overridden in a derived class....
Definition: ZMQApp.details.h:152
Belle2::ZMQStandardApp::initialize
virtual void initialize()
Override in a derived class to initialize the connections from the given command line arguments....
Definition: ZMQApp.details.h:127
Belle2::ZMQStandardApp::m_timeout
unsigned int m_timeout
If set to a value != 0, will call handleTimeout with this frequency (in seconds).
Definition: ZMQApp.h:85
Belle2::ZMQStandardApp::m_input
std::unique_ptr< AInputConnection > m_input
Pointer to the input connection. Should be set in initialize.
Definition: ZMQApp.h:77
Belle2::ZMQStandardApp::fillMonitoringJSON
virtual void fillMonitoringJSON(std::stringstream &buffer) const
Using the connections, fill up a buffer with the content to be monitored.
Definition: ZMQApp.details.h:230