Struct garage_rpc::rpc_helper::NetApp
source · pub struct NetApp {
pub version_tag: [u8; 16],
pub netid: Key,
pub id: PublicKey,
pub privkey: SecretKey,
/* private fields */
}
Expand description
NetApp is the main class that handles incoming and outgoing connections.
NetApp can be used in a stand-alone fashion or together with a peering strategy.
If using it alone, you will want to set on_connect
and on_disconnect
events
in order to manage information about the current peer list.
Fields§
§version_tag: [u8; 16]
Version tag, 8 bytes for netapp version, 8 bytes for app version
netid: Key
Network secret key
id: PublicKey
Our peer ID
privkey: SecretKey
Private key associated with our peer ID
Implementations§
source§impl NetApp
impl NetApp
sourcepub fn new(
app_version_tag: u64,
netid: Key,
privkey: SecretKey,
bind_outgoing_to: Option<IpAddr>
) -> Arc<NetApp>
pub fn new( app_version_tag: u64, netid: Key, privkey: SecretKey, bind_outgoing_to: Option<IpAddr> ) -> Arc<NetApp>
Creates a new instance of NetApp, which can serve either as a full p2p node,
or just as a passive client. To upgrade to a full p2p node, spawn a listener
using .listen()
Our Peer ID is the public key associated to the secret key given here.
sourcepub fn on_connected<F>(&self, handler: F)
pub fn on_connected<F>(&self, handler: F)
Set the handler to be called when a new connection (incoming or outgoing) has been successfully established. Do not set this if using a peering strategy, as the peering strategy will need to set this itself.
sourcepub fn on_disconnected<F>(&self, handler: F)
pub fn on_disconnected<F>(&self, handler: F)
Set the handler to be called when an existing connection (incoming or outgoing) has been closed by either party. Do not set this if using a peering strategy, as the peering strategy will need to set this itself.
sourcepub fn endpoint<M, H>(self: &Arc<NetApp>, path: String) -> Arc<Endpoint<M, H>>where
M: Message + 'static,
H: StreamingEndpointHandler<M> + 'static,
pub fn endpoint<M, H>(self: &Arc<NetApp>, path: String) -> Arc<Endpoint<M, H>>where
M: Message + 'static,
H: StreamingEndpointHandler<M> + 'static,
Create a new endpoint with path path
,
that handles messages of type M
.
H
is the type of the object that should handle requests
to this endpoint on the local node. If you don’t want
to handle request on the local node (e.g. if this node
is only a client in the network), define the type H
to be ()
.
This function will panic if the endpoint has already been
created.
sourcepub async fn listen(
self: Arc<NetApp>,
listen_addr: SocketAddr,
public_addr: Option<SocketAddr>,
must_exit: Receiver<bool>
)
pub async fn listen( self: Arc<NetApp>, listen_addr: SocketAddr, public_addr: Option<SocketAddr>, must_exit: Receiver<bool> )
Main listening process for our app. This future runs during the whole run time of our application. If this is not called, the NetApp instance remains a passive client.
sourcepub fn drop_all_handlers(&self)
pub fn drop_all_handlers(&self)
Drop all endpoint handlers, as well as handlers for connection/disconnection events. (This disables the peering strategy)
Use this when terminating to break reference cycles
sourcepub async fn try_connect(
self: Arc<NetApp>,
ip: SocketAddr,
id: PublicKey
) -> Result<(), Error>
pub async fn try_connect( self: Arc<NetApp>, ip: SocketAddr, id: PublicKey ) -> Result<(), Error>
Attempt to connect to a peer, given by its ip:port and its public key.
The public key will be checked during the secret handshake process.
This function returns once the connection has been established and a
successfull handshake was made. At this point we can send messages to
the other node with Netapp::request
sourcepub fn disconnect(self: &Arc<NetApp>, id: &PublicKey)
pub fn disconnect(self: &Arc<NetApp>, id: &PublicKey)
Close the outgoing connection we have to a node specified by its public key, if such a connection is currently open.