feat: working glue

Co-authored-by: alemi <me@alemi.dev>
This commit is contained in:
zaaarf 2024-08-07 02:43:25 +02:00
parent 4bb35f1727
commit 84996489e1
No known key found for this signature in database
GPG key ID: 102E445F4C3F829B
10 changed files with 77 additions and 90 deletions

View file

@ -1,7 +1,7 @@
package mp.code; package mp.code;
import mp.code.data.TextChange; import mp.code.data.TextChange;
import mp.code.exceptions.CodeMPLibException; import mp.code.exceptions.CodeMPException;
public class BufferController { public class BufferController {
private final long ptr; private final long ptr;
@ -20,20 +20,20 @@ public class BufferController {
return get_content(this.ptr); return get_content(this.ptr);
} }
private static native TextChange try_recv(long self) throws CodeMPLibException; private static native TextChange try_recv(long self) throws CodeMPException;
public TextChange tryRecv() throws CodeMPLibException { public TextChange tryRecv() throws CodeMPException {
return try_recv(this.ptr); return try_recv(this.ptr);
} }
private static native void send(long self, TextChange change) throws CodeMPLibException; private static native void send(long self, TextChange change) throws CodeMPException;
public void send(TextChange change) throws CodeMPLibException { public void send(TextChange change) throws CodeMPException {
send(this.ptr, change); send(this.ptr, change);
} }
private static native void free(long self); private static native void free(long self);
@Override @Override
@SuppressWarnings("removal") @SuppressWarnings("removal")
protected void finalize() throws Throwable { protected void finalize() {
free(this.ptr); free(this.ptr);
} }
} }

View file

@ -1,19 +1,17 @@
package mp.code; package mp.code;
import mp.code.data.Cursor; import mp.code.exceptions.CodeMPException;
import mp.code.exceptions.CodeMPLibException;
import java.util.Optional; import java.util.Optional;
import java.util.UUID;
public class Client { public class Client {
private final long ptr; private final long ptr;
private final String url; private final String url;
private static native long setup_tracing(String path); public static native long setup_tracing(String path);
private static native long connect(String url) throws CodeMPLibException; private static native long connect(String url) throws CodeMPException;
public Client(String url) throws CodeMPLibException { public Client(String url) throws CodeMPException {
this.ptr = connect(url); this.ptr = connect(url);
this.url = url; this.url = url;
} }
@ -22,13 +20,13 @@ public class Client {
return this.url; return this.url;
} }
private static native void login(long self, String username, String password, String workspace) throws CodeMPLibException; 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 CodeMPLibException { public void login(String username, String password, String workspace) throws CodeMPException {
login(this.ptr, username, password, workspace); login(this.ptr, username, password, workspace);
} }
private static native long join_workspace(long self, String id) throws CodeMPLibException; private static native long join_workspace(long self, String id) throws CodeMPException;
public Workspace joinWorkspace(String id) throws CodeMPLibException { public Workspace joinWorkspace(String id) throws CodeMPException {
return new Workspace(join_workspace(this.ptr, id)); return new Workspace(join_workspace(this.ptr, id));
} }
@ -48,36 +46,5 @@ public class Client {
protected void finalize() { protected void finalize() {
free(this.ptr); free(this.ptr);
} }
// TODO - remove everything past this line
public static void main(String[] args) throws CodeMPLibException, InterruptedException {
Client c = new Client("http://alemi.dev:50053");
c.login(UUID.randomUUID().toString(), "lmaodefaultpassword", "glue");
Workspace workspace = c.joinWorkspace("glue");
System.out.println(workspace.getWorkspaceId());
while(true) {
Cursor cursor = workspace.getCursor().tryRecv();
if(cursor == null) System.out.println("null!");
else {
System.out.printf(
"sr: %d, sc: %d, er: %d, ec: %d, cursor: %s, buffer: %s\n",
cursor.startRow,
cursor.startCol,
cursor.endRow,
cursor.endCol,
cursor.user,
cursor.buffer
);
}
Thread.sleep(100);
}
//System.out.println("Done!");
}
static {
System.loadLibrary("codemp");
setup_tracing(null);
}
} }

View file

@ -1,7 +1,7 @@
package mp.code; package mp.code;
import mp.code.data.Cursor; import mp.code.data.Cursor;
import mp.code.data.TextChange; import mp.code.exceptions.CodeMPException;
public class CursorController { public class CursorController {
private final long ptr; private final long ptr;
@ -10,20 +10,20 @@ public class CursorController {
this.ptr = ptr; this.ptr = ptr;
} }
private static native Cursor try_recv(long self); private static native Cursor try_recv(long self) throws CodeMPException;
public Cursor tryRecv() { public Cursor tryRecv() throws CodeMPException {
return try_recv(this.ptr); return try_recv(this.ptr);
} }
private static native void send(long self, Cursor cursor); private static native void send(long self, Cursor cursor) throws CodeMPException;
public void send(TextChange change, Cursor cursor) { public void send(Cursor cursor) throws CodeMPException {
send(this.ptr, cursor); send(this.ptr, cursor);
} }
private static native void free(long self); private static native void free(long self);
@Override @Override
@SuppressWarnings("removal") @SuppressWarnings("removal")
protected void finalize() throws Throwable { protected void finalize() {
free(this.ptr); free(this.ptr);
} }
} }

View file

@ -1,6 +1,6 @@
package mp.code; package mp.code;
import mp.code.exceptions.CodeMPLibException; import mp.code.exceptions.CodeMPException;
public class Workspace { public class Workspace {
private final long ptr; private final long ptr;
@ -24,47 +24,50 @@ public class Workspace {
return new BufferController(get_buffer(this.ptr, path)); return new BufferController(get_buffer(this.ptr, path));
} }
private static native void get_file_tree(long self); private static native String[] get_file_tree(long self);
public void getFileTree() { public String[] getFileTree() {
// TODO vector? return get_file_tree(this.ptr);
} }
private static native long create_buffer(String path) throws CodeMPLibException; private static native long create_buffer(String path) throws CodeMPException;
public BufferController createBuffer(String path) throws CodeMPLibException { public BufferController createBuffer(String path) throws CodeMPException {
return new BufferController(create_buffer(path)); return new BufferController(create_buffer(path));
} }
private static native long attach_to_buffer(long self) throws CodeMPLibException; private static native long attach_to_buffer(long self) throws CodeMPException;
public BufferController attachToBuffer() throws CodeMPLibException { public BufferController attachToBuffer() throws CodeMPException {
return new BufferController(attach_to_buffer(ptr)); return new BufferController(attach_to_buffer(ptr));
} }
private static native void fetch_buffers(long self) throws CodeMPLibException; private static native void fetch_buffers(long self) throws CodeMPException;
public void fetchBuffers() throws CodeMPLibException { public void fetchBuffers() throws CodeMPException {
fetch_buffers(this.ptr); fetch_buffers(this.ptr);
} }
private static native void fetch_users(long self) throws CodeMPLibException; private static native void fetch_users(long self) throws CodeMPException;
public void fetchUsers() throws CodeMPLibException { public void fetchUsers() throws CodeMPException {
fetch_buffers(this.ptr); fetch_buffers(this.ptr);
} }
private static native void list_buffer_users(long self, String path) throws CodeMPLibException; private static native String[] list_buffer_users(long self, String path) throws CodeMPException;
public void listBufferUsers(String path) throws CodeMPLibException { public String[] listBufferUsers(String path) throws CodeMPException {
// TODO pass vector return list_buffer_users(this.ptr, path);
} }
private static native void delete_buffer(long self, String path) throws CodeMPLibException; private static native void delete_buffer(long self, String path) throws CodeMPException;
public void deleteBuffer(String path) throws CodeMPLibException { public void deleteBuffer(String path) throws CodeMPException {
delete_buffer(this.ptr, path); delete_buffer(this.ptr, path);
} }
// TODO select_buffer private static native BufferController select_buffer(long self, long timeout) throws CodeMPException;
public BufferController selectBuffer(long timeout) throws CodeMPException {
return select_buffer(this.ptr, timeout);
}
private static native void free(long self); private static native void free(long self);
@Override @Override
@SuppressWarnings("removal") @SuppressWarnings("removal")
protected void finalize() throws Throwable { protected void finalize() {
free(this.ptr); free(this.ptr);
} }
} }

View file

@ -1,6 +1,6 @@
package mp.code.exceptions; package mp.code.exceptions;
public class ChannelException extends CodeMPLibException { public class ChannelException extends CodeMPException {
public ChannelException(String input) { public ChannelException(String input) {
super(input); super(input);
} }

View file

@ -3,8 +3,8 @@ package mp.code.exceptions;
/** /**
* A generic class for all our exceptions coming through the JNI from the library. * A generic class for all our exceptions coming through the JNI from the library.
*/ */
public abstract class CodeMPLibException extends Exception { public abstract class CodeMPException extends Exception {
protected CodeMPLibException(String msg) { protected CodeMPException(String msg) {
super(msg); super(msg);
} }
} }

View file

@ -1,6 +1,6 @@
package mp.code.exceptions; package mp.code.exceptions;
public class DeadlockedException extends CodeMPLibException { public class DeadlockedException extends CodeMPException {
public DeadlockedException(String s) { public DeadlockedException(String s) {
super(s); super(s);
} }

View file

@ -1,6 +1,6 @@
package mp.code.exceptions; package mp.code.exceptions;
public class InvalidStateException extends CodeMPLibException { public class InvalidStateException extends CodeMPException {
public InvalidStateException(String message) { public InvalidStateException(String message) {
super(message); super(message);
} }

View file

@ -1,6 +1,6 @@
package mp.code.exceptions; package mp.code.exceptions;
public class TransportException extends CodeMPLibException { public class TransportException extends CodeMPException {
public TransportException(String message) { public TransportException(String message) {
super(message); super(message);
} }

View file

@ -1,4 +1,4 @@
use jni::{objects::{JClass, JString}, sys::{jlong, jstring}, JNIEnv}; use jni::{objects::{JClass, JObject, JString}, sys::{jlong, jobjectArray, jstring}, JNIEnv};
use crate::Workspace; use crate::Workspace;
use super::{util::JExceptable, RT}; use super::{util::JExceptable, RT};
@ -62,13 +62,22 @@ pub extern "system" fn Java_mp_code_Workspace_create_1buffer<'local>(
/// Gets the filetree. /// Gets the filetree.
#[no_mangle] #[no_mangle]
pub extern "system" fn Java_mp_code_Workspace_get_1file_1tree( pub extern "system" fn Java_mp_code_Workspace_get_1file_1tree(
_env: JNIEnv, mut env: JNIEnv,
_class: JClass, _class: JClass,
self_ptr: jlong, self_ptr: jlong,
) { ) -> jobjectArray {
let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) }; let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) };
let _file_tree = workspace.filetree(); let file_tree = workspace.filetree();
todo!() // how to return Vec<String> ? []String ? let class = env.find_class("java/lang/String").expect("Failed to find class!");
let arr = env.new_object_array(file_tree.len() as i32, class, JObject::null())
.expect("failed creating array");
for (idx, path) in file_tree.iter().enumerate() {
let js = env.new_string(path).expect("Failed to create String!");
env.set_object_array_element(&arr, idx as i32, js)
.expect("Failed to set array element!")
}
arr.as_raw()
} }
/// Attaches to a buffer and returns a pointer to its [crate::buffer::Controller]. /// Attaches to a buffer and returns a pointer to its [crate::buffer::Controller].
@ -115,15 +124,23 @@ pub extern "system" fn Java_mp_code_Workspace_list_1buffer_1users<'local>(
_class: JClass<'local>, _class: JClass<'local>,
self_ptr: jlong, self_ptr: jlong,
input: JString<'local>, input: JString<'local>,
) { ) -> jobjectArray {
let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) }; let workspace = unsafe { Box::leak(Box::from_raw(self_ptr as *mut Workspace)) };
let buffer = unsafe { env.get_string_unchecked(&input).expect("Couldn't get java string!") }; let buffer = unsafe { env.get_string_unchecked(&input).expect("Couldn't get java string!") };
let _users = RT.block_on(workspace.list_buffer_users(buffer.to_str().expect("Not UTF-8!"))) let users = RT.block_on(workspace.list_buffer_users(buffer.to_str().expect("Not UTF-8!")))
.jexcept(&mut env) .jexcept(&mut env);
.into_iter()
.map(|x| x.id.to_string()) let class = env.find_class("java/lang/String").expect("Failed to find class!");
.collect::<Vec<String>>(); let arr = env.new_object_array(users.len() as i32, class, JObject::null())
todo!() // how to return Vec<String>? .expect("failed creating array");
for (idx, user) in users.iter().enumerate() {
let js = env.new_string(&user.id).expect("Failed to create String!");
env.set_object_array_element(&arr, idx as i32, js)
.expect("Failed to set array element!")
}
arr.as_raw()
} }
/// Deletes a buffer. /// Deletes a buffer.