udp_poster.cpp
Go to the documentation of this file.
1 /*
2  * @brief udp_poster implements udp posts to start and stop multiScan136.
3  *
4  * Copyright (C) 2020,2021 Ing.-Buero Dr. Michael Lehning, Hildesheim
5  * Copyright (C) 2020,2021 SICK AG, Waldkirch
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * All rights reserved.
20  *
21  * Redistribution and use in source and binary forms, with or without
22  * modification, are permitted provided that the following conditions are met:
23  *
24  * * Redistributions of source code must retain the above copyright
25  * notice, this list of conditions and the following disclaimer.
26  * * Redistributions in binary form must reproduce the above copyright
27  * notice, this list of conditions and the following disclaimer in the
28  * documentation and/or other materials provided with the distribution.
29  * * Neither the name of SICK AG nor the names of its
30  * contributors may be used to endorse or promote products derived from
31  * this software without specific prior written permission
32  * * Neither the name of Ing.-Buero Dr. Michael Lehning nor the names of its
33  * contributors may be used to endorse or promote products derived from
34  * this software without specific prior written permission
35  *
36  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
37  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
40  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
41  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
42  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
43  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
44  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
46  * POSSIBILITY OF SUCH DAMAGE.
47  *
48  * Authors:
49  * Michael Lehning <michael.lehning@lehning.de>
50  *
51  * Copyright 2020 SICK AG
52  * Copyright 2020 Ing.-Buero Dr. Michael Lehning
53  *
54  */
57 
58 /*
59  * @brief Default constructor.
60  * @param[in] ip ip address of a multiScan136, f.e. "127.0.0.1" (localhost, loopback) for an emulator or "192.168.0.1" for multiScan136
61  * @param[in] udp_port ip port, f.e. 2115 (default port for multiScan136 emulator)
62  */
64 : m_ip(ip), m_port(udp_port), m_sender_impl(0), m_receiver_impl(0)
65 {
67  if (!m_sender_impl->IsOpen())
68  {
69  ROS_ERROR_STREAM("## ERROR UdpPoster::Init(): can't open socket, UdpSenderSocketImpl(" << m_ip << "," << m_port << ") failed.");
70  delete m_sender_impl;
71  m_sender_impl = 0;
72  }
75  {
76  ROS_ERROR_STREAM("## ERROR UdpPoster::Init(): can't open socket, UdpReceiverSocketImpl::Init(" << m_ip << "," << m_port << ") failed.");
77  delete m_receiver_impl;
78  m_receiver_impl = 0;
79  }
80 }
81 
82 /*
83  * @brief Default destructor.
84  */
86 {
87  if (m_sender_impl)
88  {
89  delete m_sender_impl;
90  m_sender_impl = 0;
91  }
92  if (m_receiver_impl)
93  {
94  delete m_receiver_impl;
95  m_receiver_impl = 0;
96  }
97 }
98 
99 /*
100  * @brief Returns the ip address to send udp messages.
101  */
102 const std::string& sick_scansegment_xd::UdpPoster::IP(void) const
103 {
104  return m_ip;
105 }
106 
107 /*
108  * @brief Returns the port to send udp messages.
109  */
111 {
112  return m_port;
113 }
114 
115 /*
116  * @brief Posts a request message.
117  * @param[in] request message to send
118  * @param[in] response message received
119  * @return true on success, otherwise false
120  */
121 bool sick_scansegment_xd::UdpPoster::Post(const std::string& request, std::string& response)
122 {
123  if (!m_sender_impl)
124  {
125  ROS_ERROR_STREAM("## ERROR UdpPoster::Post(): udp sender socket not initialized");
126  return false;
127  }
128  std::vector<uint8_t> request_data(request.begin(), request.end());
129  if (!m_sender_impl->Send(request_data))
130  {
131  ROS_ERROR_STREAM("## ERROR UdpPoster::Post(): failed to send " << request_data.size() << " byte message \"" << request << "\"");
132  return false;
133  }
134  if (!m_receiver_impl)
135  {
136  ROS_ERROR_STREAM("## ERROR UdpPoster::Post(): udp receiver socket not initialized");
137  return false;
138  }
139  std::vector<uint8_t> response_data(1024, 0);
140  size_t byte_received = m_receiver_impl->Receive(response_data);
141  response_data.resize(byte_received);
142  response = std::string(response_data.begin(), response_data.end());
143  return true; // todo
144 }
response
const std::string response
sick_scansegment_xd::UdpPoster::m_sender_impl
UdpSenderSocketImpl * m_sender_impl
Definition: udp_poster.h:122
udp_sockets.h
sick_scansegment_xd::UdpPoster::m_receiver_impl
UdpReceiverSocketImpl * m_receiver_impl
Definition: udp_poster.h:123
sick_scansegment_xd::UdpPoster::m_ip
std::string m_ip
Definition: udp_poster.h:120
sick_scansegment_xd::UdpPoster::UdpPoster
UdpPoster(const std::string &ip="192.168.0.1", int udp_port=2115)
Definition: udp_poster.cpp:63
udp_poster.h
sick_scansegment_xd::UdpSenderSocketImpl::IsOpen
bool IsOpen(void) const
Definition: udp_sockets.h:336
sick_scansegment_xd::UdpPoster::IP
const std::string & IP(void) const
Definition: udp_poster.cpp:102
multiscan_pcap_player.udp_port
int udp_port
Definition: multiscan_pcap_player.py:137
sick_scansegment_xd::UdpPoster::~UdpPoster
~UdpPoster()
Definition: udp_poster.cpp:85
sick_scansegment_xd::UdpReceiverSocketImpl::Init
bool Init(const std::string &udp_sender, int udp_port, bool blocking=false)
Definition: udp_sockets.h:139
sick_scansegment_xd::UdpPoster::Port
const int & Port(void) const
Definition: udp_poster.cpp:110
sick_scansegment_xd::UdpPoster::Post
bool Post(const std::string &request, std::string &response)
Definition: udp_poster.cpp:121
ROS_ERROR_STREAM
#define ROS_ERROR_STREAM(...)
Definition: sick_scan_ros2_example.cpp:72
sick_scansegment_xd::UdpPoster::m_port
int m_port
Definition: udp_poster.h:121
sick_scansegment_xd::UdpSenderSocketImpl
class UdpSenderSocketImpl implements the udp socket for sending udp packages
Definition: udp_sockets.h:272
sick_scansegment_xd::UdpReceiverSocketImpl
Definition: udp_sockets.h:106


sick_scan_xd
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Fri Oct 25 2024 02:47:12