Skip to content

Commit

Permalink
Read Provider for usage in mydht read service (skipping access to tunnel
Browse files Browse the repository at this point in the history
service)
  • Loading branch information
cheme committed Oct 26, 2017
1 parent c6faa7d commit 7f6a160
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 10 deletions.
102 changes: 101 additions & 1 deletion src/full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use bincode::{
use super::{
Peer,
TunnelReader,
TunnelReadProv,
TunnelReaderError,
TunnelReaderNoRep,
TunnelWriterExt,
Expand Down Expand Up @@ -91,7 +92,7 @@ pub trait GenTunnelTraits {
/// Reply writer use only to include a reply envelope
type RW : TunnelWriterExt;
type REP : ReplyProvider<Self::P, MultipleReplyInfo<<Self::P as Peer>::Address>>;
type SP : SymProvider<Self::SSW,Self::SSR>;
type SP : SymProvider<Self::SSW,Self::SSR> + Clone;
type TNR : TunnelNoRep<P=Self::P,W=Self::RW>;
type EP : ErrorProvider<Self::P, MultipleErrorInfo>;
}
Expand All @@ -118,6 +119,13 @@ pub struct Full<TT : GenTunnelTraits> {
pub reply_once_buf_size : usize,
pub _p : PhantomData<TT>,
}
pub struct FullReadProv<TT : GenTunnelTraits> {
pub me : TT::P,
pub limiter_proto_r : TT::LR,
pub sym_prov : TT::SP,
pub _p : PhantomData<TT>,
}


type Shadows<P,RI,EI,LW,TW> = CompExtW<MultiWExt<TunnelShadowW<P,RI,EI,LW,TW>>,LW>;
//type Shadows<P : Peer, RI : Info, EI : Info,LW : ExtWrite,TW : TunnelWriterExt> = CompExtW<MultiWExt<TunnelShadowW<P,RI,EI,LW,TW>>,LW>;
Expand Down Expand Up @@ -274,6 +282,7 @@ pub fn clone_shadows_keys<P : Peer, RI : RepInfo, EI : Info,LW : ExtWrite,TW : T
res
}
impl<TT : GenTunnelTraits> TunnelNoRep for Full<TT> {
type ReadProv = FullReadProv<TT>;
type P = TT::P;
type W = FullW<MultipleReplyInfo<<TT::P as Peer>::Address>, MultipleErrorInfo, TT::P, TT::LW,TT::RW>;
type TR = FullR<MultipleReplyInfo<<TT::P as Peer>::Address>, MultipleErrorInfo, TT::P, TT::LR>;
Expand Down Expand Up @@ -484,8 +493,99 @@ impl<TT : GenTunnelTraits> TunnelNoRep for Full<TT> {
},
}
}

fn new_tunnel_read_prov (&self) -> Self::ReadProv {
FullReadProv {
me : self.me.clone(),
limiter_proto_r : self.limiter_proto_r.clone(),
sym_prov : self.sym_prov.clone(),
_p : PhantomData,
}
}
}
impl<TT : GenTunnelTraits> TunnelReadProv for FullReadProv<TT> {
type T = Full<TT>;
fn new_reader (&mut self) -> <Self::T as TunnelNoRep>::TR {
let s = self.me.new_shadr();
FullR {
error_code : None,
state: TunnelState::TunnelState,
current_cache_id: None,
current_reply_info: None,
current_error_info: None,
next_proxy_peer : None,
tunnel_id : None, // TODO useless remove??
shad : CompExtR(s,self.limiter_proto_r.clone()),
content_limiter : self.limiter_proto_r.clone(),
need_content_limiter : false,
read_cache : false,
}
}

fn new_dest_reader<R : Read> (&mut self, mut or : <Self::T as TunnelNoRep>::TR, r : &mut R) -> Result<Option<<Self::T as TunnelNoRep>::DR>> {
Ok(match or.state {
TunnelState::TunnelState => {
None
},
TunnelState::QueryOnce => {
Some(DestFull {
origin_read : or,
kind : DestFullKind::Id,
})
},
TunnelState::QueryCached => {
// same as query once because no bidirect cache yet (only replycache route use cache and we
// do not resend) and previous cache id in origin read
Some(DestFull {
origin_read : or,
kind : DestFullKind::Id,
})
},
TunnelState::ReplyOnce => {

// TODO this is awkward : move key reading into or method to avoid those lifetime related
// copies TODO duplicate code move in function
let mut current_error_info = or.current_error_info;
or.current_error_info = None;
let mut current_reply_info = or.current_reply_info;
or.current_reply_info = None;
let mut ks : Vec<Vec<u8>>;
{
let mut inr = CompExtRInner(r, &mut or);
let len : usize = bin_decode(&mut inr, Infinite).map_err(|e|BincErr(e))?;
ks = Vec::with_capacity(len);
for _ in 0..len {
current_error_info.as_mut().unwrap().read_read_info(&mut inr)?;// should always be init.
current_reply_info.as_mut().unwrap().read_read_info(&mut inr)?;// should always be init.
// TODO replace unwrap by return
let k : &Vec<u8> = current_reply_info.as_ref().ok_or(IoError::new(IoErrorKind::Other, "unparsed reply info"))?
.get_reply_key().as_ref().ok_or(IoError::new(IoErrorKind::Other, "no reply key for reply info : wrong reply info"))?;
ks.push (k.clone());
}
}
let cr = new_dest_cached_reader_ext(ks.into_iter().map(|k|self.sym_prov.new_sym_reader(k)).collect(), self.limiter_proto_r.clone());

or.current_reply_info = current_reply_info;
or.current_error_info = current_error_info;
or.read_end(r)?;
Some(DestFull {
origin_read : or,
kind : DestFullKind::Multi(cr),
})
},
TunnelState::ReplyCached => {
None
},
TunnelState::QErrorCached => {
None
},
})
}
}




impl<TT : GenTunnelTraits> Tunnel for Full<TT> {
// reply info info needed to established conn
type RI = MultipleReplyInfo<<TT::P as Peer>::Address>;
Expand Down
20 changes: 11 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ pub trait TunnelReaderExt : ExtRead {
/// Tunnel trait could be in a single tunnel impl, but we use multiple to separate concerns a bit
/// When a tunnel implement multiple trait it has property of all trait ('new_writer' of a
/// TunnelNoRep if Tunnel trait is also implemented will write necessary info for reply).
pub trait TunnelNoRep {
// type ReadProv : TunnelReadProv<TR=Self::TR,DR=DR>,
pub trait TunnelNoRep : Sized {
type ReadProv : TunnelReadProv<T=Self>;
/// Peer with their address and their asym shadow scheme
type P : Peer;
/// actual writer (tunnel logic using tunnel writer)
Expand All @@ -149,17 +149,19 @@ pub trait TunnelNoRep {
fn new_proxy_writer (&mut self, Self::TR, &<Self::P as Peer>::Address) -> Result<(Self::PW, <Self::P as Peer>::Address)>;
fn new_dest_reader<R : Read> (&mut self, Self::TR, &mut R) -> Result<Self::DR>;

//fn new_tunnel_read_prov (&self) -> Self::ReadProv;
fn new_tunnel_read_prov (&self) -> Self::ReadProv;
}
/*

/// Subset of TunelReader for reading without reverence to cache or route provider
/// Can be use directly with a read stream without accessing a central tunnel impl.
pub trait TunnelReadProv {
type TR : TunnelReaderNoRep;
type DR : TunnelReaderExt<TR=Self::TR>;
fn new_reader (&mut self) -> Self::TR;
fn new_dest_reader<R : Read> (&mut self, Self::TR, &mut R) -> Result<Self::DR>;
}*/
type T : TunnelNoRep;
fn new_reader (&mut self) -> <Self::T as TunnelNoRep>::TR;
/// same as tunnel dest reader but not mandatory (for instance we do not want to share cache
/// informations)
fn new_dest_reader<R : Read> (&mut self, <Self::T as TunnelNoRep>::TR, &mut R) -> Result<Option<<Self::T as TunnelNoRep>::DR>>;
}

/// tunnel with reply
pub trait Tunnel : TunnelNoRep where Self::TR : TunnelReader<RI=Self::RI> {
// reply info info needed to established conn -> TODO type reply info looks useless : we create reply
Expand Down
10 changes: 10 additions & 0 deletions src/nope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use readwrite_comp::{
use super::{
TunnelWriter,
TunnelNoRep,
TunnelReadProv,
TunnelWriterExt,
TunnelReaderExt,
TunnelCache,
Expand Down Expand Up @@ -257,6 +258,7 @@ impl<P : Peer> TunnelNope<P> {
}
}
impl<P : Peer> TunnelNoRep for TunnelNope<P> {
type ReadProv = Self;
type P = P;
type W = Nope;
type TR = Nope;
Expand All @@ -268,4 +270,12 @@ impl<P : Peer> TunnelNoRep for TunnelNope<P> {
fn new_writer_with_route (&mut self, _ : &[&Self::P]) -> Self::W {Nope}
fn new_proxy_writer (&mut self, _ : Self::TR, _ : &<Self::P as Peer>::Address) -> Result<(Self::PW,<Self::P as Peer>::Address)> {panic!("Nope do not implement that")}
fn new_dest_reader<R : Read> (&mut self, _ : Self::TR, _ : &mut R) -> Result<Self::DR> {Ok(Nope)}
fn new_tunnel_read_prov (&self) -> Self::ReadProv {TunnelNope::new()}
}
impl<P : Peer> TunnelReadProv for TunnelNope<P> {
type T = Self;
fn new_reader (&mut self) -> <Self::T as TunnelNoRep>::TR { Nope }
fn new_dest_reader<R : Read> (&mut self, _ : <Self::T as TunnelNoRep>::TR, _ : &mut R) -> Result<Option<<Self::T as TunnelNoRep>::DR>> { Ok(Some(Nope)) }
}


0 comments on commit 7f6a160

Please sign in to comment.