Belle II Software development
ZMQApp.h
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#pragma once
9
10#include <framework/pcore/zmq/utils/ZMQParent.h>
11#include <framework/pcore/zmq/connections/ZMQSimpleConnection.h>
12#include <daq/rfarm/event/hltsocket/HLTMainLoop.h>
13#include <framework/pcore/zmq/messages/ZMQMessageFactory.h>
14#include <framework/logging/Logger.h>
15
16#include <boost/program_options.hpp>
17#include <iostream>
18
19namespace po = boost::program_options;
20
21namespace Belle2 {
45 template<class AInputConnection, class AOutputConnection>
47 public:
54 void initFromConsole(const std::string& description, int argc, char* argv[]);
55
61 void main();
62
63 protected:
65 std::shared_ptr<ZMQParent> m_parent;
67 std::unique_ptr<AInputConnection> m_input;
69 std::unique_ptr<AOutputConnection> m_output;
71 std::unique_ptr<ZMQSimpleConnection> m_monitor;
73 bool m_terminate = false;
75 unsigned int m_timeout = 0;
78
80 virtual void initialize();
82 virtual void addOptions(po::options_description& desc);
86 virtual void handleInput();
88 virtual void handleOutput();
90 virtual void handleTimeout();
92 virtual void fillMonitoringJSON(std::stringstream& buffer) const;
94 bool terminated() const;
96 void resetTimer();
98 void pollEvent(bool pollOnInput);
99
100 private:
108 std::chrono::system_clock::time_point m_start;
109
111 bool checkTimer();
113 void updateTimer();
115 void handleMonitoring();
116 };
118}
119
121#include <daq/hbasf2/apps/ZMQApp.details.h>
Generic base class for all standalone ZMQ applications.
Definition: ZMQApp.h:46
bool m_monitorHasPriority
Flag to break out of the polling loop to check for monitoring messages. Except for the finalcollector...
Definition: ZMQApp.h:77
std::shared_ptr< ZMQParent > m_parent
Pointer to the ZMQParent to be used as base for all connections.
Definition: ZMQApp.h:65
std::unique_ptr< AOutputConnection > m_output
Pointer to the output connection. Should be set in initialize.
Definition: ZMQApp.h:69
HLTMainLoop m_mainLoop
Internal signal handler.
Definition: ZMQApp.h:102
std::unique_ptr< ZMQSimpleConnection > m_monitor
Pointer to the monitoring connection. Should be set in initialize.
Definition: ZMQApp.h:71
bool m_terminate
Can be set by functions to terminate the main loop at the next possibility.
Definition: ZMQApp.h:73
std::unique_ptr< AInputConnection > m_input
Pointer to the input connection. Should be set in initialize.
Definition: ZMQApp.h:67
std::string m_monitoringAddress
Storage for the monitoring address for the cmd arguments.
Definition: ZMQApp.h:104
unsigned int m_timeout
If set to a value != 0, will call handleTimeout with this frequency (in seconds).
Definition: ZMQApp.h:75
int m_remainingTime
Counter for the remaining time until a timeout happens.
Definition: ZMQApp.h:106
std::chrono::system_clock::time_point m_start
Start time for the timeout.
Definition: ZMQApp.h:108
void pollEvent(bool pollOnInput)
Poll until a single event is retreived.
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 ...
virtual void initialize()
Override in a derived class to initialize the connections from the given command line arguments....
void handleMonitoring()
Handle an incoming message on the monitoring socket by either calling handleExternalSignal() or by pa...
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...
void updateTimer()
Helper function to update the remaining time.
virtual void fillMonitoringJSON(std::stringstream &buffer) const
Using the connections, fill up a buffer with the content to be monitored.
virtual void handleExternalSignal(EMessageTypes)
Will get called for every signal message on the monitoring connection. Can be overridden in a derived...
virtual void handleInput()
Will get called for every message on the input connection. Can be overridden in a derived class....
bool terminated() const
Check if the main loop will be exited on next occasion. Can be set via the "m_terminate" flag.
void main()
Start the main loop polling on the output and monitoring connections and eventually also on the input...
void resetTimer()
Helper function to reset the start time and the remaining time.
virtual void handleOutput()
Will get called for every message on the output connection. Can be overridden in a derived class....
bool checkTimer()
Helper function to check, if the timeout should happen.
virtual void handleTimeout()
Will get called on a timeout. Can be overridden in a derived class. Empty by default.
EMessageTypes
Type the messages can have.
Abstract base class for different kinds of events.