-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
jni: Add EnvoyEngine layer for library development (#158)
For an explanation of how to fill out the fields, please see the relevant section in [PULL_REQUESTS.md](https://github.com/envoyproxy/envoy/blob/master/PULL_REQUESTS.md) Description: Add EnvoyEngine layer for library development Risk Level: low Testing: ci, local Docs Changes: na Release Notes: na [Optional Fixes #Issue] [Optional Deprecated:] Signed-off-by: JP Simard <jp@jpsim.com>
- Loading branch information
Showing
7 changed files
with
97 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
6 changes: 0 additions & 6 deletions
6
mobile/library/java/io/envoyproxy/envoymobile/EnvoyEmptyClass.kt
This file was deleted.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
mobile/library/java/io/envoyproxy/envoymobile/EnvoyEngine.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package io.envoyproxy.envoymobile; | ||
|
||
import android.content.Context; | ||
import android.net.ConnectivityManager; | ||
|
||
public class EnvoyEngine { | ||
|
||
// Internal reference to helper object used to load and initialize the native library. | ||
// Volatile to ensure double-checked locking works correctly. | ||
private static volatile EnvoyEngine loader = null; | ||
|
||
// Private helper class used by the load method to ensure the native library and its | ||
// dependencies are loaded and initialized at most once. | ||
private EnvoyEngine(Context context) { | ||
System.loadLibrary("envoy_jni"); | ||
initialize((ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE)); | ||
} | ||
|
||
// Load and initialize Envoy and its dependencies, but only once. | ||
public static void load(Context context) { | ||
if (loader != null) { | ||
return; | ||
} | ||
|
||
synchronized (EnvoyEngine.class) { | ||
if (loader != null) { | ||
return; | ||
} | ||
|
||
loader = new EnvoyEngine(context); | ||
} | ||
} | ||
|
||
private static native int initialize(ConnectivityManager connectivityManager); | ||
|
||
private static native boolean isAresInitialized(); | ||
|
||
public static native int run(String config); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,47 @@ | ||
package io.envoyproxy.envoymobile | ||
|
||
import android.content.Context | ||
import android.net.ConnectivityManager | ||
import io.envoyproxy.envoymobile.EnvoyEngine | ||
|
||
class Envoy { | ||
// Wrapper class that allows for easy calling of Envoy's JNI interface in native Java. | ||
class Envoy( | ||
context: Context, | ||
config: String | ||
) { | ||
|
||
fun load() { | ||
System.loadLibrary("envoy_jni") | ||
} | ||
// Dedicated thread for running this instance of Envoy. | ||
private val runner: Thread | ||
|
||
// Create a new Envoy instance. The Envoy runner Thread is started as part of instance | ||
// initialization with the configuration provided. If the Envoy native library and its | ||
// dependencies haven't been loaded and initialized yet, this will happen lazily when | ||
// the first instance is created. | ||
init { | ||
// Lazily initialize Envoy and its dependencies, if necessary. | ||
load(context) | ||
|
||
fun run(context: Context, config: String) { | ||
val thread = Thread(Runnable { | ||
initialize(context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager) | ||
runEnvoy(config.trim()) | ||
runner = Thread(Runnable { | ||
EnvoyEngine.run(config.trim()) | ||
}) | ||
thread.start() | ||
|
||
runner.start() | ||
} | ||
|
||
private external fun initialize(connectivityManager: ConnectivityManager): Int | ||
// Returns whether the Envoy instance is currently active and running. | ||
fun isRunning(): Boolean { | ||
val state = runner.state | ||
return state != Thread.State.NEW && state != Thread.State.TERMINATED | ||
} | ||
|
||
private external fun isAresInitialized(): Boolean | ||
// Returns whether the Envoy instance is terminated. | ||
fun isTerminated(): Boolean { | ||
return runner.state == Thread.State.TERMINATED | ||
} | ||
|
||
private external fun runEnvoy(config: String): Int | ||
companion object { | ||
@JvmStatic | ||
fun load(context: Context) { | ||
EnvoyEngine.load(context) | ||
} | ||
} | ||
} |
12 changes: 0 additions & 12 deletions
12
mobile/library/kotlin/io/envoyproxy/envoymobile/EnvoyKotlinEmptyClass.java
This file was deleted.
Oops, something went wrong.