source: pacpussensors/trunk/Vislab/20150310_lib3dv-1.2.0/src/utils.cc

Last change on this file was 136, checked in by ldecherf, 8 years ago

Doc

File size: 5.9 KB
Line 
1/* 3dv-client/utils.cc
2 *
3 * Copyright (C) 2013 VisLab
4 *
5 * This file is part of lib3dv; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "utils.h"
20
21#ifdef __GNUC__
22
23#include <errno.h>
24#include <arpa/inet.h>
25#include <sys/socket.h>
26#include <netdb.h>
27#include <ifaddrs.h>
28#include <string.h>
29#include <stdlib.h>
30#include <unistd.h>
31
32#include <iostream>
33
34std::vector<boost::asio::ip::address> list_interface_addresses()
35{
36 std::vector<boost::asio::ip::address> result;
37
38 ifaddrs* ifaddr;
39
40 if(getifaddrs(&ifaddr) == -1)
41 {
42 std::cerr << "[EE] 3dv-client: " << strerror(errno) << std::endl;
43 return result;
44 }
45
46 for(ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
47 {
48 if(ifa->ifa_addr == NULL)
49 continue;
50
51 int family = ifa->ifa_addr->sa_family;
52
53 if(family == AF_INET /*|| family == AF_INET6*/)
54 {
55 char host[NI_MAXHOST];
56
57 int s = getnameinfo(ifa->ifa_addr, (family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
58
59 if(s)
60 {
61 std::cerr << "[EE] 3dv-client: " << gai_strerror(s) << std::endl;
62 continue;
63 }
64
65 boost::asio::ip::address address = boost::asio::ip::address::from_string(host);
66
67 if(!address.is_loopback())
68 result.push_back(boost::asio::ip::address::from_string(host));
69 }
70 }
71
72 freeifaddrs(ifaddr);
73
74 return result;
75}
76
77#elif _WIN32
78
79#include <winsock2.h>
80#include <iphlpapi.h>
81
82std::vector<boost::asio::ip::address> list_interface_addresses()
83{
84 std::vector<boost::asio::ip::address> result;
85 IP_ADAPTER_ADDRESSES* adapter_addresses(NULL);
86 IP_ADAPTER_ADDRESSES* adapter(NULL);
87
88 // Start with a 16 KB buffer and resize if needed -
89 // multiple attempts in case interfaces change while
90 // we are in the middle of querying them.
91 DWORD adapter_addresses_buffer_size = 16 * 1024;
92 for(int attempts = 0; attempts != 3; ++attempts)
93 {
94 adapter_addresses = (IP_ADAPTER_ADDRESSES*)malloc(adapter_addresses_buffer_size);
95 assert(adapter_addresses);
96
97 DWORD error = ::GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_FRIENDLY_NAME, NULL, adapter_addresses, &adapter_addresses_buffer_size);
98
99 if(error == ERROR_SUCCESS)
100 break;
101 else if(error == ERROR_BUFFER_OVERFLOW)
102 {
103 // Try again with the new size
104 free(adapter_addresses);
105 adapter_addresses = NULL;
106 continue;
107 }
108 }
109
110 // Iterate through all of the adapters
111 for(adapter = adapter_addresses; NULL != adapter; adapter = adapter->Next)
112 {
113 // Skip loopback adapters
114 if (IF_TYPE_SOFTWARE_LOOPBACK == adapter->IfType)
115 continue;
116
117 // Parse all IPv4 addresses
118 for(IP_ADAPTER_UNICAST_ADDRESS* address = adapter->FirstUnicastAddress; address != NULL; address = address->Next)
119 {
120 if(address->Address.lpSockaddr->sa_family == AF_INET)
121 {
122 SOCKADDR_IN* ipv4 = reinterpret_cast<SOCKADDR_IN*>(address->Address.lpSockaddr);
123 result.push_back(boost::asio::ip::address_v4(ntohl(ipv4->sin_addr.s_addr)));
124 }
125 }
126 }
127
128 free(adapter_addresses);
129 adapter_addresses = NULL;
130
131 return result;
132}
133
134#endif
135
136#ifdef PCL
137
138#include "display.h"
139
140void image_callback_display(boost::shared_ptr<const lib3dv::image> image, uint32_t guid, display& display, uint8_t log_level)
141{
142 display.update(image);
143}
144
145void terrain_callback_display(boost::shared_ptr<const lib3dv::terrain> terrain, uint32_t guid, display& display, uint8_t log_level)
146{
147 display.update(terrain);
148}
149
150void obstacles_callback_display(boost::shared_ptr<const std::vector<lib3dv::obstacle> > obstacles, uint32_t guid, display& display, uint8_t log_level)
151{
152 display.update(obstacles);
153}
154
155void motion_callback_display(boost::shared_ptr<const lib3dv::motion> motion, uint32_t guid, display& display, uint8_t log_level)
156{
157 display.update(motion);
158}
159
160void classification_callback_display(boost::shared_ptr<const lib3dv::classification> classificat, uint32_t guid, display& display, uint8_t log_level)
161{
162 display.update(classificat);
163}
164
165#endif
166
167#include "disk_writer.h"
168#include "display.h"
169
170#ifdef PCL
171void signal_handler(const boost::system::error_code& error, int signal_number, boost::shared_ptr<disk_writer>& disk_writer, boost::shared_ptr<display>& display, boost::asio::io_service& sighandler)
172{
173 std::cout << std::endl;
174
175 sighandler.stop();
176}
177
178void timeout_callback(boost::shared_ptr<disk_writer>& disk_writer, boost::shared_ptr<display>& display, boost::asio::io_service& sighandler)
179{
180 std::cout << std::endl << "[EE] 3dv-client: device error NETWORK_TIMEOUT" << std::endl;
181
182 sighandler.stop();
183}
184#else
185void signal_handler(const boost::system::error_code& error, int signal_number, boost::shared_ptr<disk_writer>& disk_writer, boost::asio::io_service& sighandler)
186{
187 std::cout << std::endl;
188
189 sighandler.stop();
190}
191
192void timeout_callback(boost::shared_ptr<disk_writer>& disk_writer, boost::asio::io_service& sighandler)
193{
194 std::cout << std::endl << "[EE] 3dv-client: device error NETWORK_TIMEOUT" << std::endl;
195
196 sighandler.stop();
197}
198#endif
Note: See TracBrowser for help on using the repository browser.