This is the C++ handler which will be called by clientSML when the event fires. Then from here we need to call back to Java to pass back the message.
| 848 | // This is the C++ handler which will be called by clientSML when the event fires. |
| 849 | // Then from here we need to call back to Java to pass back the message. |
| 850 | static void SystemEventHandler(sml::smlSystemEventId id, void* pUserData, sml::Kernel* pKernel) |
| 851 | { |
| 852 | // The user data is the class we declared above, where we store the Java data to use in the callback. |
| 853 | JavaCallbackData* pJavaData = (JavaCallbackData*)pUserData ; |
| 854 | |
| 855 | // Now try to call back to Java |
| 856 | JNIEnv* jenv = pJavaData->GetEnv() ; |
| 857 | |
| 858 | // We start from the Java object whose method we wish to call. |
| 859 | jobject jobj = pJavaData->m_HandlerObject ; |
| 860 | jclass cls = jenv->GetObjectClass(jobj) ; |
| 861 | |
| 862 | if (cls == 0) |
| 863 | { |
| 864 | printf("Failed to get Java class\n") ; |
| 865 | return ; |
| 866 | } |
| 867 | |
| 868 | // Look up the Java method we want to call. |
| 869 | // The method name is passed in by the user (and needs to match exactly, including case). |
| 870 | // The method should be owned by the m_HandlerObject that the user also passed in. |
| 871 | // Any slip here and you get a NoSuchMethod exception and my Java VM shuts down. |
| 872 | jmethodID mid = jenv->GetMethodID(cls, pJavaData->m_HandlerMethod.c_str(), "(ILjava/lang/Object;Lsml/Kernel;)V") ; |
| 873 | |
| 874 | if (mid == 0) |
| 875 | { |
| 876 | printf("Failed to get Java method\n") ; |
| 877 | return ; |
| 878 | } |
| 879 | |
| 880 | // Make the method call. |
| 881 | jenv->CallVoidMethod(jobj, mid, (int)id, pJavaData->m_CallbackData, pJavaData->m_KernelObject); |
| 882 | } |
| 883 | |
| 884 | // This is the hand-written JNI method for registering a callback. |
| 885 | // I'm going to model it after the existing SWIG JNI methods so hopefully it'll be easier to patch this into SWIG eventually. |