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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//! Module discovery and auto-loading.
//!
//! Watches directories for module files and auto-loads them.
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::time::Duration;
use dashmap::DashMap;
/// Module file information.
#[derive(Debug, Clone)]
pub struct ModuleFile {
/// Path to the module file.
pub path: PathBuf,
/// Module name (derived from filename).
pub name: String,
/// File size in bytes.
pub size: u64,
/// Last modified timestamp.
pub modified: std::time::SystemTime,
}
/// Discovery configuration.
#[derive(Debug, Clone)]
pub struct DiscoveryConfig {
/// Directories to watch.
pub watch_dirs: Vec<PathBuf>,
/// File extensions to look for.
pub extensions: Vec<String>,
/// Polling interval for file system.
pub poll_interval: Duration,
/// Whether to auto-load discovered modules.
pub auto_load: bool,
/// Whether to reload on file changes.
pub auto_reload: bool,
}
impl Default for DiscoveryConfig {
fn default() -> Self {
Self {
watch_dirs: vec![PathBuf::from("./modules")],
extensions: vec![
".so".to_string(), // Linux
".dll".to_string(), // Windows
".dylib".to_string(), // macOS
],
poll_interval: Duration::from_secs(5),
auto_load: true,
auto_reload: false,
}
}
}
/// Discovered module status.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ModuleStatus {
/// Module discovered but not loaded.
Discovered,
/// Module is loading.
Loading,
/// Module is loaded and active.
Loaded,
/// Module failed to load.
Failed,
/// Module file was removed.
Removed,
}
/// Discovered module information.
#[derive(Debug, Clone)]
pub struct DiscoveredModule {
/// Module file information.
pub file: ModuleFile,
/// Current status.
pub status: ModuleStatus,
/// Load attempts.
pub attempts: u32,
/// Last error message.
pub last_error: Option<String>,
/// Discovery timestamp.
pub discovered_at: std::time::SystemTime,
}
/// Module discovery service.
#[derive(Debug)]
pub struct ModuleDiscovery {
/// Configuration.
config: DiscoveryConfig,
/// Discovered modules.
modules: DashMap<String, DiscoveredModule>,
/// Discovery running flag.
running: AtomicBool,
/// Total discoveries.
total_discoveries: AtomicU64,
}
impl ModuleDiscovery {
/// Creates a new discovery service.
pub fn new(config: DiscoveryConfig) -> Self {
Self {
config,
modules: DashMap::new(),
running: AtomicBool::new(false),
total_discoveries: AtomicU64::new(0),
}
}
/// Creates a discovery service with default configuration.
pub fn with_defaults() -> Self {
Self::new(DiscoveryConfig::default())
}
/// Scans configured directories for modules.
pub fn scan(&self) -> Vec<ModuleFile> {
let mut found = Vec::new();
for dir in &self.config.watch_dirs {
if !dir.exists() {
continue;
}
if let Ok(entries) = std::fs::read_dir(dir) {
for entry in entries.flatten() {
let path = entry.path();
// Check extension
let ext = path.extension()
.and_then(|e| e.to_str())
.unwrap_or("");
if !self.config.extensions.iter().any(|e| e == &format!(".{}", ext)) {
continue;
}
// Get file info
if let Ok(metadata) = path.metadata() {
let name = path.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("unknown")
.to_string();
found.push(ModuleFile {
path,
name,
size: metadata.len(),
modified: metadata.modified().unwrap_or(std::time::UNIX_EPOCH),
});
}
}
}
}
found
}
/// Registers a discovered module.
pub fn register(&self, file: ModuleFile) {
let module = DiscoveredModule {
file,
status: ModuleStatus::Discovered,
attempts: 0,
last_error: None,
discovered_at: std::time::SystemTime::now(),
};
self.modules.insert(module.file.name.clone(), module);
self.total_discoveries.fetch_add(1, Ordering::Relaxed);
}
/// Updates module status.
pub fn update_status(&self, name: &str, status: ModuleStatus) {
if let Some(mut module) = self.modules.get_mut(name) {
module.status = status;
}
}
/// Records a load failure.
pub fn record_failure(&self, name: &str, error: String) {
if let Some(mut module) = self.modules.get_mut(name) {
module.attempts += 1;
module.last_error = Some(error);
module.status = ModuleStatus::Failed;
}
}
/// Returns discovered modules.
pub fn discovered_modules(&self) -> Vec<DiscoveredModule> {
self.modules.iter().map(|e| e.value().clone()).collect()
}
/// Returns modules by status.
pub fn modules_by_status(&self, status: ModuleStatus) -> Vec<DiscoveredModule> {
self.modules
.iter()
.filter(|e| e.value().status == status)
.map(|e| e.value().clone())
.collect()
}
/// Returns whether discovery is running.
pub fn is_running(&self) -> bool {
self.running.load(Ordering::Relaxed)
}
/// Returns total discoveries.
pub fn total_discoveries(&self) -> u64 {
self.total_discoveries.load(Ordering::Relaxed)
}
}
impl Default for ModuleDiscovery {
fn default() -> Self {
Self::with_defaults()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_discovery_config() {
let config = DiscoveryConfig::default();
assert!(!config.watch_dirs.is_empty());
assert_eq!(config.extensions.len(), 3);
}
#[test]
fn test_discovery_creation() {
let discovery = ModuleDiscovery::with_defaults();
assert!(!discovery.is_running());
assert_eq!(discovery.total_discoveries(), 0);
}
}