discover-thread.cc
Go to the documentation of this file.
1 /*
2  * rcdiscover - the network discovery tool for Roboception devices
3  *
4  * Copyright (c) 2017 Roboception GmbH
5  * All rights reserved
6  *
7  * Author: Raphael Schaller
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the copyright holder nor the names of its contributors
20  * may be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 // placed here to make sure to include winsock2.h before windows.h
37 #include "rcdiscover/discover.h"
38 #include "rcdiscover/ping.h"
39 
40 #include "discover-thread.h"
41 
42 #include "discover-frame.h"
43 #include "event-ids.h"
44 #include "rcdiscover/utils.h"
45 
46 #include <vector>
47 #include <algorithm>
48 #include <future>
49 #include <chrono>
50 
51 #include <wx/window.h>
52 
53 wxThread::ExitCode DiscoverThread::Entry()
54 {
55  std::vector<wxVector<wxVariant>> device_list;
56 
57  try
58  {
59  rcdiscover::Discover discover;
60  discover.broadcastRequest();
61 
62  std::chrono::steady_clock::time_point tstart=std::chrono::steady_clock::now();
63  std::chrono::steady_clock::time_point tend=tstart;
64 
65  std::vector<rcdiscover::DeviceInfo> infos;
66 
67  while (discover.getResponse(infos, 100) ||
68  std::chrono::duration<double, std::milli>(tend-tstart).count() < 1000)
69  {
70  tend=std::chrono::steady_clock::now();
71  }
72 
73  std::sort(infos.begin(), infos.end());
74  const auto it = std::unique(infos.begin(), infos.end(),
75  [](const rcdiscover::DeviceInfo &lhs,
76  const rcdiscover::DeviceInfo &rhs)
77  {
78  return lhs.getMAC() == rhs.getMAC() &&
79  lhs.getIfaceName() ==
80  rhs.getIfaceName();
81  });
82  infos.erase(it, infos.end());
83 
84  std::vector<std::future<bool>> reachable(infos.size());
85  std::transform(
86  infos.begin(), infos.end(), reachable.begin(),
87  [](const rcdiscover::DeviceInfo &info) {
88  return info.isValid()
89  ? std::async(
90  std::launch::async,
91  [&info] { return checkReachabilityOfSensor(info); })
92  : std::future<bool>{};
93  });
94 
95  const rcdiscover::DeviceInfo *last_info = nullptr;
96 
97  size_t i = 0;
98  for (auto info_it = infos.cbegin(); info_it != infos.cend(); ++info_it, ++i)
99  {
100  const auto& info = *info_it;
101  if (!info.isValid())
102  {
103  continue;
104  }
105 
106  if (last_info && last_info->getMAC() == info.getMAC())
107  {
108  device_list.back()[DiscoverFrame::IFACE] = wxVariant(
109  device_list.back()[DiscoverFrame::IFACE].GetString() + ',' +
110  info.getIfaceName());
111  continue;
112  }
113 
114  last_info = &info;
115 
116  const std::string &name = info.getUserName().empty()
117  ? info.getModelName()
118  : info.getUserName();
119 
120  const std::string &manufacturer = info.getManufacturerName();
121 
122  wxVector<wxVariant> data(DiscoverFrame::NUM_COLUMNS);
123  data[DiscoverFrame::NAME] = wxVariant(name);
124  data[DiscoverFrame::MANUFACTURER] = wxVariant(manufacturer);
125  data[DiscoverFrame::MODEL] = wxVariant(info.getModelName());
126  data[DiscoverFrame::SERIAL] = wxVariant(info.getSerialNumber());
127  data[DiscoverFrame::IP] = wxVariant(ip2string(info.getIP()));
128  data[DiscoverFrame::MAC] = wxVariant(mac2string(info.getMAC()));
129  data[DiscoverFrame::IFACE] = wxVariant(info.getIfaceName());
130  data[DiscoverFrame::REACHABLE] = wxVariant(
131  reachable[i].get() ? L"\u2713" : L"\u2717");
132 
133  device_list.push_back(std::move(data));
134  }
135  }
136  catch (const std::exception &ex)
137  {
138  wxThreadEvent event(wxEVT_COMMAND_DISCOVERY_ERROR);
139  event.SetString(ex.what());
140  parent_->GetEventHandler()->QueueEvent(event.Clone());
141 
142  return ExitCode(1);
143  }
144 
145  wxThreadEvent event(wxEVT_COMMAND_DISCOVERY_COMPLETED);
146  event.SetPayload(device_list);
147  parent_->GetEventHandler()->QueueEvent(event.Clone());
148 
149  return ExitCode(0);
150 }
const std::string & getIfaceName() const
Returns the name of the interface on which this device was found.
Definition: deviceinfo.h:180
std::string ip2string(const uint32_t ip)
Definition: utils.h:57
uint64_t getMAC() const
Returns 6 bytes with the MAC address of the device.
Definition: deviceinfo.h:96
void broadcastRequest()
Broadcasts a discovery command request.
Definition: discover.cc:85
std::string mac2string(const uint64_t mac)
Definition: utils.h:45
virtual ExitCode Entry() override
wxWindow * parent_
bool getResponse(std::vector< DeviceInfo > &info, int timeout_per_socket=1000)
Returns a discovery response.
Definition: discover.cc:107
const std::string & getUserName() const
Returns the user name.
Definition: deviceinfo.h:168
bool checkReachabilityOfSensor(const DeviceInfo &info)
Check whether an device is reachable via ICMP.
Definition: ping.cc:95


rcdiscover
Author(s): Heiko Hirschmueller , Raphael Schaller
autogenerated on Sat Oct 22 2022 02:52:17