DESERT 3.5.1
Loading...
Searching...
No Matches
position_listener.h
Go to the documentation of this file.
1//
2// Copyright (c) 2017 Regents of the SIGNET lab, University of Padova.
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions
7// are met:
8// 1. Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright
11// notice, this list of conditions and the following disclaimer in the
12// documentation and/or other materials provided with the distribution.
13// 3. Neither the name of the University of Padova (SIGNET lab) nor the
14// names of its contributors may be used to endorse or promote products
15// derived from this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28//
29
39#ifndef _POSITION_LISTENER_H_
40#define _POSITION_LISTENER_H_
41
42#include <unistd.h>
43#include <sys/socket.h>
44#include <sys/time.h>
45#include <arpa/inet.h>
46#include <netinet/in.h>
47#define SOCKET_TYPE int
48#define SET_SOCKOPT_TYPE void *
49#define SENDTO_TYPE const char *
50#define SOCKET_ERROR -1
51
52#include <cstring>
53#include <stdexcept>
54#include <vector>
55
56#include "stoppable_thread.h"
57#include "position_data.h"
58#include "logging.h"
59
63template<typename Owner>
65{
66public:
67 PositionListener(Owner *owner, uint16_t port, timeval read_timeout)
68 {
69 p_Owner = owner;
70 m_Port = port;
71 m_ReadTimeout = read_timeout;
72 }
74 {
75 if (m_SocketFD)
76 ::close(m_SocketFD);
77 }
79 virtual void Run()
80 {
81 try
82 {
83 if (p_Owner->debugLevel() > 0) LOG_MSG_INFO("Starting position data listener on port " << m_Port);
84 // set up socket....
85 m_SocketFD = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
86 if (m_SocketFD < 0)
87 throw std::runtime_error("PositionListener::ListenLoop()::socket()");
88
89 // allow to reuse the address
90 int reuse = 1;
91 if (setsockopt(m_SocketFD, SOL_SOCKET, SO_REUSEADDR, (SET_SOCKOPT_TYPE)&reuse, sizeof(reuse)) == -1)
92 throw std::runtime_error("PositionListener::ListenLoop::setsockopt::reuse");
93
94 // setup receive buffer to be large enough
95 int rx_buffer_size = 1024;
96 if (setsockopt(m_SocketFD, SOL_SOCKET, SO_RCVBUF, (SET_SOCKOPT_TYPE)&rx_buffer_size, sizeof(rx_buffer_size)) == -1)
97 throw std::runtime_error("PositionListener::ListenLoop()::setsockopt::rcvbuf");
98
99 // construct a datagram address structure
100 struct sockaddr_in dg_addr;
101 memset(&dg_addr, 0, sizeof(dg_addr));
102 dg_addr.sin_family = AF_INET;
103 // listen on any address
104 dg_addr.sin_addr.s_addr = htonl(INADDR_ANY);
105 dg_addr.sin_port = htons(m_Port);
106
107 if (bind(m_SocketFD, (struct sockaddr *)&dg_addr, sizeof(dg_addr)) == -1)
108 throw std::runtime_error("PositionListener::ListenLoop()::bind");
109
110 // reserve receive buffer, make it large enough (2 * serialized size for now)
111 PositionData pd;
112 std::vector<char> incoming_buffer(pd.size() * 2);
113 while (!StopRequested())
114 {
115 if (ReadyToRead())
116 {
117 if (p_Owner->debugLevel() >= 3)
118 LOG_MSG_INFO("Trying to read data from peer");
119 int num_bytes_read = recvfrom(m_SocketFD, (char *)incoming_buffer.data(), (int)incoming_buffer.size(), 0, 0, 0);
120 if (num_bytes_read < 0)
121 {
122 LOG_MSG_ERROR("Error reading from UDP port: " << num_bytes_read);
123 continue;
124 }
125 if (p_Owner->debugLevel() >= 3)
126 LOG_MSG_INFO("Received " << num_bytes_read << " bytes from peer");
127 try
128 {
129 pd.deserialize(incoming_buffer.data(), incoming_buffer.size());
130 p_Owner->setPosition(pd);
131 }
132 catch (const std::exception &e)
133 {
134 LOG_MSG_ERROR("Caught exception while reading position data: " << e.what() << " - " << std::strerror(errno));
135 }
136 }
137 }
138 if (p_Owner->debugLevel() > 0) LOG_MSG_INFO("Stopping position data listener on port " << m_Port);
139 }
140 catch (const std::exception &e)
141 {
142 LOG_MSG_ERROR("Caught exception in listening thread: " << e.what() << " - " << std::strerror(errno));
143 }
144 }
145protected:
150 {
151 int nfds;
152 fd_set fdset;
153
154 FD_ZERO(&fdset);
155 FD_SET(m_SocketFD, &fdset);
156
157 nfds = (int)m_SocketFD;
158 int ret = select(nfds + 1, &fdset, NULL, NULL, &m_ReadTimeout);
159 if (ret == SOCKET_ERROR)
160 {
161 LOG_MSG_ERROR("Error on select: " << ret);
162 }
163 return ret == 1;
164 }
170 uint16_t m_Port;
172 Owner *p_Owner;
173};
174
175#endif // _POSITION_LISTENER_H_
Position listener thread with UDP socket.
bool ReadyToRead()
Uses select() to do a timed wait for new data.
virtual void Run()
Thread function, runs until thread is stopped.
virtual ~PositionListener()
SOCKET_TYPE m_SocketFD
Socket descriptor.
timeval m_ReadTimeout
Timeout for the select call in ReadyToRead()
uint16_t m_Port
UDP port number to read position from.
Owner * p_Owner
Owner instance.
PositionListener(Owner *owner, uint16_t port, timeval read_timeout)
A stoppable C++11 thread implementation.
bool StopRequested()
Returns if a stop was requested.
Provides the definition of PositionData struct.
#define LOG_MSG_INFO(msg)
Definition logging.h:75
#define LOG_MSG_ERROR(msg)
Definition logging.h:77
Provides the definition of PositionData struct.
#define SOCKET_ERROR
#define SET_SOCKOPT_TYPE
#define SOCKET_TYPE
Provides a simple C++11 thread the StoppableThread class.
Position data structure for submitting node positions to DESERT in ENU coordinates.
constexpr size_t size() const
Compute required buffer size for (de-)serialization.
bool deserialize(char *buffer, size_t buffer_size)
Deserialize the position data from the buffer.