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
use crate::datas::id::Id;
use std::marker::PhantomData;
use std::mem::MaybeUninit;

use crate::tasks::HubError;

use crate::protocol::packet::Subscribe;
use crate::protocol::packet::Unsubscribe;
use crate::protocol::Protocol;
use anyhow::Result;
use bytes::Bytes;
use log::debug;
use ringbuf::Consumer;
use std::sync::Arc;

type SharedRb = ringbuf::SharedRb<u16, Vec<MaybeUninit<u16>>>;

#[derive(Debug, Clone)]
pub struct TracePublishQos<T> {
    pub(crate) protocol: Protocol,
    pub(crate) id: u32,
    pub topic: Arc<String>,
    pub packet_id: u16,
    qos: PhantomData<T>,
    pub payload: Arc<Bytes>,
    pub retain: bool,
}

// impl TracePublish {
//     pub fn new(topic: Arc<String>, qos: QoS, payload: Arc<Bytes>, retain: bool) -> Self {
//         match qos {
//             QoS::AtMostOnce => Self::QoS0(TracePublishQos {
//                 id: Id::id(),
//                 packet_id: 0,
//                 topic,
//                 qos: PhantomData,
//                 payload,
//                 retain,
//             }),
//             QoS::AtLeastOnce => Self::QoS1(TracePublishQos {
//                 id: Id::id(),
//                 packet_id: 0,
//                 topic,
//                 qos: PhantomData,
//                 payload,
//                 retain,
//             }),
//             QoS::ExactlyOnce => Self::QoS2(TracePublishQos {
//                 id: Id::id(),
//                 packet_id: 0,
//                 topic,
//                 qos: PhantomData,
//                 payload,
//                 retain,
//             }),
//         }
//     }
// pub fn id(&self) -> u32 {
//     match self {
//         TracePublish::QoS0(value) => value.id(),
//         TracePublish::QoS1(value) => value.id(),
//         TracePublish::QoS2(value) => value.id(),
//     }
// }

// pub(crate) fn set_packet_id(&mut self, packet_id: u16) -> &mut Self {
//     match self {
//         TracePublish::QoS0(value) => {
//             value.set_packet_id(packet_id);
//         }
//         TracePublish::QoS1(value) => {
//             value.set_packet_id(packet_id);
//         }
//         TracePublish::QoS2(value) => {
//             value.set_packet_id(packet_id);
//         }
//     };
//     self
// }
// pub(crate) fn packet_id(&self) -> u16 {
//     self.packet_id()
// }
// }

impl<T> TracePublishQos<T> {
    pub fn init(
        topic: Arc<String>,
        payload: Arc<Bytes>,
        retain: bool,
        protocol: Protocol,
        id: u32,
    ) -> Self {
        Self {
            id,
            packet_id: 0,
            topic,
            qos: PhantomData,
            payload,
            retain,
            protocol,
        }
    }
    pub fn id(&self) -> u32 {
        self.id
    }
    pub(crate) async fn set_packet_id(
        &mut self,
        b: &mut Consumer<u16, Arc<SharedRb>>,
    ) -> Result<(), HubError> {
        self.packet_id = request_id(b).await?;
        Ok(())
    }
    pub(crate) fn packet_id(&self) -> u16 {
        self.packet_id
    }
}

impl<T> PartialEq for TracePublishQos<T> {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl<T> PartialEq<u32> for TracePublishQos<T> {
    fn eq(&self, other: &u32) -> bool {
        self.id == *other
    }
}

#[derive(Debug, Clone)]
pub struct TraceSubscribe {
    pub(crate) id: u32,
    pub(crate) subscribe: Subscribe,
}

impl TraceSubscribe {
    pub(crate) async fn set_packet_id(
        &mut self,
        b: &mut Consumer<u16, Arc<SharedRb>>,
    ) -> Result<(), HubError> {
        let packet_id = request_id(b).await?;
        self.subscribe.set_packet_id(packet_id);
        Ok(())
    }
    pub(crate) fn packet_id(&self) -> u16 {
        self.subscribe.packet_id()
    }
}

#[derive(Debug, Clone)]
pub struct TraceUnubscribe {
    pub(crate) id: u32,
    pub(crate) unsubscribe: Unsubscribe,
}
impl TraceUnubscribe {
    pub fn new(unsubscribe: Unsubscribe) -> Self {
        Self {
            id: Id::id(),
            unsubscribe,
        }
    }
    pub(crate) async fn set_packet_id(
        &mut self,
        b: &mut Consumer<u16, Arc<SharedRb>>,
    ) -> Result<(), HubError> {
        self.unsubscribe.set_packet_id(request_id(b).await?);
        Ok(())
    }
    pub(crate) fn packet_id(&self) -> u16 {
        self.unsubscribe.packet_id()
    }
}

async fn request_id(b: &mut Consumer<u16, Arc<SharedRb>>) -> Result<u16, HubError> {
    if let Some(id) = b.pop() {
        debug!("request id: {}", id);
        Ok(id)
    } else {
        Err(HubError::PacketIdErr(format!(
            "buffer of packet id is empty"
        )))
    }
}