clientapp
Code generation tools for creating for-real ClientApp's using Go/JNI. Currently just a usable POC, if you're doing everything right it works perfectly and it's reasonably simple. In the future, the goal is foolproof and automatic.
Credit: https://github.com/sridharv/gojava for figuring out how to hack
gomobile bind
and enable this technique.
Usage: First, adapt your application so that it can be controlled by the I2P ClientAppManager by implementing this interface:
type ClientApp interface {
GetDisplayName() string
GetName() string
GetState() ClientAppState
GetClientState() int
StartupClient()
Shutdown()
}
in your Go application. Once you have done that, import the library:
import "i2pgit.org/idk/clientapp"
and call clientapp.GenerateJava(&ExampleClientApp)
during your code generation
phase to generate the .java
wrapper. This small script will work:
//go:build generate
// +build generate
package main
import (
"io/ioutil"
. "i2pgit.org/idk/clientapp"
)
func main() {
ioutil.WriteFile(ClientAppName()+".java", []byte(GenerateJava(&ExampleClientApp{})), 0644)
}
Which you can call by adding the a go generate
to your source file:
//go:generate go run --tags=generate i2pgit.org/idk/clientapp/cmd
Build your application and generate any .jar
files for your Go library using
go build
or gojava build
first. Then, run go generate
. You should end up
with a file named ExampleClientApp.java
in the build directory. The hard part's
over.
Copy the .jar
file containing your JNI bindings and your Go library to your
classpath. Then, copy the generated .java
file to the desired location in your
codebase. Now you can use your Go ClientApp from Java with real state tracking,
you can call Go functions from Java and with a little more effort, call Java
functions from Go.
Likely application: Yggdrasil plugin. IPFS plugin. Syndie plugin. VPN plugin? Syncthing plugin? Really a ton of stuff.