VLink  2.0.0
A high-performance communication middleware
discovery_reporter.h
浏览该文件的文档.
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_reporter.h
26  * @brief Process-wide discovery broadcaster that advertises live VLink endpoints.
27  *
28  * @details
29  * @c DiscoveryReporter is the announce side of the VLink discovery subsystem. It runs
30  * on a private @c MessageLoop and emits periodic UDP multicast (default @c 239.255.0.100)
31  * payloads that describe every @c NodeImpl currently registered in the process: their
32  * URLs, communication kind, host name, PID, application name and an optional CPU usage
33  * sample. Listeners on the network -- typically @c DiscoveryViewer instances or the
34  * @c vlink-cli tool -- consume those payloads to rebuild a live endpoint topology.
35  *
36  * Discovery flow:
37  *
38  * @verbatim
39  * NodeImpl::init_ext() ----> DiscoveryReporter::add()
40  * |
41  * v
42  * +-----------+ periodic timer UDP multicast
43  * | loop | ------------------> 239.255.0.100
44  * +-----------+
45  * ^ |
46  * | v
47  * NodeImpl::deinit_ext() ----> remove() DiscoveryViewer
48  * @endverbatim
49  *
50  * The process reporter is owned by the VLink runtime and is not exposed as a public
51  * singleton. @c add() and @c remove() are invoked automatically by
52  * @c NodeImpl::init_ext() / @c NodeImpl::deinit_ext(); normal user code should rarely
53  * touch this class directly.
54  *
55  * @par Example
56  * @code
57  * // Disable discovery entirely:
58  * // export VLINK_DISCOVER_DISABLE=1
59  *
60  * // Advanced tooling may construct an explicit reporter instance.
61  * vlink::DiscoveryReporter reporter;
62  * reporter.async_run();
63  * @endcode
64  *
65  * @note Discovery rides on a dedicated UDP socket and is independent of any VLink
66  * transport backend (intra/shm/dds/zenoh/...). Set @c VLINK_DISCOVER_DISABLE=1 to
67  * disable the runtime-owned reporter for a given process.
68  */
69 
70 #pragma once
71 
72 #include <memory>
73 #include <string>
74 
75 #include "../base/macros.h"
76 #include "../base/message_loop.h"
77 
78 namespace vlink {
79 
80 class NodeImpl;
81 
82 /**
83  * @class DiscoveryReporter
84  * @brief Periodic multicast broadcaster of the process's active VLink endpoints.
85  */
87  public:
88  /**
89  * @brief Builds the reporter, opens its UDP socket and arms the periodic timer.
90  *
91  * @details
92  * The @c MessageLoop is left idle; callers are responsible for calling @c async_run().
93  * The runtime-owned process reporter does this automatically.
94  */
96 
97  /**
98  * @brief Stops the loop and releases the UDP socket.
99  *
100  * @details
101  * An offline notification is only emitted when offline reporting is compiled in; the
102  * default build omits it.
103  */
104  ~DiscoveryReporter() override;
105 
106  /**
107  * @brief Registers a node so that subsequent broadcasts include it.
108  *
109  * @details
110  * Invoked automatically by @c NodeImpl::init_ext().
111  *
112  * @param node Node endpoint to track.
113  */
114  void add(NodeImpl* node);
115 
116  /**
117  * @brief Unregisters a node so that subsequent broadcasts exclude it.
118  *
119  * @details
120  * Invoked automatically by @c NodeImpl::deinit_ext().
121  *
122  * @param node Node endpoint to drop.
123  */
124  void remove(NodeImpl* node);
125 
126  protected:
127  size_t get_max_task_count() const override;
128 
129  uint32_t get_max_elapsed_time() const override;
130 
131  void on_begin() override;
132 
133  void on_end() override;
134 
135  private:
136  void rebuild_message();
137 
138  void send_report();
139 
140  void send_offline();
141 
142  static const std::string& get_host_name();
143 
144  static const std::string& get_app_name();
145 
146  struct Impl;
147  std::unique_ptr<Impl> impl_;
148 
150 };
151 
152 } // namespace vlink
#define VLINK_EXPORT
Definition: macros.h:81
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
Deletes the copy constructor and copy-assignment operator of classname.
Definition: macros.h:174