API: Use string type instead of u8.
[vpp.git] / extras / japi / java / jvpp-core / jvpp_core.c
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <vnet/vnet.h>
17 #include <jvpp-common/jvpp_common.h>
18 #include <vpp/api/vpe_msg_enum.h>
19 #define vl_typedefs             /* define message structures */
20 #include <vpp/api/vpe_all_api_h.h>
21 #undef vl_typedefs
22
23 #include <vnet/api_errno.h>
24 #include <vlibapi/api.h>
25 #include <vlibmemory/api.h>
26 #include <jni.h>
27 #include <jvpp_core.h>
28
29 // TODO: generate jvpp_plugin_name.c files (or at least reuse plugin's main structure)
30 typedef struct {
31     /* Pointer to shared memory queue */
32     svm_queue_t * vl_input_queue;
33
34     /* VPP api client index */
35     u32 my_client_index;
36
37     /* Callback object and class references enabling asynchronous Java calls */
38     jobject callbackObject;
39     jclass callbackClass;
40
41 } core_main_t;
42
43 core_main_t core_main __attribute__((aligned (64)));
44
45 #include "io_fd_vpp_jvpp_core_JVppCoreImpl.h"
46 #include "jvpp_core_gen.h"
47
48 JNIEXPORT void JNICALL Java_io_fd_vpp_jvpp_core_JVppCoreImpl_init0
49 (JNIEnv * env, jclass clazz, jobject callback, jlong queue_address, jint my_client_index) {
50     core_main_t * plugin_main = &core_main;
51     plugin_main->my_client_index = my_client_index;
52     plugin_main->vl_input_queue = uword_to_pointer (queue_address, svm_queue_t *);
53
54     plugin_main->callbackObject = (*env)->NewGlobalRef(env, callback);
55     plugin_main->callbackClass = (jclass)(*env)->NewGlobalRef(env, (*env)->GetObjectClass(env, callback));
56
57     // verify API has not changed since jar generation (exit on mismatch)
58     #define _(N)             \
59         if (get_message_id(env, #N) == 0) return;
60         foreach_supported_api_message;
61     #undef _
62
63     #define _(N,n)                                  \
64         vl_msg_api_set_handlers(get_message_id(env, #N), #n,     \
65                 vl_api_##n##_t_handler,             \
66                 vl_noop_handler,                    \
67                 vl_noop_handler,              \
68                 vl_noop_handler,               \
69                 sizeof(vl_api_##n##_t), 1);
70         foreach_api_reply_handler;
71     #undef _
72 }
73
74 JNIEXPORT void JNICALL Java_io_fd_vpp_jvpp_core_JVppCoreImpl_close0
75 (JNIEnv *env, jclass clazz) {
76     core_main_t * plugin_main = &core_main;
77
78     // cleanup:
79     (*env)->DeleteGlobalRef(env, plugin_main->callbackClass);
80     (*env)->DeleteGlobalRef(env, plugin_main->callbackObject);
81
82     plugin_main->callbackClass = NULL;
83     plugin_main->callbackObject = NULL;
84 }
85
86 jint JNI_OnLoad(JavaVM *vm, void *reserved) {
87     JNIEnv* env;
88
89     if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) {
90         return JNI_EVERSION;
91     }
92
93     if (cache_class_references(env) != 0) {
94         clib_warning ("Failed to cache class references\n");
95         return JNI_ERR;
96     }
97
98     return JNI_VERSION_1_8;
99 }
100
101 void JNI_OnUnload(JavaVM *vm, void *reserved) {
102     JNIEnv* env;
103     if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) {
104         return;
105     }
106     delete_class_references(env);
107 }
108
109
110