pub fn new
	(
		addr_in     : Addr<Self>                                                              ,
		addr_out    : Addr<Self>                                                              ,
		incoming    : impl BoundsIn<Wf>                                                       ,
		outgoing    : impl BoundsOut<Wf>                                                      ,
		exec        : impl SpawnHandle<Result<Response<Wf>, PeerErr>> + Send + Sync + 'static ,
		bp          : Option< Arc<Semaphore> >                                                ,
		grace_period: Option< Duration >                                                      ,
	)

		-> Result< Self, PeerErr >

	{
		trace!( "{}: Create peer", &addr_in );


		let exec: Arc< dyn SpawnHandle<Result<Response<Wf>, PeerErr>> + Send + Sync + 'static > = Arc::new(exec);
		let (nursery, nursery_stream) = Nursery::new( exec.clone() );


		let nursery_handle = exec.spawn_handle( Self::listen_request_results( nursery_stream, addr_out ) )

			.map_err( |_| -> PeerErr
			{
				let ctx = PeerErrCtx::default()

					.peer_id  ( addr_in.id()                                          )
					.peer_name( addr_in.name()                                        )
					.context  ( Some( "Request results stream for peer".to_string() ) )
				;

				PeerErr::Spawn{ ctx }
			})?
		;


		nursery.nurse( Self::listen_incoming( incoming, addr_in.clone(), bp.clone() ) )

			.map_err( |_| -> PeerErr
			{
				let ctx = PeerErrCtx
				{
					context: "Incoming stream for peer".to_string().into(),
					..Default::default()
				};

				PeerErr::Spawn{ ctx }
			})?
		;


		Ok( Self
		{
			id             : addr_in.id()               ,
			name           : addr_in.name()             ,
			outgoing       : Some( Box::new(outgoing) ) ,
			responses      : HashMap::new()             ,
			services       : HashMap::new()             ,
			pharos         : Pharos::default()          ,
			timeout        : Duration::from_secs(60)    ,
			backpressure   : bp                         ,
			permits        : Vec::new()                 ,
			closed         : false                      ,
			nursery_stream : Some( nursery_handle )     ,
			addr           : Some( addr_in )            ,
			nursery                                     ,
			grace_period                                ,

			// must not start at 0. Zero has a special meaning.
			//
			conn_id_counter: AtomicU64::new(1),
		})
	}
/// Create a new peer to represent a connection to some remote.
///
/// Ideally you don't have to call this method directly. Wire formats need to have a convenience function
/// to create your peer from an `AsyncRead`/`AsyncWrite`. They make some assumptions about default values and
/// if you need more control, you might have to call this directly.
///
/// In any case have a look at [`CborWF::create_peer`] to see how this is called.
///
/// `addr_in` and `addr_out`: These are addresses to this very actor (Peer). Since this actor sits at the
/// gate of both incoming and outgoing messages, we need to double up it's mailbox with a priority queue
/// in order to avoid a deadlock. See the [guide level docs](https://thespis-rs.github.io/thespis_guide/pitfalls.html#1deadlocks)
/// for some background information about this problem.
///
/// *grace_period*: When the remote closes the connection, we could immediately drop all outstanding tasks related to
/// this peer. This makes sense for a request-response type connection, as it doesn't make sense to
/// continue using resources processing requests for which we can no longer send the response. However
/// it is not always desirable. For one way information flow, we might want to finish processing all the
/// outstanding packets before closing down. This also applies when you send a `CloseConnection` message to
/// this peer locally.
//
Create a new peer to represent a connection to some remote.
Ideally you don't have to call this method directly. Wire formats need to have a convenience function
to create your peer from an `AsyncRead`/`AsyncWrite`. They make some assumptions about default values and
if you need more control, you might have to call this directly.

In any case have a look at [`CborWF::create_peer`] to see how this is called.

`addr_in` and `addr_out`: These are addresses to this very actor (Peer). Since this actor sits at the
gate of both incoming and outgoing messages, we need to double up it's mailbox with a priority queue
in order to avoid a deadlock. See the [guide level docs]
for some background information about this problem.

grace_period*: When the remote closes the connection, we could immediately drop all outstanding tasks related to
this peer. This makes sense for a request-response type connection, as it doesn't make sense to
continue using resources processing requests for which we can no longer send the response. However
it is not always desirable. For one way information flow, we might want to finish processing all the
outstanding packets before closing down. This also applies when you send a `CloseConnection` message to
this peer locally.
Create a new peer to represent a connection to some remote . Ideally you don ' t have to call this method directly . Wire formats need to have a convenience function to create your peer from an ` AsyncRead ` / ` AsyncWrite ` . They make some assumptions about default values and if you need more control you might have to call this directly . In any case have a look at [ ` CborWF :: create_peer ` ] to see how this is called . ` addr_in ` and ` addr_out ` : These are addresses to this very actor ( Peer ) . Since this actor sits at the gate of both incoming and outgoing messages we need to double up it ' s mailbox with a priority queue in order to avoid a deadlock . See the [ guide level docs ] for some background information about this problem . grace_period * : When the remote closes the connection we could immediately drop all outstanding tasks related to this peer . This makes sense for a request - response type connection as it doesn ' t make sense to continue using resources processing requests for which we can no longer send the response . However it is not always desirable . For one way information flow we might want to finish processing all the outstanding packets before closing down . This also applies when you send a ` CloseConnection ` message to this peer locally .
pub fn new
	(
		addr_in     : Addr<Self>                                                              ,
		addr_out    : Addr<Self>                                                              ,
		incoming    : impl BoundsIn<Wf>                                                       ,
		outgoing    : impl BoundsOut<Wf>                                                      ,
		exec        : impl SpawnHandle<Result<Response<Wf>, PeerErr>> + Send + Sync + 'static ,
		bp          : Option< Arc<Semaphore> >                                                ,
		grace_period: Option< Duration >                                                      ,
	)
		-> Result< Self, PeerErr >
	{
		trace!( "{}: Create peer", &addr_in );
		let exec: Arc< dyn SpawnHandle<Result<Response<Wf>, PeerErr>> + Send + Sync + 'static > = Arc::new(exec);
		let (nursery, nursery_stream) = Nursery::new( exec.clone() );
		let nursery_handle = exec.spawn_handle( Self::listen_request_results( nursery_stream, addr_out ) )
			.map_err( |_| -> PeerErr
			{
				let ctx = PeerErrCtx::default()
					.peer_id  ( addr_in.id()                                          )
					.peer_name( addr_in.name()                                        )
					.context  ( Some( "Request results stream for peer".to_string() ) )
				;
				PeerErr::Spawn{ ctx }
			})?
		;
		nursery.nurse( Self::listen_incoming( incoming, addr_in.clone(), bp.clone() ) )
			.map_err( |_| -> PeerErr
			{
				let ctx = PeerErrCtx
				{
					context: "Incoming stream for peer".to_string().into(),
					..Default::default()
				};
				PeerErr::Spawn{ ctx }
			})?
		;
		Ok( Self
		{
			id             : addr_in.id()               ,
			name           : addr_in.name()             ,
			outgoing       : Some( Box::new(outgoing) ) ,
			responses      : HashMap::new()             ,
			services       : HashMap::new()             ,
			pharos         : Pharos::default()          ,
			timeout        : Duration::from_secs(60)    ,
			backpressure   : bp                         ,
			permits        : Vec::new()                 ,
			closed         : false                      ,
			nursery_stream : Some( nursery_handle )     ,
			addr           : Some( addr_in )            ,
			nursery                                     ,
			grace_period                                ,
			// must not start at 0. Zero has a special meaning.
			//
			conn_id_counter: AtomicU64::new(1),
		})
	}
pub fn new ( addr_in : Addr < Self > , addr_out : Addr < Self > , incoming : impl BoundsIn < Wf > , outgoing : impl BoundsOut < Wf > , exec : impl SpawnHandle < Result < Response < Wf > , PeerErr > > + Send + Sync + ' static , bp : Option < Arc < Semaphore > > , grace_period : Option < Duration > , ) -> Result < Self , PeerErr > { trace ! ( " " addr_in ) ; let exec : Arc < dyn SpawnHandle < Result < Response < Wf > , PeerErr > > + Send + Sync + ' static > = Arc :: new ( exec ) ; let ( nursery , nursery_stream ) = Nursery :: new ( exec . clone ( ) ) ; let nursery_handle = exec . spawn_handle ( Self :: listen_request_results ( nursery_stream , addr_out ) ) . map_err ( | _ | -> PeerErr { let ctx = PeerErrCtx :: default ( ) . peer_id ( addr_in . id ( ) ) . peer_name ( addr_in . name ( ) ) . context ( Some ( " " . to_string ( ) ) ) ; PeerErr :: Spawn { ctx } } ) ? ; nursery . nurse ( Self :: listen_incoming ( incoming , addr_in . clone ( ) , bp . clone ( ) ) ) . map_err ( | _ | -> PeerErr { let ctx = PeerErrCtx { context : " " . to_string ( ) . into ( ) , .. Default :: default ( ) } ; PeerErr :: Spawn { ctx } } ) ? ; Ok ( Self { id : addr_in . id ( ) , name : addr_in . name ( ) , outgoing : Some ( Box :: new ( outgoing ) ) , responses : HashMap :: new ( ) , services : HashMap :: new ( ) , pharos : Pharos :: default ( ) , timeout : Duration :: from_secs ( 60 ) , backpressure : bp , permits : Vec :: new ( ) , closed : false , nursery_stream : Some ( nursery_handle ) , addr : Some ( addr_in ) , nursery , grace_period , // must not start at 0. Zero has a special meaning. // conn_id_counter : AtomicU64 :: new ( 1 ) , } ) }
Create a new peer to represent a connection to some remote.
Create a new peer to represent a connection to some remote .
addr_in
Addr<Self>
addr_out
Addr<Self>
incoming
impl BoundsIn<Wf>
outgoing
impl BoundsOut<Wf>
exec
impl SpawnHandle<Result<Response<Wf>, PeerErr>> + Send + Sync + 'static
bp
Option< Arc<Semaphore> >
grace_period
Option< Duration >
addr_in
Addr<Self>
addr_out
Addr<Self>
incoming
impl BoundsIn<Wf>
outgoing
impl BoundsOut<Wf>
exec
impl SpawnHandle<Result<Response<Wf>, PeerErr>> + Send + Sync + 'static
bp
Option< Arc<Semaphore> >
grace_period
Option< Duration >