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 3, 2024
1 parent 9df9c7f commit fd5c309
Show file tree
Hide file tree
Showing 22 changed files with 212 additions and 78 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/publish_jar.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

# GitHub recommends pinning actions to a commit SHA.
# To get a newer version, you will need to update the SHA.
# You can also reference a tag or branch, but the action may change without warning.

name: Publish java client to GitHub Packages

on:
pull_request:

jobs:
publish:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: checkout
uses: actions/checkout@v4

- name: java setup
uses: actions/setup-java@v4
with:
java-version: '11'
distribution: 'corretto'

- name: Setup Gradle
uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0

- name: Publish cac client
run: |
cd clients/java/cac-client
./gradlew build
./gradlew publish
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Publish exp client
run: |
cd clients/java/exp-client
./gradlew build
./gradlew publish
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
9 changes: 4 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ test_logs
.pre-commit-config.yaml
.cargo

#java client
clients/java/cac-client/.gradle
clients/java/cac-client/build
clients/java/exp-client/.gradle
clients/java/exp-client/build
#gradle files
.gradle
build
.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.
48 changes: 24 additions & 24 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 'superposition'
version '1.0.1'

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
// publications {
// myLib(MavenPublication) {
// from (components.java)
// artifactId = 'CacClient'
// }
// }
repositories {
maven {
name = "GitHubPackages"
url = uri('https://maven.pkg.github.com/juspay/superposition')
credentials {
username = "Superposition Bot"
password = System.getenv("GITHUB_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 = 'cac-client'
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 'superposition'
version '1.0.1'

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/juspay/superposition')
credentials {
username = "Superposition Bot"
password = System.getenv("GITHUB_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 = "exp-client"
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
Loading

0 comments on commit fd5c309

Please sign in to comment.