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
use serde::Serialize;
use crate::AgentId;
use super::*;
use crate::Authenticable;
/// Properties of an outgoing event.
#[derive(Debug, Serialize)]
pub struct OutgoingEventProperties {
label: &'static str,
#[serde(default, skip_serializing_if = "Option::is_none")]
agent_id: Option<AgentId>,
#[serde(flatten)]
long_term_timing: Option<LongTermTimingProperties>,
#[serde(flatten)]
short_term_timing: OutgoingShortTermTimingProperties,
#[serde(flatten)]
tracking: Option<TrackingProperties>,
#[serde(default, skip_serializing_if = "Option::is_none")]
local_tracking_label: Option<String>,
#[serde(skip)]
tags: ExtraTags,
}
impl OutgoingEventProperties {
/// Builds [OutgoingEventProperties](struct.OutgoingEventProperties.html).
///
/// Use this function only if you're dispatching an event from scratch.
///
/// If you make a reaction event on an incoming request or another event consider using
/// [IncomingRequestProperties::to_event](struct.IncomingRequestProperties.html#method.to_event)
/// or [IncomingEventProperties::to_event](struct.IncomingEventProperties.html#method.to_event)
/// respectively.
///
/// # Arguments
///
/// * `label` – event label.
/// * `short_term_timing` – event's short term timing properties.
///
/// # Example
///
/// ```
/// let props = OutgoingEventProperties::new(
/// "agent.enter",
/// OutgoingShortTermTimingProperties::new(Utc::now()),
/// );
/// ```
pub fn new(label: &'static str, short_term_timing: OutgoingShortTermTimingProperties) -> Self {
Self {
label,
long_term_timing: None,
short_term_timing,
tracking: None,
agent_id: None,
local_tracking_label: None,
tags: Default::default(),
}
}
pub fn set_agent_id(&mut self, agent_id: AgentId) -> &mut Self {
self.agent_id = Some(agent_id);
self
}
pub fn set_long_term_timing(&mut self, timing: LongTermTimingProperties) -> &mut Self {
self.long_term_timing = Some(timing);
self
}
pub fn set_tracking(&mut self, tracking: TrackingProperties) -> &mut Self {
self.tracking = Some(tracking);
self
}
pub fn set_local_tracking_label(&mut self, label: String) -> &mut Self {
self.local_tracking_label = Some(label);
self
}
pub fn tags(&self) -> &ExtraTags {
&self.tags
}
pub fn set_tags(&mut self, tags: ExtraTags) -> &mut Self {
self.tags = tags;
self
}
}
pub type OutgoingEvent<T> = OutgoingMessageContent<T, OutgoingEventProperties>;
impl<T> OutgoingEvent<T>
where
T: serde::Serialize,
{
/// Builds a broadcast event to publish.
///
/// # Arguments
///
/// * `payload` – any serializable value.
/// * `properties` – properties of the outgoing event.
/// * `to_uri` – broadcast resource path.
/// See [Destination](../enum.Destination#variant.Broadcast) for details.
///
/// # Example
///
/// ```
/// let short_term_timing = OutgoingShortTermTimingProperties::until_now(start_timestamp);
///
/// let message = OutgoingEvent::broadcast(
/// json!({ "foo": "bar" }),
/// request.to_event("message.create", short_term_timing),
/// "rooms/123/events",
/// );
/// ```
pub fn broadcast(
payload: T,
properties: OutgoingEventProperties,
to_uri: &str,
) -> OutgoingMessage<T> {
OutgoingMessage::Event(Self::new(
payload,
properties,
Destination::Broadcast(to_uri.to_owned()),
))
}
/// Builds a multicast event to publish.
///
/// # Arguments
///
/// * `payload` – any serializable value.
/// * `properties` – properties of the outgoing event.
/// * `to` – destination [AccountId](../struct.AccountId.html).
///
/// # Example
///
/// ```
/// let short_term_timing = OutgoingShortTermTimingProperties::until_now(start_timestamp);
///
/// let message = OutgoingRequest::multicast(
/// json!({ "foo": "bar" }),
/// request.to_event("message.create", short_term_timing),
/// &AccountId::new("service_name", "svc.example.org"),
/// );
/// ```
pub fn multicast<A>(
payload: T,
properties: OutgoingEventProperties,
to: &A,
version: &str,
) -> OutgoingMessage<T>
where
A: Authenticable,
{
OutgoingMessage::Event(Self::new(
payload,
properties,
Destination::Multicast(to.as_account_id().to_owned(), version.to_owned()),
))
}
/// Builds a unicast event to publish.
///
/// # Arguments
///
/// * `payload` – any serializable value.
/// * `properties` – properties of the outgoing event.
/// * `to` – destination [AgentId](../struct.AgentId.html).
/// * `version` – destination agent's API version.
///
/// # Example
///
/// ```
/// let short_term_timing = OutgoingShortTermTimingProperties::until_now(start_timestamp);
///
/// let message = OutgoingRequest::multicast(
/// json!({ "foo": "bar" }),
/// request.to_event("message.create", short_term_timing),
/// &AgentId::new("instance01", AccountId::new("service_name", "svc.example.org")),
/// "v1",
/// );
/// ```
pub fn unicast<A>(
payload: T,
properties: OutgoingEventProperties,
to: &A,
version: &str,
) -> OutgoingMessage<T>
where
A: Addressable,
{
OutgoingMessage::Event(Self::new(
payload,
properties,
Destination::Unicast(to.as_agent_id().to_owned(), version.to_owned()),
))
}
}
impl<T: serde::Serialize> Publishable for OutgoingEvent<T> {
fn destination_topic(&self, publisher: &Address) -> Result<String, Error> {
match self.destination {
Destination::Broadcast(ref uri) => Ok(format!(
"apps/{app}/api/{version}/{uri}",
app = publisher.id().as_account_id(),
version = publisher.version(),
uri = uri,
)),
Destination::Multicast(ref account_id, ref version) => Ok(format!(
"agents/{agent_id}/api/{version}/out/{app}",
agent_id = publisher.id(),
version = version,
app = account_id,
)),
_ => Err(Error::new(&format!(
"destination = '{:?}' is incompatible with event message type",
self.destination,
))),
}
}
fn qos(&self) -> QoS {
QoS::AtLeastOnce
}
}