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.
| 705 | // This is the C++ handler which will be called by clientSML when the event fires. |
| 706 | // Then from here we need to call back to Java to pass back the message. |
| 707 | static void OutputEventHandler(void* pUserData, sml::Agent* pAgent, char const* pAttributeName, sml::WMElement* pWmeAdded) |
| 708 | { |
| 709 | // The user data is the class we declared above, where we store the Java data to use in the callback. |
| 710 | JavaCallbackData* pJavaData = (JavaCallbackData*)pUserData ; |
| 711 | |
| 712 | // Now try to call back to Java |
| 713 | JNIEnv* jenv = pJavaData->GetEnv() ; |
| 714 | |
| 715 | // We start from the Java object whose method we wish to call. |
| 716 | jobject jobj = pJavaData->m_HandlerObject ; |
| 717 | jclass cls = jenv->GetObjectClass(jobj) ; |
| 718 | |
| 719 | if (cls == 0) |
| 720 | { |
| 721 | printf("Failed to get Java class\n") ; |
| 722 | return ; |
| 723 | } |
| 724 | |
| 725 | // Look up the Java method we want to call. |
| 726 | // The method name is passed in by the user (and needs to match exactly, including case). |
| 727 | // The method should be owned by the m_HandlerObject that the user also passed in. |
| 728 | // Any slip here and you get a NoSuchMethod exception and my Java VM shuts down. |
| 729 | // Method sig here is: |
| 730 | // Object userData, String agentName, String attributeName, WMElement* wmeAdded |
| 731 | jmethodID mid = jenv->GetMethodID(cls, pJavaData->m_HandlerMethod.c_str(), "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/String;Lsml/WMElement;)V") ; |
| 732 | |
| 733 | if (mid == 0) |
| 734 | { |
| 735 | printf("Failed to get Java method\n") ; |
| 736 | return ; |
| 737 | } |
| 738 | |
| 739 | // Convert our C++ strings to Java strings |
| 740 | jstring agentName = pAgent != NULL ? jenv->NewStringUTF(pAgent->GetAgentName()) : 0 ; |
| 741 | jstring attributeName = pAttributeName != NULL ? jenv->NewStringUTF(pAttributeName) : 0 ; |
| 742 | |
| 743 | // Wrap our C++ WMElement object with a SWIG sml/WMElement Java object so we can |
| 744 | // pass it back to Java. |
| 745 | // OK, time to roll up our JNI sleeves and get dirty. We need to create a new Java object. |
| 746 | // Step one is getting the constructor for that class: <init> as the method name and void (V) |
| 747 | char const* kClassName = "sml/WMElement" ; |
| 748 | jclass jsmlClass = jenv->FindClass(kClassName) ; |
| 749 | |
| 750 | if (!jsmlClass) |
| 751 | { |
| 752 | // Cleaning up the local references just in case |
| 753 | jenv->DeleteLocalRef(agentName); |
| 754 | jenv->DeleteLocalRef(attributeName); |
| 755 | return ; |
| 756 | } |
| 757 | |
| 758 | // We want to grab the SWIG constructor which in Java is: |
| 759 | // protected WMElement(long cPtr, boolean cMemoryOwn) { |
| 760 | // swigCMemOwn = cMemoryOwn; |
| 761 | // swigCPtr = cPtr; |
| 762 | // } |
| 763 | // J == long, Z == boolean so looking for constructor (long, boolean) |
| 764 | jmethodID cons = jenv->GetMethodID(jsmlClass, "<init>", "(JZ)V") ; |
nothing calls this directly
no test coverage detected