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.
| 256 | // This is the C++ handler which will be called by clientSML when the event fires. |
| 257 | // Then from here we need to call back to Java to pass back the message. |
| 258 | static void RunEventHandler(sml::smlRunEventId id, void* pUserData, sml::Agent* pAgent, sml::smlPhase phase) |
| 259 | { |
| 260 | // The user data is the class we declared above, where we store the Java data to use in the callback. |
| 261 | JavaCallbackData* pJavaData = (JavaCallbackData*)pUserData ; |
| 262 | |
| 263 | // Now try to call back to Java |
| 264 | JNIEnv* jenv = pJavaData->GetEnv(); |
| 265 | |
| 266 | // We start from the Java object whose method we wish to call. |
| 267 | jobject jobj = pJavaData->m_HandlerObject ; |
| 268 | jclass cls = jenv->GetObjectClass(jobj) ; |
| 269 | |
| 270 | if (cls == 0) |
| 271 | { |
| 272 | printf("Failed to get Java class\n") ; |
| 273 | return ; |
| 274 | } |
| 275 | |
| 276 | // Look up the Java method we want to call. |
| 277 | // The method name is passed in by the user (and needs to match exactly, including case). |
| 278 | // The method should be owned by the m_HandlerObject that the user also passed in. |
| 279 | // Any slip here and you get a NoSuchMethod exception and my Java VM shuts down. |
| 280 | jmethodID mid = jenv->GetMethodID(cls, pJavaData->m_HandlerMethod.c_str(), "(ILjava/lang/Object;Lsml/Agent;I)V") ; |
| 281 | |
| 282 | if (mid == 0) |
| 283 | { |
| 284 | printf("Failed to get Java method\n") ; |
| 285 | return ; |
| 286 | } |
| 287 | |
| 288 | // Make the method call. |
| 289 | jenv->CallVoidMethod(jobj, mid, (int)id, pJavaData->m_CallbackData, pJavaData->m_AgentObject, (int)phase); |
| 290 | } |
| 291 | |
| 292 | // This is the hand-written JNI method for registering a callback. |
| 293 | // I'm going to model it after the existing SWIG JNI methods so hopefully it'll be easier to patch this into SWIG eventually. |