Skip to content

Commit

Permalink
Create structure of new token-getting Server in Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
Pixaurora committed Sep 5, 2024
1 parent 417fbda commit 2afae8b
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.pixaurora.kitten_heart.impl;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executor;
Expand All @@ -19,7 +20,7 @@
import net.pixaurora.kitten_heart.impl.service.MinecraftUICompat;
import net.pixaurora.kitten_heart.impl.service.ServiceLoading;
import net.pixaurora.kitten_thoughts.impl.KittenThoughts;
import net.pixaurora.kitten_thoughts.impl.scrobbler.ScrobblerSetup;
import net.pixaurora.kitten_thoughts.impl.http.server.Server;

public class KitTunes {
public static final Logger LOGGER = LoggerFactory.getLogger(Constants.MOD_ID);
Expand All @@ -46,8 +47,13 @@ public static void init() {

KittenThoughts.init();

String helloMessage = ScrobblerSetup.hello("CoolCat");
KitTunes.LOGGER.info(helloMessage);
try {
String helloMessage = Server.create("token").runServer();

KitTunes.LOGGER.info(helloMessage);
} catch (IOException e) {
KitTunes.LOGGER.info("Failed to run native code!", e);
}
}

public static void tick() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package net.pixaurora.kitten_thoughts.impl.http.server;

import java.io.IOException;

public interface Server {
public static Server create(String tokenArgName) {
return new ServerImpl(tokenArgName);
}

public String runServer() throws IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.pixaurora.kitten_thoughts.impl.http.server;

public class ServerImpl implements Server {
private final long pointer;

public ServerImpl(String tokenArgName) {
this.pointer = create(tokenArgName);
}

private static native long create(String tokenArgName);

@Override
public String runServer() {
return this.runServer0();
}

private native String runServer0();
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.pixaurora.kitten_thoughts.impl.util;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
Expand Down
8 changes: 8 additions & 0 deletions projects/kitten-thoughts/src/main/rust/bridge.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use crate::Result;
use jni::{objects::JObject, sys::jlong, JNIEnv};

mod http;

fn get_rust_pointer<'local>(env: &mut JNIEnv<'local>, object: &JObject<'local>) -> Result<jlong> {
Ok(env.get_field(object, "pointer", "J")?.try_into()?)
}
1 change: 1 addition & 0 deletions projects/kitten-thoughts/src/main/rust/bridge/http.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod server;
62 changes: 62 additions & 0 deletions projects/kitten-thoughts/src/main/rust/bridge/http/server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use crate::Result;

use jni::{
objects::{JClass, JObject, JString},
sys::jlong,
JNIEnv,
};

use crate::{bridge::get_rust_pointer, http::server::Server};

fn create<'local>(token_arg_name: JString<'local>, env: &mut JNIEnv<'local>) -> Result<jlong> {
let token_arg_name: String = env.get_string(&token_arg_name)?.into();

let server = Server::new(token_arg_name);

Ok(Box::into_raw(Box::from(server)) as jlong)
}

fn run_server<'local>(
object: &JObject<'local>,
env: &mut JNIEnv<'local>,
) -> Result<JString<'local>> {
let pointer = get_rust_pointer(env, object)?;
let server = unsafe { &*(pointer as *mut Server) };

let token = server.run_server()?;

Ok(env.new_string(token)?)
}

#[no_mangle]
pub extern "system" fn Java_net_pixaurora_kitten_1thoughts_impl_http_server_ServerImpl_create<
'local,
>(
mut env: JNIEnv<'local>,
_class: JClass<'local>,
token_arg_name: JString<'local>,
) -> jlong {
match create(token_arg_name, &mut env) {
Ok(pointer) => pointer,
Err(b) => {
b.throw(&mut env);
jlong::default()
}
}
}

#[no_mangle]
pub extern "system" fn Java_net_pixaurora_kitten_1thoughts_impl_http_server_ServerImpl_runServer0<
'local,
>(
mut env: JNIEnv<'local>,
object: JObject<'local>,
) -> JString<'local> {
match run_server(&object, &mut env) {
Ok(token) => token,
Err(error) => {
error.throw(&mut env);
JString::default().into()
}
}
}
44 changes: 44 additions & 0 deletions projects/kitten-thoughts/src/main/rust/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use std::fmt::Display;

use jni::errors::Error as JNIError;
use jni::JNIEnv;
use std::io::Error as IOError;

#[derive(Debug)]
pub enum Error {
JNI(JNIError),
IO(IOError),
}

pub type Result<T> = core::result::Result<T, Error>;

impl Error {
pub fn throw(&self, env: &mut JNIEnv) {
let a = env
.throw_new("java/io/IOException", self.to_string())
.unwrap();
}
}

impl std::error::Error for Error {}

impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::JNI(error) => error.fmt(f),
Error::IO(error) => error.fmt(f),
}
}
}

impl From<JNIError> for Error {
fn from(value: JNIError) -> Self {
Error::JNI(value)
}
}

impl From<IOError> for Error {
fn from(value: IOError) -> Self {
Error::IO(value)
}
}
6 changes: 0 additions & 6 deletions projects/kitten-thoughts/src/main/rust/hello.rs

This file was deleted.

1 change: 1 addition & 0 deletions projects/kitten-thoughts/src/main/rust/http.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod server;
18 changes: 18 additions & 0 deletions projects/kitten-thoughts/src/main/rust/http/server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::Result;

pub struct Server {
token_arg_name: String,
}

impl Server {
pub fn new(token_arg_name: String) -> Self {
Server { token_arg_name }
}

pub fn run_server(&self) -> Result<String> {
Ok(format!(
"Wait... this isn't a token! Token argument name is {}",
self.token_arg_name
))
}
}
44 changes: 5 additions & 39 deletions projects/kitten-thoughts/src/main/rust/lib.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,6 @@
use jni::{
objects::{JClass, JString},
sys::jstring,
JNIEnv,
};
mod bridge;
pub mod errors;
pub mod http;

pub mod hello;

#[no_mangle]
pub extern "system" fn Java_net_pixaurora_kitten_1thoughts_impl_scrobbler_ScrobblerSetup_hello<
'local,
>(
mut env: JNIEnv<'local>,
_class: JClass<'local>,
input: JString<'local>,
) -> jstring {
let input: String = env
.get_string(&input)
.expect("Couldn't get java string!")
.into();

let output = hello::hello(input);
let output = env
.new_string(output)
.expect("Couldn't create java string!");

output.into_raw()
}

#[cfg(test)]
mod tests {
use super::hello::hello;

#[test]
fn hello_message() {
let output = hello("Coolcat".into());

assert_eq!(output, "Hello, Coolcat!");
}
}
pub use errors::Error;
pub use errors::Result;

0 comments on commit 2afae8b

Please sign in to comment.