Skip to content

Commit

Permalink
jni: update java library interfaces (#326)
Browse files Browse the repository at this point in the history
Updating java library layer to conform to the streaming interfaces changes. Also, tweaks to make the java layer more consistent with the ObjC layer in envoyproxy/envoy-mobile#327

Signed-off-by: Alan Chiu <achiu@lyft.com>

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: Updating java library interfaces
Risk Level: low
Testing: n/a
Docs Changes: n/a
Release Notes: n/a
[Optional Fixes #Issue]
[Optional Deprecated:]

Signed-off-by: JP Simard <jp@jpsim.com>
  • Loading branch information
Alan Chiu authored and jpsim committed Nov 29, 2022
1 parent 1bc24f5 commit 282fee8
Show file tree
Hide file tree
Showing 10 changed files with 217 additions and 222 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@

import android.content.Context;
import android.net.ConnectivityManager;
import io.envoyproxy.envoymobile.engine.types.EnvoyStatus;

public class AndroidEngine {
public class AndroidEngineImpl {

// Internal reference to helper object used to load and initialize the native library.
// Volatile to ensure double-checked locking works correctly.
private static volatile AndroidEngine loader = null;
private static volatile AndroidEngineImpl loader = null;

private final Engine engine;
private final EnvoyEngine envoyEngine;

// Private helper class used by the load method to ensure the native library and its
// dependencies are loaded and initialized at most once.
private AndroidEngine(Context context) {
private AndroidEngineImpl(Context context) {
System.loadLibrary("envoy_jni");
initialize((ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE));
engine = new EnvoyEngine();
envoyEngine = new EnvoyEngineImpl();
}

// Load and initialize Envoy and its dependencies, but only once.
Expand All @@ -26,17 +25,18 @@ public static void load(Context context) {
return;
}

synchronized (AndroidEngine.class) {
synchronized (AndroidEngineImpl.class) {
if (loader != null) {
return;
}

loader = new AndroidEngine(context);
loader = new AndroidEngineImpl(context);
}
}

public static EnvoyStatus run(String config, String logLevel) {
return loader.engine.runEngine(config, logLevel);
public static int run(String config, String logLevel) {
// TODO: Resolve the static loader instance
return loader.envoyEngine.runWithConfig(config, logLevel);
}

private static native int initialize(ConnectivityManager connectivityManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ load("//bazel:kotlin_lib.bzl", "envoy_mobile_android_library", "envoy_mobile_jav
envoy_mobile_android_library(
name = "envoy_engine_lib",
srcs = [
"AndroidEngine.java",
"AndroidEngineImpl.java",
],
custom_package = "io.envoyproxy.envoymobile.engine",
manifest = "AndroidEngineManifest.xml",
Expand All @@ -20,8 +20,9 @@ envoy_mobile_android_library(
envoy_mobile_java_library(
name = "envoy_base_engine_lib",
srcs = [
"Engine.java",
"EnvoyEngine.java",
"EnvoyEngineImpl.java",
"EnvoyStream.java",
"JniLibrary.java",
],
visibility = ["//visibility:public"],
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,117 +1,30 @@
package io.envoyproxy.envoymobile.engine;

import io.envoyproxy.envoymobile.engine.types.EnvoyData;
import io.envoyproxy.envoymobile.engine.types.EnvoyHeaders;
import io.envoyproxy.envoymobile.engine.types.EnvoyObserver;
import io.envoyproxy.envoymobile.engine.types.EnvoyStatus;
import io.envoyproxy.envoymobile.engine.types.EnvoyStream;

public class EnvoyEngine implements Engine {
public interface EnvoyEngine {
/**
* Open an underlying HTTP stream.
* Creates a new stream with the provided observer.
*
* @param observer, the observer that will run the stream callbacks.
* @return EnvoyStream, with a stream handle and a success status, or a failure status.
* @param observer The observer for receiving callbacks from the stream.
* @return A stream that may be used for sending data.
*/
@Override
public EnvoyStream startStream(EnvoyObserver observer) {
// TODO: Implement
throw new UnsupportedOperationException("TODO: Implement me");
}
EnvoyStream startStream(EnvoyObserver observer);

/**
* Send headers over an open HTTP stream. This method can be invoked once and needs to be called
* before send_data.
* Run the Envoy engine with the provided config and log level.
*
* @param stream, the stream to send headers over.
* @param headers, the headers to send.
* @param endStream, supplies whether this is headers only.
* @return EnvoyStatus, the resulting status of the operation.
* @param config The configuration file with which to start Envoy.
* @return A status indicating if the action was successful.
*/
@Override
public EnvoyStatus sendHeaders(EnvoyStream stream, EnvoyHeaders headers, boolean endStream) {
// TODO: Implement
throw new UnsupportedOperationException("TODO: Implement me");
}
int runWithConfig(String config);

/**
* Send data over an open HTTP stream. This method can be invoked multiple times.
* Run the Envoy engine with the provided config and log level.
*
* @param stream, the stream to send data over.
* @param data, the data to send.
* @param endStream, supplies whether this is the last data in the stream.
* @return EnvoyStatus, the resulting status of the operation.
* @param config The configuration file with which to start Envoy.
* @param logLevel The log level to use when starting Envoy.
* @return int A status indicating if the action was successful.
*/
@Override
public EnvoyStatus sendData(EnvoyStream stream, EnvoyData data, boolean endStream) {
// TODO: Implement
throw new UnsupportedOperationException("TODO: Implement me");
}

/**
* Send metadata over an HTTP stream. This method can be invoked multiple times.
*
* @param stream, the stream to send metadata over.
* @param metadata, the metadata to send.
* @param endStream, supplies whether this is the last data in the stream.
* @return EnvoyStatus, the resulting status of the operation.
*/
@Override
public EnvoyStatus sendMetadata(EnvoyStream stream, EnvoyHeaders metadata, boolean endStream) {
// TODO: Implement
throw new UnsupportedOperationException("TODO: Implement me");
}

/**
* Send trailers over an open HTTP stream. This method can only be invoked once per stream.
* Note that this method implicitly ends the stream.
*
* @param stream, the stream to send trailers over.
* @param trailers, the trailers to send.
* @return EnvoyStatus, the resulting status of the operation.
*/
@Override
public EnvoyStatus sendTrailers(EnvoyStream stream, EnvoyHeaders trailers) {
// TODO: Implement
throw new UnsupportedOperationException("TODO: Implement me");
}

/**
* Half-close an HTTP stream. The stream will be observable and may return further data
* via the observer callbacks. However, nothing further may be sent.
*
* @param stream, the stream to close.
* @return EnvoyStatus, the resulting status of the operation.
*/
@Override
public EnvoyStatus locallyCloseStream(EnvoyStream stream) {
// TODO: Implement
throw new UnsupportedOperationException("TODO: Implement me");
}

/**
* Detach all observers from a stream and send an EnvoyStatuserrupt upstream if supported by
* transport.
*
* @param stream, the stream to evict.
* @return EnvoyStatus, the resulting status of the operation.
*/
@Override
public EnvoyStatus resetStream(EnvoyStream stream) {
// TODO: Implement
throw new UnsupportedOperationException("TODO: Implement me");
}

/**
* External entry poEnvoyStatus for library.
*
* @param config, the configuration blob to run envoy with.
* @param logLevel, the logging level to run envoy with.
* @return EnvoyStatus, the resulting status of the operation.
*/
@Override
public EnvoyStatus runEngine(String config, String logLevel) {
int status = JniLibrary.runEngine(config, logLevel);
return status == 0 ? EnvoyStatus.ENVOY_SUCCESS : EnvoyStatus.ENVOY_FAILURE;
}
int runWithConfig(String config, String logLevel);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.envoyproxy.envoymobile.engine;

import io.envoyproxy.envoymobile.engine.types.EnvoyObserver;

public class EnvoyEngineImpl implements EnvoyEngine {

private final long engineHandle;

public EnvoyEngineImpl() { this.engineHandle = JniLibrary.initEngine(); }

/**
* Creates a new stream with the provided observer.
*
* @param observer The observer for receiving callbacks from the stream.
* @return A stream that may be used for sending data.
*/
@Override
public EnvoyStream startStream(EnvoyObserver observer) {
long streamHandle = JniLibrary.initStream(engineHandle);
return new EnvoyStream(streamHandle, observer);
}

/**
* Run the Envoy engine with the provided config and log level.
*
* @param config The configuration file with which to start Envoy.
* @return A status indicating if the action was successful.
*/
@Override
public int runWithConfig(String config) {
return runWithConfig(config, "info");
}

/**
* Run the Envoy engine with the provided config and log level.
*
* @param config The configuration file with which to start Envoy.
* @param logLevel The log level to use when starting Envoy.
* @return int A status indicating if the action was successful.
*/
@Override
public int runWithConfig(String config, String logLevel) {
try {
return JniLibrary.runEngine(config, logLevel);
} catch (Throwable throwable) {
// TODO: Need to have a way to log the exception somewhere
return 1;
}
}
}
Loading

0 comments on commit 282fee8

Please sign in to comment.