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
use std::fmt;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use crate::channel::ChannelShared;
use crate::command_queue::InterestKind;
use crate::errors::CelerisError;
use crate::events::{ListenerLocation, MessageListener, PresenceListener, Registration};
use crate::random::generate_message_id;
use crate::values::{MessageMetadata, PresenceEvent, PresencePage};
/// A handle for one segment of a channel. It holds only its id:
/// subscriptions, listeners and the connection belong to the channel, so any
/// number of handles for one segment share them. Obtain one with
/// [`Channel::segment`](crate::Channel::segment) or
/// [`Channel::default_segment`](crate::Channel::default_segment).
#[derive(Clone)]
pub struct Segment {
channel: Arc<ChannelShared>,
segment_id: String,
} // end struct Segment
impl fmt::Debug for Segment {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("Segment")
.field("segment_id", &self.segment_id)
.finish()
} // end method fmt
} // end impl Debug for Segment
impl Segment {
pub(crate) fn new(channel: Arc<ChannelShared>, segment_id: &str) -> Segment {
Segment {
channel,
segment_id: segment_id.to_owned(),
}
} // end fn new
/// The segment's id.
pub fn id(&self) -> &str {
&self.segment_id
} // end method id
/// Registers interest in the segment's messages. The first interest in a
/// segment subscribes it, and it stays subscribed until every interest is
/// cancelled; subscriptions are restored after every reconnect. The
/// default segment is joined automatically and never subscribed or
/// unsubscribed.
///
/// A subscription needs read access to the segment. No acknowledgement
/// exists: a denial arrives later, as a `PermissionDeniedError`, through
/// [`ChannelEventHandler::on_error`](crate::ChannelEventHandler::on_error).
/// It fails with [`CelerisError::NotConnected`] once the channel is closing
/// or closed.
pub fn subscribe(&self) -> Result<Subscription, CelerisError> {
self.channel
.subscribe(InterestKind::Message, &self.segment_id)
} // end method subscribe
/// Registers interest in the segment's presence joins and leaves. Watching
/// presence is not membership: it delivers no messages, and the watcher is
/// not itself announced or listed.
pub fn subscribe_presence(&self) -> Result<Subscription, CelerisError> {
self.channel
.subscribe(InterestKind::Presence, &self.segment_id)
} // end method subscribe_presence
/// Registers a listener for the segment's deliveries. Each message id is
/// delivered once within a bounded window (REV-01).
pub fn on_message(
&self,
listener: impl Fn(&[u8], &MessageMetadata) + Send + Sync + 'static,
) -> Registration {
let listener: Arc<MessageListener> = Arc::new(listener);
let segment_id = self.segment_id.clone();
self.channel.register(
ListenerLocation::SegmentMessage(self.segment_id.clone()),
move |listeners| listeners.segment_messages.entry(segment_id).or_default(),
listener,
)
} // end method on_message
/// Registers a listener for joins and leaves. Events arrive only while a
/// presence subscription is held: the server sends them to presence
/// subscribers alone (PRES-01).
pub fn on_presence(
&self,
listener: impl Fn(&PresenceEvent) + Send + Sync + 'static,
) -> Registration {
let listener: Arc<PresenceListener> = Arc::new(listener);
let segment_id = self.segment_id.clone();
self.channel.register(
ListenerLocation::Presence(self.segment_id.clone()),
move |listeners| listeners.presence.entry(segment_id).or_default(),
listener,
)
} // end method on_presence
/// Sends `payload` to the segment with a generated message id. It returns
/// once the socket accepted the bytes. That is not receipt, delivery or
/// durability: the protocol has no acknowledgements.
///
/// A publish needs write access and does not join the segment. To receive
/// from the segment, your own publishes included, call
/// [`subscribe`](Segment::subscribe).
///
/// While the channel reconnects, the publish waits in the queue and is sent
/// after the reconnect, once the restored subscriptions have gone out. It
/// then fails with the error that
/// [`on_error`](crate::ChannelEventHandler::on_error) reports if recovery
/// stops in [`Failed`](crate::ChannelState::Failed), and with
/// [`CelerisError::Cancelled`] if the channel closes.
///
/// It fails with [`CelerisError::NotConnected`] before the first connect,
/// while connecting, and once failed or closed; with
/// [`CelerisError::Backpressure`] when the publish queue (64 by default) is
/// already full; and with [`CelerisError::DeliveryUnknown`] when the socket
/// failed during the write, which is never resent. The plan's cap counts
/// the whole encoded command, not only the payload. A publish over it
/// still succeeds here and is refused afterwards with a
/// `MessageSizeLimitError` through `on_error`.
///
/// Dropping the returned future takes the publish back if the socket has
/// not started to write it.
pub async fn publish(&self, payload: impl AsRef<[u8]>) -> Result<(), CelerisError> {
let message_id = generate_message_id();
self.channel
.publish(&self.segment_id, payload.as_ref(), &message_id)
.await
} // end method publish
/// [`Segment::publish`] with your own message id. Receivers drop a repeated
/// id within their deduplication window, so reuse an id only to resend the
/// same message.
pub async fn publish_with_message_id(
&self,
payload: impl AsRef<[u8]>,
message_id: &str,
) -> Result<(), CelerisError> {
self.channel
.publish(&self.segment_id, payload.as_ref(), message_id)
.await
} // end method publish_with_message_id
/// One page of the segment's presence. One query may be in flight per
/// channel; it fails with [`CelerisError::OperationInProgress`] while
/// another is pending, and with [`CelerisError::Timeout`] after the
/// presence query timeout. `page` starts at 1, and `per_page` is 1 to 100.
///
/// Dropping the returned future frees the query slot.
pub async fn presence_list(
&self,
page: i32,
per_page: i32,
) -> Result<PresencePage, CelerisError> {
self.channel
.query_presence(&self.segment_id, page, per_page)
.await
} // end method presence_list
} // end impl Segment
/// One registered interest. Cancel it when done; dropping it keeps the
/// interest.
pub struct Subscription {
channel: Arc<ChannelShared>,
kind: InterestKind,
segment_id: String,
cancelled: AtomicBool,
} // end struct Subscription
impl fmt::Debug for Subscription {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("Subscription")
.field("segment_id", &self.segment_id)
.field("presence", &(self.kind == InterestKind::Presence))
.finish()
} // end method fmt
} // end impl Debug for Subscription
impl Subscription {
pub(crate) fn new(
channel: Arc<ChannelShared>,
kind: InterestKind,
segment_id: &str,
) -> Subscription {
Subscription {
channel,
kind,
segment_id: segment_id.to_owned(),
cancelled: AtomicBool::new(false),
}
} // end fn new
/// Releases the interest at once. Cancelling the last message interest
/// unsubscribes the segment, and cancelling the last presence interest
/// stops its presence events; neither affects the other. Calling `cancel`
/// again does nothing.
pub fn cancel(&self) {
if self.cancelled.swap(true, Ordering::SeqCst) {
return;
}
self.channel
.lock()
.release_interest(self.kind, &self.segment_id);
} // end method cancel
} // end impl Subscription