Skip to content

Commit 148b27a

Browse files
committed
feat(collector): integrate config and add weather/radioactive harvesters
1 parent cb3fbfd commit 148b27a

1 file changed

Lines changed: 53 additions & 24 deletions

File tree

src/trueentropy/collector.py

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,14 @@
3333
import threading
3434
import time
3535

36+
from trueentropy.config import TrueEntropyConfig, get_config
3637
from trueentropy.harvesters.base import BaseHarvester
3738
from trueentropy.harvesters.external import ExternalHarvester
3839
from trueentropy.harvesters.network import NetworkHarvester
40+
from trueentropy.harvesters.radioactive import RadioactiveHarvester
3941
from trueentropy.harvesters.system import SystemHarvester
4042
from trueentropy.harvesters.timing import TimingHarvester
43+
from trueentropy.harvesters.weather import WeatherHarvester
4144
from trueentropy.pool import EntropyPool
4245

4346
# -----------------------------------------------------------------------------
@@ -66,8 +69,7 @@
6669
def start_background_collector(
6770
pool: EntropyPool,
6871
interval: float = 1.0,
69-
enable_network: bool = True,
70-
enable_external: bool = True,
72+
config: TrueEntropyConfig | None = None,
7173
) -> None:
7274
"""
7375
Start the background entropy collector thread.
@@ -78,19 +80,18 @@ def start_background_collector(
7880
Args:
7981
pool: The EntropyPool to feed entropy into
8082
interval: Seconds between collection cycles (default: 1.0)
81-
enable_network: Whether to enable network harvester (default: True)
82-
enable_external: Whether to enable external API harvester (default: True)
83+
config: Configuration to use (default: global config)
8384
8485
Note:
8586
- Only one collector can run at a time
8687
- The collector is a daemon thread (exits when main program exits)
8788
- Call stop_background_collector() for clean shutdown
89+
- Respects the global configuration set by trueentropy.configure()
8890
8991
Example:
90-
>>> from trueentropy import get_pool
91-
>>> from trueentropy.collector import start_background_collector
92-
>>> pool = get_pool()
93-
>>> start_background_collector(pool, interval=2.0)
92+
>>> import trueentropy
93+
>>> trueentropy.configure(offline_mode=True) # Use only local sources
94+
>>> trueentropy.start_collector(interval=2.0)
9495
"""
9596
global _collector_thread, _stop_event
9697

@@ -99,20 +100,34 @@ def start_background_collector(
99100
logger.warning("Background collector is already running")
100101
return
101102

103+
# Use provided config or global config
104+
cfg = config or get_config()
105+
102106
# Create stop event
103107
_stop_event = threading.Event()
104108

105-
# Create and configure harvesters
106-
harvesters: list[BaseHarvester] = [
107-
TimingHarvester(),
108-
SystemHarvester(),
109-
]
109+
# Create harvesters based on configuration
110+
harvesters: list[BaseHarvester] = []
110111

111-
if enable_network:
112-
harvesters.append(NetworkHarvester())
112+
# Offline sources (always process if enabled)
113+
if cfg.enable_timing:
114+
harvesters.append(TimingHarvester())
115+
if cfg.enable_system:
116+
harvesters.append(SystemHarvester())
113117

114-
if enable_external:
118+
# Network-dependent sources
119+
if cfg.enable_network:
120+
harvesters.append(NetworkHarvester())
121+
if cfg.enable_external:
115122
harvesters.append(ExternalHarvester())
123+
if cfg.enable_weather:
124+
harvesters.append(WeatherHarvester())
125+
if cfg.enable_radioactive:
126+
harvesters.append(RadioactiveHarvester())
127+
128+
if not harvesters:
129+
logger.warning("No harvesters enabled in configuration")
130+
return
116131

117132
# Create collector thread
118133
_collector_thread = threading.Thread(
@@ -124,8 +139,11 @@ def start_background_collector(
124139

125140
# Start the thread
126141
_collector_thread.start()
142+
143+
mode = "OFFLINE" if cfg.offline_mode else "ONLINE"
127144
logger.info(
128-
f"Background collector started with {len(harvesters)} harvesters, " f"interval={interval}s"
145+
f"Background collector started ({mode}) with {len(harvesters)} harvesters, "
146+
f"interval={interval}s"
129147
)
130148

131149

@@ -246,7 +264,7 @@ def _collector_loop(
246264
logger.debug("Collector loop exiting")
247265

248266

249-
def collect_once(pool: EntropyPool) -> int:
267+
def collect_once(pool: EntropyPool, config: TrueEntropyConfig | None = None) -> int:
250268
"""
251269
Perform a single collection cycle.
252270
@@ -255,16 +273,27 @@ def collect_once(pool: EntropyPool) -> int:
255273
256274
Args:
257275
pool: The EntropyPool to feed entropy into
276+
config: Configuration to use (default: global config)
258277
259278
Returns:
260279
Total entropy bits collected
261280
"""
262-
harvesters: list[BaseHarvester] = [
263-
TimingHarvester(),
264-
SystemHarvester(),
265-
NetworkHarvester(),
266-
ExternalHarvester(),
267-
]
281+
cfg = config or get_config()
282+
283+
harvesters: list[BaseHarvester] = []
284+
285+
if cfg.enable_timing:
286+
harvesters.append(TimingHarvester())
287+
if cfg.enable_system:
288+
harvesters.append(SystemHarvester())
289+
if cfg.enable_network:
290+
harvesters.append(NetworkHarvester())
291+
if cfg.enable_external:
292+
harvesters.append(ExternalHarvester())
293+
if cfg.enable_weather:
294+
harvesters.append(WeatherHarvester())
295+
if cfg.enable_radioactive:
296+
harvesters.append(RadioactiveHarvester())
268297

269298
total_bits = 0
270299

0 commit comments

Comments
 (0)