1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use ockam_core::compat::sync::Arc;
use ockam_core::compat::vec::Vec;
use ockam_core::flow_control::{FlowControlId, FlowControls};
use ockam_core::{Address, AllowAll, IncomingAccessControl};

/// Trust Options for a Forwarding Service
pub struct RelayServiceOptions {
    pub(super) service_incoming_access_control: Arc<dyn IncomingAccessControl>,
    pub(super) relays_incoming_access_control: Arc<dyn IncomingAccessControl>,
    pub(super) consumer_service: Vec<FlowControlId>,
    pub(super) consumer_relay: Vec<FlowControlId>,
}

impl RelayServiceOptions {
    /// Default constructor without Access Control
    pub fn new() -> Self {
        Self {
            service_incoming_access_control: Arc::new(AllowAll),
            relays_incoming_access_control: Arc::new(AllowAll),
            consumer_service: vec![],
            consumer_relay: vec![],
        }
    }

    /// Mark that this Relay service is a Consumer for to the given [`FlowControlId`]
    pub fn service_as_consumer(mut self, id: &FlowControlId) -> Self {
        self.consumer_service.push(id.clone());

        self
    }

    /// Mark that spawned Relays are Consumers for to the given [`FlowControlId`]
    pub fn relay_as_consumer(mut self, id: &FlowControlId) -> Self {
        self.consumer_relay.push(id.clone());

        self
    }

    /// Set Service Incoming Access Control
    pub fn with_service_incoming_access_control_impl(
        mut self,
        access_control: impl IncomingAccessControl,
    ) -> Self {
        self.service_incoming_access_control = Arc::new(access_control);
        self
    }

    /// Set Service Incoming Access Control
    pub fn with_service_incoming_access_control(
        mut self,
        access_control: Arc<dyn IncomingAccessControl>,
    ) -> Self {
        self.service_incoming_access_control = access_control;
        self
    }

    /// Set spawned relays Incoming Access Control
    pub fn with_relays_incoming_access_control_impl(
        mut self,
        access_control: impl IncomingAccessControl,
    ) -> Self {
        self.relays_incoming_access_control = Arc::new(access_control);
        self
    }

    /// Set spawned relays Incoming Access Control
    pub fn with_relays_incoming_access_control(
        mut self,
        access_control: Arc<dyn IncomingAccessControl>,
    ) -> Self {
        self.relays_incoming_access_control = access_control;
        self
    }

    pub(super) fn setup_flow_control_for_relay_service(
        &self,
        flow_controls: &FlowControls,
        address: &Address,
    ) {
        for id in &self.consumer_service {
            flow_controls.add_consumer(address.clone(), id);
        }
    }

    pub(super) fn setup_flow_control_for_relay(
        &self,
        flow_controls: &FlowControls,
        address: &Address,
    ) {
        for id in &self.consumer_relay {
            flow_controls.add_consumer(address.clone(), id);
        }
    }
}

impl Default for RelayServiceOptions {
    fn default() -> Self {
        Self::new()
    }
}