libcluon  0.0.148
OD4Session.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017-2018 Christian Berger
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 
9 #ifndef CLUON_OD4SESSION_HPP
10 #define CLUON_OD4SESSION_HPP
11 
12 #include "cluon/Time.hpp"
13 #include "cluon/ToProtoVisitor.hpp"
14 #include "cluon/UDPReceiver.hpp"
15 #include "cluon/UDPSender.hpp"
16 #include "cluon/cluon.hpp"
17 #include "cluon/cluonDataStructures.hpp"
18 
19 #include <chrono>
20 #include <cstdint>
21 #include <functional>
22 #include <memory>
23 #include <mutex>
24 #include <string>
25 #include <unordered_map>
26 #include <utility>
27 
28 namespace cluon {
77  private:
78  OD4Session(const OD4Session &) = delete;
79  OD4Session(OD4Session &&) = delete;
80  OD4Session &operator=(const OD4Session &) = delete;
81  OD4Session &operator=(OD4Session &&) = delete;
82 
83  public:
93  OD4Session(uint16_t CID, std::function<void(cluon::data::Envelope &&envelope)> delegate = nullptr) noexcept;
94 
100  void send(cluon::data::Envelope &&envelope) noexcept;
101 
110  bool dataTrigger(int32_t messageIdentifier, std::function<void(cluon::data::Envelope &&envelope)> delegate) noexcept;
111 
122  void timeTrigger(float freq, std::function<bool()> delegate) noexcept;
123 
131  template <typename T>
132  void send(T &message, const cluon::data::TimeStamp &sampleTimeStamp = cluon::data::TimeStamp(), uint32_t senderStamp = 0) noexcept {
133  try {
134  std::lock_guard<std::mutex> lck(m_senderMutex);
135  cluon::ToProtoVisitor protoEncoder;
136 
137  cluon::data::Envelope envelope;
138  {
139  envelope.dataType(static_cast<int32_t>(message.ID()));
140  message.accept(protoEncoder);
141  envelope.serializedData(protoEncoder.encodedData());
142  envelope.sent(cluon::time::now());
143  envelope.sampleTimeStamp((0 == (sampleTimeStamp.seconds() + sampleTimeStamp.microseconds())) ? envelope.sent() : sampleTimeStamp);
144  envelope.senderStamp(senderStamp);
145  }
146 
147  send(std::move(envelope));
148  } catch (...) {} // LCOV_EXCL_LINE
149  }
150 
151  public:
152  bool isRunning() noexcept;
153 
154  private:
155  void callback(std::string &&data, std::string &&from, std::chrono::system_clock::time_point &&timepoint) noexcept;
156  void sendInternal(std::string &&dataToSend) noexcept;
157 
158  private:
159  std::unique_ptr<cluon::UDPReceiver> m_receiver;
160  cluon::UDPSender m_sender;
161 
162  std::mutex m_senderMutex{};
163 
164  std::function<void(cluon::data::Envelope &&envelope)> m_delegate{nullptr};
165 
166  std::mutex m_mapOfDataTriggeredDelegatesMutex{};
167  std::unordered_map<int32_t, std::function<void(cluon::data::Envelope &&envelope)>, UseUInt32ValueAsHashKey> m_mapOfDataTriggeredDelegates{};
168 };
169 
170 } // namespace cluon
171 #endif
Definition: cluon.hpp:65
#define LIBCLUON_API
Definition: cluon.hpp:56
void send(T &message, const cluon::data::TimeStamp &sampleTimeStamp=cluon::data::TimeStamp(), uint32_t senderStamp=0) noexcept
Definition: OD4Session.hpp:132
cluon::data::TimeStamp now() noexcept
Definition: Time.hpp:70
std::string encodedData() const noexcept
Definition: ToProtoVisitor.cpp:15
Definition: cluon.hpp:70
Definition: UDPSender.hpp:56
Definition: OD4Session.hpp:76
Definition: ToProtoVisitor.hpp:23