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
/// RTPS Writer
/// Entity diagram found in formal-14-09-01 on manual page 71
/// Behavior statemachine found in formal-14-09-01
use serde::Serialize;
use std::error::Error;
use std::thread;
use std::sync::atomic::{Ordering};
use std::io;
use std::io::{Cursor};
use std::net::UdpSocket;
use std;
use super::super::super::common_types::*;
use super::super::*;
use super::super::super::submessage::*;
use super::super::super::message::*;
use super::super::super::cdr::*;
pub struct StatelessWriter {
guid: Guid,
/// List of unicast locators (transport, address, port combinations) that can be used to send messages to this Endpoint. The list may be empty.
unicast_locator_list: LocatorList,
/// List of multicast locators (transport, address, port combinations) that can be used to send messages to this Endpoint. The list may be empty.
multicast_locator_list: LocatorList,
reliability_level: ReliabilityKind,
topic_kind: TopicKind,
push_mode: bool,
heartbeat_period: Duration,
nack_response_delay: Duration,
nack_suppression_duration: Duration,
writer_cache: HistoryCache,
last_change_sequence_number: SequenceNumber,
heartbeat_count: u32,
// resend_data_period: Duration,
// Better name would be `reader_locator_list` to match other
// properties. But this follows the RTPS spec naming.
// Also a slightly different type, includes EntityId
// if that's been negotiated
// TODO: make private and rely on discovery
pub reader_locators: Vec<(Locator, Option<EntityId>)>,
socket: Option<std::sync::Arc<UdpSocket>>,
handle: Option<SpawnableTaskHandle>
}
impl StatelessWriter {
/// You must provide at least one entry in the unicast locator list
/// or it will fail to produce heartbeats.
/// TODO: change `new() -> Self` to `new() -> Option<Self>`
pub fn new(init_args: WriterInitArgs) -> Self {
StatelessWriter {
guid: init_args.guid,
unicast_locator_list: init_args.unicast_locator_list,
multicast_locator_list: init_args.multicast_locator_list,
reliability_level: init_args.reliability_level,
topic_kind: init_args.topic_kind,
push_mode: init_args.push_mode,
heartbeat_period: init_args.heartbeat_period,
nack_response_delay: init_args.nack_response_delay,
nack_suppression_duration: init_args.nack_suppression_duration,
writer_cache: HistoryCache::new(),
last_change_sequence_number: 0,
heartbeat_count: 0,
reader_locators: init_args.reader_locators,
socket: None,
handle: None
}
}
fn push_mode(&self) -> bool {
self.push_mode
}
fn heartbeat_period(&self) -> Duration {
self.heartbeat_period
}
fn nack_response_delay(&self) -> Duration {
self.nack_response_delay
}
fn nack_suppression_duration(&self) -> Duration {
self.nack_response_delay
}
pub fn heartbeat(&mut self, reader_id: EntityId) -> SubmessageVariant {
let max = self.writer_cache.get_seq_num_max().unwrap_or(0);
let min = self.writer_cache.get_seq_num_min().unwrap_or(0);
let heartbeat = SubmessageVariant::Heartbeat {
reader_id: reader_id,
writer_id: self.guid.entity_id,
first_sn: min,
last_sn: max,
count: self.heartbeat_count
};
self.heartbeat_count += 1;
heartbeat
}
pub fn serialize_message<'a>(buf: &'a mut [u8], message: &Message) -> Result<&'a [u8], io::Error> {
let position = {
let new_slice = &mut buf[..];
let writeable_buf = Cursor::new(new_slice);
let mut serializer = CdrSerializer {
endianness: Endianness::Big,
write_handle: writeable_buf
};
match message.serialize(&mut serializer) {
Ok(_) => (),
Err(err) => {
return Err(io::Error::new(io::ErrorKind::Other, err.description()))
},
}
serializer.write_handle.position() as usize
};
Ok(&buf[0..position])
}
}
impl EntityTrait for StatelessWriter {
fn guid(&self) -> Guid {
self.guid
}
}
impl EndpointTrait for StatelessWriter {
fn topic_kind(&self) -> TopicKind {
self.topic_kind
}
fn set_socket(&mut self, sock: std::sync::Arc<UdpSocket>) {
self.socket = Some(sock);
}
fn unicast_locator_list(&self) -> &LocatorList {
&self.unicast_locator_list
}
fn mut_unicast_locator_list(&mut self) -> &mut LocatorList {
&mut self.unicast_locator_list
}
fn multicast_locator_list(&mut self) -> &mut LocatorList {
&mut self.multicast_locator_list
}
}
impl WriterTrait for StatelessWriter {
fn new_change(&mut self, kind: ChangeKind, handle: InstanceHandle, data: ArcBuffer) -> CacheChange {
self.last_change_sequence_number += 1;
let change = CacheChange::new(kind, self.guid, handle, self.last_change_sequence_number, data);
self.writer_cache.add_change(&change).unwrap();
change
}
}
impl SpawnableTaskTrait for StatelessWriter {
fn werk(&mut self, buf: &mut [u8]) -> io::Result<()> {
// 1. Spam the reader with your id, locator, and a heartbeat requesting their cache state back
// 2. Read their
// TODO: `self.reader_locators.clone()` necessary because `self.heartbeat` wants mut ref.
for (reader_locator, reader) in self.reader_locators.clone() {
// TODO: Skip the intermediate vec and just serialize to the buf
let mut submsgs = vec![];
// Who I am
submsgs.push(SubmessageVariant::InfoSource {
protocol_version: ProtocolVersion::VERSION_2_2,
vendor_id: [20,10],
guid_prefix: self.guid.guid_prefix
});
for change in self.writer_cache.iter() {
submsgs.push(change.to_submessage(self.guid).variant)
}
let heartbeat = match reader {
Some(reader) => self.heartbeat(reader),
None => self.heartbeat(EntityId::builtin_unknown())
};
submsgs.push(heartbeat);
let used_buf = try!(StatelessWriter::serialize_message(buf, &Message::new(submsgs.into_iter().map(|smv| Submessage{variant: smv}).collect())));
try!(reader_locator.write(used_buf));
/* TODO: move to a transaction where the count only goes up once. for now we increment on each host
at the writer level.Perhaps we could do count at the ReaderLocator level?
let heartbeat = match reader {
Some(reader) => self.heartbeat(reader),
None => self.heartbeat(EntityId::builtin_unknown())
};
for location in &self.unicast_locator_list {
let used_buf = try!(StatelessWriter::serialize_message(buf, &Message::new(vec![Submessage{ variant: heartbeat.clone() }])));
try!(location.write(used_buf));
} */
}
std::thread::sleep(std::time::Duration::from_millis(10));
let socket = match self.socket {
Some(ref sock) => sock,
None => unreachable!()
};
let (size, /* socketAddr */ _) = try!(socket.recv_from(buf));
let data = &buf[0..size];
let mut reader = io::Cursor::new(data);
/*
for change in self.writer_cache.iter() {
let position = {
let new_slice = &mut buf[..];
let writeable_buf = Cursor::new(new_slice);
let mut serializer = CdrSerializer {
endianness: Endianness::Big,
write_handle: writeable_buf
};
let message = Message::new(vec![
change.to_submessage(self.guid)
]);
// need control flow to create my non-io::Error stuff.
match message.serialize(&mut serializer) {
Ok(_) => (),
Err(err) => {
return Err(io::Error::new(io::ErrorKind::Other, err.description()))
},
}
serializer.write_handle.position() as usize
};
let used_buf: &[u8] = &buf[0..position];
for location in &mut self.unicast_locator_list {
try!(location.write(used_buf));
}
}
*/
Ok(())
}
fn stop(&mut self) {
match self.handle {
Some(ref mut handle) => {
handle.stop_signal.store(true, Ordering::Relaxed);
},
None => unreachable!()
}
}
fn join(self) -> thread::Result<SpawnableTaskStats> {
let can_join = self.handle.is_some();
if can_join {
self.handle.unwrap().join()
} else {
Err(Box::new("cannot join thread. never spawned."))
}
}
}