industrial_protocols_kernel/
lib.rs1pub mod bridge;
39pub mod circuit;
40pub mod config;
41pub mod connection;
42pub mod error;
43pub mod event;
44pub mod gateway;
45pub mod registry;
46pub mod retry;
47pub mod sdk;
48
49use config::ConfigRepository;
50use connection::{ConnectionManager, ConnectionStrategy};
51use event::EventBus;
52use gateway::GatewayEngine;
53use registry::ProtocolRegistry;
54use sdk::Protocol;
55use std::io;
56
57pub struct Kernel {
62 registry: ProtocolRegistry,
63 conn_manager: ConnectionManager,
64 config: Option<ConfigRepository>,
65 config_error: Option<String>,
66 events: EventBus,
67 gateway: GatewayEngine,
68}
69
70impl Kernel {
71 pub fn builder() -> KernelBuilder {
73 KernelBuilder::new()
74 }
75
76 pub fn boot(&mut self) -> io::Result<()> {
78 if let Some(ref err) = self.config_error {
79 return Err(io::Error::new(io::ErrorKind::InvalidData, err.clone()));
80 }
81 if let Some(ref config) = self.config {
82 for device_id in config.device_ids() {
83 let dev = config.get_device(&device_id).unwrap();
84 if self.registry.get(&dev.protocol).is_none() {
85 return Err(io::Error::new(
86 io::ErrorKind::NotFound,
87 format!(
88 "protocol '{}' not registered (needed by device '{device_id}')",
89 dev.protocol
90 ),
91 ));
92 }
93 }
94 }
95 Ok(())
96 }
97
98 pub fn shutdown(&mut self) -> io::Result<()> {
100 self.gateway.stop();
101 self.conn_manager.shutdown()
102 }
103
104 pub fn protocol_registry(&self) -> &ProtocolRegistry {
106 &self.registry
107 }
108
109 pub fn connection_manager(&self) -> &ConnectionManager {
111 &self.conn_manager
112 }
113
114 pub fn config(&self) -> &ConfigRepository {
119 self.config.as_ref().expect("config not loaded")
120 }
121
122 pub fn event_bus(&self) -> &EventBus {
124 &self.events
125 }
126
127 pub fn gateway(&self) -> &GatewayEngine {
129 &self.gateway
130 }
131
132 pub fn gateway_mut(&mut self) -> &mut GatewayEngine {
134 &mut self.gateway
135 }
136}
137
138pub struct KernelBuilder {
150 config: Option<ConfigRepository>,
151 config_error: Option<String>,
152 protocols: Vec<Box<dyn Protocol>>,
153 connection_strategy: ConnectionStrategy,
154 event_capacity: usize,
155 gateway_interval_ms: u64,
156}
157
158impl KernelBuilder {
159 fn new() -> Self {
160 KernelBuilder {
161 config: None,
162 config_error: None,
163 protocols: Vec::new(),
164 connection_strategy: ConnectionStrategy::Persistent,
165 event_capacity: 64,
166 gateway_interval_ms: 100,
167 }
168 }
169
170 pub fn config_from_str(mut self, toml_str: &str) -> Self {
174 match ConfigRepository::parse(toml_str) {
175 Ok(config) => self.config = Some(config),
176 Err(e) => self.config_error = Some(format!("invalid TOML config: {e}")),
177 }
178 self
179 }
180
181 pub fn config_from_file(mut self, path: impl AsRef<std::path::Path>) -> Self {
185 match ConfigRepository::load(path) {
186 Ok(config) => self.config = Some(config),
187 Err(e) => self.config_error = Some(format!("failed to load config file: {e}")),
188 }
189 self
190 }
191
192 pub fn with_protocol(mut self, protocol: Box<dyn Protocol>) -> Self {
194 self.protocols.push(protocol);
195 self
196 }
197
198 pub fn with_connection_strategy(mut self, strategy: ConnectionStrategy) -> Self {
200 self.connection_strategy = strategy;
201 self
202 }
203
204 pub fn with_event_capacity(mut self, cap: usize) -> Self {
206 self.event_capacity = cap;
207 self
208 }
209
210 pub fn with_gateway_interval(mut self, ms: u64) -> Self {
212 self.gateway_interval_ms = ms;
213 self
214 }
215
216 pub fn build(self) -> Kernel {
218 let mut registry = ProtocolRegistry::new();
219 for p in self.protocols {
220 registry.register(p);
221 }
222 Kernel {
223 registry,
224 conn_manager: ConnectionManager::new(self.connection_strategy),
225 config: self.config,
226 config_error: self.config_error,
227 events: EventBus::new(self.event_capacity),
228 gateway: GatewayEngine::new(self.gateway_interval_ms),
229 }
230 }
231}