#pragma once #include "common/RhoStd.h" #include "logging/RhoLog.h" #include "rhodes/JNIRhodes.h" #include "MethodExecutorJni.h" #include "MethodResultJni.h" namespace rho { using rho::apiGenerator::MethodResultJni; using rho::apiGenerator::MethodExecutorJni; class CTimerBase : public MethodExecutorJni { protected: DEFINE_LOGCLASS; static const char* const FACTORY_SINGLETON_CLASS; static jclass s_clsFactorySingleton; static jmethodID s_midFactorySetInstance; static jmethodID s_midFactoryGetInstance; //ITimerFactory staff static const char* const IFACTORY_CLASS; static jclass s_clsIFactory; static jmethodID s_midGetApiSingleton; static jmethodID s_midGetApiObject; //TimerSingletonBase staff static const char* const SINGLETON_BASE_CLASS; static jclass s_clsSingletonBase; //TimerBase staff static const char* const OBJECT_BASE_CLASS; static jclass s_clsObjectBase; //Method tasks static const char* const CREATE_TASK_CLASS; static jclass s_clscreateTask; static jmethodID s_midcreateTask; static const char* const START_TASK_CLASS; static jclass s_clsstartTask; static jmethodID s_midstartTask; static const char* const STOP_TASK_CLASS; static jclass s_clsstopTask; static jmethodID s_midstopTask; static const char* const ISALIVE_TASK_CLASS; static jclass s_clsisAliveTask; static jmethodID s_midisAliveTask; static JNIEnv* jniInit(JNIEnv* env); static JNIEnv* jniInit(); static jobject getFactory(JNIEnv* env); static jobject getSingleton(JNIEnv* env); rho::String m_id; jobject getObject(JNIEnv* env); public: static void setJavaFactory(JNIEnv* env, jobject jFactory); CTimerBase(const rho::String& id) : MethodExecutorJni(), m_id(id) {} virtual ~CTimerBase() {} }; template class CTimerProxy : public CTimerBase { public: CTimerProxy(const rho::String& id) : CTimerBase(id) {} virtual ~CTimerProxy() {} static void create(const T& argsAdapter, MethodResultJni& result) { LOG(TRACE) + "create"; JNIEnv *env = jniInit(); if (!env) { LOG(FATAL) + "JNI initialization failed"; return; } jhobject jhObject = getSingleton(env); jhobject jhTask = env->NewObject(s_clscreateTask, s_midcreateTask, jhObject.get(), static_cast(result)); run(env, jhTask.get(), result, rho::apiGenerator::NOT_FORCE_THREAD); if(env->ExceptionCheck() == JNI_TRUE) { rho::String message = rho::common::clearException(env); LOG(ERROR) + message; result.setError(message); } } void start(const T& argsAdapter, MethodResultJni& result) { LOG(TRACE) + "start"; JNIEnv *env = jniInit(); if (!env) { LOG(FATAL) + "JNI initialization failed"; return; } jhobject jhObject = getObject(env); if(argsAdapter.size() <= 0) { LOG(ERROR) + "Wrong number of arguments: 'interval' must be set ^^^"; result.setArgError("Wrong number of arguments: 'interval' must be set"); return; } jholder< jint > jhinterval = rho_cast< jint >(env, argsAdapter[0]); jhobject jhTask = env->NewObject(s_clsstartTask, s_midstartTask, jhObject.get(), jhinterval.get(), static_cast(result)); run(env, jhTask.get(), result, rho::apiGenerator::NOT_FORCE_THREAD); if(env->ExceptionCheck() == JNI_TRUE) { rho::String message = rho::common::clearException(env); LOG(ERROR) + message; result.setError(message); } } void stop(const T& argsAdapter, MethodResultJni& result) { LOG(TRACE) + "stop"; JNIEnv *env = jniInit(); if (!env) { LOG(FATAL) + "JNI initialization failed"; return; } jhobject jhObject = getObject(env); jhobject jhTask = env->NewObject(s_clsstopTask, s_midstopTask, jhObject.get(), static_cast(result)); run(env, jhTask.get(), result, rho::apiGenerator::NOT_FORCE_THREAD); if(env->ExceptionCheck() == JNI_TRUE) { rho::String message = rho::common::clearException(env); LOG(ERROR) + message; result.setError(message); } } void isAlive(const T& argsAdapter, MethodResultJni& result) { LOG(TRACE) + "isAlive"; JNIEnv *env = jniInit(); if (!env) { LOG(FATAL) + "JNI initialization failed"; return; } jhobject jhObject = getObject(env); jhobject jhTask = env->NewObject(s_clsisAliveTask, s_midisAliveTask, jhObject.get(), static_cast(result)); run(env, jhTask.get(), result, rho::apiGenerator::NOT_FORCE_THREAD); if(env->ExceptionCheck() == JNI_TRUE) { rho::String message = rho::common::clearException(env); LOG(ERROR) + message; result.setError(message); } } }; }