rta_for_fps_lib/
server.rs

1//! Module for Server definition
2//!
3//! and functions to be used with one or multiple Servers
4
5use crate::curve::AggregateExt;
6
7use crate::iterators::server::constrained_demand::ConstrainedServerDemandIterator;
8use crate::iterators::{CurveIterator, ReclassifyIterator};
9use crate::task::Task;
10use crate::time::TimeUnit;
11
12/// Marker Type for aggregated server demand curve
13#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Copy, Clone)]
14pub struct AggregatedServerDemand;
15
16/// Marker Type for constrained server demand curve
17#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Copy, Clone)]
18pub struct ConstrainedServerDemand;
19
20/// Marker Type for aggregated higher priority server demand curve
21#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Copy, Clone)]
22pub struct HigherPriorityServerDemand;
23
24/// Marker Type for aggregated higher priority server actual Execution curve
25#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Copy, Clone)]
26pub struct HigherPriorityServerExecution;
27
28/// Marker Type for unconstrained server demand curve
29#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Copy, Clone)]
30pub struct UnconstrainedServerExecution;
31
32/// Marker Type for constrained server execution curve
33#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Copy, Clone)]
34pub struct ActualServerExecution;
35
36/// Type Representing a Server
37/// with a given set of tasks,
38/// a capacity for fulfilling demand,
39/// a replenishment interval for how
40/// often the capacity is restored
41/// ,and a server type determining if the
42/// capacity is available only at the beginning of the interval
43/// or until it is used up
44#[derive(Debug, Clone)]
45pub struct Server<'a> {
46    /// The Tasks that produce Demand for this Server
47    /// Sorted by priority with lower index equalling higher priority
48    pub tasks: &'a [Task],
49    /// The properties of the Server
50    pub properties: ServerProperties,
51}
52
53/// The Properties of a server
54#[derive(Debug, Clone, Copy)]
55pub struct ServerProperties {
56    /// The capacity for fulfilling Demand
57    pub capacity: TimeUnit,
58    /// How often the capacity is available
59    pub interval: TimeUnit,
60    /// How the available capacity behaves
61    pub server_type: ServerKind,
62}
63
64/// The Type of a Server
65#[derive(Debug, Clone, Copy)]
66pub enum ServerKind {
67    /// Indicated that the Server is a Deferrable Server
68    /// as described/defined in Section 5.2 Paragraph 2 of the paper
69    Deferrable,
70    /// Indicates that the Server is a Periodic Server
71    /// as described/defined in Section 5.2 Paragraph 4 of the paper
72    Periodic,
73}
74
75impl<'a> Server<'a> {
76    /// Create a new Server with the given Tasks and properties
77    #[must_use]
78    pub const fn new(
79        tasks: &'a [Task],
80        capacity: TimeUnit,
81        interval: TimeUnit,
82        server_type: ServerKind,
83    ) -> Self {
84        Server {
85            tasks,
86            properties: ServerProperties {
87                capacity,
88                interval,
89                server_type,
90            },
91        }
92    }
93
94    /// Get a a reference to a slice of the Servers contained Tasks
95    #[must_use]
96    pub const fn as_tasks(&self) -> &'a [Task] {
97        self.tasks
98    }
99
100    /// Calculate the aggregated demand Curve of a given Server up to a specified limit
101    /// As defined in Definition 11. in the paper
102    #[must_use]
103    pub fn aggregated_demand_curve_iter(
104        &self,
105    ) -> impl CurveIterator<CurveKind = AggregatedServerDemand> + Clone + '_ {
106        self.tasks
107            .iter()
108            .map(|task| task.into_iter())
109            .aggregate::<ReclassifyIterator<_, _>>()
110    }
111
112    /// Calculate the constrained demand curve
113    #[must_use]
114    pub fn constraint_demand_curve_iter(
115        &self,
116    ) -> impl CurveIterator<CurveKind = ConstrainedServerDemand> + Clone + '_ {
117        ConstrainedServerDemandIterator::new(self.properties, self.aggregated_demand_curve_iter())
118    }
119}