-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(network): implement custom stream protocol (#391)
* feat(network): implement custom stream protocol * chore(network): compress open stream command handler
- Loading branch information
Showing
10 changed files
with
386 additions
and
74 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
use std::pin::Pin; | ||
use std::task::{Context, Poll}; | ||
|
||
use futures_util::{Sink as FuturesSink, SinkExt, Stream as FuturesStream}; | ||
use libp2p::PeerId; | ||
use tokio::io::BufStream; | ||
use tokio_util::codec::Framed; | ||
use tokio_util::compat::{Compat, FuturesAsyncReadCompatExt}; | ||
|
||
use super::{types, EventLoop}; | ||
|
||
mod codec; | ||
|
||
pub use codec::{CodecError, Message}; | ||
|
||
pub(crate) const CALIMERO_STREAM_PROTOCOL: libp2p::StreamProtocol = | ||
libp2p::StreamProtocol::new("/calimero/stream/0.0.1"); | ||
|
||
#[derive(Debug)] | ||
pub struct Stream { | ||
inner: Framed<BufStream<Compat<libp2p::Stream>>, codec::MessageJsonCodec>, | ||
} | ||
|
||
impl Stream { | ||
pub fn new(stream: libp2p::Stream) -> Self { | ||
let stream = BufStream::new(stream.compat()); | ||
let stream = Framed::new(stream, codec::MessageJsonCodec); | ||
Stream { inner: stream } | ||
} | ||
} | ||
|
||
impl FuturesStream for Stream { | ||
type Item = Result<Message, CodecError>; | ||
|
||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
let inner = Pin::new(&mut self.get_mut().inner); | ||
inner.poll_next(cx) | ||
} | ||
} | ||
|
||
impl FuturesSink<Message> for Stream { | ||
type Error = CodecError; | ||
|
||
fn poll_ready(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
self.inner.poll_ready_unpin(cx) | ||
} | ||
|
||
fn start_send(mut self: Pin<&mut Self>, item: Message) -> Result<(), Self::Error> { | ||
self.inner.start_send_unpin(item) | ||
} | ||
|
||
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
self.inner.poll_flush_unpin(cx) | ||
} | ||
|
||
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
self.inner.poll_close_unpin(cx) | ||
} | ||
} | ||
|
||
impl EventLoop { | ||
pub(crate) async fn handle_incoming_stream( | ||
&mut self, | ||
(peer, stream): (PeerId, libp2p::Stream), | ||
) { | ||
self.event_sender | ||
.send(types::NetworkEvent::StreamOpened { | ||
peer_id: peer, | ||
stream: Stream::new(stream), | ||
}) | ||
.await | ||
.expect("Failed to send stream opened event"); | ||
} | ||
|
||
pub(crate) async fn open_stream(&mut self, peer_id: PeerId) -> eyre::Result<Stream> { | ||
let stream = match self | ||
.swarm | ||
.behaviour() | ||
.stream | ||
.new_control() | ||
.open_stream(peer_id, CALIMERO_STREAM_PROTOCOL) | ||
.await | ||
{ | ||
Ok(stream) => stream, | ||
Err(err) => { | ||
eyre::bail!("Failed to open stream: {:?}", err); | ||
} | ||
}; | ||
|
||
Ok(Stream::new(stream)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
use bytes::{Bytes, BytesMut}; | ||
use serde::{Deserialize, Serialize}; | ||
use thiserror::Error; | ||
use tokio_util::codec::{Decoder, Encoder, LengthDelimitedCodec}; | ||
|
||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] | ||
pub struct Message { | ||
pub data: Vec<u8>, | ||
} | ||
|
||
#[derive(Debug, Error)] | ||
#[error("CodecError")] | ||
pub enum CodecError { | ||
StdIo(#[from] std::io::Error), | ||
SerDe(serde_json::Error), | ||
} | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct MessageJsonCodec; | ||
|
||
impl Decoder for MessageJsonCodec { | ||
type Item = Message; | ||
type Error = CodecError; | ||
|
||
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> { | ||
let mut length_codec = LengthDelimitedCodec::new(); | ||
let Some(frame) = length_codec.decode(src)? else { | ||
return Ok(None); | ||
}; | ||
|
||
serde_json::from_slice(&frame) | ||
.map(Some) | ||
.map_err(CodecError::SerDe) | ||
} | ||
} | ||
|
||
impl Encoder<Message> for MessageJsonCodec { | ||
type Error = CodecError; | ||
|
||
fn encode(&mut self, item: Message, dst: &mut BytesMut) -> Result<(), Self::Error> { | ||
let mut length_codec = LengthDelimitedCodec::new(); | ||
let json = serde_json::to_vec(&item).map_err(CodecError::SerDe)?; | ||
|
||
length_codec | ||
.encode(Bytes::from(json), dst) | ||
.map_err(CodecError::StdIo) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use futures_util::StreamExt; | ||
use tokio_test::io::Builder; | ||
use tokio_util::codec::FramedRead; | ||
|
||
#[test] | ||
fn test_my_frame_encoding_decoding() { | ||
let request = Message { | ||
data: "Hello".bytes().collect(), | ||
}; | ||
let response = Message { | ||
data: "World".bytes().collect(), | ||
}; | ||
|
||
let mut buffer = BytesMut::new(); | ||
let mut codec = MessageJsonCodec; | ||
codec.encode(request.clone(), &mut buffer).unwrap(); | ||
codec.encode(response.clone(), &mut buffer).unwrap(); | ||
|
||
let decoded_request = codec.decode(&mut buffer).unwrap(); | ||
assert_eq!(decoded_request, Some(request)); | ||
|
||
let decoded_response = codec.decode(&mut buffer).unwrap(); | ||
assert_eq!(decoded_response, Some(response)); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_multiple_objects_stream() { | ||
let request = Message { | ||
data: "Hello".bytes().collect(), | ||
}; | ||
let response = Message { | ||
data: "World".bytes().collect(), | ||
}; | ||
|
||
let mut buffer = BytesMut::new(); | ||
let mut codec = MessageJsonCodec; | ||
codec.encode(request.clone(), &mut buffer).unwrap(); | ||
codec.encode(response.clone(), &mut buffer).unwrap(); | ||
|
||
let mut stream = Builder::new().read(&buffer.freeze()).build(); | ||
let mut framed = FramedRead::new(&mut stream, MessageJsonCodec); | ||
|
||
let decoded_request = framed.next().await.unwrap().unwrap(); | ||
assert_eq!(decoded_request, request); | ||
|
||
let decoded_response = framed.next().await.unwrap().unwrap(); | ||
assert_eq!(decoded_response, response); | ||
|
||
let decoded3 = framed.next().await; | ||
assert_eq!(decoded3.is_none(), true); | ||
} | ||
} |
Oops, something went wrong.