From a9bab2bb03aeda11253412d6862eccf2e0238d78 Mon Sep 17 00:00:00 2001 From: zaaarf Date: Thu, 8 Aug 2024 02:45:52 +0200 Subject: [PATCH] feat: java glue leave_workspace, detach, new connect --- dist/java/src/mp/code/Client.java | 12 +++--- dist/java/src/mp/code/Workspace.java | 6 +++ dist/java/src/mp/code/data/DetachResult.java | 7 ++++ src/ffi/java/buffer.rs | 2 +- src/ffi/java/client.rs | 42 ++++++++++---------- src/ffi/java/workspace.rs | 23 +++++++++++ 6 files changed, 63 insertions(+), 29 deletions(-) create mode 100644 dist/java/src/mp/code/data/DetachResult.java diff --git a/dist/java/src/mp/code/Client.java b/dist/java/src/mp/code/Client.java index 934900b..a0fd532 100644 --- a/dist/java/src/mp/code/Client.java +++ b/dist/java/src/mp/code/Client.java @@ -9,7 +9,7 @@ import java.util.Optional; public class Client { private final long ptr; - public static native Client connect(String url) throws CodeMPException; + public static native Client connect(String url, String username, String password) throws CodeMPException; Client(long ptr) { this.ptr = ptr; } @@ -19,16 +19,16 @@ public class Client { return get_url(this.ptr); } - private static native void login(long self, String username, String password, String workspace) throws CodeMPException; - public void login(String username, String password, String workspace) throws CodeMPException { - login(this.ptr, username, password, workspace); - } - private static native Workspace join_workspace(long self, String id) throws CodeMPException; public Workspace joinWorkspace(String id) throws CodeMPException { return join_workspace(this.ptr, id); } + private static native boolean leave_workspace(long self, String id); + public boolean leaveWorkspace(String id) { + return leave_workspace(this.ptr, id); + } + private static native Workspace get_workspace(long self); public Optional getWorkspace() { return Optional.ofNullable(get_workspace(this.ptr)); diff --git a/dist/java/src/mp/code/Workspace.java b/dist/java/src/mp/code/Workspace.java index 1d796c4..edf5197 100644 --- a/dist/java/src/mp/code/Workspace.java +++ b/dist/java/src/mp/code/Workspace.java @@ -2,6 +2,7 @@ package mp.code; import java.util.Optional; +import mp.code.data.DetachResult; import mp.code.exceptions.CodeMPException; public class Workspace { @@ -41,6 +42,11 @@ public class Workspace { return attach_to_buffer(ptr, path); } + private static native DetachResult detach_from_buffer(long self, String path); + public DetachResult detachFromBuffer(String path) { + return detach_from_buffer(this.ptr, path); + } + private static native void fetch_buffers(long self) throws CodeMPException; public void fetchBuffers() throws CodeMPException { fetch_buffers(this.ptr); diff --git a/dist/java/src/mp/code/data/DetachResult.java b/dist/java/src/mp/code/data/DetachResult.java new file mode 100644 index 0000000..3210eb9 --- /dev/null +++ b/dist/java/src/mp/code/data/DetachResult.java @@ -0,0 +1,7 @@ +package mp.code.data; + +public enum DetachResult { + NOT_ATTACHED, + DETACHING, + ALREADY_DETACHED +} diff --git a/src/ffi/java/buffer.rs b/src/ffi/java/buffer.rs index 66a2199..ee5b0a8 100644 --- a/src/ffi/java/buffer.rs +++ b/src/ffi/java/buffer.rs @@ -95,7 +95,7 @@ pub extern "system" fn Java_mp_code_BufferController_send<'local>( let controller = unsafe { Box::leak(Box::from_raw(self_ptr as *mut crate::buffer::Controller)) }; controller.send(crate::api::TextChange { start: start as u32, - end: end as u32 + end: end as u32, content }).jexcept(&mut env); } diff --git a/src/ffi/java/client.rs b/src/ffi/java/client.rs index 4a471e5..df39196 100644 --- a/src/ffi/java/client.rs +++ b/src/ffi/java/client.rs @@ -1,4 +1,4 @@ -use jni::{objects::{JClass, JObject, JString, JValueGen}, sys::{jlong, jobject}, JNIEnv}; +use jni::{objects::{JClass, JObject, JString, JValueGen}, sys::{jboolean, jlong, jobject}, JNIEnv}; use crate::{client::Client, Workspace}; use super::{util::JExceptable, RT}; @@ -8,10 +8,14 @@ use super::{util::JExceptable, RT}; pub extern "system" fn Java_mp_code_Client_connect<'local>( mut env: JNIEnv, _class: JClass<'local>, - input: JString<'local> + url: JString<'local>, + user: JString<'local>, + pwd: JString<'local> ) -> jobject { - let url: String = env.get_string(&input).expect("Couldn't get java string!").into(); - RT.block_on(crate::Client::new(&url)) + let url: String = env.get_string(&url).expect("Couldn't get java string!").into(); + let user: String = env.get_string(&user).expect("Couldn't get java string!").into(); + let pwd: String = env.get_string(&pwd).expect("Couldn't get java string!").into(); + RT.block_on(crate::Client::new(&url, &user, &pwd)) .map(|client| Box::into_raw(Box::new(client)) as jlong) .map(|ptr| { let class = env.find_class("mp/code/Client").expect("Failed to find class"); @@ -20,24 +24,6 @@ pub extern "system" fn Java_mp_code_Client_connect<'local>( }).jexcept(&mut env).as_raw() } -/// Logs in to a specific [Workspace]. -#[no_mangle] -pub extern "system" fn Java_mp_code_Client_login<'local>( - mut env: JNIEnv<'local>, - _class: JClass<'local>, - self_ptr: jlong, - user: JString<'local>, - pwd: JString<'local>, - workspace: JString<'local> -) { - let client = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Client)) }; - let user: String = env.get_string(&user).expect("Couldn't get java string!").into(); - let pwd: String = env.get_string(&pwd).expect("Couldn't get java string!").into(); - let workspace: String = env.get_string(&workspace).expect("Couldn't get java string!").into(); - RT.block_on(client.login(user, pwd, Some(workspace))) - .jexcept(&mut env) -} - /// Joins a [Workspace] and returns a pointer to it. #[no_mangle] pub extern "system" fn Java_mp_code_Client_join_1workspace<'local>( @@ -71,6 +57,18 @@ fn spawn_updater(workspace: Workspace) -> Workspace { workspace } +/// Leaves a [Workspace] and returns whether or not the client was in such workspace. +#[no_mangle] +pub extern "system" fn Java_mp_code_Client_leave_1workspace<'local>( + env: JNIEnv<'local>, + _class: JClass<'local>, + self_ptr: jlong, + input: JString<'local> +) -> jboolean { + let client = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Client)) }; + let workspace_id = unsafe { env.get_string_unchecked(&input).expect("Couldn't get java string!") }; + client.leave_workspace(workspace_id.to_str().expect("Not UTF-8")) as jboolean +} /// Gets a [Workspace] by name and returns a pointer to it. #[no_mangle] pub extern "system" fn Java_mp_code_Client_get_1workspace<'local>( diff --git a/src/ffi/java/workspace.rs b/src/ffi/java/workspace.rs index 766c9ce..e5c332a 100644 --- a/src/ffi/java/workspace.rs +++ b/src/ffi/java/workspace.rs @@ -103,6 +103,29 @@ pub extern "system" fn Java_mp_code_Workspace_attach_1to_1buffer<'local>( }).jexcept(&mut env).as_raw() } +#[no_mangle] +pub extern "system" fn Java_mp_code_Workspace_detach_1from_1buffer<'local>( + mut env: JNIEnv, + _class: JClass<'local>, + self_ptr: jlong, + input: JString<'local> +) -> jobject { + let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) }; + let path = unsafe { env.get_string_unchecked(&input).expect("Couldn't get java string!") }; + let name = match workspace.detach(path.to_str().expect("Not UTF-8")) { + crate::workspace::DetachResult::NotAttached => "NOT_ATTACHED", + crate::workspace::DetachResult::Detaching => "DETACHED", + crate::workspace::DetachResult::AlreadyDetached => "ALREADY_DETACHED" + }; + + let class = env.find_class("mp/code/data/DetachResult").expect("Failed to find class!"); + env.get_static_field(class, name, "Lmp/code/data/DetachResult;") + .expect("Failed to get field!") + .l() + .expect("Field was of wrong type!") + .as_raw() +} + /// Updates the local buffer list. #[no_mangle] pub extern "system" fn Java_mp_code_Workspace_fetch_1buffers(