VLink 2.0.0
A high-performance communication middleware
Loading...
Searching...
No Matches
discovery_reporter.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_reporter.h
26 * @brief Process-level discovery reporter that broadcasts node metadata to DiscoveryViewer instances.
27 *
28 * @details
29 * @c DiscoveryReporter runs as a background @c MessageLoop and periodically reports the
30 * list of active @c NodeImpl endpoints (publishers, subscribers, clients, servers, etc.)
31 * to any @c DiscoveryViewer instances on the same host or network.
32 *
33 * Internally it:
34 * 1. Collects all registered @c NodeImpl objects.
35 * 2. Serialises their metadata (URL, type, process info, CPU profiler data) into a discovery message.
36 * 3. Sends the message via UDP multicast/broadcast (default address @c 239.255.0.100).
37 * 4. Sends an offline notification on destruction.
38 *
39 * A process-global singleton is available via @c global_get(); it is created on first use
40 * and destroyed with the process.
41 *
42 * @note
43 * - @c add() and @c remove() are called automatically by @c NodeImpl constructors and destructors.
44 * - Users generally do not need to interact with @c DiscoveryReporter directly.
45 * - The reporter uses UDP multicast/broadcast for discovery, not any VLink transport backend.
46 */
47
48#pragma once
49
50#include <memory>
51#include <string>
52
53#include "../base/macros.h"
55
56namespace vlink {
57
58class NodeImpl;
59
60/**
61 * @class DiscoveryReporter
62 * @brief Background @c MessageLoop that reports active nodes to the discovery subsystem.
63 *
64 * @details
65 * Automatically started and stopped by the VLink runtime. Callers should not need to
66 * manage this object directly unless building custom tooling.
67 */
69 public:
70 /**
71 * @brief Returns the process-global @c DiscoveryReporter singleton.
72 *
73 * @details
74 * Created on first call. The singleton is destroyed when the process exits.
75 *
76 * @return Raw pointer to the global @c DiscoveryReporter.
77 */
79
80 /**
81 * @brief Constructs the reporter and starts its background loop.
82 */
84
85 /**
86 * @brief Destructor -- sends an offline notification and stops the loop.
87 */
89
90 /**
91 * @brief Registers a @c NodeImpl endpoint for periodic reporting.
92 *
93 * @details
94 * Called automatically by @c NodeImpl on construction.
95 *
96 * @param node Node to add.
97 */
98 void add(NodeImpl* node);
99
100 /**
101 * @brief Unregisters a @c NodeImpl endpoint from periodic reporting.
102 *
103 * @details
104 * Called automatically by @c NodeImpl on destruction.
105 *
106 * @param node Node to remove.
107 */
108 void remove(NodeImpl* node);
109
110 protected:
111 size_t get_max_task_count() const override;
112
113 uint32_t get_max_elapsed_time() const override;
114
115 void on_begin() override;
116
117 void on_end() override;
118
119 private:
120 void rebuild_message();
121
122 void send_report();
123
124 void send_offline();
125
126 static const std::string& get_host_name();
127
128 static const std::string& get_app_name();
129
130 std::unique_ptr<struct DiscoveryReporterImpl> impl_;
131
133};
134
135} // 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.