diff --git a/src/ffi/lua.rs b/src/ffi/lua.rs index 0672476..29cea38 100644 --- a/src/ffi/lua.rs +++ b/src/ffi/lua.rs @@ -10,6 +10,7 @@ lazy_static::lazy_static!{ // TODO use a runtime::Builder::new_current_thread() runtime to not behave like malware static ref RT : tokio::runtime::Runtime = tokio::runtime::Builder::new_current_thread().enable_all().build().expect("could not create tokio runtime"); static ref LOG : broadcast::Sender = broadcast::channel(32).0; + static ref STORE : dashmap::DashMap = dashmap::DashMap::default(); } fn runtime_drive_forever(_: &Lua, ():()) -> LuaResult<()> { @@ -24,7 +25,13 @@ impl From:: for LuaError { } fn connect(_: &Lua, (host, username, password): (String, String, String)) -> LuaResult { - Ok(RT.block_on(CodempClient::new(host, username, password))?) + let client = RT.block_on(CodempClient::new(host, username, password))?; + STORE.insert(client.user_id().to_string(), client.clone()); + Ok(client) +} + +fn get_client(_: &Lua, (id,): (String,)) -> LuaResult> { + Ok(STORE.get(&id).map(|x| x.value().clone())) } impl LuaUserData for CodempClient { @@ -240,9 +247,10 @@ fn codemp_lua(lua: &Lua) -> LuaResult { // entrypoint exports.set("connect", lua.create_function(connect)?)?; + exports.set("get_client", lua.create_function(get_client)?)?; // runtime - exports.set("spawn_runtime_driver", lua.create_function(runtime_drive_forever)?)?; + exports.set("runtime_drive_forever", lua.create_function(runtime_drive_forever)?)?; // logging exports.set("setup_logger", lua.create_function(setup_logger)?)?;