Line data Source code
1 : /**
2 : * @file DaphneV2Interface.hpp
3 : *
4 : * Definition of the interface protocol to the daphne V3 boards
5 : * It's basically just a wrapper around a zmq socket
6 : *
7 : * This is part of the DUNE DAQ Software Suite, copyright 2020.
8 : * Licensing/copyright details are in the COPYING file that you should have
9 : * received with this code.
10 : *
11 : */
12 :
13 : #ifndef DAPHNEMODULES_SRC_DAPHNEV3INTERFACE_HPP_
14 : #define DAPHNEMODULES_SRC_DAPHNEV3INTERFACE_HPP_
15 :
16 : #include <ers/ers.hpp>
17 : #include "logging/Logging.hpp" // NOTE: if ISSUES ARE DECLARED BEFORE include logging/Logging.hpp, TLOG_DEBUG<<issue wont work.
18 :
19 : #include <zmq.hpp>
20 : #include "daphnemodules/daphne_control_high.pb.h"
21 : #include "daphnemodules/CommonIssues.hpp"
22 :
23 : #include <memory>
24 : #include <mutex>
25 : #include <functional>
26 : #include <string>
27 :
28 :
29 :
30 : namespace dunedaq {
31 :
32 3 : ERS_DECLARE_ISSUE( daphnemodules,
33 : SocketCreationError,
34 : "Failed to create a socket",
35 : ERS_EMPTY
36 : )
37 :
38 5 : ERS_DECLARE_ISSUE( daphnemodules,
39 : FailedPing,
40 : "Failed to ping daphne board at " << ip << ':' << port,
41 : ((std::string)ip)((int)port)
42 : )
43 :
44 7 : ERS_DECLARE_ISSUE( daphnemodules,
45 : FailedSend,
46 : "Failed to send message of type " << type,
47 : ((std::string)type)
48 : )
49 :
50 3 : ERS_DECLARE_ISSUE( daphnemodules,
51 : FailedReceive,
52 : "Failed to receive message from " << connection,
53 : ((std::string)connection)
54 : )
55 :
56 3 : ERS_DECLARE_ISSUE( daphnemodules,
57 : EmptyPayload,
58 : "Empty payload received from " << connection,
59 : ((std::string)connection)
60 : )
61 :
62 3 : ERS_DECLARE_ISSUE( daphnemodules,
63 : TypeMismatch,
64 : "Received message of type " << type << " instead of " << expected,
65 : ((std::string)type)((std::string)expected)
66 : )
67 :
68 :
69 3 : ERS_DECLARE_ISSUE( daphnemodules,
70 : FailedDecoding,
71 : "Failed to de-serialise to " << type << ". Message: " << message,
72 : ((std::string)type)((std::string)message)
73 : )
74 :
75 3 : ERS_DECLARE_ISSUE( daphnemodules,
76 : UnexpectedDirection,
77 : "Message received with unexpeted properties. Version: " << version << ", direction " << ". Message: " << message,
78 : ((uint32_t)version)((std::string)direction)((std::string)message)
79 : )
80 :
81 :
82 :
83 : } // namespace dunedaq
84 :
85 :
86 : namespace dunedaq::daphnemodules {
87 :
88 : class DaphneV3Interface {
89 :
90 : public:
91 : DaphneV3Interface( std::string address, // it can contain the port or not
92 : std::string routing, // this should be the name of the controller module
93 : std::chrono::milliseconds timeout = std::chrono::milliseconds(500));
94 :
95 :
96 0 : ~DaphneV3Interface() { close(); }
97 :
98 : DaphneV3Interface(const DaphneV3Interface &) = delete;
99 : DaphneV3Interface & operator= (const DaphneV3Interface & ) = delete;
100 : DaphneV3Interface(DaphneV3Interface &&) = delete;
101 : DaphneV3Interface & operator= (DaphneV3Interface &&) = delete;
102 :
103 : // this takes the serilised message and encodes it into the envelope
104 : // It returns the serialised reply
105 :
106 : daphne::ControlEnvelopeV2 send( std::string && message, daphne::MessageTypeV2 );
107 :
108 : // this takes the serilised message and encodes it into the envelope
109 : // It returns the de-serialised objects
110 : template<class T>
111 : T send( std::string && message, daphne::MessageTypeV2 sent_type, daphne::MessageTypeV2 received_type );
112 :
113 : bool validate_connection();
114 :
115 : protected:
116 : void _send( std::string && message, daphne::MessageTypeV2 );
117 : daphne::ControlEnvelopeV2 _receive();
118 :
119 : void close();
120 :
121 : private:
122 : zmq::context_t m_context;
123 :
124 : zmq::socket_t m_socket;
125 : mutable std::mutex m_access_mutex;
126 :
127 : std::string m_connection;
128 :
129 : std::chrono::milliseconds m_timeout{1000};
130 :
131 : uint64_t m_message_counter = 0;
132 :
133 : inline static const size_t s_default_control_port = 40001;
134 : };
135 :
136 :
137 : } // namespce dunedaq::daphnemodules
138 :
139 : #include <DaphneV3Interface.hxx>
140 :
141 : #endif // DAPHNEMODULES_SRC_DAPHNEV3INTERFACE_HPP_
|