mirror of
https://github.com/hexedtech/codemp.git
synced 2024-11-22 15:24:48 +01:00
feat: java glue leave_workspace, detach, new connect
This commit is contained in:
parent
893c3d31e0
commit
a9bab2bb03
6 changed files with 63 additions and 29 deletions
12
dist/java/src/mp/code/Client.java
vendored
12
dist/java/src/mp/code/Client.java
vendored
|
@ -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<Workspace> getWorkspace() {
|
||||
return Optional.ofNullable(get_workspace(this.ptr));
|
||||
|
|
6
dist/java/src/mp/code/Workspace.java
vendored
6
dist/java/src/mp/code/Workspace.java
vendored
|
@ -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);
|
||||
|
|
7
dist/java/src/mp/code/data/DetachResult.java
vendored
Normal file
7
dist/java/src/mp/code/data/DetachResult.java
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
package mp.code.data;
|
||||
|
||||
public enum DetachResult {
|
||||
NOT_ATTACHED,
|
||||
DETACHING,
|
||||
ALREADY_DETACHED
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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>(
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Reference in a new issue