VLink 2.0.0
A high-performance communication middleware
Loading...
Searching...
No Matches
discovery_viewer.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 by Thun Lu. All rights reserved.
3 * Author: Thun Lu <thun.lu@zohomail.cn>
4 * Repo: https://github.com/thun-res/vlink
5 * _ __ __ _ __
6 * | | / / / / (_) ____ / /__
7 * | | / / / / / / / __ \ / //_/
8 * | |/ / / /___ / / / / / / / ,<
9 * |___/ /_____/ /_/ /_/ /_/ /_/|_|
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 */
23
24/**
25 * @file discovery_viewer.h
26 * @brief Real-time view of all active VLink endpoints discovered on the current host or network.
27 *
28 * @details
29 * @c DiscoveryViewer subscribes to @c DiscoveryReporter broadcasts and maintains a live
30 * list of all known VLink processes and their endpoints. It is used by the @c vlink-cli
31 * tool and by monitoring applications.
32 *
33 * Each entry in the @c Info list describes one topic URL and the set of processes that
34 * publish or subscribe to it. Entries are sorted for stable display.
35 *
36 * @par Filter modes
37 * | FilterType | Shows |
38 * | ---------------- | ------------------------------------------------------- |
39 * | kFilterNone | All discovered endpoints |
40 * | kFilterAvailable | Only endpoints that have at least one live process |
41 * | kFilterNative | Only endpoints from the same host |
42 *
43 * @par Usage
44 * @code
45 * vlink::DiscoveryViewer viewer(vlink::DiscoveryViewer::kFilterAvailable);
46 * viewer.register_callback([](const std::vector<vlink::DiscoveryViewer::Info>& list) {
47 * for (auto& info : list) {
48 * VLOG_I("URL: ", info.url, " type: ", info.type);
49 * }
50 * });
51 * viewer.async_run();
52 * @endcode
53 *
54 * @note
55 * - The callback is invoked on the viewer's event loop thread.
56 * - Use @c global_get() to share one viewer across the application.
57 * - Endpoints that do not send a heartbeat within a timeout are removed automatically.
58 */
59
60#pragma once
61
62#include <cstdint>
63#include <functional>
64#include <memory>
65#include <string>
66#include <vector>
67
68#include "../base/macros.h"
70#include "../impl/types.h"
71
72namespace vlink {
73
74/**
75 * @class DiscoveryViewer
76 * @brief Background @c MessageLoop that aggregates live VLink endpoint discovery data.
77 */
79 public:
80 /**
81 * @brief Controls which endpoints are included in the discovery view.
82 *
83 * | Value | Meaning |
84 * | ---------------- | ---------------------------------------------------- |
85 * | kFilterNone | All discovered endpoints |
86 * | kFilterAvailable | Endpoints with at least one live process |
87 * | kFilterNative | Endpoints from the local host only |
88 */
89 enum FilterType : uint8_t {
90 kFilterNone = 0, ///< Show all endpoints.
91 kFilterAvailable = 1, ///< Show only endpoints with live processes.
92 kFilterNative = 2, ///< Show only local-host endpoints.
93 };
94
95 /**
96 * @struct Process
97 * @brief Information about one process that hosts a VLink endpoint.
98 */
99 struct VLINK_EXPORT Process final {
100 uint32_t type{0}; ///< ImplType bitmask of all communication types in this process.
101 std::string host; ///< Hostname of the process.
102 uint32_t pid{0}; ///< Process ID.
103 std::string name; ///< Process name.
104 std::string ip; ///< IP address of the process host.
105 double profiler{-1}; ///< CPU utilisation reported by the process (-1 if unavailable).
106
107 /**
108 * @brief Sorts processes for stable display (by host, pid).
109 *
110 * @param target Right-hand side.
111 * @return @c true if @c *this should sort before @p target.
112 */
113 bool operator<(const Process& target) const noexcept;
114 };
115
116 /**
117 * @struct Info
118 * @brief Aggregated discovery entry for one VLink URL.
119 *
120 * @details
121 * Holds the URL, communication type bitmask, serialisation type, coarse schema family,
122 * and the list of processes that have registered an endpoint for this URL.
123 */
124 struct VLINK_EXPORT Info final {
125 int sort_index{-1}; ///< Stable sort key (assigned by the viewer).
126 uint32_t type{0}; ///< ImplType bitmask.
127 std::string url; ///< Full VLink URL string.
128 std::string ser_type; ///< Serialisation type (e.g., "demo.proto.PointCloud").
129 SchemaType schema_type{SchemaType::kUnknown}; ///< Coarse schema family derived from discovery metadata.
130 std::vector<Process> process_list; ///< Processes hosting this endpoint.
131
132 /**
133 * @brief Sorts entries for stable display (by sort_index, url).
134 *
135 * @param target Right-hand side.
136 * @return @c true if @c *this should sort before @p target.
137 */
138 bool operator<(const Info& target) const noexcept;
139 };
140
141 /**
142 * @brief Callback fired whenever the discovery information is updated.
143 *
144 * @details
145 * Invoked on the viewer's event loop thread. The vector is a snapshot of the
146 * current live endpoint list.
147 */
148 using Callback = std::function<void(const std::vector<Info>& info_list)>;
149
150 /**
151 * @brief Converts a transport string to the corresponding @c ImplType value.
152 *
153 * @param str Transport string (e.g., @c "dds", @c "shm").
154 * @return Corresponding @c ImplType, or 0 if unknown.
155 */
156 [[nodiscard]] static ImplType convert_type(std::string_view str);
157
158 /**
159 * @brief Returns a display string for an @c ImplType bitmask.
160 *
161 * @param type ImplType bitmask.
162 * @return Human-readable type string.
163 */
164 [[nodiscard]] static std::string convert_type_to_view(uint32_t type);
165
166 /**
167 * @brief Returns a combined type-and-process display string.
168 *
169 * @param type ImplType bitmask.
170 * @param process_list List of processes to include in the display.
171 * @return Combined display string.
172 */
173 [[nodiscard]] static std::string convert_type_to_view(uint32_t type, const std::vector<Process>& process_list);
174
175 /**
176 * @brief Returns the intra-process address used by the discovery subsystem.
177 *
178 * @return Discovery listen address string.
179 */
180 [[nodiscard]] static std::string get_listen_address();
181
182 /**
183 * @brief Returns the process-global @c DiscoveryViewer singleton.
184 *
185 * @details
186 * Created with @c kFilterNone on first call. The singleton is destroyed on process exit.
187 *
188 * @return Raw pointer to the global @c DiscoveryViewer.
189 */
191
192 /**
193 * @brief Constructs a @c DiscoveryViewer with the given filter type.
194 *
195 * @param type Controls which endpoints are included. Default: @c kFilterNone.
196 */
198
199 /**
200 * @brief Destructor -- stops the viewer loop.
201 */
203
204 /**
205 * @brief Registers the callback invoked when the endpoint list changes.
206 *
207 * @details
208 * Replaces any previously registered callback. The callback is invoked on the
209 * viewer's loop thread.
210 *
211 * @param callback Function receiving the updated @c Info list.
212 */
213 void register_callback(Callback&& callback);
214
215 /**
216 * @brief Returns a snapshot of the current discovery information.
217 *
218 * @return Copy of the live @c Info list at the time of the call.
219 */
220 [[nodiscard]] std::vector<Info> get_info_list();
221
222 /**
223 * @brief Returns the serialisation type string for a given URL.
224 *
225 * @param url Topic URL to look up.
226 * @return Serialisation type (e.g., @c "demo.proto.PointCloud"), or empty if not known.
227 */
228 [[nodiscard]] std::string get_ser_type(const std::string& url) const;
229
230 /**
231 * @brief Returns the coarse schema family for a given URL.
232 *
233 * @param url Topic URL to look up.
234 * @return Schema family, or @c SchemaType::kUnknown if not known.
235 */
236 [[nodiscard]] SchemaType get_schema_type(const std::string& url) const;
237
238 protected:
239 size_t get_max_task_count() const override;
240
241 uint32_t get_max_elapsed_time() const override;
242
243 void on_begin() override;
244
245 void on_end() override;
246
247 private:
248 void process_timeout();
249
250 void process_offline(std::string_view hostname, uint32_t pid, std::string_view process_name);
251
252 void sort_url();
253
254 void report_list();
255
256 std::unique_ptr<struct DiscoveryViewerImpl> impl_;
257
259};
260
261} // namespace vlink
Platform-independent macro definitions for the VLink library.
#define VLINK_EXPORT
Definition macros.h:85
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
Deletes the copy constructor and copy-assignment operator of classname.
Definition macros.h:184
Single-threaded event loop with three queue types, timer management and task scheduling.
Core type definitions shared across all VLink node implementations.