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
use std::{
pin::Pin,
task::{Context, Poll},
};
use derive_more::From;
use futures::Stream;
use thiserror::Error;
use tokio::sync::{mpsc, oneshot};
#[derive(Debug, Error)]
#[error("the RequestSender cannot be used because the state machine shut down")]
pub struct StateMachineShutdown;
use crate::{
mask::object::MaskObject,
LocalSeedDict,
ParticipantPublicKey,
PetError as Error,
SumParticipantEphemeralPublicKey,
SumParticipantPublicKey,
UpdateParticipantPublicKey,
};
pub struct SumRequest {
pub participant_pk: SumParticipantPublicKey,
pub ephm_pk: SumParticipantEphemeralPublicKey,
}
pub struct UpdateRequest {
pub participant_pk: UpdateParticipantPublicKey,
pub local_seed_dict: LocalSeedDict,
pub masked_model: MaskObject,
}
pub struct Sum2Request {
pub participant_pk: ParticipantPublicKey,
pub mask: MaskObject,
}
pub type SumResponse = Result<(), Error>;
pub type UpdateResponse = Result<(), Error>;
pub type Sum2Response = Result<(), Error>;
pub enum Request {
Sum((SumRequest, oneshot::Sender<SumResponse>)),
Update((UpdateRequest, oneshot::Sender<UpdateResponse>)),
Sum2((Sum2Request, oneshot::Sender<Sum2Response>)),
}
#[derive(From)]
pub struct RequestSender<R>(mpsc::UnboundedSender<R>);
impl<R> Clone for RequestSender<R> {
fn clone(&self) -> Self {
RequestSender(self.0.clone())
}
}
impl<R> RequestSender<R> {
pub fn send(&self, req: R) -> Result<(), StateMachineShutdown> {
self.0.send(req).map_err(|_| StateMachineShutdown)
}
}
#[derive(From)]
pub struct RequestReceiver<R>(mpsc::UnboundedReceiver<R>);
impl<R> Stream for RequestReceiver<R> {
type Item = R;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
trace!("RequestReceiver: polling");
Pin::new(&mut self.get_mut().0).poll_next(cx)
}
}
impl<R> RequestReceiver<R> {
pub fn new() -> (Self, RequestSender<R>) {
let (tx, rx) = mpsc::unbounded_channel::<R>();
let receiver = RequestReceiver::from(rx);
let handle = RequestSender::from(tx);
(receiver, handle)
}
pub fn close(&mut self) {
self.0.close()
}
pub async fn recv(&mut self) -> Option<R> {
self.0.recv().await
}
pub fn try_recv(&mut self) -> Result<R, tokio::sync::mpsc::error::TryRecvError> {
self.0.try_recv()
}
}
#[cfg(test)]
mod tests {
use super::*;
fn drop<T>(_t: T) {}
#[tokio::test]
async fn test_channel() {
let (mut recv, snd) = RequestReceiver::<()>::new();
snd.send(()).unwrap();
recv.recv().await.unwrap();
drop(snd);
assert!(recv.recv().await.is_none());
}
}