constellation-rs 0.1.10

Constellation is a framework for Rust (nightly) that aides in the writing, debugging and deployment of distributed programs.
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
#![allow(clippy::large_enum_variant)]

use super::*;
use serde::{de::DeserializeOwned, Serialize};
use std::{
	collections::hash_map::DefaultHasher, hash::{Hash, Hasher}, net::SocketAddr
};
use tcp_typed::Notifier;

/// Used to determine which side should be connecter/client and which connectee/server/listener.
fn ord(a: &SocketAddr, b: &SocketAddr) -> bool {
	let a = (a.ip(), a.port());
	let b = (b.ip(), b.port());
	assert_ne!(a, b);
	let mut a_hasher = DefaultHasher::new();
	let mut b_hasher = DefaultHasher::new();
	loop {
		a.hash(&mut a_hasher);
		let a_hash = a_hasher.finish();
		b.hash(&mut b_hasher);
		let b_hash = b_hasher.finish();
		if b_hash != a_hash {
			break b_hash > a_hash;
		}
	}
}

#[derive(Debug)]
pub enum InnerConnectingPoll {
	Connecting(InnerConnecting),
	Connected(InnerConnected),
	RemoteClosed(InnerRemoteClosed),
	Killed,
}
#[derive(Debug)]
pub enum InnerConnecting {
	Outgoing(Option<Connection>),
	Incoming(Option<Connection>),
}
impl InnerConnecting {
	pub fn new(
		bind: SocketAddr, local: SocketAddr, remote: SocketAddr, incoming: Option<Connection>,
		notifier: &impl Notifier,
	) -> InnerConnectingPoll {
		if ord(&local, &remote) {
			assert!(incoming.is_none());
			Self::Outgoing(Some(Connection::connect(bind, remote, notifier)))
		} else {
			Self::Incoming(incoming)
		}
		.poll(notifier)
	}

	pub fn add_incoming(&mut self, incoming: Connection, notifier: &impl Notifier) {
		if let Self::Incoming(ref mut prev_incoming) = self {
			if let Some(mut prev_incoming) = prev_incoming.take() {
				prev_incoming.kill(notifier).unwrap()();
			}
			*prev_incoming = Some(incoming);
			notifier.queue();
		} else {
			panic!();
		}
	}

	pub fn poll(mut self, notifier: &impl Notifier) -> InnerConnectingPoll {
		match self {
			Self::Incoming(ref mut incoming) => {
				if incoming.is_some() {
					incoming.as_mut().unwrap().poll(notifier);
					if !incoming.as_ref().unwrap().connecting() {
						return match InnerConnected::new(incoming.take().unwrap(), notifier) {
							InnerConnectedPoll::Connected(connected) => {
								InnerConnectingPoll::Connected(connected)
							}
							InnerConnectedPoll::RemoteClosed(remote_closed) => {
								InnerConnectingPoll::RemoteClosed(remote_closed)
							}
							InnerConnectedPoll::Killed => InnerConnectingPoll::Killed,
						};
					}
				}
			}
			Self::Outgoing(ref mut outgoing) => {
				if outgoing.is_some() {
					outgoing.as_mut().unwrap().poll(notifier);
					if !outgoing.as_ref().unwrap().connecting() {
						return match InnerConnected::new(outgoing.take().unwrap(), notifier) {
							InnerConnectedPoll::Connected(connected) => {
								InnerConnectingPoll::Connected(connected)
							}
							InnerConnectedPoll::RemoteClosed(remote_closed) => {
								InnerConnectingPoll::RemoteClosed(remote_closed)
							}
							InnerConnectedPoll::Killed => InnerConnectingPoll::Killed,
						};
					}
				}
			}
		}
		InnerConnectingPoll::Connecting(self)
	}

	pub fn close(self, notifier: &impl Notifier) -> InnerConnectingLocalClosedPoll {
		InnerConnectingLocalClosed::new(
			match self {
				Self::Outgoing(outgoing) => Either::Left(outgoing),
				Self::Incoming(incoming) => Either::Left(incoming),
			},
			notifier,
		)
	}
}

#[derive(Debug)]
pub enum InnerConnectingLocalClosedPoll {
	ConnectingLocalClosed(InnerConnectingLocalClosed),
	LocalClosed(InnerLocalClosed),
	Closing(InnerClosing),
	Closed,
	Killed,
}
#[derive(Debug)]
pub enum InnerConnectingLocalClosed {
	Outgoing(Option<Connection>),
	Incoming(Option<Connection>),
}
impl InnerConnectingLocalClosed {
	fn new(
		connection: Either<Option<Connection>, Option<Connection>>, notifier: &impl Notifier,
	) -> InnerConnectingLocalClosedPoll {
		match connection {
			Either::Left(outgoing) => Self::Outgoing(outgoing),
			Either::Right(incoming) => Self::Incoming(incoming),
		}
		.poll(notifier)
	}

	pub fn add_incoming(&mut self, incoming: Connection, notifier: &impl Notifier) {
		if let Self::Incoming(ref mut prev_incoming) = self {
			if let Some(mut prev_incoming) = prev_incoming.take() {
				prev_incoming.kill(notifier).unwrap()();
			}
			*prev_incoming = Some(incoming);
			notifier.queue();
		} else {
			panic!();
		}
	}

	pub fn poll(mut self, notifier: &impl Notifier) -> InnerConnectingLocalClosedPoll {
		match self {
			Self::Incoming(ref mut incoming) => {
				if incoming.is_some() {
					incoming.as_mut().unwrap().poll(notifier);
					if incoming.as_ref().unwrap().closable() {
						incoming.as_mut().unwrap().close(notifier).unwrap()();
					}
					if !incoming.as_ref().unwrap().connecting() {
						return match InnerLocalClosed::new(
							incoming.take().unwrap(),
							serde_pipe::Serializer::new(),
							serde_pipe::Deserializer::new(),
							false,
							notifier,
						) {
							InnerLocalClosedPoll::LocalClosed(local_closed) => {
								InnerConnectingLocalClosedPoll::LocalClosed(local_closed)
							}
							InnerLocalClosedPoll::Closing(closing) => {
								InnerConnectingLocalClosedPoll::Closing(closing)
							}
							InnerLocalClosedPoll::Closed => InnerConnectingLocalClosedPoll::Closed,
							InnerLocalClosedPoll::Killed => InnerConnectingLocalClosedPoll::Killed,
						};
					}
				}
				if incoming.is_none() {
					return InnerConnectingLocalClosedPoll::Closed;
				}
			}
			Self::Outgoing(ref mut outgoing) => {
				if outgoing.is_some() {
					outgoing.as_mut().unwrap().poll(notifier);
					if outgoing.as_ref().unwrap().closable() {
						outgoing.as_mut().unwrap().close(notifier).unwrap()();
					}
					if !outgoing.as_ref().unwrap().connecting() {
						return match InnerLocalClosed::new(
							outgoing.take().unwrap(),
							serde_pipe::Serializer::new(),
							serde_pipe::Deserializer::new(),
							false,
							notifier,
						) {
							InnerLocalClosedPoll::LocalClosed(local_closed) => {
								InnerConnectingLocalClosedPoll::LocalClosed(local_closed)
							}
							InnerLocalClosedPoll::Closing(closing) => {
								InnerConnectingLocalClosedPoll::Closing(closing)
							}
							InnerLocalClosedPoll::Closed => InnerConnectingLocalClosedPoll::Closed,
							InnerLocalClosedPoll::Killed => InnerConnectingLocalClosedPoll::Killed,
						};
					}
				}
				if outgoing.is_none() {
					return InnerConnectingLocalClosedPoll::Closed;
				}
			}
		}
		InnerConnectingLocalClosedPoll::ConnectingLocalClosed(self)
	}
}

#[derive(Debug)]
pub enum InnerConnectedPoll {
	Connected(InnerConnected),
	RemoteClosed(InnerRemoteClosed),
	Killed,
}
#[derive(Debug)]
pub struct InnerConnected {
	connection: Connection,
	send_serializer: serde_pipe::Serializer,
	recv_deserializer: serde_pipe::Deserializer,
	recv_deserializer_given: bool,
}
impl InnerConnected {
	fn new(connection: Connection, notifier: &impl Notifier) -> InnerConnectedPoll {
		Self {
			connection,
			send_serializer: serde_pipe::Serializer::new(),
			recv_deserializer: serde_pipe::Deserializer::new(),
			recv_deserializer_given: false,
		}
		.poll(notifier)
	}

	pub fn poll(mut self, notifier: &impl Notifier) -> InnerConnectedPoll {
		assert!(!self.connection.connecting());
		let mut progress = true;
		loop {
			if self.connection.sendable() {
				while self.connection.send_avail().unwrap() > 0 && self.send_serializer.pull_avail()
				{
					self.connection.send(notifier).unwrap()(self.send_serializer.pull().unwrap()());
					progress = true;
				}
			}
			if self.connection.recvable() {
				while self.connection.recv_avail().unwrap() > 0
					&& self.recv_deserializer.push_avail()
				{
					self.recv_deserializer.push().unwrap()(
						self.connection.recv(notifier).unwrap()(),
					);
					progress = true;
				}
			}
			if !progress {
				break;
			}
			progress = false;
			self.connection.poll(notifier);
		}
		if !self.connection.recvable() && self.recv_deserializer.empty().is_none() {
			return match InnerRemoteClosed::new(
				self.connection,
				self.send_serializer,
				false,
				notifier,
			) {
				InnerRemoteClosedPoll::RemoteClosed(remote_closed) => {
					InnerConnectedPoll::RemoteClosed(remote_closed)
				}
				InnerRemoteClosedPoll::Killed => InnerConnectedPoll::Killed,
			};
		}
		if !self.connection.valid() {
			return InnerConnectedPoll::Killed;
		}
		InnerConnectedPoll::Connected(self)
	}

	pub fn send_avail(&self) -> bool {
		self.send_serializer.push_avail()
	}

	pub fn send<T: Serialize + 'static>(&mut self, t: T, notifier: &impl Notifier) {
		self.send_serializer.push().unwrap()(t);
		notifier.queue();
	}

	pub fn recv_avail<T: DeserializeOwned + 'static, E: Notifier>(&mut self, notifier: &E) -> bool {
		if !self.recv_deserializer_given {
			self.recv_deserializer_given = true;
			notifier.queue(); // TODO: we only actually need to do this if self.poll() is gonna return Either::Right
		}
		self.recv_deserializer.pull::<T>().is_some()
	}

	pub fn recv<T: DeserializeOwned + 'static>(&mut self, notifier: &impl Notifier) -> T {
		self.recv_deserializer_given = false;
		let ret = self.recv_deserializer.pull::<T>().unwrap()();
		notifier.queue();
		ret
	}

	pub fn drain(mut self, notifier: &impl Notifier) -> InnerRemoteClosedPoll {
		if let Some(empty) = self.recv_deserializer.empty() {
			empty();
		}
		InnerRemoteClosed::new(self.connection, self.send_serializer, true, notifier)
	}

	pub fn close(self, notifier: &impl Notifier) -> InnerLocalClosedPoll {
		InnerLocalClosed::new(
			self.connection,
			self.send_serializer,
			self.recv_deserializer,
			self.recv_deserializer_given,
			notifier,
		)
	}
}

#[derive(Debug)]
pub enum InnerRemoteClosedPoll {
	RemoteClosed(InnerRemoteClosed),
	Killed,
}
#[derive(Debug)]
pub struct InnerRemoteClosed {
	connection: Connection,
	send_serializer: serde_pipe::Serializer,
	drain: bool,
}
impl InnerRemoteClosed {
	fn new(
		connection: Connection, send_serializer: serde_pipe::Serializer, drain: bool,
		notifier: &impl Notifier,
	) -> InnerRemoteClosedPoll {
		Self {
			connection,
			send_serializer,
			drain,
		}
		.poll(notifier)
	}

	pub fn poll(mut self, notifier: &impl Notifier) -> InnerRemoteClosedPoll {
		if self.drain && !self.connection.recvable() {
			self.drain = false;
		}
		if self.drain {
			let mut progress = false;
			while self.connection.recv_avail().unwrap() > 0 {
				let _ = self.connection.recv(notifier).unwrap()();
				progress = true;
			}
			if progress {
				self.connection.poll(notifier);
			}
			if !self.connection.recvable() {
				self.drain = false;
			}
		} else {
			assert!(!self.connection.recvable());
		}
		let mut progress = true;
		loop {
			if self.connection.sendable() {
				while self.connection.send_avail().unwrap() > 0 && self.send_serializer.pull_avail()
				{
					self.connection.send(notifier).unwrap()(self.send_serializer.pull().unwrap()());
					progress = true;
				}
			}
			if !progress {
				break;
			}
			progress = false;
			self.connection.poll(notifier);
		}
		if !self.connection.valid() {
			return InnerRemoteClosedPoll::Killed;
		}
		InnerRemoteClosedPoll::RemoteClosed(self)
	}

	pub fn send_avail(&self) -> bool {
		self.send_serializer.push_avail()
	}

	pub fn send<T: Serialize + 'static>(&mut self, t: T, notifier: &impl Notifier) {
		self.send_serializer.push().unwrap()(t);
		notifier.queue();
	}

	pub fn close(self, notifier: &impl Notifier) -> InnerClosingPoll {
		InnerClosing::new(self.connection, self.send_serializer, false, notifier)
	}
}

#[derive(Debug)]
pub enum InnerLocalClosedPoll {
	LocalClosed(InnerLocalClosed),
	Closing(InnerClosing),
	Closed,
	Killed,
}
#[derive(Debug)]
pub struct InnerLocalClosed {
	connection: Connection,
	send_serializer: serde_pipe::Serializer,
	recv_deserializer: serde_pipe::Deserializer,
	recv_deserializer_given: bool,
}
impl InnerLocalClosed {
	fn new(
		connection: Connection, send_serializer: serde_pipe::Serializer,
		recv_deserializer: serde_pipe::Deserializer, recv_deserializer_given: bool,
		notifier: &impl Notifier,
	) -> InnerLocalClosedPoll {
		Self {
			connection,
			send_serializer,
			recv_deserializer,
			recv_deserializer_given,
		}
		.poll(notifier)
	}

	pub fn poll(mut self, notifier: &impl Notifier) -> InnerLocalClosedPoll {
		assert!(!self.connection.connecting());
		let mut progress = true;
		loop {
			if self.connection.sendable() {
				while self.connection.send_avail().unwrap() > 0 && self.send_serializer.pull_avail()
				{
					self.connection.send(notifier).unwrap()(self.send_serializer.pull().unwrap()());
					progress = true;
				}
			}
			if self.connection.recvable() {
				while self.connection.recv_avail().unwrap() > 0
					&& self.recv_deserializer.push_avail()
				{
					self.recv_deserializer.push().unwrap()(
						self.connection.recv(notifier).unwrap()(),
					);
					progress = true;
				}
			}
			if !progress {
				break;
			}
			progress = false;
			self.connection.poll(notifier);
		}
		if self.connection.sendable() && !self.send_serializer.pull_avail() {
			self.connection.close(notifier).unwrap()();
		}
		// if !self.connection.recvable() {
		// 	assert!(!self.recv_deserializer.push_avail(), "{}: CLOSED WHILE PENDING RECV", ::internal::pid());
		// }
		if !self.connection.recvable() && self.recv_deserializer.empty().is_none() {
			// self.recv_deserializer.pull_avail() {
			// assert!(!self.recv_deserializer_given);
			return match InnerClosing::new(self.connection, self.send_serializer, false, notifier) {
				InnerClosingPoll::Closing(closing) => InnerLocalClosedPoll::Closing(closing),
				InnerClosingPoll::Closed => InnerLocalClosedPoll::Closed,
				InnerClosingPoll::Killed => InnerLocalClosedPoll::Killed,
			};
		}
		if !self.connection.valid() {
			return InnerLocalClosedPoll::Killed;
		}
		InnerLocalClosedPoll::LocalClosed(self)
	}

	pub fn recv_avail<T: DeserializeOwned + 'static, E: Notifier>(&mut self, notifier: &E) -> bool {
		if !self.recv_deserializer_given {
			self.recv_deserializer_given = true;
			notifier.queue(); // TODO: we only actually need to do this if self.poll() is gonna return Either::Right
		}
		self.recv_deserializer.pull::<T>().is_some()
	}

	pub fn recv<T: DeserializeOwned + 'static>(&mut self, notifier: &impl Notifier) -> T {
		self.recv_deserializer_given = false;
		let ret = self.recv_deserializer.pull::<T>().unwrap()();
		notifier.queue();
		ret
	}

	pub fn drain(mut self, notifier: &impl Notifier) -> InnerClosingPoll {
		if let Some(empty) = self.recv_deserializer.empty() {
			empty();
		}
		InnerClosing::new(self.connection, self.send_serializer, true, notifier)
	}
}

#[derive(Debug)]
pub enum InnerClosingPoll {
	Closing(InnerClosing),
	Closed,
	Killed,
}
#[derive(Debug)]
pub struct InnerClosing {
	connection: Connection,
	send_serializer: serde_pipe::Serializer,
	drain: bool,
}
impl InnerClosing {
	fn new(
		connection: Connection, send_serializer: serde_pipe::Serializer, drain: bool,
		notifier: &impl Notifier,
	) -> InnerClosingPoll {
		Self {
			connection,
			send_serializer,
			drain,
		}
		.poll(notifier)
	}

	pub fn poll(mut self, notifier: &impl Notifier) -> InnerClosingPoll {
		if self.drain && !self.connection.recvable() {
			self.drain = false;
		}
		if self.drain {
			let mut progress = false;
			while self.connection.recv_avail().unwrap() > 0 {
				let _ = self.connection.recv(notifier).unwrap()();
				progress = true;
			}
			if progress {
				self.connection.poll(notifier);
			}
			if !self.connection.recvable() {
				self.drain = false;
			}
		} else {
			assert!(!self.connection.recvable());
		}
		let mut progress = true;
		loop {
			if self.connection.sendable() {
				while self.connection.send_avail().unwrap() > 0 && self.send_serializer.pull_avail()
				{
					self.connection.send(notifier).unwrap()(self.send_serializer.pull().unwrap()());
					progress = true;
				}
			}
			if !progress {
				break;
			}
			progress = false;
			self.connection.poll(notifier);
		}
		if self.connection.sendable() && !self.send_serializer.pull_avail() {
			self.connection.close(notifier).unwrap()();
		}
		if !self.connection.valid() {
			return InnerClosingPoll::Killed;
		}
		if self.connection.closed() {
			return InnerClosingPoll::Closed;
		}
		InnerClosingPoll::Closing(self)
	}
}