VPP-1525: additional fixes for strings in Japi 77/16477/4
authorMichal Cmarada <mcmarada@cisco.com>
Fri, 4 Jan 2019 12:29:25 +0000 (13:29 +0100)
committerMichal Cmarada <mcmarada@cisco.com>
Fri, 4 Jan 2019 12:29:25 +0000 (13:29 +0100)
Change-Id: Ic5aa09fdc360cdded16d3c4693f0d4b2067f66d6
Signed-off-by: Michal Cmarada <mcmarada@cisco.com>
extras/japi/java/jvpp-core/io/fd/vpp/jvpp/core/examples/CallbackCliApiExample.java [new file with mode: 0644]
extras/japi/java/jvpp-core/jvpp_core.c
extras/japi/java/jvpp-core/jvpp_core.h
extras/japi/java/jvpp/gen/jvppgen/jni_impl_gen.py
extras/japi/java/jvpp/gen/jvppgen/jvpp_model.py

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 (file)
index 0000000..01715d3
--- /dev/null
@@ -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());
+        }
+    }
+}
index 0df8589..9e5ef1f 100644 (file)
@@ -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));
+}
index bbf7a71..032dd33 100644 (file)
 // *   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);
index 717a42c..2941778 100755 (executable)
@@ -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)
index da1e01f..9a3204e 100755 (executable)
@@ -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')
         })