Sagar Srivastav | 5c3bfab | 2016-11-20 20:31:49 -0800 | [diff] [blame] | 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 | |
| 18 | #include <ioam/lib-pot/pot_msg_enum.h> |
| 19 | #define vl_typedefs /* define message structures */ |
| 20 | #include <ioam/lib-pot/pot_all_api_h.h> |
| 21 | #undef vl_typedefs |
| 22 | |
Sagar Srivastav | 5c3bfab | 2016-11-20 20:31:49 -0800 | [diff] [blame] | 23 | #include <vnet/api_errno.h> |
| 24 | #include <vlibapi/api.h> |
| 25 | #include <vlibmemory/api.h> |
| 26 | |
| 27 | #if VPPJNI_DEBUG == 1 |
| 28 | #define DEBUG_LOG(...) clib_warning(__VA_ARGS__) |
| 29 | #else |
| 30 | #define DEBUG_LOG(...) |
| 31 | #endif |
| 32 | |
| 33 | #include <jvpp-common/jvpp_common.h> |
| 34 | |
Damjan Marion | cb034b9 | 2016-12-28 18:38:59 +0100 | [diff] [blame] | 35 | #include "jvpp-ioampot/io_fd_vpp_jvpp_ioampot_JVppIoampotImpl.h" |
Sagar Srivastav | 5c3bfab | 2016-11-20 20:31:49 -0800 | [diff] [blame] | 36 | #include "jvpp_ioam_pot.h" |
Damjan Marion | cb034b9 | 2016-12-28 18:38:59 +0100 | [diff] [blame] | 37 | #include "jvpp-ioampot/jvpp_ioampot_gen.h" |
Sagar Srivastav | 5c3bfab | 2016-11-20 20:31:49 -0800 | [diff] [blame] | 38 | |
| 39 | /* |
| 40 | * Class: io_fd_vpp_jvpp_ioampot_JVppIoampotImpl |
| 41 | * Method: init0 |
| 42 | * Signature: (JI)V |
| 43 | */ |
| 44 | JNIEXPORT void JNICALL Java_io_fd_vpp_jvpp_ioampot_JVppIoampotImpl_init0 |
| 45 | (JNIEnv *env, jclass clazz, jobject callback, jlong queue_address, jint my_client_index) { |
| 46 | ioampot_main_t * plugin_main = &ioampot_main; |
Sagar Srivastav | 5c3bfab | 2016-11-20 20:31:49 -0800 | [diff] [blame] | 47 | clib_warning ("Java_io_fd_vpp_jvpp_ioampot_JVppIoampotImpl_init0"); |
| 48 | |
| 49 | plugin_main->my_client_index = my_client_index; |
Damjan Marion | 31d4348 | 2017-04-26 14:25:56 +0200 | [diff] [blame] | 50 | plugin_main->vl_input_queue = uword_to_pointer (queue_address, unix_shared_memory_queue_t *); |
Sagar Srivastav | 5c3bfab | 2016-11-20 20:31:49 -0800 | [diff] [blame] | 51 | |
Marek Gradzki | 2291a36 | 2017-02-20 09:14:13 +0100 | [diff] [blame] | 52 | plugin_main->callbackObject = (*env)->NewGlobalRef(env, callback); |
| 53 | plugin_main->callbackClass = (jclass)(*env)->NewGlobalRef(env, (*env)->GetObjectClass(env, callback)); |
Sagar Srivastav | 5c3bfab | 2016-11-20 20:31:49 -0800 | [diff] [blame] | 54 | |
Marek Gradzki | 2291a36 | 2017-02-20 09:14:13 +0100 | [diff] [blame] | 55 | // verify API has not changed since jar generation |
| 56 | #define _(N) \ |
| 57 | get_message_id(env, #N); |
| 58 | foreach_supported_api_message; |
| 59 | #undef _ |
Sagar Srivastav | 5c3bfab | 2016-11-20 20:31:49 -0800 | [diff] [blame] | 60 | |
Marek Gradzki | 2291a36 | 2017-02-20 09:14:13 +0100 | [diff] [blame] | 61 | #define _(N,n) \ |
| 62 | vl_msg_api_set_handlers(get_message_id(env, #N), #n, \ |
| 63 | vl_api_##n##_t_handler, \ |
| 64 | vl_noop_handler, \ |
Marek Gradzki | 0eb2b16 | 2017-04-06 09:37:00 +0200 | [diff] [blame] | 65 | vl_noop_handler, \ |
| 66 | vl_noop_handler, \ |
Marek Gradzki | 2291a36 | 2017-02-20 09:14:13 +0100 | [diff] [blame] | 67 | sizeof(vl_api_##n##_t), 1); |
| 68 | foreach_api_reply_handler; |
| 69 | #undef _ |
Sagar Srivastav | 5c3bfab | 2016-11-20 20:31:49 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | JNIEXPORT void JNICALL Java_io_fd_vpp_jvpp_ioampot_JVppIoampotImpl_close0 |
| 73 | (JNIEnv *env, jclass clazz) { |
| 74 | ioampot_main_t * plugin_main = &ioampot_main; |
| 75 | |
| 76 | // cleanup: |
| 77 | (*env)->DeleteGlobalRef(env, plugin_main->callbackClass); |
| 78 | (*env)->DeleteGlobalRef(env, plugin_main->callbackObject); |
| 79 | |
| 80 | plugin_main->callbackClass = NULL; |
| 81 | plugin_main->callbackObject = NULL; |
| 82 | } |
| 83 | |
| 84 | /* Attach thread to JVM and cache class references when initiating JVPP iOAM POT */ |
| 85 | jint JNI_OnLoad(JavaVM *vm, void *reserved) { |
| 86 | JNIEnv* env; |
| 87 | |
| 88 | if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) { |
| 89 | return JNI_EVERSION; |
| 90 | } |
| 91 | |
| 92 | if (cache_class_references(env) != 0) { |
| 93 | clib_warning ("Failed to cache class references\n"); |
| 94 | return JNI_ERR; |
| 95 | } |
| 96 | |
| 97 | return JNI_VERSION_1_8; |
| 98 | } |
| 99 | |
| 100 | /* Clean up cached references when disposing JVPP iOAM POT */ |
| 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 | } |