Skip to content

Commit

Permalink
fix: more cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
Ankit Mahato authored and Ankit Mahato committed Aug 5, 2024
1 parent 01b2f9f commit 83c1b11
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 201 deletions.
2 changes: 1 addition & 1 deletion clients/java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ dependencies {
}

application {
mainClassName = 'CAC.CacClient'
mainClassName = 'CAC.Client'
}

181 changes: 108 additions & 73 deletions clients/java/src/main/java/CAC/CacClient.java
Original file line number Diff line number Diff line change
@@ -1,92 +1,127 @@
package CAC;

import java.io.File;
import java.io.IOException;

import jnr.ffi.LibraryLoader;
import jnr.ffi.Pointer;

public class CacClient {

public static void main(String[] args) {
String dylib = "cac_client";
File currentDir = new File(System.getProperty("user.dir"));
String libraryPath = currentDir.getParentFile().getParentFile() + "/target/debug";
String tenant = "dev";
public interface RustLib {
String cac_last_error_message();

System.out.println("---------------------");
int cac_new_client(String tenant, long updateFrequency, String hostname);

// Create an instance of the wrapper class
CacClientWrapper wrapper = new CacClientWrapper(libraryPath, dylib);
void cac_free_client(long ptr);

int newClient;
try {
newClient = wrapper.wrappedCacNewClient(tenant, 10, "http://localhost:8080");
System.out.println("New client created successfully. Client ID: " + newClient);
} catch (IOException e) {
System.err.println(e.getMessage());
long cac_get_client(String tenant);

Pointer cac_get_config(long clientPtr, String filterQuery, String filterPrefix);

Pointer cac_get_default_config(long clientPtr, String filterKeys);

void cac_start_polling_update(String tenant);

Pointer cac_get_last_modified(long clientPtr);

Pointer cac_get_resolved_config(long clientPtr, String filterQuery, String filterPrefix, String merge_strategy);

void cac_free_string(Pointer s);
}

private static RustLib rustLib;

public CacClient(String libraryPath, String libraryName) {
System.setProperty("jnr.ffi.library.path", libraryPath);

// Load the Rust library
CacClient.rustLib = LibraryLoader.create(RustLib.class).load(libraryName);
}

public int cacNewClient(String tenant, long updateFrequency, String hostname) throws IOException {
int result = rustLib.cac_new_client(tenant, updateFrequency, hostname);
if (result > 0) {
String errorMessage = rustLib.cac_last_error_message();
throw new IOException("Failed to create new client: " + errorMessage);
}
return result;
}

System.out.println("---------------------");
public long getCacClient(String tenant) throws IOException {
long clientPtr = rustLib.cac_get_client(tenant);
if (clientPtr == 0) {
String errorMessage = rustLib.cac_last_error_message();
throw new IOException("Failed to get CAC client: " + errorMessage);
}
return clientPtr;
}

try {
wrapper.startPollingUpdate(tenant);
System.out.println("Started polling update for tenant: " + tenant);
} catch (IOException e) {
System.err.println(e.getMessage());
public String getConfig(long clientPtr, String filterQuery, String filterPrefix) throws IOException {
Pointer result = rustLib.cac_get_config(clientPtr, filterQuery, filterPrefix);
if (result == null) {
String errorMessage = rustLib.cac_last_error_message();
throw new IOException("Failed to get config: " + errorMessage);
}
String config = getStringAndFree(result);
return config;
}

public String getDefaultConfig(long clientPtr, String filterKeys) throws IOException {
Pointer result = rustLib.cac_get_default_config(clientPtr, filterKeys);
if (result == null) {
String errorMessage = rustLib.cac_last_error_message();
throw new IOException("Failed to get default config: " + errorMessage);
}
String config = getStringAndFree(result);
return config;
}

public void startPollingUpdate(String tenant) throws IOException {
rustLib.cac_start_polling_update(tenant);
}

public String getLastModified(long clientPtr) throws IOException {
Pointer result = rustLib.cac_get_last_modified(clientPtr);
if (result == null) {
String errorMessage = rustLib.cac_last_error_message();
throw new IOException("Failed to get last modified: " + errorMessage);
}
String lastModified = getStringAndFree(result);
return lastModified;
}

System.out.println("---------------------");
public String getResolvedConfig(long clientPtr, String filterQuery, String filterPrefix, String mergeStrategy)
throws IOException {
Pointer result = rustLib.cac_get_resolved_config(clientPtr, filterQuery, filterPrefix, mergeStrategy);
if (result == null) {
String errorMessage = rustLib.cac_last_error_message();
throw new IOException("Failed to get resolved config: " + errorMessage);
}
String resolvedConfig = getStringAndFree(result);
return resolvedConfig;
}

public String wrappedLastError() throws IOException {
String errorMessage = rustLib.cac_last_error_message();
if (errorMessage != null) {
return errorMessage;
} else {
return "No error";
}
}

long clientPtr;
public void freeString(Pointer ptr) {
rustLib.cac_free_string(ptr);
}

public String getStringAndFree(Pointer ptr) {
if (ptr == null) {
return null;
}
try {
clientPtr = wrapper.wrappedCacGetClient(tenant);
System.out.println("Result from wrappedCacGetClient: " + clientPtr);

String config;
try {
config = wrapper.getConfig(clientPtr, null, null);
System.out.println("Config: " + config);
} catch (IOException e) {
System.err.println(e.getMessage());
}

System.out.println("---------------------");

String defaultConfig;
try {
defaultConfig = wrapper.getDefaultConfig(clientPtr, null);
System.out.println("Default Config: " + defaultConfig);
} catch (IOException e) {
System.err.println(e.getMessage());
}

System.out.println("---------------------");

String lastModified;
try {
lastModified = wrapper.getLastModified(clientPtr);
System.out.println("Last Modified: " + lastModified);
} catch (IOException e) {
System.err.println(e.getMessage());
}

System.out.println("---------------------");

String resolvedConfig;
try {
resolvedConfig = wrapper.getResolvedConfig(clientPtr, "{\"clientId\": \"zepto\"}", null, "MERGE");
System.out.println("Resolved Config: " + resolvedConfig);
} catch (IOException e) {
System.err.println(e.getMessage());
}

System.out.println("---------------------");

} catch (IOException e) {
System.err.println(e.getMessage());
return ptr.getString(0);
} finally {
freeString(ptr);
}
}
}

// rm -rf .gradle
// export PATH=$JAVA_HOME/bin:$PATH
// export JAVA_HOME=/opt/homebrew/opt/openjdk
// arch -arm64 ./gradlew run
127 changes: 0 additions & 127 deletions clients/java/src/main/java/CAC/CacClientWrapper.java

This file was deleted.

Loading

0 comments on commit 83c1b11

Please sign in to comment.