hiroz 0.1.0

Native Rust ROS 2 implementation using Zenoh
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
use std::{
    marker::PhantomData,
    sync::{Arc, Mutex, atomic::AtomicUsize},
    time::Duration,
};

use tracing::{debug, info, trace};
use zenoh::{
    Result, Session, Wait, bytes, key_expr::KeyExpr, liveliness::LivelinessToken, query::Query,
    sample::Sample,
};

use std::sync::atomic::Ordering;

use crate::topic_name;

use crate::{
    Builder,
    attachment::{Attachment, GidArray},
    common::DataHandler,
    entity::EndpointEntity,
    impl_with_type_info,
    msg::{ZDeserializer, ZMessage, ZService},
    queue::BoundedQueue,
};

#[derive(Debug)]
pub struct ZClientBuilder<T> {
    pub(crate) entity: EndpointEntity,
    pub(crate) session: Arc<Session>,
    pub(crate) clock: crate::time::ZClock,
    pub(crate) keyexpr_format: hiroz_protocol::KeyExprFormat,
    pub(crate) querier_timeout: Duration,
    pub(crate) _phantom_data: PhantomData<T>,
}

impl_with_type_info!(ZClientBuilder<T>);
impl_with_type_info!(ZServerBuilder<T>);

/// A ROS 2-style reusable service handle for typed request/response calls.
///
/// Create a client via [`ZNode::create_client`](crate::node::ZNode::create_client).
/// Invoke the service with [`call`](ZClient::call) or [`call_with_timeout`](ZClient::call_with_timeout).
///
/// # Example
///
/// ```rust,ignore
/// use hiroz::prelude::*;
/// use std::time::Duration;
///
/// // client: ZClient<MyService>
/// let response = client.call_with_timeout(&request, Duration::from_secs(5)).await?;
/// ```
pub struct ZClient<T: ZService> {
    // TODO: replace this with the sample sn
    sn: AtomicUsize,
    // TODO: replace this with zenoh's global entity id
    gid: GidArray,
    inner: zenoh::query::Querier<'static>,
    #[allow(dead_code)] // RAII: revokes liveliness token on drop
    lv_token: LivelinessToken,
    topic: String,
    clock: crate::time::ZClock,
    #[cfg(feature = "rmw")]
    completed_tx: flume::Sender<Sample>,
    #[cfg(feature = "rmw")]
    completed_rx: flume::Receiver<Sample>,
    _phantom_data: PhantomData<T>,
}

impl<T: ZService> std::fmt::Debug for ZClient<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ZClient")
            .field("topic", &self.topic)
            .finish_non_exhaustive()
    }
}

impl<T> Builder for ZClientBuilder<T>
where
    T: ZService,
{
    type Output = ZClient<T>;

    #[tracing::instrument(name = "client_build", skip(self), fields(
        service = %self.entity.topic
    ))]
    fn build(mut self) -> Result<Self::Output> {
        let Some(node) = self.entity.node.as_ref() else {
            return Err(zenoh::Error::from("client build requires node identity"));
        };
        // Qualify the service name according to ROS 2 rules
        let qualified_service =
            topic_name::qualify_service_name(&self.entity.topic, &node.namespace, &node.name)
                .map_err(|e| zenoh::Error::from(format!("Failed to qualify service: {}", e)))?;

        self.entity.topic = qualified_service.clone();
        debug!("[CLN] Qualified service: {}", qualified_service);

        let topic_ke = self.keyexpr_format.topic_key_expr(&self.entity)?;
        let key_expr = (*topic_ke).clone(); // Deref and clone the KeyExpr
        debug!("[CLN] Key expression: {}", key_expr);

        let inner = self
            .session
            .declare_querier(key_expr)
            .target(zenoh::query::QueryTarget::All)
            .consolidation(zenoh::query::ConsolidationMode::None)
            .timeout(self.querier_timeout)
            .wait()?;
        let lv_ke = self
            .keyexpr_format
            .liveliness_key_expr(&self.entity, &self.session.zid())?;
        let lv_token = self
            .session
            .liveliness()
            .declare_token((*lv_ke).clone())
            .wait()?;
        #[cfg(feature = "rmw")]
        let (completed_tx, completed_rx) = {
            let depth = match self.entity.qos.history {
                hiroz_protocol::qos::QosHistory::KeepLast(n) => n,
                hiroz_protocol::qos::QosHistory::KeepAll => 1000,
            };
            flume::bounded(depth)
        };
        debug!("[CLN] Client ready: service={}", self.entity.topic);

        Ok(ZClient {
            sn: AtomicUsize::new(1), // Start at 1 for ROS compatibility
            inner,
            lv_token,
            gid: crate::entity::endpoint_gid(&self.entity)
                .expect("local endpoint always has node identity"),
            topic: self.entity.topic.clone(),
            clock: self.clock,
            #[cfg(feature = "rmw")]
            completed_tx,
            #[cfg(feature = "rmw")]
            completed_rx,
            _phantom_data: Default::default(),
        })
    }
}

impl<T> ZClient<T>
where
    T: ZService,
{
    fn new_attachment(&self) -> Attachment {
        Attachment::with_clock(
            self.sn.fetch_add(1, Ordering::AcqRel) as _,
            self.gid,
            &self.clock,
        )
    }

    async fn call_sample(&self, payload: impl Into<bytes::ZBytes>) -> Result<Sample> {
        let attachment = self.new_attachment();
        let (response_tx, response_rx) = tokio::sync::oneshot::channel();
        let response_tx = Arc::new(Mutex::new(Some(response_tx)));

        self.inner
            .get()
            .payload(payload)
            .attachment(attachment)
            .callback(move |reply| match reply.into_result() {
                Ok(sample) => {
                    let sender = response_tx
                        .lock()
                        .expect("service reply sender mutex poisoned")
                        .take();
                    match sender {
                        Some(sender) => {
                            if sender.send(sample).is_err() {
                                tracing::warn!(
                                    "Service call receiver dropped before reply delivery"
                                );
                            }
                        }
                        None => {
                            tracing::warn!("Service call received extra reply after completion");
                        }
                    }
                }
                Err(error) => {
                    tracing::debug!("Service reply error: {error:?}");
                }
            })
            .await?;

        let sample = response_rx.await.map_err(|_| {
            zenoh::Error::from("Service call ended before any response was received")
        })?;

        Ok(sample)
    }

    /// Call the service and wait indefinitely for the first reply.
    pub async fn call(&self, msg: &T::Request) -> Result<T::Response>
    where
        T::Request: ZMessage,
        T::Response: ZMessage,
        for<'a> <T::Response as ZMessage>::Serdes:
            ZDeserializer<Output = T::Response, Input<'a> = &'a [u8]>,
    {
        let sample = self.call_sample(msg.serialize()).await?;
        let payload_bytes = sample.payload().to_bytes();
        let msg = <T::Response as ZMessage>::deserialize(&payload_bytes[..])
            .map_err(|e| zenoh::Error::from(e.to_string()))?;
        Ok(msg)
    }

    /// Call the service and fail if no reply arrives before `timeout` elapses.
    pub async fn call_with_timeout(
        &self,
        msg: &T::Request,
        timeout: Duration,
    ) -> Result<T::Response>
    where
        T::Request: ZMessage,
        T::Response: ZMessage,
        for<'a> <T::Response as ZMessage>::Serdes:
            ZDeserializer<Output = T::Response, Input<'a> = &'a [u8]>,
    {
        // On timeout the call future is dropped. The Zenoh querier callback is still
        // running briefly; it will hit the `None` sender branch and log a warning.
        // This is expected and harmless.
        tokio::time::timeout(timeout, self.call(msg))
            .await
            .map_err(|_| zenoh::Error::from(format!("Service call timed out after {timeout:?}")))?
    }
}

impl<T> ZClient<T>
where
    T: ZService,
{
    #[cfg(feature = "rmw")]
    #[tracing::instrument(name = "rmw_send_request", skip(self, msg, notify), fields(
        service = %self.topic,
        sn = self.sn.load(Ordering::Acquire),
        payload_len = tracing::field::Empty
    ))]
    pub fn rmw_send_request<F>(&self, msg: &T::Request, notify: F) -> Result<i64>
    where
        F: Fn() + Send + Sync + 'static,
    {
        let completed_tx = self.completed_tx.clone();
        let attachment = self.new_attachment();
        let sn = attachment.sequence_number;
        self.inner
            .get()
            .payload(msg.serialize())
            .attachment(attachment)
            .callback(move |reply| {
                match reply.into_result() {
                    Ok(sample) => {
                        if completed_tx.try_send(sample).is_err() {
                            tracing::warn!(
                                "Client response queue full, dropping response (QoS depth enforced)"
                            );
                        }
                        notify();
                    }
                    Err(err) => {
                        // Handle timeout and other reply errors gracefully
                        // This can happen when a service is not available or times out
                        tracing::debug!("Client reply error: {:?}", err);
                    }
                }
            })
            .wait()?;
        Ok(sn)
    }

    #[cfg(feature = "rmw")]
    pub fn rmw_try_take_response_sample(&self) -> Result<Option<Sample>> {
        match self.completed_rx.try_recv() {
            Ok(sample) => Ok(Some(sample)),
            Err(flume::TryRecvError::Empty) => Ok(None),
            Err(flume::TryRecvError::Disconnected) => {
                Err(zenoh::Error::from("Client response channel disconnected"))
            }
        }
    }

    #[cfg(feature = "rmw")]
    pub fn rmw_has_responses(&self) -> bool {
        !self.completed_rx.is_empty()
    }
}

#[derive(Debug)]
pub struct ZServerBuilder<T> {
    pub(crate) entity: EndpointEntity,
    pub(crate) session: Arc<Session>,
    pub(crate) clock: crate::time::ZClock,
    pub(crate) keyexpr_format: hiroz_protocol::KeyExprFormat,
    pub(crate) _phantom_data: PhantomData<T>,
}

impl<T> ZClientBuilder<T> {
    /// Set the QoS profile for this client.
    pub fn with_qos(mut self, qos: crate::qos::QosProfile) -> Self {
        self.entity.qos = qos.to_protocol_qos();
        self
    }

    pub(crate) fn with_querier_timeout(mut self, timeout: Duration) -> Self {
        self.querier_timeout = timeout;
        self
    }

    /// Get a reference to the entity (for internal and rmw use).
    pub fn entity(&self) -> &EndpointEntity {
        &self.entity
    }
}

impl<T> ZServerBuilder<T> {
    /// Set the QoS profile for this server.
    pub fn with_qos(mut self, qos: crate::qos::QosProfile) -> Self {
        self.entity.qos = qos.to_protocol_qos();
        self
    }

    /// Get a reference to the entity (for internal and rmw use).
    pub fn entity(&self) -> &EndpointEntity {
        &self.entity
    }
}

pub struct ZServer<T: ZService, Q = Query> {
    key_expr: KeyExpr<'static>,
    #[allow(dead_code)] // RAII: deregisters the queryable on drop
    inner: zenoh::query::Queryable<()>,
    #[allow(dead_code)] // RAII: revokes liveliness token on drop
    lv_token: LivelinessToken,
    clock: crate::time::ZClock,
    pub(crate) queue: Option<Arc<BoundedQueue<Q>>>,
    _phantom_data: PhantomData<T>,
}

impl<T: ZService, Q> std::fmt::Debug for ZServer<T, Q> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ZServer")
            .field("key_expr", &self.key_expr.as_str())
            .finish_non_exhaustive()
    }
}

impl<T, Q> ZServer<T, Q>
where
    T: ZService,
{
    /// Access the receiver queue.
    ///
    /// # Panics
    ///
    /// Panics if the server was built with `build_with_callback()` and has no queue.
    /// Action servers always have queues and will never panic.
    pub fn queue(&self) -> &Arc<BoundedQueue<Q>> {
        self.queue
            .as_ref()
            .expect("Server was built with callback mode, no queue available")
    }

    /// Access the receiver queue if present (returns `None` in callback mode).
    pub fn try_queue(&self) -> Option<&Arc<BoundedQueue<Q>>> {
        self.queue.as_ref()
    }
}

impl<T> ZServerBuilder<T>
where
    T: ZService,
{
    /// Internal method that all build variants use.
    fn build_internal<Q>(
        mut self,
        handler: DataHandler<Query>,
        queue: Option<Arc<BoundedQueue<Q>>>,
    ) -> Result<ZServer<T, Q>> {
        let Some(node) = self.entity.node.as_ref() else {
            return Err(zenoh::Error::from("service build requires node identity"));
        };
        let qualified_service =
            topic_name::qualify_service_name(&self.entity.topic, &node.namespace, &node.name)
                .map_err(|e| zenoh::Error::from(format!("Failed to qualify service: {}", e)))?;

        self.entity.topic = qualified_service;

        let topic_ke = self.keyexpr_format.topic_key_expr(&self.entity)?;
        let key_expr = (*topic_ke).clone(); // Deref and clone the KeyExpr
        tracing::debug!("[SRV] KE: {key_expr}");

        info!("[SRV] Declaring queryable on key expression: {}", key_expr);

        let inner = self
            .session
            .declare_queryable(&key_expr)
            .complete(true)
            .callback(move |query| {
                info!(
                    "[SRV] Query received: ke={}, selector={}, parameters={}",
                    query.key_expr(),
                    query.selector(),
                    query.parameters()
                );

                if let Some(att) = query.attachment() {
                    info!("[SRV] Query has attachment: {} bytes", att.len());
                } else {
                    info!("[SRV] Query has NO attachment");
                }

                if let Some(payload) = query.payload() {
                    info!("[SRV] Query has payload: {} bytes", payload.len());
                } else {
                    info!("[SRV] Query has NO payload");
                }

                handler.handle(query);
            })
            .wait()?;

        let lv_ke = self
            .keyexpr_format
            .liveliness_key_expr(&self.entity, &self.session.zid())?;
        let lv_token = self
            .session
            .liveliness()
            .declare_token((*lv_ke).clone())
            .wait()?;

        Ok(ZServer {
            key_expr,
            inner,
            lv_token,
            clock: self.clock,
            queue,
            _phantom_data: Default::default(),
        })
    }

    pub fn build_with_callback<F>(self, callback: F) -> Result<ZServer<T, ()>>
    where
        F: Fn(Query) + Send + Sync + 'static,
    {
        self.build_internal(DataHandler::Callback(Arc::new(callback)), None)
    }

    #[cfg(feature = "rmw")]
    pub fn build_with_notifier<F>(self, notify: F) -> Result<ZServer<T>>
    where
        F: Fn() + Send + Sync + 'static,
    {
        let queue_size = match self.entity.qos.history {
            hiroz_protocol::qos::QosHistory::KeepLast(depth) => depth,
            hiroz_protocol::qos::QosHistory::KeepAll => usize::MAX,
        };
        let queue = Arc::new(BoundedQueue::new(queue_size));
        self.build_internal(
            DataHandler::QueueWithNotifier {
                queue: queue.clone(),
                notifier: Arc::new(notify),
            },
            Some(queue),
        )
    }
}

impl<T> Builder for ZServerBuilder<T>
where
    T: ZService,
{
    type Output = ZServer<T>;

    fn build(self) -> Result<Self::Output> {
        let queue_size = match self.entity.qos.history {
            hiroz_protocol::qos::QosHistory::KeepLast(depth) => depth,
            hiroz_protocol::qos::QosHistory::KeepAll => usize::MAX,
        };
        let queue = Arc::new(BoundedQueue::new(queue_size));
        self.build_internal(DataHandler::Queue(queue.clone()), Some(queue))
    }
}

/// Identifies a service request by the client's GUID and its per-client sequence number.
///
/// `source_timestamp` is metadata (when the request was sent); it is NOT part of the
/// identity and is excluded from `PartialEq`/`Eq`/`Hash` so that `pending` map lookups
/// work correctly when the response header only carries `(writer_guid, sequence_number)`.
#[derive(Debug, Clone)]
pub struct RequestId {
    pub sequence_number: i64,
    pub writer_guid: GidArray,
    pub source_timestamp: i64,
}

impl PartialEq for RequestId {
    fn eq(&self, other: &Self) -> bool {
        self.sequence_number == other.sequence_number && self.writer_guid == other.writer_guid
    }
}

impl Eq for RequestId {}

impl std::hash::Hash for RequestId {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.sequence_number.hash(state);
        self.writer_guid.hash(state);
    }
}

impl From<Attachment> for RequestId {
    fn from(value: Attachment) -> Self {
        Self {
            sequence_number: value.sequence_number,
            writer_guid: value.source_gid,
            source_timestamp: value.source_timestamp,
        }
    }
}

pub struct ServiceReply<T: ZService> {
    request_id: RequestId,
    key_expr: KeyExpr<'static>,
    query: Query,
    clock: crate::time::ZClock,
    replied: bool,
    _phantom_data: PhantomData<T>,
}

impl<T: ZService> Drop for ServiceReply<T> {
    fn drop(&mut self) {
        if !self.replied {
            tracing::warn!(
                sn = self.request_id.sequence_number,
                "ServiceReply dropped without sending a reply — client will wait until querier timeout"
            );
        }
    }
}

impl<T: ZService> ServiceReply<T> {
    pub fn id(&self) -> &RequestId {
        &self.request_id
    }

    pub fn reply_blocking(mut self, msg: &T::Response) -> Result<()> {
        self.replied = true;
        let attachment = Attachment::with_clock(
            self.request_id.sequence_number,
            self.request_id.writer_guid,
            &self.clock,
        );
        self.query
            .reply(&self.key_expr, msg.serialize())
            .attachment(attachment)
            .wait()
    }

    pub async fn reply(mut self, msg: &T::Response) -> Result<()> {
        self.replied = true;
        let attachment = Attachment::with_clock(
            self.request_id.sequence_number,
            self.request_id.writer_guid,
            &self.clock,
        );
        self.query
            .reply(&self.key_expr, msg.serialize())
            .attachment(attachment)
            .await
    }
}

#[must_use = "dropping without calling reply leaves the client waiting indefinitely"]
pub struct ServiceRequest<T: ZService> {
    message: T::Request,
    reply: ServiceReply<T>,
}

impl<T: ZService> ServiceRequest<T> {
    pub fn id(&self) -> &RequestId {
        self.reply.id()
    }

    pub fn message(&self) -> &T::Request {
        &self.message
    }

    pub fn into_parts(self) -> (T::Request, ServiceReply<T>) {
        (self.message, self.reply)
    }

    pub fn reply_blocking(self, response: &T::Response) -> Result<()> {
        self.reply.reply_blocking(response)
    }

    pub async fn reply(self, response: &T::Response) -> Result<()> {
        self.reply.reply(response).await
    }
}

impl<T> ZServer<T, Query>
where
    T: ZService,
{
    fn decode_request(&self, query: Query) -> Result<ServiceRequest<T>>
    where
        T::Request: ZMessage + Send + Sync + 'static,
        for<'a> <T::Request as ZMessage>::Serdes:
            ZDeserializer<Output = T::Request, Input<'a> = &'a [u8]>,
    {
        let attachment_bytes = query
            .attachment()
            .ok_or_else(|| zenoh::Error::from("Service request missing attachment"))?;
        let attachment: Attachment = attachment_bytes.try_into()?;
        let request_id: RequestId = attachment.into();

        let payload_bytes = query
            .payload()
            .map(|payload| payload.to_bytes())
            .unwrap_or_default();
        let message = <T::Request as ZMessage>::deserialize(&payload_bytes[..])
            .map_err(|e| zenoh::Error::from(e.to_string()))?;

        Ok(ServiceRequest {
            message,
            reply: ServiceReply {
                request_id,
                key_expr: self.key_expr.clone(),
                query,
                clock: self.clock.clone(),
                replied: false,
                _phantom_data: PhantomData,
            },
        })
    }

    pub fn try_take_request(&mut self) -> Result<Option<ServiceRequest<T>>>
    where
        T::Request: ZMessage + Send + Sync + 'static,
        for<'a> <T::Request as ZMessage>::Serdes:
            ZDeserializer<Output = T::Request, Input<'a> = &'a [u8]>,
    {
        let queue = self.queue.as_ref().ok_or_else(|| {
            zenoh::Error::from("Server was built with callback, no queue available")
        })?;
        match queue.try_recv() {
            Some(query) => self.decode_request(query).map(Some),
            None => Ok(None),
        }
    }

    /// Take the next request as raw payload bytes without typed deserialization.
    ///
    /// Used by the RMW layer, which performs its own C FFI deserialization on the raw bytes.
    /// Returns `(payload_bytes, reply_token)` so the caller can fill an existing message buffer.
    #[cfg(feature = "rmw")]
    pub fn try_take_request_raw(&mut self) -> Result<Option<(Vec<u8>, ServiceReply<T>)>> {
        let queue = self.queue.as_ref().ok_or_else(|| {
            zenoh::Error::from("Server was built with callback, no queue available")
        })?;
        let Some(query) = queue.try_recv() else {
            return Ok(None);
        };
        let attachment_bytes = query
            .attachment()
            .ok_or_else(|| zenoh::Error::from("Service request missing attachment"))?;
        let attachment: Attachment = attachment_bytes.try_into()?;
        let request_id: RequestId = attachment.into();
        let payload_bytes = query
            .payload()
            .map(|p| p.to_bytes().to_vec())
            .unwrap_or_default();
        let reply = ServiceReply {
            request_id,
            key_expr: self.key_expr.clone(),
            query,
            clock: self.clock.clone(),
            replied: false,
            _phantom_data: PhantomData,
        };
        Ok(Some((payload_bytes, reply)))
    }

    /// Blocks waiting to receive the next request on the service and then deserializes the payload.
    ///
    /// This method may fail if the message does not deserialize as the requested type.
    #[tracing::instrument(name = "take_request", skip(self), fields(
        service = %self.key_expr,
        sn = tracing::field::Empty,
        payload_len = tracing::field::Empty
    ))]
    pub fn take_request(&mut self) -> Result<ServiceRequest<T>>
    where
        T::Request: ZMessage + Send + Sync + 'static,
        for<'a> <T::Request as ZMessage>::Serdes:
            ZDeserializer<Output = T::Request, Input<'a> = &'a [u8]>,
    {
        trace!("[SRV] Waiting for request");

        let queue = self.queue.as_ref().ok_or_else(|| {
            zenoh::Error::from("Server was built with callback, no queue available")
        })?;
        let query = queue.recv();
        self.decode_request(query)
    }

    /// Awaits the next request on the service and then deserializes the payload.
    ///
    /// This method may fail if the message does not deserialize as the requested type.
    pub async fn async_take_request(&mut self) -> Result<ServiceRequest<T>>
    where
        T::Request: ZMessage + Send + Sync + 'static,
        for<'a> <T::Request as ZMessage>::Serdes:
            ZDeserializer<Output = T::Request, Input<'a> = &'a [u8]>,
    {
        let queue = self.queue.as_ref().ok_or_else(|| {
            zenoh::Error::from("Server was built with callback, no queue available")
        })?;
        let query = queue.recv_async().await;
        self.decode_request(query)
    }
}

#[cfg(test)]
mod tests {
    // -----------------------------------------------------------------------
    // Topic name qualification for service names
    // Service names follow the same rules as topic names
    // -----------------------------------------------------------------------

    #[test]
    fn test_qualify_service_absolute_unchanged() {
        let result = crate::topic_name::qualify_service_name("/add_two_ints", "/", "node").unwrap();
        assert_eq!(result, "/add_two_ints");
    }

    #[test]
    fn test_qualify_service_relative_adds_slash() {
        let result = crate::topic_name::qualify_service_name("add_two_ints", "/", "node").unwrap();
        assert_eq!(result, "/add_two_ints");
    }

    #[test]
    fn test_qualify_service_with_namespace() {
        let result =
            crate::topic_name::qualify_service_name("add_two_ints", "/ns", "node").unwrap();
        assert_eq!(result, "/ns/add_two_ints");
    }

    #[test]
    fn test_qualify_service_multipart_name() {
        let result =
            crate::topic_name::qualify_service_name("/my/service/name", "/", "node").unwrap();
        assert_eq!(result, "/my/service/name");
    }

    // -----------------------------------------------------------------------
    // QoS stored in builder entity reflects the protocol values
    // -----------------------------------------------------------------------

    #[test]
    fn test_protocol_qos_default_is_reliable() {
        let qos = crate::qos::QosProfile::default();
        let proto = qos.to_protocol_qos();
        assert_eq!(
            proto.reliability,
            hiroz_protocol::qos::QosReliability::Reliable
        );
    }

    #[test]
    fn test_protocol_qos_default_is_volatile() {
        let qos = crate::qos::QosProfile::default();
        let proto = qos.to_protocol_qos();
        assert_eq!(
            proto.durability,
            hiroz_protocol::qos::QosDurability::Volatile
        );
    }

    #[test]
    fn test_request_id_clone_and_eq() {
        let key = crate::service::RequestId {
            writer_guid: [1u8; 16],
            sequence_number: 42,
            source_timestamp: 0,
        };
        let key2 = key.clone();
        assert_eq!(key.sequence_number, key2.sequence_number);
        assert_eq!(key.writer_guid, key2.writer_guid);
        assert_eq!(key.source_timestamp, key2.source_timestamp);
    }

    #[test]
    fn test_zclientbuilder_with_qos_sets_reliability() {
        use crate::qos::{QosProfile, QosReliability};
        let qos = QosProfile {
            reliability: QosReliability::BestEffort,
            ..Default::default()
        };
        let proto = qos.to_protocol_qos();
        assert_eq!(
            proto.reliability,
            hiroz_protocol::qos::QosReliability::BestEffort
        );
    }
}