From a0dbab6fe21a7b8285567fc097dd93d75f3084de Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Mon, 27 Jul 2020 01:12:41 +0100 Subject: [PATCH] reorganise global.rs --- src/cmdlistener.rs | 2 +- src/global.rs | 98 ++++++++++++++++++++++++---------------------- 2 files changed, 53 insertions(+), 47 deletions(-) diff --git a/src/cmdlistener.rs b/src/cmdlistener.rs index 948986e6..54ed415c 100644 --- a/src/cmdlistener.rs +++ b/src/cmdlistener.rs @@ -246,7 +246,7 @@ fn execute(cs: &mut CommandStream, cmd: MgmtCommand) -> MgmtResponse { let scope = cs.get_scope()?; Some(scope) }; - let mut games = list_games(scope); + let mut games = Instance::list_names(scope); games.sort_unstable(); GamesList { games } }, diff --git a/src/global.rs b/src/global.rs index 06775ac0..d78a3fce 100644 --- a/src/global.rs +++ b/src/global.rs @@ -15,7 +15,7 @@ visible_slotmap_key!{ ClientId('C') } #[serde(transparent)] pub struct RawToken (pub String); -// ---------- data structure ---------- +// ---------- public data structure ---------- #[derive(Debug,Serialize,Deserialize)] #[derive(Eq,PartialEq,Ord,PartialOrd,Hash)] @@ -27,12 +27,6 @@ pub struct InstanceName { #[derive(Debug,Clone)] pub struct InstanceRef (Arc>); -#[derive(Debug)] -pub struct InstanceContainer { - live : bool, - g : Instance, -} - #[derive(Debug)] pub struct Instance { pub name : Arc, @@ -61,32 +55,13 @@ pub struct InstanceGuard<'g> { pub gref : InstanceRef, } -#[derive(Default)] -struct Global { - // lock hierarchy: this is the innermost lock - games : RwLock,InstanceRef>>, - players : RwLock>, - clients : RwLock>, - // xxx delete instances at some point! -} - #[derive(Debug,Default)] pub struct TokenRegistry { tr : HashSet, id : PhantomData, } -#[derive(Debug,Serialize,Deserialize)] -struct InstanceSaveAccesses { - tokens_players : Vec<(RawTokenStr, PlayerId)>, -} - -display_as_debug!{InstanceLockError} -impl From> for InstanceLockError { - fn from(_: PoisonError) -> Self { Self::GameCorrupted } -} - -// ---------- API ---------- +// ---------- Token Table API ---------- #[derive(Clone,Debug)] pub struct InstanceAccessDetails { @@ -114,21 +89,44 @@ pub struct PrivateCaller(()); // workaround for inability to have private trait methods const PRIVATE_Y : PrivateCaller = PrivateCaller(()); -// ========== implementations ========== +// ========== internal data structures ========== -// ---------- newtypes and type aliases ---------- +lazy_static! { + static ref GLOBAL : Global = Default::default(); +} -impl Borrow for RawToken { - fn borrow(&self) -> &str { &self.0 } +#[derive(Default)] +struct Global { + // lock hierarchy: this is the innermost lock + games : RwLock,InstanceRef>>, + players : RwLock>, + clients : RwLock>, + // xxx delete instances at some point! } -// ---------- data structure ---------- +#[derive(Debug)] +pub struct InstanceContainer { + live : bool, + g : Instance, +} -lazy_static! { - static ref GLOBAL : Global = Default::default(); +#[derive(Debug,Serialize,Deserialize)] +struct InstanceSaveAccesses { + tokens_players : Vec<(RawTokenStr, PlayerId)>, +} + +display_as_debug!{InstanceLockError} +impl From> for InstanceLockError { + fn from(_: PoisonError) -> Self { Self::GameCorrupted } } -// ---------- Player and instance API ---------- +// ========== implementations ========== + +impl Borrow for RawToken { + fn borrow(&self) -> &str { &self.0 } +} + +// ---------- Main instance API ---------- impl InstanceRef { #[throws(InstanceLockError)] @@ -192,8 +190,21 @@ impl Instance { InstanceGuard::forget_all_tokens(&mut g.tokens_players); InstanceGuard::forget_all_tokens(&mut g.tokens_clients); } + + pub fn list_names(scope: Option<&ManagementScope>) + -> Vec> { + let games = GLOBAL.games.read().unwrap(); + let out : Vec> = + games.keys() + .filter(|k| scope == None || scope == Some(&k.scope)) + .map(|k| k.clone()) + .collect(); + out + } } +// ---------- Simple trait implementations ---------- + impl Deref for InstanceGuard<'_> { type Target = Instance; fn deref(&self) -> &Instance { &self.c.g } @@ -202,6 +213,8 @@ impl DerefMut for InstanceGuard<'_> { fn deref_mut(&mut self) -> &mut Instance { &mut self.c.g } } +// ---------- Player and token functionality ---------- + impl InstanceGuard<'_> { #[throws(OE)] pub fn player_new(&mut self, newplayer: PlayerState) -> PlayerId { @@ -232,7 +245,11 @@ impl InstanceGuard<'_> { let mut global = global.write().unwrap(); for t in tokens.tr.drain() { global.remove(&t); } } +} +// ---------- save/load ---------- + +impl InstanceGuard<'_> { fn savefile(name: &InstanceName, prefix: &str, suffix: &str) -> String { let scope_prefix = { use ManagementScope::*; match &name.scope { Server => format!(""), @@ -336,17 +353,6 @@ impl InstanceGuard<'_> { } } -pub fn list_games(scope: Option<&ManagementScope>) - -> Vec> { - let games = GLOBAL.games.read().unwrap(); - let out : Vec> = - games.keys() - .filter(|k| scope == None || scope == Some(&k.scope)) - .map(|k| k.clone()) - .collect(); - out -} - // ---------- Lookup and token API ---------- impl AccessId for PlayerId { -- 2.30.2