zerodds-corba-ccm 1.0.0-rc.1

OMG CCM 4.0 — Component Container, CIF, CIDL-Modell, TimerEventService. Migrationspfad fuer CORBA-Bestandscode auf den ZeroDDS-Bus.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 ZeroDDS Contributors

//! CCM 4.0 Lifecycle-Constraints + Receptacle-State-Machine +
//! Configurator-Iface — Spec §6.4.2 / §6.5.2 / §6.10.
//!
//! Diese Module decken die Datenmodell-Seite der Lifecycle-Regeln
//! ab. Die echte Container-Runtime-Durchsetzung erfolgt in
//! `container.rs`; wir liefern hier die Constraint-Validators und
//! State-Machinen, die diese Runtime nutzen kann.

use alloc::collections::BTreeMap;
use alloc::string::String;
use alloc::sync::Arc;
use alloc::vec::Vec;
use std::sync::Mutex;

use crate::component_def::{AttributeDef, ComponentDef};

// ---------------------------------------------------------------------------
// §6.4.2 Semantics of Facet References — Lifecycle-Constraints
// ---------------------------------------------------------------------------

/// Spec §6.4.2 — Facet-Lifetime-Constraint.
///
/// "The lifetime of a facet [...] is bound to the lifetime of the
/// component instance that provides it." Wir modellieren das als
/// Constraint-Check: ein Facet-Reference darf NICHT laenger leben
/// als die Component-Instance.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FacetLifetimeViolation {
    /// Facet wurde nach Component-Destroy noch verwendet.
    UseAfterComponentDestroy,
    /// Facet-Reference ist orphaned (keine Component-Backref).
    OrphanedFacetReference,
}

/// Validiert eine Facet-Reference gegen den Component-Lifecycle.
///
/// `component_alive` muss `true` sein wenn die Component-Instance
/// noch existiert; `facet_in_component` muss `true` sein wenn das
/// Facet wirklich zur Component gehoert.
///
/// # Errors
/// `FacetLifetimeViolation` wenn das Constraint verletzt ist.
pub fn check_facet_lifetime(
    component_alive: bool,
    facet_in_component: bool,
) -> Result<(), FacetLifetimeViolation> {
    if !facet_in_component {
        return Err(FacetLifetimeViolation::OrphanedFacetReference);
    }
    if !component_alive {
        return Err(FacetLifetimeViolation::UseAfterComponentDestroy);
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// §6.5.2 Receptacles — Connection-State-Machine
// ---------------------------------------------------------------------------

/// Spec §6.5.2 — Connection-State-Machine fuer Receptacles.
///
/// Ein Receptacle durchlaeuft die States `Disconnected → Connected →
/// Disconnected`. `connect()` bei Simplex-Multiplicity ist nur in
/// `Disconnected` erlaubt; `disconnect()` nur in `Connected`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConnectionState {
    /// Kein Provider gebunden.
    Disconnected,
    /// Provider gebunden (Object-Reference vorhanden).
    Connected,
}

/// Connection-Lifecycle-Errors.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConnectionError {
    /// `connect` auf einem bereits verbundenen Simplex-Receptacle.
    AlreadyConnected,
    /// `disconnect` auf einem ungebundenen Receptacle.
    NoConnection,
    /// Receptacle existiert nicht in der Component.
    UnknownReceptacle,
}

/// Receptacle-Connection-Manager.
///
/// Pflegt pro `(receptacle_name, connection_id)` den Connection-
/// State. Bei Multiplex-Receptacles koennen mehrere Connections
/// parallel existieren; bei Simplex-Receptacles ist `connect`
/// nur bei `Disconnected` erlaubt.
#[derive(Default)]
pub struct ReceptacleManager {
    states: Mutex<BTreeMap<(String, u64), ConnectionState>>,
    next_id: std::sync::atomic::AtomicU64,
}

impl core::fmt::Debug for ReceptacleManager {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("ReceptacleManager").finish()
    }
}

impl ReceptacleManager {
    /// Konstruktor.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Spec §6.5.2 — `connect(receptacle, ref) -> connection_id`.
    /// Liefert eine neue `ConnectionId`; bei Simplex (caller muss
    /// `is_simplex` setzen) wird ein bereits existierender Connect
    /// als `AlreadyConnected` rejected.
    ///
    /// # Errors
    /// `ConnectionError::AlreadyConnected` wenn Simplex-Receptacle
    /// schon verbunden.
    pub fn connect(&self, receptacle: &str, is_simplex: bool) -> Result<u64, ConnectionError> {
        let mut g = self
            .states
            .lock()
            .map_err(|_| ConnectionError::UnknownReceptacle)?;
        if is_simplex
            && g.iter()
                .any(|((n, _), st)| n == receptacle && *st == ConnectionState::Connected)
        {
            return Err(ConnectionError::AlreadyConnected);
        }
        let id = self
            .next_id
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        g.insert((receptacle.to_string(), id), ConnectionState::Connected);
        Ok(id)
    }

    /// Spec §6.5.2 — `disconnect(receptacle, connection_id)`.
    ///
    /// # Errors
    /// `ConnectionError::NoConnection` wenn die Connection nicht
    /// existiert oder bereits getrennt wurde.
    pub fn disconnect(&self, receptacle: &str, id: u64) -> Result<(), ConnectionError> {
        let mut g = self
            .states
            .lock()
            .map_err(|_| ConnectionError::UnknownReceptacle)?;
        let key = (receptacle.to_string(), id);
        match g.get(&key) {
            Some(ConnectionState::Connected) => {
                g.insert(key, ConnectionState::Disconnected);
                Ok(())
            }
            _ => Err(ConnectionError::NoConnection),
        }
    }

    /// Anzahl aktive Connections (state = Connected).
    pub fn active_connections(&self, receptacle: &str) -> usize {
        self.states.lock().map_or(0, |g| {
            g.iter()
                .filter(|((n, _), st)| n == receptacle && **st == ConnectionState::Connected)
                .count()
        })
    }
}

// ---------------------------------------------------------------------------
// §6.10 Configuration with Attributes — Configurator
// ---------------------------------------------------------------------------

/// Spec §6.10 — Configuration-Errors.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ConfigError {
    /// Attribut existiert nicht in der Component-Definition.
    UnknownAttribute(String),
    /// Attribut ist read-only und kann nicht gesetzt werden.
    ReadOnly(String),
    /// Wertkonvertierung fehlgeschlagen.
    InvalidValue(String),
}

/// Spec §6.10 — Configurator-Interface.
///
/// Ein Configurator setzt Attribute auf einer Component-Instance,
/// typischerweise vor `configuration_complete()`. Wir modellieren
/// das als Trait, sodass Caller eigene Configurators registrieren
/// koennen.
pub trait Configurator: Send + Sync {
    /// Setzt ein Attribut.
    ///
    /// # Errors
    /// Siehe [`ConfigError`].
    fn set_attribute(&self, name: &str, value: &[u8]) -> Result<(), ConfigError>;

    /// Liest ein Attribut.
    ///
    /// # Errors
    /// `ConfigError::UnknownAttribute` wenn nicht gesetzt.
    fn get_attribute(&self, name: &str) -> Result<Vec<u8>, ConfigError>;
}

/// Default-Configurator-Implementation auf Basis der
/// `ComponentDef::attributes`-Liste mit BTreeMap-Storage.
pub struct StandardConfigurator {
    schema: Vec<AttributeDef>,
    values: Mutex<BTreeMap<String, Vec<u8>>>,
}

impl core::fmt::Debug for StandardConfigurator {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("StandardConfigurator")
            .field("attribute_count", &self.schema.len())
            .finish()
    }
}

impl StandardConfigurator {
    /// Konstruktor — leitet Schema aus `ComponentDef::attributes` ab.
    #[must_use]
    pub fn new(component: &ComponentDef) -> Self {
        Self {
            schema: component.attributes.clone(),
            values: Mutex::new(BTreeMap::new()),
        }
    }
}

impl Configurator for StandardConfigurator {
    fn set_attribute(&self, name: &str, value: &[u8]) -> Result<(), ConfigError> {
        let attr = self
            .schema
            .iter()
            .find(|a| a.name == name)
            .ok_or_else(|| ConfigError::UnknownAttribute(name.to_string()))?;
        if attr.readonly {
            return Err(ConfigError::ReadOnly(name.to_string()));
        }
        if let Ok(mut g) = self.values.lock() {
            g.insert(name.to_string(), value.to_vec());
            Ok(())
        } else {
            Err(ConfigError::InvalidValue(name.to_string()))
        }
    }

    fn get_attribute(&self, name: &str) -> Result<Vec<u8>, ConfigError> {
        let g = self
            .values
            .lock()
            .map_err(|_| ConfigError::UnknownAttribute(name.to_string()))?;
        g.get(name)
            .cloned()
            .ok_or_else(|| ConfigError::UnknownAttribute(name.to_string()))
    }
}

/// Configurator-Registry fuer Container-Wide Configurator-Lookup.
#[derive(Default)]
pub struct ConfiguratorRegistry {
    by_repo_id: Mutex<BTreeMap<String, Arc<dyn Configurator>>>,
}

impl core::fmt::Debug for ConfiguratorRegistry {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("ConfiguratorRegistry").finish()
    }
}

impl ConfiguratorRegistry {
    /// Konstruktor.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Registriert einen Configurator fuer eine Component-Repo-ID.
    pub fn register(&self, repo_id: &str, c: Arc<dyn Configurator>) {
        if let Ok(mut g) = self.by_repo_id.lock() {
            g.insert(repo_id.to_string(), c);
        }
    }

    /// Liefert den Configurator fuer eine Repo-ID.
    pub fn get(&self, repo_id: &str) -> Option<Arc<dyn Configurator>> {
        self.by_repo_id
            .lock()
            .ok()
            .and_then(|g| g.get(repo_id).cloned())
    }
}

#[cfg(test)]
#[allow(clippy::expect_used)]
mod tests {
    use super::*;
    use crate::component_def::{AttributeDef, ComponentDef};

    fn sample_component() -> ComponentDef {
        ComponentDef {
            name: "Sample".into(),
            repository_id: "IDL:demo/Sample:1.0".into(),
            base_component: None,
            supported_interfaces: alloc::vec![],
            facets: alloc::vec![],
            receptacles: alloc::vec![],
            event_sources: alloc::vec![],
            event_sinks: alloc::vec![],
            attributes: alloc::vec![
                AttributeDef {
                    name: "rate".into(),
                    type_spec: "long".into(),
                    readonly: false,
                    set_raises: alloc::vec![],
                    get_raises: alloc::vec![],
                },
                AttributeDef {
                    name: "version".into(),
                    type_spec: "string".into(),
                    readonly: true,
                    set_raises: alloc::vec![],
                    get_raises: alloc::vec![],
                },
            ],
            primary_key: alloc::vec![],
        }
    }

    // §6.4.2 Facet-Lifetime
    #[test]
    fn facet_lifetime_passes_when_alive() {
        assert!(check_facet_lifetime(true, true).is_ok());
    }

    #[test]
    fn facet_lifetime_rejects_use_after_destroy() {
        assert_eq!(
            check_facet_lifetime(false, true),
            Err(FacetLifetimeViolation::UseAfterComponentDestroy)
        );
    }

    #[test]
    fn facet_lifetime_rejects_orphaned() {
        assert_eq!(
            check_facet_lifetime(true, false),
            Err(FacetLifetimeViolation::OrphanedFacetReference)
        );
    }

    // §6.5.2 Receptacle-State-Machine
    #[test]
    fn receptacle_simplex_connect_then_disconnect() {
        let m = ReceptacleManager::new();
        let id = m.connect("port", true).expect("ok");
        assert_eq!(m.active_connections("port"), 1);
        m.disconnect("port", id).expect("ok");
        assert_eq!(m.active_connections("port"), 0);
    }

    #[test]
    fn receptacle_simplex_double_connect_rejected() {
        let m = ReceptacleManager::new();
        let _ = m.connect("port", true).expect("ok");
        assert_eq!(
            m.connect("port", true),
            Err(ConnectionError::AlreadyConnected)
        );
    }

    #[test]
    fn receptacle_multiplex_allows_multiple_connects() {
        let m = ReceptacleManager::new();
        let _ = m.connect("multi", false).expect("ok");
        let _ = m.connect("multi", false).expect("ok");
        assert_eq!(m.active_connections("multi"), 2);
    }

    #[test]
    fn receptacle_disconnect_unknown_id_rejected() {
        let m = ReceptacleManager::new();
        assert_eq!(
            m.disconnect("port", 999),
            Err(ConnectionError::NoConnection)
        );
    }

    #[test]
    fn receptacle_double_disconnect_rejected() {
        let m = ReceptacleManager::new();
        let id = m.connect("port", true).expect("ok");
        m.disconnect("port", id).expect("ok");
        assert_eq!(m.disconnect("port", id), Err(ConnectionError::NoConnection));
    }

    // §6.10 Configurator
    #[test]
    fn configurator_set_get_roundtrip() {
        let c = StandardConfigurator::new(&sample_component());
        c.set_attribute("rate", b"42").expect("ok");
        assert_eq!(c.get_attribute("rate").expect("ok"), b"42");
    }

    #[test]
    fn configurator_rejects_unknown_attribute() {
        let c = StandardConfigurator::new(&sample_component());
        assert!(matches!(
            c.set_attribute("bogus", b"x"),
            Err(ConfigError::UnknownAttribute(_))
        ));
    }

    #[test]
    fn configurator_rejects_readonly_attribute() {
        let c = StandardConfigurator::new(&sample_component());
        assert!(matches!(
            c.set_attribute("version", b"x"),
            Err(ConfigError::ReadOnly(_))
        ));
    }

    #[test]
    fn configurator_get_unknown_returns_unknown_attribute() {
        let c = StandardConfigurator::new(&sample_component());
        assert!(matches!(
            c.get_attribute("never_set"),
            Err(ConfigError::UnknownAttribute(_))
        ));
    }

    #[test]
    fn configurator_registry_register_and_lookup() {
        let r = ConfiguratorRegistry::new();
        let c: Arc<dyn Configurator> = Arc::new(StandardConfigurator::new(&sample_component()));
        r.register("IDL:demo/Sample:1.0", c);
        assert!(r.get("IDL:demo/Sample:1.0").is_some());
        assert!(r.get("IDL:demo/Other:1.0").is_none());
    }
}