$darkmode
osi_transceiver_tcp.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Robert Bosch GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  */
23 #pragma once
24 
25 #include <memory> // for shared_ptr<>
26 #include <string> // for string
27 #include <vector> // for vector<>
28 
29 #include <boost/asio.hpp> // for streamsize
30 
31 #include <cloe/core.hpp> // for Json, Logger
32 #include <cloe/utility/tcp_transceiver.hpp> // for TcpTransceiver
33 
34 #include "osi_sensordata.pb.h" // for SensorData
35 
36 #include "osi_transceiver.hpp" // for OsiTransceiver
37 #include "osi_utils.hpp" // for osi_logger
38 
39 namespace osii {
40 
45  public:
46  using TcpTransceiver::TcpTransceiver;
47 
48  bool has_sensor_data() const override {
49  return this->tcp_available_data() >= static_cast<std::streamsize>(sizeof(osi3::SensorData));
50  }
51 
52  std::vector<std::shared_ptr<osi3::SensorData>> receive_sensor_data() override {
53  std::vector<std::shared_ptr<osi3::SensorData>> msgs;
54  while (this->has_sensor_data()) {
55  num_received_++;
56  msgs.push_back(this->receive_sensor_data_wait());
57  }
58  return msgs;
59  }
60 
61  void to_json(cloe::Json& j) const override {
62  j = cloe::Json{
63  {"connection_endpoint", this->tcp_endpoint()},
64  {"connection_ok", this->tcp_is_ok()},
65  {"num_errors", this->num_errors_},
66  {"num_messages_sent", this->num_sent_},
67  {"num_messages_received", this->num_received_},
68  };
69  }
70 
71  friend void to_json(cloe::Json& j, const OsiTransceiverTcp& t) { t.to_json(j); }
72 
73  protected:
77  std::shared_ptr<osi3::SensorData> receive_sensor_data_wait();
78 
79  private:
80  // Statistics for interest's sake
81  uint64_t num_errors_{0};
82  uint64_t num_sent_{0};
83  uint64_t num_received_{0};
84 };
85 
87  public:
88  using TcpTransceiverFactory::TcpTransceiverFactory;
89 
90  protected:
91  cloe::Logger factory_logger() const override { return osi_logger(); }
92  const char* instance_name() const override { return "OsiTransceiverTcp"; }
93 };
94 
95 } // namespace osii
Definition: tcp_transceiver.hpp:149
Definition: tcp_transceiver.hpp:59
bool tcp_is_ok() const
Definition: tcp_transceiver.hpp:92
std::streamsize tcp_available_data() const
Definition: tcp_transceiver.hpp:112
Definition: osi_transceiver_tcp.hpp:86
Definition: osi_transceiver_tcp.hpp:44
std::shared_ptr< osi3::SensorData > receive_sensor_data_wait()
Definition: osi_transceiver_tcp.cpp:42
bool has_sensor_data() const override
Definition: osi_transceiver_tcp.hpp:48
std::vector< std::shared_ptr< osi3::SensorData > > receive_sensor_data() override
Definition: osi_transceiver_tcp.hpp:52
Definition: osi_transceiver.hpp:50