libe3 0.6.1
C++ library for E3AP RAN vendor integration
Loading...
Searching...
No Matches
dapp_subscription_state.hpp
Go to the documentation of this file.
1/**
2 * @file dapp_subscription_state.hpp
3 * @brief Per-instance state for the dApp role.
4 *
5 * Holds the dApp-assigned identifier (received from the RAN in the
6 * SetupResponse), the list of RAN functions the RAN advertised, and the
7 * subscriptions this dApp has actively negotiated.
8 *
9 * A dApp may hold MULTIPLE concurrent subscriptions — one per distinct
10 * RAN function. At most one subscription per (dapp, ran_function) pair,
11 * mirroring the RAN-side uniqueness in SubscriptionManager.
12 *
13 * This struct is held by E3Interface when config.role == E3Role::DAPP.
14 * It is the dApp-role counterpart of SubscriptionManager, which stays
15 * strictly RAN-side. Exactly one of `subscription_manager_` / `dapp_state_`
16 * is non-null on a given E3Interface instance.
17 *
18 * SPDX-FileCopyrightText: Copyright (c) 2026 Northeastern University
19 * SPDX-License-Identifier: Apache-2.0
20 */
21
22#ifndef LIBE3_DAPP_SUBSCRIPTION_STATE_HPP
23#define LIBE3_DAPP_SUBSCRIPTION_STATE_HPP
24
25#include "types.hpp"
26
27#include <chrono>
28#include <cstdint>
29#include <mutex>
30#include <optional>
31#include <unordered_map>
32#include <vector>
33
34namespace libe3 {
35
37public:
39
42
43 /**
44 * @brief Store the assignment from a SetupResponse.
45 * Called once after a positive SetupResponse arrives.
46 */
47 void record_setup_response(uint32_t dapp_id,
48 std::vector<RanFunctionDef> functions) {
49 std::lock_guard<std::mutex> lock(mu_);
50 assigned_dapp_id = dapp_id;
51 remote_ran_functions = std::move(functions);
52 }
53
54 /**
55 * @brief Forget the RAN's assignment and every subscription (session ended).
56 */
58 std::lock_guard<std::mutex> lock(mu_);
59 assigned_dapp_id.reset();
63 ran_function_expiry.clear();
64 }
65
66 /**
67 * @brief Record a successful SubscriptionResponse.
68 * Idempotent: re-recording the same (rf_id, sub_id) is fine.
69 *
70 * @param ran_function_id RAN function the subscription is for
71 * @param subscription_id Subscription id the RAN assigned
72 * @param expires_at When the subscriptionTime the dApp asked for runs out,
73 * unset for no limit. The RAN ends the subscription without a
74 * message, so the dApp forgets it too, see expire_subscriptions().
75 * Counted from the response, so it is never earlier than the RAN's.
76 */
78 uint32_t ran_function_id, uint32_t subscription_id,
79 std::optional<std::chrono::steady_clock::time_point> expires_at = std::nullopt) {
80 std::lock_guard<std::mutex> lock(mu_);
81 ran_function_to_subscription_id[ran_function_id] = subscription_id;
82 subscription_id_to_ran_function[subscription_id] = ran_function_id;
83 if (expires_at) {
84 ran_function_expiry[ran_function_id] = *expires_at;
85 } else {
86 ran_function_expiry.erase(ran_function_id);
87 }
88 }
89
90 /**
91 * @brief Forget every subscription whose time is up at `now`.
92 * @param now instant each subscription's deadline is compared against
93 * @return the RAN functions whose subscription was forgotten
94 */
95 std::vector<uint32_t> expire_subscriptions(std::chrono::steady_clock::time_point now) {
96 std::lock_guard<std::mutex> lock(mu_);
97 std::vector<uint32_t> expired;
98 for (auto it = ran_function_expiry.begin(); it != ran_function_expiry.end();) {
99 if (now < it->second) {
100 ++it;
101 continue;
102 }
103 const uint32_t rf_id = it->first;
104 auto sub_it = ran_function_to_subscription_id.find(rf_id);
105 if (sub_it != ran_function_to_subscription_id.end()) {
106 subscription_id_to_ran_function.erase(sub_it->second);
108 }
109 expired.push_back(rf_id);
110 it = ran_function_expiry.erase(it);
111 }
112 return expired;
113 }
114
115 /**
116 * @brief Remove the subscription for a given RAN function.
117 * @return true if a subscription was removed; false if none existed.
118 */
119 bool remove_subscription_by_rf(uint32_t ran_function_id) {
120 std::lock_guard<std::mutex> lock(mu_);
121 auto it = ran_function_to_subscription_id.find(ran_function_id);
122 if (it == ran_function_to_subscription_id.end()) {
123 return false;
124 }
125 const uint32_t sub_id = it->second;
128 ran_function_expiry.erase(ran_function_id);
129 return true;
130 }
131
132 /**
133 * @brief Remove the subscription with a given subscription ID.
134 * @return true if a subscription was removed; false if none existed.
135 */
136 bool remove_subscription_by_id(uint32_t subscription_id) {
137 std::lock_guard<std::mutex> lock(mu_);
138 auto it = subscription_id_to_ran_function.find(subscription_id);
139 if (it == subscription_id_to_ran_function.end()) {
140 return false;
141 }
142 const uint32_t rf_id = it->second;
145 ran_function_expiry.erase(rf_id);
146 return true;
147 }
148
149 /**
150 * @brief Snapshot of active subscription IDs.
151 */
152 std::vector<uint32_t> active_subscriptions() const {
153 std::lock_guard<std::mutex> lock(mu_);
154 std::vector<uint32_t> out;
155 out.reserve(subscription_id_to_ran_function.size());
156 for (const auto& [sub_id, rf_id] : subscription_id_to_ran_function) {
157 (void)rf_id;
158 out.push_back(sub_id);
159 }
160 return out;
161 }
162
163 /**
164 * @brief Snapshot of RAN function IDs this dApp is currently subscribed to.
165 */
166 std::vector<uint32_t> subscribed_ran_functions() const {
167 std::lock_guard<std::mutex> lock(mu_);
168 std::vector<uint32_t> out;
169 out.reserve(ran_function_to_subscription_id.size());
170 for (const auto& [rf_id, sub_id] : ran_function_to_subscription_id) {
171 (void)sub_id;
172 out.push_back(rf_id);
173 }
174 return out;
175 }
176
177 /**
178 * @brief Look up subscription ID for a RAN function.
179 */
180 std::optional<uint32_t> subscription_id_for(uint32_t ran_function_id) const {
181 std::lock_guard<std::mutex> lock(mu_);
182 auto it = ran_function_to_subscription_id.find(ran_function_id);
183 if (it == ran_function_to_subscription_id.end()) return std::nullopt;
184 return it->second;
185 }
186
187 // -----------------------------------------------------------------------
188 // Public state members (kept public for direct inspection in tests and
189 // for handlers that need to update multiple fields atomically inside
190 // E3Interface). Callers MUST hold `mu` when reading or writing these
191 // directly; the helpers above already take the lock.
192 // -----------------------------------------------------------------------
193
194 mutable std::mutex mu;
195
196 /// Assigned by the RAN in SetupResponse. nullopt until setup completes.
197 std::optional<uint32_t> assigned_dapp_id;
198
199 /// RAN functions advertised by the RAN in SetupResponse.
200 std::vector<RanFunctionDef> remote_ran_functions;
201
202 /// rf_id -> sub_id
203 std::unordered_map<uint32_t, uint32_t> ran_function_to_subscription_id;
204 /// sub_id -> rf_id (reverse, used for SubscriptionDelete)
205 std::unordered_map<uint32_t, uint32_t> subscription_id_to_ran_function;
206 /// rf_id -> when its subscriptionTime runs out; absent for no limit
207 std::unordered_map<uint32_t, std::chrono::steady_clock::time_point> ran_function_expiry;
208
209private:
210 // Single mutex; the public-facing `mu` (above) is an alias for
211 // documentation and direct lock_guard use from E3Interface. We keep
212 // a single instance so this->mu_ and the public this->mu refer to the
213 // same lock. Internal helpers use mu_.
214 std::mutex& mu_ = mu;
215};
216
217} // namespace libe3
218
219#endif // LIBE3_DAPP_SUBSCRIPTION_STATE_HPP
std::vector< RanFunctionDef > remote_ran_functions
RAN functions advertised by the RAN in SetupResponse.
std::optional< uint32_t > assigned_dapp_id
Assigned by the RAN in SetupResponse. nullopt until setup completes.
void clear_session()
Forget the RAN's assignment and every subscription (session ended).
DAppSubscriptionState & operator=(const DAppSubscriptionState &)=delete
void record_subscription(uint32_t ran_function_id, uint32_t subscription_id, std::optional< std::chrono::steady_clock::time_point > expires_at=std::nullopt)
Record a successful SubscriptionResponse. Idempotent: re-recording the same (rf_id,...
std::unordered_map< uint32_t, uint32_t > ran_function_to_subscription_id
rf_id -> sub_id
std::optional< uint32_t > subscription_id_for(uint32_t ran_function_id) const
Look up subscription ID for a RAN function.
std::vector< uint32_t > subscribed_ran_functions() const
Snapshot of RAN function IDs this dApp is currently subscribed to.
void record_setup_response(uint32_t dapp_id, std::vector< RanFunctionDef > functions)
Store the assignment from a SetupResponse. Called once after a positive SetupResponse arrives.
std::unordered_map< uint32_t, std::chrono::steady_clock::time_point > ran_function_expiry
rf_id -> when its subscriptionTime runs out; absent for no limit
std::unordered_map< uint32_t, uint32_t > subscription_id_to_ran_function
sub_id -> rf_id (reverse, used for SubscriptionDelete)
bool remove_subscription_by_id(uint32_t subscription_id)
Remove the subscription with a given subscription ID.
bool remove_subscription_by_rf(uint32_t ran_function_id)
Remove the subscription for a given RAN function.
std::vector< uint32_t > expire_subscriptions(std::chrono::steady_clock::time_point now)
Forget every subscription whose time is up at now.
std::vector< uint32_t > active_subscriptions() const
Snapshot of active subscription IDs.
DAppSubscriptionState(const DAppSubscriptionState &)=delete
E3AP Protocol Types - Vendor-neutral type definitions.