diff options
Diffstat (limited to 'extras')
5 files changed, 104 insertions, 21 deletions
diff --git a/extras/japi/java/jvpp-core/io/fd/vpp/jvpp/core/examples/CallbackCliApiExample.java b/extras/japi/java/jvpp-core/io/fd/vpp/jvpp/core/examples/CallbackCliApiExample.java new file mode 100644 index 00000000000..01715d35c5b --- /dev/null +++ b/extras/japi/java/jvpp-core/io/fd/vpp/jvpp/core/examples/CallbackCliApiExample.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2018 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.vpp.jvpp.core.examples; + +import io.fd.vpp.jvpp.JVpp; +import io.fd.vpp.jvpp.JVppRegistry; +import io.fd.vpp.jvpp.JVppRegistryImpl; +import io.fd.vpp.jvpp.VppCallbackException; +import io.fd.vpp.jvpp.core.JVppCoreImpl; +import io.fd.vpp.jvpp.core.callback.CliInbandReplyCallback; +import io.fd.vpp.jvpp.core.dto.CliInband; +import io.fd.vpp.jvpp.core.dto.CliInbandReply; + +import java.nio.charset.StandardCharsets; + +public class CallbackCliApiExample { + + public static void main(String[] args) throws Exception { + testCallbackApi(); + } + + private static void testCallbackApi() throws Exception { + System.out.println("Testing Java callback API for Cli with JVppRegistry"); + try (final JVppRegistry registry = new JVppRegistryImpl("CallbackCliApiExample"); + final JVpp jvpp = new JVppCoreImpl()) { + registry.register(jvpp, new TestCallback()); + + System.out.println("Sending CliInband request..."); + CliInband req = new CliInband(); + req.cmd = "create loopback interface"; + final int result = jvpp.send(req); + System.out.printf("CliInband send result = %d%n", result); + + Thread.sleep(1000); + System.out.println("Disconnecting..."); + } + Thread.sleep(1000); + } + + static class TestCallback implements CliInbandReplyCallback { + + @Override + public void onCliInbandReply(final CliInbandReply msg) { + System.out.printf("Received CliInbandReply: context=%d, reply=%s", msg.context, msg.reply); + } + + @Override + public void onError(VppCallbackException ex) { + System.out.printf("Received onError exception: call=%s, context=%d, retval=%d%n", ex.getMethodName(), + ex.getCtxId(), ex.getErrorCode()); + } + } +} diff --git a/extras/japi/java/jvpp-core/jvpp_core.c b/extras/japi/java/jvpp-core/jvpp_core.c index 0df8589702c..9e5ef1fd295 100644 --- a/extras/japi/java/jvpp-core/jvpp_core.c +++ b/extras/japi/java/jvpp-core/jvpp_core.c @@ -26,6 +26,7 @@ #include <jni.h> #include <jvpp_core.h> + // TODO: generate jvpp_plugin_name.c files (or at least reuse plugin's main structure) typedef struct { /* Pointer to shared memory queue */ @@ -107,4 +108,29 @@ void JNI_OnUnload(JavaVM *vm, void *reserved) { } +static void _host_to_net_string(JNIEnv * env, jstring javaString, vl_api_string_t * vl_api_string) +{ + const char *nativeString; + // prevent null, which causes jni to crash + if (NULL != javaString) { + nativeString = (*env)->GetStringUTFChars(env, javaString, 0); + } else{ + nativeString = ""; + } + + vl_api_to_api_string(jstr_length(env, javaString), nativeString, vl_api_string); + + (*env)->ReleaseStringUTFChars(env, javaString, nativeString); +} + + +static jstring _net_to_host_string(JNIEnv * env, const vl_api_string_t * _net) +{ + return (*env)->NewStringUTF(env, (char *)_net->buf); +} + +static size_t jstr_length(JNIEnv *env, jstring string) +{ + return ((int) (*env)->GetStringUTFLength(env, string)); +} diff --git a/extras/japi/java/jvpp-core/jvpp_core.h b/extras/japi/java/jvpp-core/jvpp_core.h index bbf7a71fa31..032dd338ee0 100644 --- a/extras/japi/java/jvpp-core/jvpp_core.h +++ b/extras/japi/java/jvpp-core/jvpp_core.h @@ -28,21 +28,8 @@ // * u8 buf[0]; // * } __attribute__ ((packed)) vl_api_string_t; // */ -static void _host_to_net_string(JNIEnv * env, jstring javaString, vl_api_string_t * _net) -{ - const char *nativeString = (*env)->GetStringUTFChars(env, javaString, 0); - int len = strlen(nativeString); +static void _host_to_net_string(JNIEnv * env, jstring javaString, vl_api_string_t * vl_api_string); - vl_api_string_t * vl_api_string = (vl_api_string_t *)calloc(1, (sizeof(vl_api_string_t) + len * sizeof(u8))); - if (NULL == vl_api_string) - return; - - vl_api_string->length = len; - memcpy(vl_api_string->buf, nativeString, len); - - _net = vl_api_string; - (*env)->ReleaseStringUTFChars(env, javaString, nativeString); -} // // /** @@ -53,9 +40,11 @@ static void _host_to_net_string(JNIEnv * env, jstring javaString, vl_api_string_ // * u8 buf[0]; // * } __attribute__ ((packed)) vl_api_string_t; // */ -static jstring _net_to_host_string(JNIEnv * env, const vl_api_string_t * _net) -{ - jstring jstr = (*env)->NewStringUTF(env, (char *)_net->buf); +static jstring _net_to_host_string(JNIEnv * env, const vl_api_string_t * _net); + - return jstr; -} +// +// /** +// * Returns the length of jstring as size_t +// */ +static size_t jstr_length(JNIEnv *env, jstring string); diff --git a/extras/japi/java/jvpp/gen/jvppgen/jni_impl_gen.py b/extras/japi/java/jvpp/gen/jvppgen/jni_impl_gen.py index 717a42c452a..2941778f208 100755 --- a/extras/japi/java/jvpp/gen/jvppgen/jni_impl_gen.py +++ b/extras/japi/java/jvpp/gen/jvppgen/jni_impl_gen.py @@ -111,5 +111,6 @@ def _generate_msg_size(msg): _size_components += " + %s*sizeof(%s)" % (field.array_len_field.java_name, field.type.base_type.vpp_name) # FIXME(VPP-586): for proper nested structures support, we need generate functions computing type sizes # and use it instead of sizeof - + if field.type.name == "string": + _size_components += " + jstr_length(env, %s) * sizeof(u8)" % field.name return msg_size + "".join(_size_components) diff --git a/extras/japi/java/jvpp/gen/jvppgen/jvpp_model.py b/extras/japi/java/jvpp/gen/jvppgen/jvpp_model.py index da1e01f5a63..9a3204e3fc6 100755 --- a/extras/japi/java/jvpp/gen/jvppgen/jvpp_model.py +++ b/extras/japi/java/jvpp/gen/jvppgen/jvpp_model.py @@ -447,7 +447,7 @@ class JVppModel(object): host_to_net_function='clib_host_to_net_i64', net_to_host_function='clib_net_to_host_i64'), 'f64': SimpleType('f64', 'double', 'D', 'jdouble', 'Double'), - 'string': SimpleType('string', 'String', 'l', 'jstring', 'Object', + 'string': SimpleType('string', 'String', 'Ljava/lang/String;', 'jstring', 'Object', host_to_net_function='_host_to_net_string', net_to_host_function='_net_to_host_string') }) |