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.
| 1200 | // This is the C++ handler which will be called by clientSML when the event fires. |
| 1201 | // Then from here we need to call back to Java to pass back the message. |
| 1202 | static std::string ClientMessageHandler(sml::smlRhsEventId id, void* pUserData, sml::Agent* pAgent, |
| 1203 | char const* pFunctionName, char const* pArgument) |
| 1204 | { |
| 1205 | // The user data is the class we declared above, where we store the Java data to use in the callback. |
| 1206 | JavaCallbackData* pJavaData = (JavaCallbackData*)pUserData ; |
| 1207 | |
| 1208 | // Now try to call back to Java |
| 1209 | JNIEnv* jenv = pJavaData->GetEnv() ; |
| 1210 | |
| 1211 | // We start from the Java object whose method we wish to call. |
| 1212 | jobject jobj = pJavaData->m_HandlerObject ; |
| 1213 | jclass cls = jenv->GetObjectClass(jobj) ; |
| 1214 | |
| 1215 | if (cls == 0) |
| 1216 | { |
| 1217 | printf("Failed to get Java class\n") ; |
| 1218 | return "Error -- failed to get Java class" ; |
| 1219 | } |
| 1220 | |
| 1221 | // Look up the Java method we want to call. |
| 1222 | // The method name is passed in by the user (and needs to match exactly, including case). |
| 1223 | // The method should be owned by the m_HandlerObject that the user also passed in. |
| 1224 | // Any slip here and you get a NoSuchMethod exception and my Java VM shuts down. |
| 1225 | // Method sig here is: |
| 1226 | // Int eventID, Object userData, String agentName, String functionName, String argument returning a String. |
| 1227 | jmethodID mid = jenv->GetMethodID(cls, pJavaData->m_HandlerMethod.c_str(), "(ILjava/lang/Object;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;") ; |
| 1228 | |
| 1229 | if (mid == 0) |
| 1230 | { |
| 1231 | printf("Failed to get Java method\n") ; |
| 1232 | return "Error -- failed to get Java method" ; |
| 1233 | } |
| 1234 | |
| 1235 | // Convert our C++ strings to Java strings |
| 1236 | jstring agentName = pAgent != NULL ? jenv->NewStringUTF(pAgent->GetAgentName()) : 0 ; |
| 1237 | jstring functionName = pFunctionName != NULL ? jenv->NewStringUTF(pFunctionName) : 0 ; |
| 1238 | jstring argument = pArgument != NULL ? jenv->NewStringUTF(pArgument) : 0 ; |
| 1239 | |
| 1240 | // Make the method call. |
| 1241 | jstring result = (jstring)jenv->CallObjectMethod(jobj, mid, (int)id, pJavaData->m_CallbackData, agentName, functionName, argument) ; |
| 1242 | |
| 1243 | // Cleaning up the local references just in case |
| 1244 | jenv->DeleteLocalRef(agentName); |
| 1245 | jenv->DeleteLocalRef(functionName); |
| 1246 | jenv->DeleteLocalRef(argument); |
| 1247 | |
| 1248 | // Get the returned string |
| 1249 | std::string resultStr = "" ; |
| 1250 | |
| 1251 | if (result != 0) |
| 1252 | { |
| 1253 | // Get the C string |
| 1254 | char const* pResult = jenv->GetStringUTFChars(result, 0); |
| 1255 | |
| 1256 | // Copy it into our std::string |
| 1257 | resultStr = pResult ; |
| 1258 | |
| 1259 | // Release the Java string |
nothing calls this directly
no test coverage detected