use crate::imports::*;
use lazy_static::lazy_static;
+// ---------- newtypes and type aliases ----------
+
visible_slotmap_key!{ ClientId('C') }
visible_slotmap_key!{ PlayerId('#') }
#[derive(Clone,Debug,Eq,PartialEq,Ord,PartialOrd,Hash)]
pub struct RawToken (pub String);
-impl Borrow<str> for RawToken {
- fn borrow(&self) -> &str { &self.0 }
-}
-#[derive(Debug)]
-pub struct Client {
- pub player : PlayerId,
-}
+// ---------- data structure ----------
#[derive(Debug)]
pub struct Instance {
pub updates : SecondarySlotMap<PlayerId, PlayerUpdates>,
}
+#[derive(Debug)]
+pub struct Client {
+ pub player : PlayerId,
+}
+
+#[derive(Default)]
+struct Global {
+ // lock hierarchy: this is the innermost lock
+ players : RwLock<TokenTable<PlayerId>>,
+ clients : RwLock<TokenTable<ClientId>>,
+ // xxx delete instances at some point!
+}
+
+// ---------- API ----------
+
#[derive(Clone,Debug)]
pub struct InstanceAccessDetails<Id> {
pub g : Arc<Mutex<Instance>>,
pub type TokenTable<Id> = HashMap<RawToken, InstanceAccessDetails<Id>>;
-#[derive(Default)]
-struct Global {
- // lock hierarchy: this is the innermost lock
- players : RwLock<TokenTable<PlayerId>>,
- clients : RwLock<TokenTable<ClientId>>,
- // xxx delete instances at some point!
+pub trait AccessId : Copy + Clone + 'static {
+ fn global_tokens() -> &'static RwLock<TokenTable<Self>>;
+ const ERROR : OnlineError;
}
+// ========== implementations ==========
+
+// ---------- newtypes and type aliases ----------
+
+impl Borrow<str> for RawToken {
+ fn borrow(&self) -> &str { &self.0 }
+}
+
+// ---------- data structure ----------
+
lazy_static! {
static ref GLOBAL : Global = Default::default();
}
-pub trait AccessId : Copy + Clone + 'static {
- fn global_tokens() -> &'static RwLock<TokenTable<Self>>;
- const ERROR : OnlineError;
-}
+// ---------- API ----------
impl AccessId for PlayerId {
const ERROR : OnlineError = NoPlayer;
.ok_or(Id::ERROR)
}
+impl<'r, Id> FromParam<'r> for InstanceAccess<'r, Id>
+ where Id : AccessId
+{
+ type Error = OE;
+ #[throws(OE)]
+ fn from_param(param: &'r RawStr) -> Self {
+ let g = Id::global_tokens().read().unwrap();
+ let token = param.as_str();
+ let i = g.get(token).ok_or(Id::ERROR)?;
+ InstanceAccess { raw_token : token, i : i.clone() }
+ }
+}
+
pub fn record_token<Id : AccessId>(iad : InstanceAccessDetails<Id>)
-> RawToken {
let mut rng = thread_rng();
token
}
+// ========== ad-hoc and temporary ==========
+
const XXX_PLAYERS_TOKENS : &[(&str, &str)] = &[
("kmqAKPwK4TfReFjMor8MJhdRPBcwIBpe", "alice"),
("ccg9kzoTh758QrVE1xMY7BQWB36dNJTx", "bob"),
];
-impl<'r, Id> FromParam<'r> for InstanceAccess<'r, Id>
- where Id : AccessId
-{
- type Error = OE;
- #[throws(OE)]
- fn from_param(param: &'r RawStr) -> Self {
- let g = Id::global_tokens().read().unwrap();
- let token = param.as_str();
- let i = g.get(token).ok_or(Id::ERROR)?;
- InstanceAccess { raw_token : token, i : i.clone() }
- }
-}
-
pub fn xxx_global_setup() {
let gi = Instance {
gs : xxx_gamestate_init(),