pub mod postcard;
use crate::{
gossipsub::messages::{
GossipTopicTag,
GossipsubBroadcastRequest,
GossipsubMessage,
},
request_response::messages::{
NetworkResponse,
OutboundResponse,
RequestMessage,
ResponseMessage,
},
};
use libp2p::request_response::RequestResponseCodec;
use std::io;
pub trait GossipsubCodec {
type RequestMessage;
type ResponseMessage;
fn encode(&self, data: Self::RequestMessage) -> Result<Vec<u8>, io::Error>;
fn decode(
&self,
encoded_data: &[u8],
gossipsub_topic: GossipTopicTag,
) -> Result<Self::ResponseMessage, io::Error>;
}
pub trait RequestResponseConverter {
type OutboundResponse;
type NetworkResponse;
type ResponseMessage;
fn convert_to_network_response(
&self,
res_msg: &Self::OutboundResponse,
) -> Result<Self::NetworkResponse, io::Error>;
fn convert_to_response(
&self,
inter_msg: &Self::NetworkResponse,
) -> Result<Self::ResponseMessage, io::Error>;
}
pub trait NetworkCodec:
GossipsubCodec<
RequestMessage = GossipsubBroadcastRequest,
ResponseMessage = GossipsubMessage,
> + RequestResponseCodec<Request = RequestMessage, Response = NetworkResponse>
+ RequestResponseConverter<
NetworkResponse = NetworkResponse,
OutboundResponse = OutboundResponse,
ResponseMessage = ResponseMessage,
> + Clone
+ Send
+ 'static
{
fn get_req_res_protocol(&self) -> <Self as RequestResponseCodec>::Protocol;
}