Skip to main content

datex_core/runtime/execution/context/
remote.rs

1#[cfg(feature = "compiler")]
2use crate::compiler::scope::CompilationScope;
3use crate::{
4    global::dxb_block::OutgoingContextId,
5    prelude::*,
6    runtime::{
7        Runtime,
8        execution::context::{ExecutionContext, ExecutionMode},
9    },
10    values::core_values::endpoint::Endpoint,
11};
12use core::cell::RefCell;
13
14#[derive(Debug, Clone)]
15pub struct RemoteExecutionContext {
16    #[cfg(feature = "compiler")]
17    pub compile_scope: CompilationScope,
18    pub target: RemoteExecutionTarget,
19    pub execution_mode: ExecutionMode,
20    pub runtime: Runtime,
21}
22
23#[derive(Debug, Clone)]
24pub struct RemoteExecutionTarget {
25    endpoints: Vec<Endpoint>,
26    pub context_id: Rc<RefCell<Option<OutgoingContextId>>>,
27}
28
29impl RemoteExecutionTarget {
30    pub fn new(endpoints: Vec<Endpoint>) -> Self {
31        RemoteExecutionTarget {
32            endpoints,
33            context_id: Rc::new(RefCell::new(None)),
34        }
35    }
36    pub fn endpoints(&self) -> &[Endpoint] {
37        &self.endpoints
38    }
39}
40
41impl RemoteExecutionContext {
42    /// Creates a new remote execution context with the given endpoint.
43    pub fn new(
44        endpoints: Vec<Endpoint>,
45        execution_mode: ExecutionMode,
46        runtime: Runtime,
47    ) -> Self {
48        RemoteExecutionContext {
49            #[cfg(feature = "compiler")]
50            compile_scope: CompilationScope::new(execution_mode),
51            target: RemoteExecutionTarget::new(endpoints),
52            execution_mode,
53            runtime,
54        }
55    }
56
57    /// Adds an endpoint to the remote execution context if it doesn't already exist.
58    pub fn add_endpoint(&mut self, endpoint: Endpoint) {
59        if self.endpoints().contains(&endpoint) {
60            return;
61        }
62        self.endpoints_mut().push(endpoint);
63    }
64
65    pub fn endpoints(&self) -> &[Endpoint] {
66        &self.target.endpoints
67    }
68    pub fn endpoints_mut(&mut self) -> &mut Vec<Endpoint> {
69        &mut self.target.endpoints
70    }
71
72    /// Sets the endpoints for the remote execution context.
73    /// # Safety
74    /// The caller must ensure that the endpoints vector is not empty.
75    pub unsafe fn set_endpoints(&mut self, endpoints: Vec<Endpoint>) {
76        if endpoints.is_empty() {
77            panic!("RemoteExecutionContext: endpoints cannot be empty");
78        } else {
79            self.target.endpoints = endpoints;
80        }
81    }
82}
83
84impl ExecutionContext {
85    pub fn remote(endpoints: Vec<Endpoint>, runtime: Runtime) -> Self {
86        ExecutionContext::Remote(RemoteExecutionContext::new(
87            endpoints,
88            ExecutionMode::Static,
89            runtime,
90        ))
91    }
92
93    pub fn remote_unbounded(
94        endpoints: Vec<Endpoint>,
95        runtime: Runtime,
96    ) -> Self {
97        ExecutionContext::Remote(RemoteExecutionContext::new(
98            endpoints,
99            ExecutionMode::unbounded(),
100            runtime,
101        ))
102    }
103}