mock_udp_server.h
Go to the documentation of this file.
1 // Copyright (c) 2019-2020 Pilz GmbH & Co. KG
2 //
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU Lesser General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public License
14 // along with this program. If not, see <https://www.gnu.org/licenses/>.
15 
16 #ifndef PSEN_SCAN_TEST_MOCK_UDP_SERVER_H
17 #define PSEN_SCAN_TEST_MOCK_UDP_SERVER_H
18 
19 #include <boost/asio.hpp>
20 #include <boost/bind.hpp>
21 #include <boost/array.hpp>
22 #include <boost/thread.hpp>
23 
24 #include <gtest/gtest.h>
25 #include <gmock/gmock.h>
26 
27 #include <psen_scan/scanner_data.h>
28 
29 using boost::asio::ip::udp;
30 
31 namespace psen_scan_test
32 {
38 {
39 public:
41 
42 public:
43  MockUDPServer(const unsigned short scanner_port_write = psen_scan::PSEN_SCAN_PORT_WRITE,
44  const unsigned short scanner_port_read = psen_scan::PSEN_SCAN_PORT_READ);
45  MOCK_CONST_METHOD0(receivedUdpMsg, void());
46 
47 public:
48  void startIOService();
49 
50  void asyncReceive();
51 
52  template <unsigned int N>
53  void asyncSend(const udp::endpoint& send_endpoint, const boost::array<char, N>& send_buffer);
54 
55 private:
56  void handleReceive(const boost::system::error_code& error, std::size_t /*bytes_transferred*/);
57  void handleSend(const boost::system::error_code& /*error*/, std::size_t /*bytes_transferred*/);
58 
59 private:
60  udp::endpoint remote_endpoint_;
61 
62  boost::array<char, 100> recv_buffer_;
63 
64  boost::asio::io_service io_service_;
65  boost::thread service_thread_;
66 
67  udp::socket socket_receive_;
68  udp::socket socket_send_;
69 };
70 
72 {
73  service_thread_ = boost::thread(boost::bind(&boost::asio::io_service::run, &io_service_));
74 }
75 
76 inline MockUDPServer::MockUDPServer(const unsigned short scanner_port_write, const unsigned short scanner_port_read)
77  : socket_receive_(io_service_, udp::endpoint(udp::v4(), scanner_port_write))
78  , socket_send_(io_service_, udp::endpoint(udp::v4(), scanner_port_read))
79 {
80 }
81 
83 {
84  socket_receive_.close();
85  socket_send_.close();
86 
87  service_thread_.join();
88 }
89 
90 inline void MockUDPServer::handleSend(const boost::system::error_code& /*error*/, std::size_t /*bytes_transferred*/)
91 {
92 }
93 
94 template <unsigned int N>
95 inline void MockUDPServer::asyncSend(const udp::endpoint& send_endpoint, const boost::array<char, N>& send_buffer)
96 {
97  socket_send_.async_send_to(boost::asio::buffer(send_buffer),
98  send_endpoint,
99  boost::bind(&MockUDPServer::handleSend,
100  this,
101  boost::asio::placeholders::error,
102  boost::asio::placeholders::bytes_transferred));
103 }
104 
105 inline void MockUDPServer::handleReceive(const boost::system::error_code& error, std::size_t /*bytes_transferred*/)
106 {
107  receivedUdpMsg();
108 }
109 
111 {
112  socket_receive_.async_receive_from(boost::asio::buffer(recv_buffer_),
114  boost::bind(&MockUDPServer::handleReceive,
115  this,
116  boost::asio::placeholders::error,
117  boost::asio::placeholders::bytes_transferred));
118 }
119 
120 } // namespace psen_scan_test
121 
122 #endif // PSEN_SCAN_TEST_MOCK_UDP_SERVER_H
MockUDPServer(const unsigned short scanner_port_write=psen_scan::PSEN_SCAN_PORT_WRITE, const unsigned short scanner_port_read=psen_scan::PSEN_SCAN_PORT_READ)
boost::array< char, 100 > recv_buffer_
void asyncSend(const udp::endpoint &send_endpoint, const boost::array< char, N > &send_buffer)
boost::asio::io_service io_service_
unsigned short const PSEN_SCAN_PORT_WRITE
Definition: scanner_data.h:52
void handleReceive(const boost::system::error_code &error, std::size_t)
void handleSend(const boost::system::error_code &, std::size_t)
MOCK_CONST_METHOD0(receivedUdpMsg, void())
unsigned short const PSEN_SCAN_PORT_READ
Definition: scanner_data.h:53
Class for the UDP communication with the scanner.


psen_scan
Author(s):
autogenerated on Mon Feb 28 2022 23:16:20