-
Notifications
You must be signed in to change notification settings - Fork 0
/
bip_frame_buffer.rs
168 lines (145 loc) · 4.88 KB
/
bip_frame_buffer.rs
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
use bbqueue::{
framed::{FrameConsumer, FrameGrantW, FrameProducer},
BBBuffer,
};
use cgisf_lib::{gen_sentence, SentenceConfigBuilder};
use rand::{thread_rng, Rng};
use sframe::{
error::SframeError,
frame::{EncryptedFrameView, FrameBuffer, MediaFrameView, Truncate},
key::{DecryptionKey, EncryptionKey},
CipherSuiteVariant,
};
use std::{thread, time::Duration};
const BUF_SIZE: usize = 1024;
static BIP_BUFFER: BBBuffer<BUF_SIZE> = BBBuffer::<1024>::new();
const SECRET: &[u8] = b"SUPER SECRET PW";
const KEY_ID: u64 = 42;
const VARIANT: CipherSuiteVariant = CipherSuiteVariant::AesGcm256Sha512;
struct ProducerBuffer<'a, const N: usize> {
producer: FrameProducer<'a, N>,
samples_to_commit: usize,
grant: Option<FrameGrantW<'a, N>>,
}
impl<'a, const N: usize> FrameBuffer for ProducerBuffer<'a, N> {
type BufferSlice = Self;
fn allocate(&mut self, size: usize) -> sframe::error::Result<&mut Self::BufferSlice> {
let grant = self
.producer
.grant(size)
.map_err(|err| SframeError::Other(format!("Could not acquire grant {:?}", err)))?;
self.grant = Some(grant);
self.samples_to_commit = size;
Ok(self)
}
}
impl<'a, const N: usize> AsRef<[u8]> for ProducerBuffer<'a, N> {
fn as_ref(&self) -> &[u8] {
if let Some(grant) = &self.grant {
grant
} else {
&[]
}
}
}
impl<'a, const N: usize> AsMut<[u8]> for ProducerBuffer<'a, N> {
fn as_mut(&mut self) -> &mut [u8] {
if let Some(grant) = &mut self.grant {
grant
} else {
&mut []
}
}
}
impl<'a, const N: usize> Truncate for ProducerBuffer<'a, N> {
fn truncate(&mut self, size: usize) {
// note: not strictly necessary, truncate is only used for decryption
self.samples_to_commit -= size;
}
}
impl<'a, const N: usize> ProducerBuffer<'a, N> {
fn commit(&mut self) {
if let Some(grant) = self.grant.take() {
grant.commit(self.samples_to_commit);
}
}
}
fn sleep(name: &str) {
// Sleep for a random time to simulate load
let t = Duration::from_millis(thread_rng().gen_range(100..2000));
println!("[{}] : Sleeping for {} ms", name, t.as_millis());
thread::sleep(t);
}
fn producer_task(producer: FrameProducer<BUF_SIZE>) {
let key = EncryptionKey::derive_from(VARIANT, KEY_ID, SECRET).unwrap();
let mut frame_count: u64 = 0;
let mut buffer = ProducerBuffer {
producer,
samples_to_commit: 0,
grant: None,
};
loop {
let payload = gen_sentence(SentenceConfigBuilder::random().build());
println!(
"[Producer] Commiting frame # {} with payload '{}'",
frame_count, payload
);
let media_frame = MediaFrameView::new(frame_count, &payload);
if let Err(err) = media_frame.encrypt_into(&key, &mut buffer) {
println!(
"[Producer] Failed to encrypt frame # {} due to {}",
frame_count, err
);
}
buffer.commit();
frame_count += 1;
sleep("Producer");
}
}
fn consumer_task(mut consumer: FrameConsumer<BUF_SIZE>) {
let mut key = DecryptionKey::derive_from(VARIANT, KEY_ID, SECRET).unwrap();
loop {
// Read data from the buffer
if let Some(grant) = consumer.read() {
if let Ok(encrypted_frame) = EncryptedFrameView::try_new(grant.as_ref()) {
let decrypted = encrypted_frame.decrypt(&mut key);
if let Err(err) = decrypted {
println!(
"[Consumer] Failed to encrypt frame # {} due to {}",
encrypted_frame.header().frame_count(),
err
);
continue;
}
let decrypted = decrypted.unwrap();
let payload = std::str::from_utf8(decrypted.payload()).unwrap();
println!(
"[Consumer] Consumed frame # {}: {}",
encrypted_frame.header().frame_count(),
payload
);
grant.release();
} else {
println!("[Consumer] Invalid frame in buffer!");
}
} else {
println!("[Consumer] No frame available!");
}
sleep("Consumer");
}
}
fn main() {
// Convert the buffer to a constant buffer for the producer thread
let (producer, consumer) = BIP_BUFFER.try_split_framed().unwrap();
// Spawn the producer thread
let producer_thread = thread::spawn(move || {
producer_task(producer);
});
// Spawn the consumer thread
let consumer_thread = thread::spawn(move || {
consumer_task(consumer);
});
// Wait for both threads to finish
producer_thread.join().unwrap();
consumer_thread.join().unwrap();
}