feat: java glue leave_workspace, detach, new connect

This commit is contained in:
zaaarf 2024-08-08 02:45:52 +02:00
parent 893c3d31e0
commit a9bab2bb03
No known key found for this signature in database
GPG key ID: 102E445F4C3F829B
6 changed files with 63 additions and 29 deletions

View file

@ -9,7 +9,7 @@ import java.util.Optional;
public class Client { public class Client {
private final long ptr; 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) { Client(long ptr) {
this.ptr = ptr; this.ptr = ptr;
} }
@ -19,16 +19,16 @@ public class Client {
return get_url(this.ptr); 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; private static native Workspace join_workspace(long self, String id) throws CodeMPException;
public Workspace joinWorkspace(String id) throws CodeMPException { public Workspace joinWorkspace(String id) throws CodeMPException {
return join_workspace(this.ptr, id); 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); private static native Workspace get_workspace(long self);
public Optional<Workspace> getWorkspace() { public Optional<Workspace> getWorkspace() {
return Optional.ofNullable(get_workspace(this.ptr)); return Optional.ofNullable(get_workspace(this.ptr));

View file

@ -2,6 +2,7 @@ package mp.code;
import java.util.Optional; import java.util.Optional;
import mp.code.data.DetachResult;
import mp.code.exceptions.CodeMPException; import mp.code.exceptions.CodeMPException;
public class Workspace { public class Workspace {
@ -41,6 +42,11 @@ public class Workspace {
return attach_to_buffer(ptr, path); 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; private static native void fetch_buffers(long self) throws CodeMPException;
public void fetchBuffers() throws CodeMPException { public void fetchBuffers() throws CodeMPException {
fetch_buffers(this.ptr); fetch_buffers(this.ptr);

View file

@ -0,0 +1,7 @@
package mp.code.data;
public enum DetachResult {
NOT_ATTACHED,
DETACHING,
ALREADY_DETACHED
}

View file

@ -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)) }; let controller = unsafe { Box::leak(Box::from_raw(self_ptr as *mut crate::buffer::Controller)) };
controller.send(crate::api::TextChange { controller.send(crate::api::TextChange {
start: start as u32, start: start as u32,
end: end as u32 end: end as u32,
content content
}).jexcept(&mut env); }).jexcept(&mut env);
} }

View file

@ -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 crate::{client::Client, Workspace};
use super::{util::JExceptable, RT}; use super::{util::JExceptable, RT};
@ -8,10 +8,14 @@ use super::{util::JExceptable, RT};
pub extern "system" fn Java_mp_code_Client_connect<'local>( pub extern "system" fn Java_mp_code_Client_connect<'local>(
mut env: JNIEnv, mut env: JNIEnv,
_class: JClass<'local>, _class: JClass<'local>,
input: JString<'local> url: JString<'local>,
user: JString<'local>,
pwd: JString<'local>
) -> jobject { ) -> jobject {
let url: String = env.get_string(&input).expect("Couldn't get java string!").into(); let url: String = env.get_string(&url).expect("Couldn't get java string!").into();
RT.block_on(crate::Client::new(&url)) 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(|client| Box::into_raw(Box::new(client)) as jlong)
.map(|ptr| { .map(|ptr| {
let class = env.find_class("mp/code/Client").expect("Failed to find class"); 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() }).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. /// Joins a [Workspace] and returns a pointer to it.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_Client_join_1workspace<'local>( pub extern "system" fn Java_mp_code_Client_join_1workspace<'local>(
@ -71,6 +57,18 @@ fn spawn_updater(workspace: 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. /// Gets a [Workspace] by name and returns a pointer to it.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_Client_get_1workspace<'local>( pub extern "system" fn Java_mp_code_Client_get_1workspace<'local>(

View file

@ -103,6 +103,29 @@ pub extern "system" fn Java_mp_code_Workspace_attach_1to_1buffer<'local>(
}).jexcept(&mut env).as_raw() }).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. /// Updates the local buffer list.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_Workspace_fetch_1buffers( pub extern "system" fn Java_mp_code_Workspace_fetch_1buffers(