oa_gateway_stomp/
adapter.rs

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
//! ActiveMQ STOMP client adapter. Protocol control stays here; data crosses the engine.
//!
//! This is a client, not a server. The retry loop sits outside one
//! broker session. Each session runs on a child task so a panic is a
//! join error. [`StompConfig::on_panic`] chooses abort or reconnect.
//! The host does not restart a finished `run`.

use std::sync::Arc;

use async_trait::async_trait;
use oa_gateway_adapter::{after_join, Adapter, AdapterError, AfterSession};
use oa_gateway_agra::{unwrap as unwrap_ma, wrapper_kind};
use oa_gateway_core::{
    AdapterId, Delivery, Engine, Envelope, RouteKey, SubId, DEFAULT_CHANNEL_CAPACITY,
};
use tokio::sync::{mpsc, oneshot};
use tokio_util::sync::CancellationToken;
use tracing::{debug, info, warn};

use crate::client::{
    connect, disconnect_frame, send_frame, subscribe_frame, FrameReader, FrameWriter,
};
use crate::codec::Frame;
use crate::config::StompConfig;
use crate::dest::{
    inbound_route, sniff_content_type, sniff_type_hint, DestinationMap, HDR_ID, HDR_ORIGIN,
    HDR_STOMP_DEST, HDR_TOPIC, HDR_TYPE_HINT,
};

/// STOMP client that bridges configured topics both ways.
///
/// Inbound MESSAGE frames are stamped with `oag.origin_adapter` so
/// outbound SEND can refuse the echo when
/// [`StompConfig::suppress_echo`] is on. [`Engine::drop_adapter`] runs
/// at session start and end so a reconnect does not keep stale
/// subscriptions.
pub struct StompAdapter {
    id: AdapterId,
    config: StompConfig,
}

impl StompAdapter {
    /// Builds an adapter that is not yet connected.
    #[must_use]
    pub fn new(id: impl Into<AdapterId>, config: StompConfig) -> Self {
        Self {
            id: id.into(),
            config,
        }
    }

    #[must_use]
    pub fn id(&self) -> &AdapterId {
        &self.id
    }

    #[must_use]
    pub fn config(&self) -> &StompConfig {
        &self.config
    }

    /// Connects, subscribes, and bridges until `shutdown` or a fatal
    /// session error with reconnect off.
    ///
    /// # Errors
    ///
    /// Returns [`AdapterError::Failed`] if connect, SUBSCRIBE, or the
    /// session fails and [`StompConfig::reconnect`] is false; if a
    /// session panics and [`StompConfig::on_panic`] is `abort`; or if
    /// shutdown has already fired.
    pub async fn serve(
        self: Arc<Self>,
        engine: Arc<Engine>,
        shutdown: CancellationToken,
    ) -> Result<(), AdapterError> {
        self.serve_inner(engine, shutdown, None).await
    }

    /// Same as [`Self::serve`], then signals `ready` after the first
    /// CONNECTED and engine subscriptions.
    ///
    /// Used by tests that must not publish before the bridge is up.
    /// Later reconnects do not signal again.
    ///
    /// # Errors
    ///
    /// Same as [`Self::serve`].
    pub async fn serve_ready(
        self: Arc<Self>,
        engine: Arc<Engine>,
        shutdown: CancellationToken,
        ready: oneshot::Sender<()>,
    ) -> Result<(), AdapterError> {
        self.serve_inner(engine, shutdown, Some(ready)).await
    }

    /// Retry loop. Delay comes from [`StompConfig::reconnect_delay`].
    /// `ready` is taken on the first session only.
    async fn serve_inner(
        self: Arc<Self>,
        engine: Arc<Engine>,
        shutdown: CancellationToken,
        mut ready: Option<oneshot::Sender<()>>,
    ) -> Result<(), AdapterError> {
        loop {
            if shutdown.is_cancelled() {
                return Ok(());
            }
            let adapter = Arc::clone(&self);
            let engine = Arc::clone(&engine);
            let token = shutdown.clone();
            let ready = ready.take();
            let joined =
                tokio::spawn(async move { adapter.session(&engine, &token, ready).await }).await;
            match after_join(
                joined,
                self.config.reconnect,
                self.config.on_panic,
                &self.id,
            ) {
                AfterSession::ReturnOk => return Ok(()),
                AfterSession::ReturnErr(err) => return Err(err),
                AfterSession::Retry { message } => {
                    if shutdown.is_cancelled() {
                        return Ok(());
                    }
                    warn!(adapter = %self.id, "{message}");
                }
            }
            tokio::select! {
                () = shutdown.cancelled() => return Ok(()),
                () = tokio::time::sleep(self.config.reconnect_delay) => {}
            }
        }
    }

    /// One CONNECT through DISCONNECT.
    ///
    /// Drops this adapter's engine subscriptions before SUBSCRIBE so a
    /// previous session cannot leave keys behind. Engine subscribe uses
    /// [`RouteKey::topic`] (wildcard) for each configured topic.
    ///
    /// # Errors
    ///
    /// Returns [`AdapterError::Failed`] if the broker cannot be reached,
    /// SUBSCRIBE fails, or [`Self::drive_session`] fails.
    async fn session(
        &self,
        engine: &Arc<Engine>,
        shutdown: &CancellationToken,
        ready: Option<oneshot::Sender<()>>,
    ) -> Result<(), AdapterError> {
        let map = DestinationMap::new(self.config.destination_prefix.clone());
        let (mut reader, mut writer) = connect(&self.config).await.map_err(|err| {
            AdapterError::failed(&self.id, format!("connect {}: {err}", self.config.broker))
        })?;

        engine.drop_adapter(self.id.clone()).await;

        let (eng_tx, mut eng_rx) = mpsc::channel::<Delivery>(DEFAULT_CHANNEL_CAPACITY);
        for (i, topic) in self.config.topics.iter().enumerate() {
            let dest = map.to_stomp(topic);
            let sid = format!("sub-{i}");
            writer
                .send(&subscribe_frame(&sid, &dest))
                .await
                .map_err(|err| {
                    AdapterError::failed(&self.id, format!("SUBSCRIBE {dest}: {err}"))
                })?;
            engine
                .subscribe(
                    self.id.clone(),
                    SubId::new(format!("stomp-out-{i}")),
                    RouteKey::topic(topic.clone()),
                    eng_tx.clone(),
                )
                .await
                .map_err(|err| AdapterError::failed(&self.id, err.to_string()))?;
        }
        drop(eng_tx);

        info!(
            adapter = %self.id,
            broker = %self.config.broker,
            topics = ?self.config.topics,
            tls = self.config.tls.is_some(),
            "stomp connected"
        );
        if let Some(tx) = ready {
            let _ = tx.send(());
        }

        let result = self
            .drive_session(
                &mut reader,
                &mut writer,
                engine,
                shutdown,
                &map,
                &mut eng_rx,
            )
            .await;

        let _ = writer.send(&disconnect_frame()).await;
        engine.drop_adapter(self.id.clone()).await;
        result
    }

    /// Reads broker frames and engine deliveries until shutdown or a
    /// broken connection.
    ///
    /// A clean broker close is an error so reconnect can fire. A
    /// dropped engine channel is [`Ok`].
    ///
    /// # Errors
    ///
    /// Returns [`AdapterError::Failed`] if the broker closes, a read
    /// fails, or an outbound SEND cannot be written.
    async fn drive_session(
        &self,
        reader: &mut FrameReader,
        writer: &mut FrameWriter,
        engine: &Arc<Engine>,
        shutdown: &CancellationToken,
        map: &DestinationMap,
        eng_rx: &mut mpsc::Receiver<Delivery>,
    ) -> Result<(), AdapterError> {
        loop {
            tokio::select! {
                () = shutdown.cancelled() => {
                    info!(adapter = %self.id, "stomp shutting down");
                    return Ok(());
                }
                incoming = reader.recv() => {
                    match incoming {
                        Ok(Some(frame)) => {
                            if let Err(err) = self.handle_frame(engine, map, frame).await {
                                warn!(adapter = %self.id, error = %err, "dropping inbound stomp frame");
                            }
                        }
                        Ok(None) => {
                            return Err(AdapterError::failed(&self.id, "broker closed the connection"));
                        }
                        Err(err) => {
                            return Err(AdapterError::failed(&self.id, err.to_string()));
                        }
                    }
                }
                delivery = eng_rx.recv() => {
                    let Some(delivery) = delivery else { return Ok(()); };
                    if let Err(err) = forward_outbound(self, writer, map, delivery).await {
                        return Err(AdapterError::failed(&self.id, err));
                    }
                }
            }
        }
    }

    /// Handles one inbound frame. MESSAGE is published; ERROR fails the
    /// session; RECEIPT is ignored.
    ///
    /// # Errors
    ///
    /// Returns a message for a broker ERROR or a MESSAGE that cannot be
    /// mapped onto an envelope.
    async fn handle_frame(
        &self,
        engine: &Arc<Engine>,
        map: &DestinationMap,
        frame: Frame,
    ) -> Result<(), String> {
        match frame.command.as_str() {
            "MESSAGE" => {
                inbound_publish(self, engine, map, &frame).await?;
            }
            "ERROR" => {
                let msg = frame
                    .header("message")
                    .unwrap_or_else(|| std::str::from_utf8(&frame.body).unwrap_or("broker ERROR"));
                return Err(format!("broker ERROR: {msg}"));
            }
            "RECEIPT" => {}
            other => {
                debug!(command = other, "ignoring stomp frame");
            }
        }
        Ok(())
    }
}

/// Publishes a MESSAGE onto the engine.
///
/// Topic comes from `oag.topic` or by stripping the destination prefix.
/// Type hint comes from `oag.type_hint` or by sniffing the body. The
/// envelope is stamped with this adapter's id so the outbound path can
/// skip it. A-GRA wrappers publish wrapper and inner as two envelopes.
///
/// # Errors
///
/// Returns a message if `destination` is missing, outside the prefix,
/// or unwrap fails.
async fn inbound_publish(
    adapter: &StompAdapter,
    engine: &Arc<Engine>,
    map: &DestinationMap,
    frame: &Frame,
) -> Result<(), String> {
    let dest = frame
        .header("destination")
        .ok_or_else(|| "MESSAGE missing destination".to_string())?;
    let topic = frame
        .header(HDR_TOPIC)
        .map(str::to_owned)
        .or_else(|| map.from_stomp(dest))
        .ok_or_else(|| format!("destination {dest} is outside {}", map.prefix()))?;

    let type_hint = frame
        .header(HDR_TYPE_HINT)
        .map(str::to_owned)
        .or_else(|| sniff_type_hint(&frame.body));
    let content_type = sniff_content_type(&frame.body, frame.header("content-type"));
    let route = inbound_route(&topic, type_hint);

    let stamp = |env: Envelope| {
        let mut env = env.with_origin(&adapter.id);
        env.headers.insert(HDR_STOMP_DEST.into(), dest.to_owned());
        if let Some(mid) = frame.header("message-id") {
            env.headers
                .insert("stomp.message-id".into(), mid.to_owned());
        }
        env
    };

    if adapter.config.unwrap_ma_payloads && wrapper_kind(&frame.body).is_some() {
        let peeled = unwrap_ma(&topic, &frame.body).map_err(|e| e.to_string())?;
        log_drops(adapter, engine.publish(stamp(peeled.wrapper)).await);
        log_drops(adapter, engine.publish(stamp(peeled.inner)).await);
        return Ok(());
    }

    let envelope = Envelope::new(route, bytes::Bytes::copy_from_slice(&frame.body))
        .with_content_type(content_type);
    log_drops(adapter, engine.publish(stamp(envelope)).await);
    Ok(())
}

/// Writes a SEND for one engine delivery.
///
/// Skips envelopes whose `oag.origin_adapter` is this adapter, so a
/// MESSAGE just taken from the broker is not sent back. `stomp.*`
/// headers and `oag.origin_adapter` are not copied onto the wire.
///
/// # Errors
///
/// Returns a message if the SEND cannot be written.
async fn forward_outbound(
    adapter: &StompAdapter,
    writer: &mut FrameWriter,
    map: &DestinationMap,
    delivery: Delivery,
) -> Result<(), String> {
    if adapter.config.suppress_echo && delivery.envelope.is_echo_of(&adapter.id) {
        return Ok(());
    }

    let dest = map.to_stomp(&delivery.envelope.route.topic);
    let mut headers = vec![
        (
            "content-type".into(),
            delivery.envelope.content_type.as_str().to_owned(),
        ),
        (HDR_ORIGIN.into(), adapter.id.as_str().to_owned()),
        (HDR_TOPIC.into(), delivery.envelope.route.topic.clone()),
        (HDR_ID.into(), delivery.envelope.id.to_string()),
    ];
    if let Some(hint) = &delivery.envelope.route.type_hint {
        headers.push((HDR_TYPE_HINT.into(), hint.clone()));
    }
    for (k, v) in &delivery.envelope.headers {
        if k == HDR_ORIGIN || k == HDR_STOMP_DEST || k.starts_with("stomp.") {
            continue;
        }
        headers.push((k.clone(), v.clone()));
    }

    writer
        .send(&send_frame(
            &dest,
            headers,
            delivery.envelope.payload.to_vec(),
        ))
        .await
        .map_err(|e| e.to_string())
}

#[async_trait]
impl Adapter for StompAdapter {
    fn id(&self) -> &AdapterId {
        StompAdapter::id(self)
    }

    /// Same as [`StompAdapter::serve`].
    ///
    /// # Errors
    ///
    /// See [`StompAdapter::serve`].
    async fn run(
        self: Arc<Self>,
        engine: Arc<Engine>,
        shutdown: CancellationToken,
    ) -> Result<(), AdapterError> {
        self.serve(engine, shutdown).await
    }
}

fn log_drops(adapter: &StompAdapter, outcome: oa_gateway_core::PublishOutcome) {
    if outcome.dropped > 0 {
        warn!(
            adapter = %adapter.id,
            matched = outcome.matched,
            delivered = outcome.delivered,
            dropped = outcome.dropped,
            "engine dropped deliveries on this publish"
        );
    }
}