Module communication::actor[][src]

Actor

This module provides a riker actor that handles the communication to a remote peer over the swarm.

The CommunicationActor sends the CommunicationRequest::RequestMsgs it receives over the swarm to the corresponding remote peer and forwards incoming request to the client provided in the [CommunicationConfig]. Before forwarding any requests, the request will be validated by the firewall according to the configuration from [CommunicationConfig], additional rules or changed can be set with CommunicationRequest::ConfigureFirewall. This requires that the ToPermissionVariants trait is implemented for the generic Req type, which can be derived with the macro RequestPermissions from stronghold_utils.

If remote peers should be able to dial the local system, a CommunicationRequest::StartListening has to be sent to the CommunicationActor.

use communication::{
    actor::{
        CommunicationActor, CommunicationActorConfig, FirewallPermission, PermissionValue, ToPermissionVariants,
        VariantPermission,
    },
    behaviour::BehaviourConfig,
};
use libp2p::identity::Keypair;
use riker::actors::*;
use serde::{Deserialize, Serialize};
use stronghold_utils::RequestPermissions;

#[derive(Debug, Clone, Serialize, Deserialize, RequestPermissions)]
pub enum Request {
    Ping,
}

#[derive(Debug, Clone, Serialize, Deserialize, RequestPermissions)]
pub enum Response {
    Pong,
}

// blank client actor without any logic
#[derive(Clone, Debug)]
struct ClientActor;

impl ActorFactory for ClientActor {
    fn create() -> Self {
        ClientActor
    }
}

impl Actor for ClientActor {
    type Msg = Request;

    fn recv(&mut self, _ctx: &Context<Self::Msg>, _msg: Self::Msg, sender: Sender) {
        sender
            .expect("Sender exists")
            .try_tell(Response::Pong, None)
            .expect("Sender received response");
    }
}

let local_keys = Keypair::generate_ed25519();
let sys = ActorSystem::new().expect("Init actor system failed.");
let keys = Keypair::generate_ed25519();
let client = sys
    .actor_of::<ClientActor>("client")
    .expect("Init client actor failed.");
let actor_config = CommunicationActorConfig {
    client,
    firewall_default_in: FirewallPermission::all(),
    firewall_default_out: FirewallPermission::none(),
};
let behaviour_config = BehaviourConfig::default();
let comms_actor = sys
    .actor_of_args::<CommunicationActor<Request, Response, _, _>, _>(
        "communication",
        (local_keys, actor_config, behaviour_config),
    )
    .expect("Init communication actor failed.");

Structs

CommunicationActor

Actor responsible for creating a [P2PNetworkBehaviour] and handling all interaction with the Swarm. For each received CommunicationRequest, a CommunicationResults is returned to the sender.

CommunicationActorConfig

The actor configuration

EstablishedConnection

Information about the connection with a remote peer as maintained in the ConnectionManager.

FirewallPermission

The sum of allowed permissions. This is using the same concepts as e.g. permission values in Unix systems.

PermissionValue

The permission value for request variants. It is a bit that is set at a certain index, therefore the value is always a power of 2.

Enums

CommunicationRequest

Requests for the [CommunicationActor].

CommunicationResults

Returned results from the [CommunicationActor]

ConnectPeerError

Errors that can occur in the context of a pending Connection.

FirewallRule

Configure the firewall.

RelayDirection

Direction for which a relay peer is used.

RequestDirection

The direction of a [CommunicationRequest::RequestMsg] that firewall receives.

RequestMessageError

Traits

ToPermissionVariants

Convert an element to implement permissions.

VariantPermission

The permission value for the different variants of an enum. This allows permitting specific variants of an enum while prohibiting others. In structs or unions, it should default to PermissionValue(1)

Derive Macros

RequestPermissions

Implements the [VariantPermission] for struct/ unions with PermissionValue(1). For enums, it implements [ToPermissionVariants], which creates an according new enum Permission with Unit variants, and implements [VariantPermission] by assigning different [PermissionValue] for each variant. The permission value is the “index” in the enum as exponent for the power of 2, thus from top to bottom 1, 2, 4, 8…