static bool init_event_stuff(JNIEnv *env) { static bool initialized = false; if (initialized) return true; clsEvent = getJNIClass(RHODES_JAVA_CLASS_EVENT); if (!clsEvent) return false; fidId = getJNIClassField(env, clsEvent, "id", "Ljava/lang/String;"); if (!fidId) return false; fidTitle = getJNIClassField(env, clsEvent, "title", "Ljava/lang/String;"); if (!fidTitle) return false; fidStartDate = getJNIClassField(env, clsEvent, "startDate", "Ljava/util/Date;"); if (!fidStartDate) return false; fidEndDate = getJNIClassField(env, clsEvent, "endDate", "Ljava/util/Date;"); if (!fidEndDate) return false; fidLastModified = getJNIClassField(env, clsEvent, "lastModified", "Ljava/util/Date;"); if (!fidLastModified) return false; fidLocation = getJNIClassField(env, clsEvent, "location", "Ljava/lang/String;"); if (!fidLocation) return false; fidNotes = getJNIClassField(env, clsEvent, "notes", "Ljava/lang/String;"); if (!fidNotes) return false; fidPrivacy = getJNIClassField(env, clsEvent, "privacy", "Ljava/lang/String;"); if (!fidPrivacy) return false; fidFrequency = getJNIClassField(env, clsEvent, "frequency", "Ljava/lang/String;"); if (!fidFrequency) return false; fidInterval = getJNIClassField(env, clsEvent, "interval", "I"); if (!fidInterval) return false; initialized = true; return true; }
RHO_GLOBAL jobject JNICALL Java_com_rhomobile_rhodes_socket_SSLImpl_getRemoteSockAddr (JNIEnv *env, jobject, jint sock) { jclass clsSockAddr = getJNIClass(RHODES_JAVA_CLASS_RHOSOCKADDR); if (!clsSockAddr) return NULL; jclass clsInetAddr = getJNIClass(RHODES_JAVA_CLASS_INET4ADDRESS); if (!clsInetAddr) return NULL; jmethodID midSockAddr = getJNIClassMethod(env, clsSockAddr, "<init>", "()V"); if (!midSockAddr) return NULL; jmethodID midInetAddr = getJNIClassMethod(env, clsInetAddr, "<init>", "([BLjava/lang/String;)V"); if (!midInetAddr) return NULL; jfieldID fidInetAddr = getJNIClassField(env, clsSockAddr, "host", "Ljava/net/InetAddress;"); if (!fidInetAddr) return NULL; jfieldID fidPort = getJNIClassField(env, clsSockAddr, "port", "I"); if (!fidPort) return NULL; jbyteArray array = env->NewByteArray(4); if (!array) return NULL; jbyte *arr = env->GetByteArrayElements(array, NULL); if (!arr) return NULL; sockaddr_in sa; socklen_t salen = sizeof(sa); int e = ::getpeername(sock, (sockaddr*)&sa, &salen); if (e == -1) { RAWLOG_ERROR1("getpeername failed: %d", errno); return NULL; } uint32_t addr = ntohl(sa.sin_addr.s_addr); arr[3] = (jbyte)(addr & 0xFF); addr >>= 8; arr[2] = (jbyte)(addr & 0xFF); addr >>= 8; arr[1] = (jbyte)(addr & 0xFF); addr >>= 8; arr[0] = (jbyte)(addr & 0xFF); env->ReleaseByteArrayElements(array, arr, 0); jstring ipaddrObj = rho_cast<jstring>(::inet_ntoa(sa.sin_addr)); jobject inetaddrObj = env->NewObject(clsInetAddr, midInetAddr, array, ipaddrObj); env->DeleteLocalRef(ipaddrObj); if (!inetaddrObj) return NULL; jobject sockaddrObj = env->NewObject(clsSockAddr, midSockAddr); if (!sockaddrObj) return NULL; env->SetObjectField(sockaddrObj, fidInetAddr, inetaddrObj); env->SetIntField(sockaddrObj, fidPort, ntohs(sa.sin_port)); env->DeleteLocalRef(inetaddrObj); return sockaddrObj; }
ssize_t SSLImpl::recv(char *buf, size_t size, int *wouldblock, void *storage) { *wouldblock = 0; if (!storage) return -1; jobject obj = (jobject)storage; JNIEnv *env = jnienv(); static jfieldID fid = 0; if(!fid) { jclass clsSock = getJNIObjectClass(env, obj); if (!clsSock) return -1; fid = getJNIClassField(env, clsSock, "sockfd", "I"); env->DeleteLocalRef(clsSock); } if (!fid) return -1; jint sock = env->GetIntField(obj, fid); fd_set rfd; FD_ZERO(&rfd); FD_SET(sock, &rfd); timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; int n = ::select(sock + 1, &rfd, NULL, NULL, &tv); if (n == 0) { *wouldblock = 1; return -1; } jholder<jbyteArray> array = env->NewByteArray(size); jint result = env->CallIntMethod(obj, midRecv, array.get()); if (result > 0) { jbyte *arr = env->GetByteArrayElements(array.get(), NULL); if (!arr) return -1; std::memmove(buf, arr, result); env->ReleaseByteArrayElements(array.get(), arr, JNI_ABORT); } return result; }
jobject rhoconnect_jni_parsenotify(JNIEnv * env, const char* res) { LOG(TRACE) + "rhoconnect_jni_parsenotify: " + res; RHO_CONNECT_NOTIFY notify; memset(¬ify, 0, sizeof(notify)); rho_connectclient_parsenotify(res, ¬ify); static jclass clsNotify = getJNIClass(RHOCONNECT_JAVA_CLASS_NOTIFY); if (!clsNotify) return NULL; static jmethodID midNotify = getJNIClassMethod(env, clsNotify, "<init>", "()V"); if (!midNotify) return NULL; static jfieldID fidTotalCount = getJNIClassField(env, clsNotify, "mTotalCount", "I"); if (!fidTotalCount) return NULL; static jfieldID fidProcCount = getJNIClassField(env, clsNotify, "mProcessedCount", "I"); if (!fidProcCount) return NULL; static jfieldID fidCumulativeCount = getJNIClassField(env, clsNotify, "mCumulativeCount", "I"); if (!fidCumulativeCount) return NULL; static jfieldID fidSrcId = getJNIClassField(env, clsNotify, "mSourceId", "I"); if (!fidSrcId) return NULL; static jfieldID fidErrorCode = getJNIClassField(env, clsNotify, "mErrorCode", "I"); if (!fidErrorCode) return NULL; static jfieldID fidSrcName = getJNIClassField(env, clsNotify, "mSourceName", "Ljava/lang/String;"); if (!fidSrcName) return NULL; static jfieldID fidStatus = getJNIClassField(env, clsNotify, "mStatus", "Ljava/lang/String;"); if (!fidStatus) return NULL; static jfieldID fidSyncType = getJNIClassField(env, clsNotify, "mSyncType", "Ljava/lang/String;"); if (!fidSyncType) return NULL; static jfieldID fidErrMsg = getJNIClassField(env, clsNotify, "mErrorMessage", "Ljava/lang/String;"); if (!fidErrMsg) return NULL; static jfieldID fidParams = getJNIClassField(env, clsNotify, "mParams", "Ljava/lang/String;"); if (!fidParams) return NULL; jobject jNotify = env->NewObject(clsNotify, midNotify); if (!jNotify) return NULL; env->SetIntField(jNotify, fidTotalCount, notify.total_count); env->SetIntField(jNotify, fidProcCount, notify.processed_count); env->SetIntField(jNotify, fidCumulativeCount, notify.cumulative_count); env->SetIntField(jNotify, fidSrcId, notify.source_id); env->SetIntField(jNotify, fidErrorCode, notify.error_code); jhstring jhSrcName = rho_cast<jhstring>(env, notify.source_name); jhstring jhStatus = rho_cast<jhstring>(env, notify.status); jhstring jhSyncType = rho_cast<jhstring>(env, notify.sync_type); jhstring jhErrMsg = rho_cast<jhstring>(env, notify.error_message); jhstring jhParams = rho_cast<jhstring>(env, notify.callback_params); env->SetObjectField(jNotify, fidSrcName, jhSrcName.get()); env->SetObjectField(jNotify, fidStatus, jhStatus.get()); env->SetObjectField(jNotify, fidSyncType, jhSyncType.get()); env->SetObjectField(jNotify, fidErrMsg, jhErrMsg.get()); env->SetObjectField(jNotify, fidParams, jhParams.get()); rho_connectclient_free_syncnotify(¬ify); return jNotify; }
jobject rhoconnect_jni_parseobjectnotify(JNIEnv * env, const char* res) { LOG(TRACE) + "rhoconnect_jni_parseobjectnotify: " + res; RHO_CONNECT_OBJECT_NOTIFY notify; memset(¬ify, 0, sizeof(notify)); rho_connectclient_parse_objectnotify(res, ¬ify); static jclass clsNotify = getJNIClass(RHOCONNECT_JAVA_CLASS_OBJECTNOTIFY); if (!clsNotify) { LOG(ERROR) + "Cannot get RhoConnectObjectNotify class"; return NULL; } static jmethodID midNotify = getJNIClassMethod(env, clsNotify, "<init>", "()V"); if (!midNotify) { LOG(ERROR) + "Cannot get RhoConnectObjectNotify constructor method id"; return NULL; } static jfieldID fidDeletedObjects = getJNIClassField(env, clsNotify, "mDeletedObjects", "[Ljava/lang/String;"); if (!fidDeletedObjects) { LOG(ERROR) + "Cannot get RhoConnectObjectNotify.mDeletedObjects field id"; return NULL; } static jfieldID fidUpdatedObjects = getJNIClassField(env, clsNotify, "mUpdatedObjects", "[Ljava/lang/String;"); if (!fidUpdatedObjects) { LOG(ERROR) + "Cannot get RhoConnectObjectNotify.mUpdatedObjects field id"; return NULL; } static jfieldID fidCreatedObjects = getJNIClassField(env, clsNotify, "mCreatedObjects", "[Ljava/lang/String;"); if (!fidCreatedObjects) { LOG(ERROR) + "Cannot get RhoConnectObjectNotify.mCreatedObjects field id"; return NULL; } static jfieldID fidDeletedIds = getJNIClassField(env, clsNotify, "mDeletedSrcIds", "[I"); if (!fidDeletedIds) { LOG(ERROR) + "Cannot get RhoConnectObjectNotify.mDeletedSrcIds field id"; return NULL; } static jfieldID fidUpdatedIds = getJNIClassField(env, clsNotify, "mUpdatedSrcIds", "[I"); if (!fidUpdatedIds) { LOG(ERROR) + "Cannot get RhoConnectObjectNotify.mUpdatedSrcIds field id"; return NULL; } static jfieldID fidCreatedIds = getJNIClassField(env, clsNotify, "mCreatedSrcIds", "[I"); if (!fidCreatedIds) { LOG(ERROR) + "Cannot get RhoConnectObjectNotify.mCreatedSrcIds field id"; return NULL; } jobject jNotify = env->NewObject(clsNotify, midNotify); if (!jNotify) { LOG(ERROR) + "Cannot create RhoConnectObjectNotify instance"; return NULL; } rhoconnect_jni_fill_objectnotify(env, jNotify, notify.deleted_count, notify.deleted_objects, notify.deleted_source_ids, fidDeletedObjects, fidDeletedIds); rhoconnect_jni_fill_objectnotify(env, jNotify, notify.updated_count, notify.updated_objects, notify.updated_source_ids, fidUpdatedObjects, fidUpdatedIds); rhoconnect_jni_fill_objectnotify(env, jNotify, notify.created_count, notify.created_objects, notify.created_source_ids, fidCreatedObjects, fidCreatedIds); rho_connectclient_free_sync_objectnotify(¬ify); LOG(TRACE) + "Returning Java RhoConnectObjectNotify reference: " + (int)jNotify; return jNotify; }