2024-08-27 23:04:56 +02:00
|
|
|
use jni::{objects::{JClass, JObject, JString, JValueGen}, sys::{jboolean, jlong, jobject, jobjectArray}, JNIEnv};
|
2024-08-06 23:30:00 +02:00
|
|
|
use crate::{client::Client, Workspace};
|
|
|
|
|
2024-08-10 02:45:20 +02:00
|
|
|
use super::{JExceptable, RT};
|
2024-08-06 23:30:00 +02:00
|
|
|
|
2024-09-05 02:45:33 +02:00
|
|
|
/// Connect to a given URL and return a [Client] to interact with that server.
|
2024-08-06 23:30:00 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "system" fn Java_mp_code_Client_connect<'local>(
|
|
|
|
mut env: JNIEnv,
|
|
|
|
_class: JClass<'local>,
|
2024-08-08 02:45:52 +02:00
|
|
|
url: JString<'local>,
|
|
|
|
user: JString<'local>,
|
|
|
|
pwd: JString<'local>
|
2024-08-07 10:22:01 +02:00
|
|
|
) -> jobject {
|
2024-08-10 02:45:20 +02:00
|
|
|
let url: String = env.get_string(&url)
|
|
|
|
.map(|s| s.into())
|
|
|
|
.jexcept(&mut env);
|
|
|
|
let user: String = env.get_string(&user)
|
|
|
|
.map(|s| s.into())
|
|
|
|
.jexcept(&mut env);
|
|
|
|
let pwd: String = env.get_string(&pwd)
|
|
|
|
.map(|s| s.into())
|
|
|
|
.jexcept(&mut env);
|
2024-08-27 23:04:56 +02:00
|
|
|
RT.block_on(crate::Client::connect(&url, &user, &pwd))
|
2024-08-06 23:30:00 +02:00
|
|
|
.map(|client| Box::into_raw(Box::new(client)) as jlong)
|
2024-08-07 10:22:01 +02:00
|
|
|
.map(|ptr| {
|
2024-08-10 02:45:20 +02:00
|
|
|
env.find_class("mp/code/Client")
|
|
|
|
.and_then(|class| env.new_object(class, "(J)V", &[JValueGen::Long(ptr)]))
|
|
|
|
.jexcept(&mut env)
|
2024-08-07 10:22:01 +02:00
|
|
|
}).jexcept(&mut env).as_raw()
|
2024-08-07 00:00:24 +02:00
|
|
|
}
|
|
|
|
|
2024-09-05 02:45:33 +02:00
|
|
|
/// Join a [Workspace] and return a pointer to it.
|
2024-08-06 23:30:00 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "system" fn Java_mp_code_Client_join_1workspace<'local>(
|
|
|
|
mut env: JNIEnv<'local>,
|
|
|
|
_class: JClass<'local>,
|
|
|
|
self_ptr: jlong,
|
|
|
|
input: JString<'local>
|
2024-08-07 10:22:01 +02:00
|
|
|
) -> jobject {
|
2024-08-09 14:11:13 +02:00
|
|
|
let client = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Client)) };
|
2024-08-10 02:45:20 +02:00
|
|
|
let workspace_id = unsafe { env.get_string_unchecked(&input) }
|
|
|
|
.map(|wid| wid.to_string_lossy().to_string())
|
|
|
|
.jexcept(&mut env);
|
|
|
|
RT.block_on(client.join_workspace(workspace_id))
|
2024-08-06 23:30:00 +02:00
|
|
|
.map(|workspace| spawn_updater(workspace.clone()))
|
|
|
|
.map(|workspace| Box::into_raw(Box::new(workspace)) as jlong)
|
2024-08-07 10:22:01 +02:00
|
|
|
.map(|ptr| {
|
2024-08-10 02:45:20 +02:00
|
|
|
env.find_class("mp/code/Workspace")
|
|
|
|
.and_then(|class| env.new_object(class, "(J)V", &[JValueGen::Long(ptr)]))
|
|
|
|
.jexcept(&mut env)
|
2024-08-07 10:22:01 +02:00
|
|
|
}).jexcept(&mut env).as_raw()
|
2024-08-06 23:30:00 +02:00
|
|
|
}
|
|
|
|
|
2024-09-05 02:45:33 +02:00
|
|
|
/// Create a workspace on server, if allowed to.
|
2024-08-27 23:04:56 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "system" fn Java_mp_code_Client_create_1workspace<'local>(
|
|
|
|
mut env: JNIEnv<'local>,
|
|
|
|
_class: JClass<'local>,
|
|
|
|
self_ptr: jlong,
|
|
|
|
input: JString<'local>
|
|
|
|
) {
|
|
|
|
let client = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Client)) };
|
|
|
|
let workspace_id = unsafe { env.get_string_unchecked(&input) }
|
|
|
|
.map(|wid| wid.to_string_lossy().to_string())
|
|
|
|
.jexcept(&mut env);
|
|
|
|
RT
|
|
|
|
.block_on(client.create_workspace(workspace_id))
|
|
|
|
.jexcept(&mut env);
|
|
|
|
}
|
|
|
|
|
2024-09-05 02:45:33 +02:00
|
|
|
/// Delete a workspace on server, if allowed to.
|
2024-08-27 23:04:56 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "system" fn Java_mp_code_Client_delete_1workspace<'local>(
|
|
|
|
mut env: JNIEnv<'local>,
|
|
|
|
_class: JClass<'local>,
|
|
|
|
self_ptr: jlong,
|
|
|
|
input: JString<'local>
|
|
|
|
) {
|
|
|
|
let client = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Client)) };
|
|
|
|
let workspace_id = unsafe { env.get_string_unchecked(&input) }
|
|
|
|
.map(|wid| wid.to_string_lossy().to_string())
|
|
|
|
.jexcept(&mut env);
|
|
|
|
RT
|
|
|
|
.block_on(client.delete_workspace(workspace_id))
|
|
|
|
.jexcept(&mut env);
|
|
|
|
}
|
|
|
|
|
2024-09-05 02:45:33 +02:00
|
|
|
/// Invite another user to an owned workspace.
|
2024-08-27 23:04:56 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "system" fn Java_mp_code_Client_invite_1to_1workspace<'local>(
|
|
|
|
mut env: JNIEnv<'local>,
|
|
|
|
_class: JClass<'local>,
|
|
|
|
self_ptr: jlong,
|
|
|
|
ws: JString<'local>,
|
|
|
|
usr: JString<'local>
|
|
|
|
) {
|
|
|
|
let client = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Client)) };
|
|
|
|
let workspace_id = unsafe { env.get_string_unchecked(&ws) }
|
|
|
|
.map(|wid| wid.to_string_lossy().to_string())
|
|
|
|
.jexcept(&mut env);
|
|
|
|
let user_name = unsafe { env.get_string_unchecked(&usr) }
|
|
|
|
.map(|wid| wid.to_string_lossy().to_string())
|
|
|
|
.jexcept(&mut env);
|
|
|
|
RT
|
|
|
|
.block_on(client.invite_to_workspace(workspace_id, user_name))
|
|
|
|
.jexcept(&mut env);
|
|
|
|
}
|
|
|
|
|
2024-09-05 02:45:33 +02:00
|
|
|
/// List available workspaces.
|
2024-08-27 23:04:56 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "system" fn Java_mp_code_Client_list_1workspaces<'local>(
|
|
|
|
mut env: JNIEnv<'local>,
|
|
|
|
_class: JClass<'local>,
|
|
|
|
self_ptr: jlong,
|
|
|
|
owned: jboolean,
|
|
|
|
invited: jboolean
|
|
|
|
) -> jobjectArray {
|
|
|
|
let client = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Client)) };
|
|
|
|
let list = RT
|
|
|
|
.block_on(client.list_workspaces(owned != 0, invited != 0))
|
|
|
|
.jexcept(&mut env);
|
|
|
|
|
|
|
|
env.find_class("java/lang/String")
|
|
|
|
.and_then(|class| env.new_object_array(list.len() as i32, class, JObject::null()))
|
|
|
|
.map(|arr| {
|
|
|
|
for (idx, path) in list.iter().enumerate() {
|
|
|
|
env.new_string(path)
|
|
|
|
.and_then(|path| env.set_object_array_element(&arr, idx as i32, path))
|
|
|
|
.jexcept(&mut env)
|
|
|
|
}
|
|
|
|
arr
|
|
|
|
}).jexcept(&mut env).as_raw()
|
|
|
|
}
|
|
|
|
|
2024-08-06 23:30:00 +02:00
|
|
|
// TODO: this stays until we get rid of the arc then i'll have to find a better way
|
|
|
|
fn spawn_updater(workspace: Workspace) -> Workspace {
|
|
|
|
let w = workspace.clone();
|
|
|
|
RT.spawn(async move {
|
|
|
|
loop {
|
|
|
|
tokio::time::sleep(std::time::Duration::from_secs(60)).await;
|
|
|
|
w.fetch_buffers().await.unwrap();
|
|
|
|
w.fetch_users().await.unwrap();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
workspace
|
|
|
|
}
|
2024-08-07 10:22:01 +02:00
|
|
|
|
2024-09-05 02:45:33 +02:00
|
|
|
/// Leave a [Workspace] and return whether or not the client was in such workspace.
|
2024-08-08 02:45:52 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "system" fn Java_mp_code_Client_leave_1workspace<'local>(
|
2024-08-10 02:45:20 +02:00
|
|
|
mut env: JNIEnv<'local>,
|
2024-08-08 02:45:52 +02:00
|
|
|
_class: JClass<'local>,
|
|
|
|
self_ptr: jlong,
|
|
|
|
input: JString<'local>
|
|
|
|
) -> jboolean {
|
2024-08-09 14:11:13 +02:00
|
|
|
let client = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Client)) };
|
2024-08-10 02:45:20 +02:00
|
|
|
unsafe { env.get_string_unchecked(&input) }
|
|
|
|
.map(|wid| wid.to_string_lossy().to_string())
|
|
|
|
.map(|wid| client.leave_workspace(&wid) as jboolean)
|
|
|
|
.jexcept(&mut env)
|
2024-08-08 02:45:52 +02:00
|
|
|
}
|
2024-08-27 23:04:56 +02:00
|
|
|
|
2024-09-05 02:45:33 +02:00
|
|
|
/// Get a [Workspace] by name and returns a pointer to it.
|
2024-08-07 10:22:01 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "system" fn Java_mp_code_Client_get_1workspace<'local>(
|
|
|
|
mut env: JNIEnv<'local>,
|
|
|
|
_class: JClass<'local>,
|
|
|
|
self_ptr: jlong,
|
|
|
|
input: JString<'local>
|
|
|
|
) -> jobject {
|
2024-08-09 14:11:13 +02:00
|
|
|
let client = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Client)) };
|
2024-08-10 02:45:20 +02:00
|
|
|
let workspace_id = unsafe { env.get_string_unchecked(&input) }
|
|
|
|
.map(|wid| wid.to_string_lossy().to_string())
|
|
|
|
.jexcept(&mut env);
|
|
|
|
client.get_workspace(&workspace_id)
|
2024-08-07 10:22:01 +02:00
|
|
|
.map(|workspace| Box::into_raw(Box::new(workspace)) as jlong)
|
|
|
|
.map(|ptr| {
|
2024-08-10 02:45:20 +02:00
|
|
|
env.find_class("mp/code/Workspace")
|
|
|
|
.and_then(|class| env.new_object(class, "(J)V", &[JValueGen::Long(ptr)]))
|
|
|
|
.jexcept(&mut env)
|
|
|
|
}).unwrap_or_default().as_raw()
|
2024-08-07 10:22:01 +02:00
|
|
|
}
|
|
|
|
|
2024-09-05 02:45:33 +02:00
|
|
|
/// Refresh the client's session token.
|
2024-08-27 23:04:56 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "system" fn Java_mp_code_Client_refresh<'local>(
|
|
|
|
mut env: JNIEnv<'local>,
|
|
|
|
_class: JClass<'local>,
|
|
|
|
self_ptr: jlong,
|
|
|
|
) {
|
|
|
|
let client = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Client)) };
|
2024-09-05 02:45:33 +02:00
|
|
|
RT.block_on(client.refresh())
|
|
|
|
.jexcept(&mut env);
|
2024-08-27 23:04:56 +02:00
|
|
|
}
|
|
|
|
|
2024-09-05 02:45:33 +02:00
|
|
|
/// Set up the tracing subscriber.
|
2024-08-07 10:22:01 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "system" fn Java_mp_code_Client_setup_1tracing<'local>(
|
|
|
|
mut env: JNIEnv,
|
|
|
|
_class: JClass<'local>,
|
|
|
|
path: JString<'local>
|
|
|
|
) {
|
|
|
|
super::setup_logger(
|
|
|
|
true,
|
|
|
|
Some(path)
|
|
|
|
.filter(|p| p.is_null())
|
2024-08-10 02:45:20 +02:00
|
|
|
.map(|p| env.get_string(&p).map(|s| s.into()).jexcept(&mut env))
|
2024-08-07 10:22:01 +02:00
|
|
|
);
|
|
|
|
}
|
2024-08-08 00:29:54 +02:00
|
|
|
|
|
|
|
/// Called by the Java GC to drop a [Client].
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern "system" fn Java_mp_code_Client_free(_env: JNIEnv, _class: JClass, input: jlong) {
|
|
|
|
let _ = unsafe { Box::from_raw(input as *mut Client) };
|
|
|
|
}
|