a161c09c95b836e6f43d411c25a1b54bd63fe12f
[vpp.git] / vpp-api / java / jvpp-common / jvpp_common.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 #define _GNU_SOURCE /* for strcasestr(3) */
16
17 #include "jvpp_common.h"
18
19 #ifndef JVPP_DEBUG
20 #define JVPP_DEBUG 0
21 #endif
22
23 #if JVPP_DEBUG == 1
24 #define DEBUG_LOG(...) clib_warning(__VA_ARGS__)
25 #else
26 #define DEBUG_LOG(...)
27 #endif
28
29 /* shared jvpp main structure */
30 jvpp_main_t jvpp_main __attribute__((aligned (64)));
31
32 void call_on_error(const char* callName, int contextId, int retval,
33         jclass callbackClass, jobject callbackObject,
34         jclass callbackExceptionClass) {
35     DEBUG_LOG("\nCallOnError : callback=%s, retval=%d, context=%d\n", callName,
36             clib_net_to_host_u32(retval), clib_net_to_host_u32(context));
37     JNIEnv *env = jvpp_main.jenv;
38     if (!callbackClass) {
39         DEBUG_LOG("CallOnError : jm->callbackClass is null!\n");
40         return;
41     }
42     jmethodID excConstructor = (*env)->GetMethodID(env, callbackExceptionClass,
43             "<init>", "(Ljava/lang/String;II)V");
44     if (!excConstructor) {
45         DEBUG_LOG("CallOnError : excConstructor is null!\n");
46         return;
47     }
48     jmethodID callbackExcMethod = (*env)->GetMethodID(env, callbackClass,
49             "onError", "(Lio/fd/vpp/jvpp/VppCallbackException;)V");
50     if (!callbackExcMethod) {
51         DEBUG_LOG("CallOnError : callbackExcMethod is null!\n");
52         return;
53     }
54
55     jobject excObject = (*env)->NewObject(env, callbackExceptionClass,
56             excConstructor, (*env)->NewStringUTF(env, callName),
57             clib_net_to_host_u32(contextId), clib_net_to_host_u32(retval));
58     if (!excObject) {
59         DEBUG_LOG("CallOnError : excObject is null!\n");
60         return;
61     }
62
63     (*env)->CallVoidMethod(env, callbackObject, callbackExcMethod, excObject);
64     DEBUG_LOG("CallOnError : Response sent\n");
65 }