codemp/src/ffi/java/client.rs

145 lines
4.6 KiB
Rust
Raw Normal View History

use jni::{objects::{JClass, JObject, JString}, sys::{jboolean, jlong, jobject, jobjectArray}, JNIEnv};
2024-09-22 02:22:51 +02:00
use jni_toolbox::{jni, FromJava, IntoJava, JniToolboxError};
use crate::{api::Config, client::Client, errors::{ConnectionError, RemoteError}, ffi::java::{handle_error, null_check}, Workspace};
2024-08-06 23:30:00 +02:00
use super::{Deobjectify, JExceptable, JObjectify, tokio};
2024-08-06 23:30:00 +02:00
2024-09-22 02:22:51 +02:00
impl<'j> IntoJava<'j> for Client {
type T = jobject;
fn into_java(self, env: &mut jni::JNIEnv<'j>) -> Result<Self::T, jni::errors::Error> {
// Ok(Box::into_raw(Box::new(self)))
todo!()
}
}
impl<'j> FromJava<'j> for Client {
type T = jobject;
fn from_java(env: &mut jni::JNIEnv<'j>, value: Self::T) -> Result<Self, jni::errors::Error> {
let x = unsafe { Box::leak(Box::from_raw(value as *mut Client)) };
todo!();
Ok(x.clone())
}
}
impl<'j> FromJava<'j> for Config {
type T = JObject<'j>;
fn from_java(env: &mut jni::JNIEnv<'j>, value: Self::T) -> Result<Self, jni::errors::Error> {
Ok(Config::deobjectify(env, value)?)
}
}
impl<'j> IntoJava<'j> for crate::api::User {
type T = jobject;
fn into_java(self, env: &mut jni::JNIEnv<'j>) -> Result<Self::T, jni::errors::Error> {
Ok(self.jobjectify(env)?.into_raw())
}
2024-09-22 02:22:51 +02:00
}
impl<'j> IntoJava<'j> for Workspace {
type T = jobject;
fn into_java(self, env: &mut jni::JNIEnv<'j>) -> Result<Self::T, jni::errors::Error> {
Ok(self.jobjectify(env)?.into_raw())
}
}
2024-09-22 02:22:51 +02:00
impl JniToolboxError for ConnectionError {
fn jclass(&self) -> String { // TODO pick class based on underlying type
"mp/code/exceptions/ConnectionRemoteException".to_string()
}
}
2024-09-22 02:22:51 +02:00
impl JniToolboxError for RemoteError {
fn jclass(&self) -> String { // TODO pick class based on underlying type
"mp/code/exceptions/ConnectionRemoteException".to_string()
}
}
#[jni(package = "mp.code", class = "Client", ptr)]
fn connect(config: Config) -> Result<Client, ConnectionError> {
tokio().block_on(Client::connect(config))
}
fn asd(arg: String) -> Result<Vec<String>, String> {
Ok(arg.split('/').map(|x| x.to_string()).collect())
}
/// Gets the current [crate::api::User].
2024-09-22 02:22:51 +02:00
#[jni(package = "mp.code", class = "Client", ptr)]
fn get_user(client: Client) -> crate::api::User {
client.user().clone()
}
2024-09-05 02:45:33 +02:00
/// Join a [Workspace] and return a pointer to it.
2024-09-22 02:22:51 +02:00
#[jni(package = "mp.code", class = "Client", ptr)]
fn join_workspace(client: Client, workspace: String) -> Result<Workspace, ConnectionError> {
tokio().block_on(client.join_workspace(workspace))
2024-08-06 23:30:00 +02:00
}
2024-09-22 02:22:51 +02:00
#[jni(package = "mp.code", class = "Client")]
fn create_workspace(client: Client, workspace: String) -> Result<(), RemoteError> {
tokio().block_on(client.create_workspace(workspace))
}
2024-09-05 02:45:33 +02:00
/// Delete a workspace on server, if allowed to.
2024-09-22 02:22:51 +02:00
#[jni(package = "mp.code", class = "Client")]
fn delete_workspace(client: Client, workspace: String) -> Result<(), RemoteError> {
tokio().block_on(client.delete_workspace(workspace))
}
2024-09-05 02:45:33 +02:00
/// Invite another user to an owned workspace.
2024-09-22 02:22:51 +02:00
#[jni(package = "mp.code", class = "Client")]
fn invite_to_workspace(client: Client, workspace: String, user: String) -> Result<(), RemoteError> {
tokio().block_on(client.invite_to_workspace(workspace, user))
}
2024-09-05 02:45:33 +02:00
/// List available workspaces.
2024-09-22 02:22:51 +02:00
#[jni(package = "mp.code", class = "Client", ptr)]
fn list_workspaces(client: Client, owned: bool, invited: bool) -> Result<Vec<String>, RemoteError> {
tokio().block_on(client.list_workspaces(owned, invited))
}
/// List available workspaces.
2024-09-22 02:22:51 +02:00
#[jni(package = "mp.code", class = "Client", ptr)]
fn active_workspaces(client: Client) -> Vec<String> {
client.active_workspaces()
2024-08-06 23:30:00 +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-09-22 02:22:51 +02:00
#[jni(package = "mp.code", class = "Client")]
fn leave_workspace(client: Client, workspace: String) -> bool {
client.leave_workspace(&workspace)
}
2024-09-05 02:45:33 +02:00
/// Get a [Workspace] by name and returns a pointer to it.
2024-09-22 02:22:51 +02:00
#[jni(package = "mp.code", class = "Client", ptr)]
fn get_workspace(client: Client, workspace: String) -> Option<Workspace> {
client.get_workspace(&workspace)
}
2024-09-05 02:45:33 +02:00
/// Refresh the client's session token.
2024-09-22 02:22:51 +02:00
#[jni(package = "mp.code", class = "Client")]
fn refresh(client: Client) -> Result<(), RemoteError> {
tokio().block_on(client.refresh())
}
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) };
}
2024-09-22 02:22:51 +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();
tokio().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
}