Skip to content

Commit

Permalink
fix: Added Readme.md to use the clients
Browse files Browse the repository at this point in the history
  • Loading branch information
Ankit Mahato authored and Ankit Mahato committed Sep 1, 2024
1 parent 9df9c7f commit bc9abd0
Show file tree
Hide file tree
Showing 21 changed files with 162 additions and 73 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ test_logs
clients/java/cac-client/.gradle
clients/java/cac-client/build
clients/java/exp-client/.gradle
clients/java/exp-client/build
clients/java/exp-client/build
.LCKDemo.java~
.LCKDemo.java~
69 changes: 69 additions & 0 deletions clients/java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,78 @@ To run the application:
./gradlew run
```

## Publishing the clients locally and using it

Publish cac-client
```
cd clients/java/cac-client
./gradlew publishToMavenLocal
```

Publish exp-client
```
cd clients/java/exp-client
./gradlew publishToMavenLocal
```

### Use the clients in another project

build.gradle
```
plugins {
id 'java'
id 'application'
}
repositories {
mavenLocal() // if using local Maven repository
mavenCentral()
// maven {
// url = uri('https://your-repo-url/maven') // if using a remote repository
// }
}
dependencies {
implementation 'com.github.jnr:jnr-ffi:2.2.16'
implementation 'com.github.jnr:jffi:1.3.13'
implementation 'cac-client:CacClient:1.0.0'
implementation 'exp-client:ExpClient:1.0.0'
}
application {
mainClassName = 'Client' // main class name
}
```

Client.java
```
import cac_client.CacClient;
import exp_client.ExperimentationClient;
import cac_client.CACClientException;
import exp_client.EXPClientException;
public class Client {
public static void main(String[] args) {
CountDownLatch latch = new CountDownLatch(1);
try {
CacClient cac_wrapper = new CacClient();
// Use cac-client's functions
ExperimentationClient exp_wrapper = new ExperimentationClient();
// Use exp-client's functions
latch.await(); // This will keep the main thread alive
} catch (InterruptedException e) {
System.err.println("Main thread interrupted: " + e.getMessage());
} finally {
System.out.println("Application stopped.");
}
}
}
```

## If having issues
Try exporting these
```
export SUPERPOSITION_LIB_PATH=".../superposition/target/debug"
export PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME=/opt/homebrew/opt/openjdk
```
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
46 changes: 23 additions & 23 deletions clients/java/cac-client/build.gradle
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
plugins {
id 'java-library'
id 'maven-publish'
id 'java'
id 'application'
}

group 'cac-client'
version '1.0.0'

repositories {
mavenLocal()
mavenCentral()
}

dependencies {
implementation 'com.github.jnr:jnr-ffi:2.2.16'
implementation 'com.github.jnr:jffi:1.3.13'
// Add other dependencies your library needs
}

// Task to create a fat JAR
tasks.register('fatJar', Jar) {
archiveClassifier.set('fat')
from sourceSets.main.output
dependsOn configurations.runtimeClasspath
from {
configurations.runtimeClasspath.findAll { it.name.endsWith('jar') }.collect { zipTree(it) }
}
manifest {
attributes 'Main-Class': 'CAC.Client'
}
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}

// Make the fatJar task run as part of the build
build.dependsOn fatJar

group = 'com.yourdomain'
version = '1.0-SNAPSHOT'

publishing {
publications {
mavenJava(MavenPublication) {
from components.java
myLib(MavenPublication) {
from (components.java)
artifactId = 'CacClient'
}
}
// repositories {
// maven {
// name = "GitHubPackages"
// url = uri("https://maven.pkg.github.com/mahatoankitkumar/superposition")
// credentials {
// username = project.findProperty("gpr.user") ?: System.getenv("USERNAME")
// password = project.findProperty("gpr.key") ?: System.getenv("TOKEN")
// }
// }
// }
}

application {
mainClassName = 'example.Demo'
}
10 changes: 0 additions & 10 deletions clients/java/cac-client/gradle/libs.versions.toml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ plugins {
id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0"
}

rootProject.name = "superposition"
rootProject.name = 'CacClient'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cac_client;

public class CACClientException extends Exception {
public CACClientException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package CAC;
package cac_client;

import java.io.IOException;

Expand Down Expand Up @@ -31,18 +31,19 @@ public interface RustLib {

public static RustLib rustLib;

public CacClient(String libraryPath, String libraryName) {
public CacClient() {
String libraryName = "cac_client";
String libraryPath = System.getenv("SUPERPOSITION_LIB_PATH");
System.out.println("libraryPath" + libraryPath);
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 {
public int cacNewClient(String tenant, long updateFrequency, String hostName) throws CACClientException {
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 CAC client: " + errorMessage);
throw new CACClientException("Failed to create new CAC client: " + errorMessage);
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
package CAC;

import java.io.File;
package example;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;

import cac_client.CacClient;
import jnr.ffi.Pointer;

public class Client {
public class Demo {

private static void callCacClient() {
String dylib = "cac_client";
File currentDir = new File(System.getProperty("user.dir"));
String libraryPath = currentDir.getParentFile().getParentFile().getParentFile() + "/target/debug";
String tenant = "dev";

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

System.out.println("CAC Client");

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

CacClient wrapper = new CacClient(libraryPath, dylib);
CacClient wrapper = new CacClient();

int newClient;
try {
newClient = wrapper.cacNewClient(tenant, 1, "http://localhost:8080");
System.out.println("New client created successfully. Client ID: " + newClient);
} catch (IOException e) {
} catch (cac_client.CACClientException e) {
System.err.println(e.getMessage());
}

Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
30 changes: 25 additions & 5 deletions clients/java/exp-client/build.gradle
Original file line number Diff line number Diff line change
@@ -1,22 +1,42 @@
plugins {
id 'java-library'
id 'maven-publish'
id 'java'
id 'application'
}

group 'exp-client'
version '1.0.0'

repositories {
mavenCentral()
}

dependencies {
implementation 'com.github.jnr:jnr-ffi:2.2.16'
implementation 'com.github.jnr:jffi:1.3.13'
// Add other dependencies your library needs
}

application {
mainClassName = 'CAC.Client'
publishing {
publications {
myLib(MavenPublication) {
from (components.java)
artifactId = 'ExpClient'
}
}
// repositories {
// maven {
// name = "GitHubPackages"
// url = uri("https://maven.pkg.github.com/mahatoankitkumar/superposition")
// credentials {
// username = project.findProperty("gpr.user") ?: System.getenv("USERNAME")
// password = project.findProperty("gpr.key") ?: System.getenv("TOKEN")
// }
// }
// }
}

run {
standardInput = System.in
application {
mainClassName = 'example.Demo'
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ plugins {
id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0"
}

rootProject.name = "superposition"
rootProject.name = "ExpClient"
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
package CAC;
package example;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;

import exp_client.EXPClientException;
import exp_client.ExperimentationClient;
import jnr.ffi.Pointer;

public class Client {
public class Demo {

private static void callExperimentationClient() {
String dylib = "experimentation_client";
File currentDir = new File(System.getProperty("user.dir"));
String libraryPath = currentDir.getParentFile().getParentFile().getParentFile() + "/target/debug";
String tenant = "dev";

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


System.out.println("Experimentation Client");

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

ExperimentationClient wrapper = new ExperimentationClient(libraryPath, dylib);
ExperimentationClient wrapper = new ExperimentationClient();

int newClient;
try {
newClient = wrapper.exptNewClient(tenant, 1, "http://localhost:8080");
System.out.println("New Experimentation client created successfully. Client ID: " + newClient);
} catch (IOException e) {
} catch (EXPClientException e) {
System.err.println(e.getMessage());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package exp_client;

public class EXPClientException extends Exception {
public EXPClientException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package CAC;
package exp_client;

import java.io.IOException;

Expand Down Expand Up @@ -31,16 +31,19 @@ public interface RustLib {

public static RustLib rustLib;

public ExperimentationClient(String libraryPath, String libraryName) {
public ExperimentationClient() {
String libraryName = "experimentation_client";
String libraryPath = System.getenv("SUPERPOSITION_LIB_PATH");
System.out.println("libraryPath" + libraryPath);
System.setProperty("jnr.ffi.library.path", libraryPath);
ExperimentationClient.rustLib = LibraryLoader.create(RustLib.class).load(libraryName);
}

public int exptNewClient(String tenant, long updateFrequency, String hostName) throws IOException {
public int exptNewClient(String tenant, long updateFrequency, String hostName) throws EXPClientException {
int result = rustLib.expt_new_client(tenant, updateFrequency, hostName);
if (result > 0) {
String errorMessage = rustLib.expt_last_error_message();
throw new IOException("Failed to create new Experimentation client: " + errorMessage);
throw new EXPClientException("Failed to create new Experimentation client: " + errorMessage);
}
return result;
}
Expand Down

0 comments on commit bc9abd0

Please sign in to comment.