Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. use bytes::BytesMut;
  2. use futures::Future; // 0.1.28
  3. use futures::future::ok;
  4. use tokio;
  5. use tokio::codec::{Framed, Encoder, Decoder};
  6. use tokio::net::TcpStream;
  7.  
  8. use std::io::Error;
  9. use std::thread;
  10.  
  11. /// Exonum message
  12. pub struct SignedMessage;
  13.  
  14. // Exonum handshake return type (no generic param)
  15. type HandshakeResult =
  16. Box<dyn Future<Item = (Framed<TcpStream, MessageCodec>, Vec<u8>), Error = failure::Error>>;
  17.  
  18. // Exonum handshake trait (no generic params and self by uniq alias)
  19. pub trait Handshake {
  20. fn listen(&mut self, stream: TcpStream) -> HandshakeResult;
  21. fn send(&mut self, stream: TcpStream) -> HandshakeResult;
  22. }
  23.  
  24. /// Exonum's noise handshake
  25. pub struct NoiseHandshake;
  26.  
  27. impl Handshake for NoiseHandshake {
  28. #[allow(unreachable_code, unused_variables)]
  29. fn listen(&mut self, stream: TcpStream) -> HandshakeResult {
  30. let connect_msg = unimplemented!("Same as original impl");
  31. // Build generic codec using results of noise handshake
  32. let noise_codec = MessageCodec { codec_impl: Box::new(NoiseCodec {}) };
  33. Box::new(ok((Framed::new(stream, noise_codec), connect_msg)))
  34. }
  35.  
  36. #[allow(unreachable_code, unused_variables)]
  37. fn send(&mut self, stream: TcpStream) -> HandshakeResult {
  38. let connect_msg = unimplemented!("Same as original impl");
  39. // Build generic codec using results of noise handshake
  40. let noise_codec = MessageCodec { codec_impl: Box::new(NoiseCodec {}) };
  41. Box::new(ok((Framed::new(stream, noise_codec), connect_msg)))
  42. }
  43. }
  44.  
  45. /// Plain handshake
  46. pub struct PlainHandshake;
  47.  
  48. impl Handshake for PlainHandshake {
  49. #[allow(unreachable_code, unused_variables)]
  50. fn listen(&mut self, stream: TcpStream) -> HandshakeResult {
  51. let connect_msg = unimplemented!("Receive Connect message");
  52. unimplemented!("Send connect message");
  53. // Build generic codec using plain codec
  54. let plain_codec = MessageCodec { codec_impl: Box::new(PlainCodec {}) };
  55. Box::new(ok((Framed::new(stream, plain_codec), connect_msg)))
  56. }
  57.  
  58. #[allow(unreachable_code, unused_variables)]
  59. fn send(&mut self, stream: TcpStream) -> HandshakeResult {
  60. unimplemented!("Send Connect message");
  61. let connect_msg = unimplemented!("Receive connect message");
  62. // Build generic codec using plain codec
  63. let plain_codec = MessageCodec { codec_impl: Box::new(PlainCodec {}) };
  64. Box::new(ok((Framed::new(stream, plain_codec), connect_msg)))
  65. }
  66. }
  67.  
  68. /// Same as `Encoder + Decoder`, but with explicitly specified associated types.
  69. pub trait Codec {
  70. fn encode(&mut self, msg: SignedMessage, buf: &mut BytesMut) -> Result<(), Error>;
  71. fn decode(&mut self, buf: &mut BytesMut) -> Result<Option<Vec<u8>>, Error>;
  72. }
  73.  
  74. /// Conrete codec for Noise. Same as existing exonum `MessageCodec`.
  75. pub struct NoiseCodec;
  76.  
  77. impl Codec for NoiseCodec {
  78. fn encode(&mut self, _msg: SignedMessage, _buf: &mut BytesMut) -> Result<(), Error> {
  79. unimplemented!("Same as original impl of Encode for MessageCodec")
  80. }
  81. fn decode(&mut self, _buf: &mut BytesMut) -> Result<Option<Vec<u8>>, Error> {
  82. unimplemented!("Same as original impl of Decode for MessageCodec")
  83. }
  84. }
  85.  
  86. /// Concrete codec for a cannel with disabled Noise.
  87. pub struct PlainCodec;
  88.  
  89. impl Codec for PlainCodec {
  90. fn encode(&mut self, _msg: SignedMessage, _buf: &mut BytesMut) -> Result<(), Error> {
  91. unimplemented!(
  92. "Same as original impl of Encode for MessageCodec, \
  93. but without self.session.encrypt_msg")
  94. }
  95. fn decode(&mut self, _buf: &mut BytesMut) -> Result<Option<Vec<u8>>, Error> {
  96. unimplemented!(
  97. "Same as original impl of Decode for MessageCodec, \
  98. but without self.session.decrypt_msg")
  99. }
  100. }
  101.  
  102. /// Dynamically parametrized codec that will actually be used for communication.
  103. pub struct MessageCodec {
  104. codec_impl: Box<Codec + 'static>,
  105. }
  106.  
  107. impl Decoder for MessageCodec {
  108. type Item = Vec<u8>;
  109. type Error = Error;
  110. fn decode(&mut self, buf: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
  111. self.codec_impl.decode(buf)
  112. }
  113. }
  114.  
  115. impl Encoder for MessageCodec {
  116. type Item = SignedMessage;
  117. type Error = Error;
  118. fn encode(&mut self, msg: Self::Item, buf: &mut BytesMut) -> Result<(), Self::Error> {
  119. self.codec_impl.encode(msg, buf)
  120. }
  121. }
  122.  
  123. #[allow(unreachable_code, unused_variables)]
  124. pub fn handshake() {
  125. let (initiator, receiver): (TcpStream, TcpStream) = unimplemented!();
  126.  
  127. thread::spawn(move || {
  128. let mut handshake: Box<dyn Handshake> = Box::new(NoiseHandshake {});
  129. let (stream, connect_message) = handshake.listen(initiator).wait().unwrap();
  130. });
  131.  
  132. thread::spawn(move || {
  133. let mut handshake: Box<dyn Handshake> = Box::new(NoiseHandshake {});
  134. let (stream, connect_message) = handshake.send(receiver).wait().unwrap();
  135. });
  136. }
  137.  
  138. fn main() {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement