feat(simulation): serve atlas layer requests over the bridge — completes #969 (D-225)
Closes the server-side layer-stream loop. serve_atlas_requests (PreInput) drains the AtlasRequestBuffer and runs each through handle_atlas_request (cache hit -> Ready; miss -> resolve via BodySourceResolver + enqueue an Immediate AnalyzeBody -> Pending), buffering AtlasLayerResponses. send_atlas_responses (PostSnapshot) flushes them to the client. main.rs wires BodySourceResolverResource (base root = systems.db's 3rd ancestor; mod roots layer on later). Misses flow through the #968 background tier and a re-request hits the now-warm cache. Full path now live server-side: client request -> receive() demux -> serve -> proxy -> (cache | queue+cascade) -> response -> client. The client half (send request, decode response, render overlays) is #960. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -13,6 +13,11 @@ use bevy_ecs::schedule::IntoScheduleConfigs;
|
||||
|
||||
use crate::atlas::body_world_state::{BodyWorldStateCache, CACHE_CAPACITY};
|
||||
use crate::atlas::gen_queue::{GenCompletion, GenerationQueue};
|
||||
use crate::atlas::layer_proxy::{handle_atlas_request, AtlasLayerResponse, AtlasLayerStatus};
|
||||
use crate::atlas::source_resolver::BodySourceResolverResource;
|
||||
use crate::bridge::{AtlasRequestBuffer, AtlasResponseBuffer};
|
||||
use crate::simulation::rng::SimRng;
|
||||
use crate::simulation::time::SimulationTime;
|
||||
use crate::tick_phases::TickPhase;
|
||||
|
||||
/// Wires the D-206 background generation tier into the app (#968).
|
||||
@@ -25,7 +30,39 @@ impl Plugin for GenerationPlugin {
|
||||
.add_systems(
|
||||
Update,
|
||||
drain_generation_completions.in_set(TickPhase::PreInput),
|
||||
);
|
||||
)
|
||||
.add_systems(Update, serve_atlas_requests.in_set(TickPhase::PreInput));
|
||||
}
|
||||
}
|
||||
|
||||
/// Drain inbound atlas layer requests and serve each through the proxy (#969,
|
||||
/// D-225): cache hit → Ready, miss → resolve + enqueue + Pending. Responses are
|
||||
/// buffered for the bridge to flush in `PostSnapshot`.
|
||||
fn serve_atlas_requests(
|
||||
mut requests: ResMut<AtlasRequestBuffer>,
|
||||
mut responses: ResMut<AtlasResponseBuffer>,
|
||||
mut cache: ResMut<BodyWorldStateCache>,
|
||||
queue: Res<GenerationQueue>,
|
||||
resolver: Option<Res<BodySourceResolverResource>>,
|
||||
rng: Option<Res<SimRng>>,
|
||||
time: Option<Res<SimulationTime>>,
|
||||
) {
|
||||
if requests.0.is_empty() {
|
||||
return;
|
||||
}
|
||||
let world_seed = rng.as_ref().map(|r| r.seed()).unwrap_or(0);
|
||||
let tick = time.as_ref().map(|t| t.tick).unwrap_or(0);
|
||||
let pending: Vec<_> = requests.0.drain(..).collect();
|
||||
for req in pending {
|
||||
let resp = match resolver.as_ref() {
|
||||
Some(r) => handle_atlas_request(&req, &mut cache, &queue, &r.0, world_seed, tick),
|
||||
None => AtlasLayerResponse {
|
||||
body_id: req.body_id.clone(),
|
||||
status: AtlasLayerStatus::Error("no body source resolver".to_string()),
|
||||
layer1: None,
|
||||
},
|
||||
};
|
||||
responses.0.push(resp);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,4 +150,32 @@ mod tests {
|
||||
"drain system should insert the analyzed body into the cache"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn serve_drains_requests_into_responses() {
|
||||
use crate::atlas::cascade::CascadeLayer;
|
||||
use crate::atlas::layer_proxy::AtlasLayerRequest;
|
||||
|
||||
let mut world = World::new();
|
||||
world.insert_resource(AtlasRequestBuffer(vec![AtlasLayerRequest {
|
||||
body_id: "GJ1c".to_string(),
|
||||
up_to: CascadeLayer::Topography,
|
||||
}]));
|
||||
world.insert_resource(AtlasResponseBuffer::default());
|
||||
world.insert_resource(BodyWorldStateCache::new(CACHE_CAPACITY));
|
||||
world.insert_resource(GenerationQueue::with_threads(1));
|
||||
// No resolver / SimRng / SimulationTime — all optional in the system.
|
||||
|
||||
let mut sched = Schedule::default();
|
||||
sched.add_systems(serve_atlas_requests);
|
||||
sched.run(&mut world);
|
||||
|
||||
let responses = world.resource::<AtlasResponseBuffer>();
|
||||
assert_eq!(responses.0.len(), 1, "request should produce one response");
|
||||
assert_eq!(responses.0[0].body_id, "GJ1c");
|
||||
// No resolver wired → Error status (exercises the drain + push path).
|
||||
assert!(matches!(responses.0[0].status, AtlasLayerStatus::Error(_)));
|
||||
// The request buffer was drained.
|
||||
assert!(world.resource::<AtlasRequestBuffer>().0.is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user