client.rs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. use rand::RngCore;
  2. use rand::rngs::ThreadRng;
  3. use std::thread::*;
  4. use std::sync::mpsc::*;
  5. use crate::params;
  6. pub struct Client {
  7. r: usize,
  8. thread_handle: JoinHandle<()>,
  9. incoming_query: SyncSender<usize>,
  10. incoming_response: SyncSender<Vec<u8>>,
  11. outgoing_data: Receiver<Vec<u8>>,
  12. pub pub_params: Vec<u8>,
  13. }
  14. impl Client {
  15. pub fn new(r: usize) -> Self {
  16. let (incoming_query, incoming_query_recv) = sync_channel(0);
  17. let (incoming_response, incoming_response_recv) = sync_channel(0);
  18. let (outgoing_data_send, outgoing_data) = sync_channel(0);
  19. let thread_handle = spawn(move || {
  20. let spiral_params = params::get_spiral_params(r);
  21. let mut clientrng = rand::thread_rng();
  22. let mut spiral_client =
  23. spiral_rs::client::Client::init(&spiral_params, &mut clientrng);
  24. // The first communication is the pub_params
  25. let pub_params = spiral_client.generate_keys().serialize();
  26. outgoing_data_send.send(pub_params).unwrap();
  27. });
  28. let pub_params = outgoing_data.recv().unwrap();
  29. Client {
  30. r,
  31. thread_handle,
  32. incoming_query,
  33. incoming_response,
  34. outgoing_data,
  35. pub_params,
  36. }
  37. }
  38. }