Пример #1
0
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;

    jholder<jbyteArray> array = jholder<jbyteArray>(env->NewByteArray(4));
    if (!array) return NULL;
    jbyte *arr = env->GetByteArrayElements(array.get(), 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.get(), arr, 0);

    jhstring ipaddrObj = rho_cast<jhstring>(::inet_ntoa(sa.sin_addr));
    jhobject inetaddrObj = jhobject(env->NewObject(clsInetAddr, midInetAddr, array.get(), ipaddrObj.get()));
    if (!inetaddrObj) return NULL;

    jhobject sockaddrObj = jhobject(env->NewObject(clsSockAddr, midSockAddr));
    if (!sockaddrObj) return NULL;

    env->SetObjectField(sockaddrObj.get(), fidInetAddr, inetaddrObj.get());
    env->SetIntField(sockaddrObj.get(), fidPort, ntohs(sa.sin_port));

    return sockaddrObj.release();
}
Пример #2
0
RHO_GLOBAL VALUE event_fetch_by_id(const char *id)
{
    JNIEnv *env = jnienv();
    jclass cls = getJNIClass(RHODES_JAVA_CLASS_EVENT_STORE);
    if (!cls) return Qnil;
    jmethodID mid = getJNIClassStaticMethod(env, cls, "fetch", "(Ljava/lang/String;)Ljava/lang/Object;");
    if (!mid) return Qnil;
    jclass clsString = getJNIClass(RHODES_JAVA_CLASS_STRING);
    if (!clsString) return Qnil;

    RHO_TRACE("event_fetch_by_id (1)");
    jhstring jId = rho_cast<jhstring>(env, id);
    RHO_TRACE("event_fetch_by_id (2)");
    jhobject jRet = jhobject(env->CallStaticObjectMethod(cls, mid, jId.get()));
    RHO_TRACE("event_fetch_by_id (3)");
    if (env->IsInstanceOf(jRet.get(), clsString))
    {
        RHO_TRACE("event_fetch_by_id (3.1)");
        std::string error = rho_cast<std::string>(env, (jstring)jRet.get());
        RHO_TRACE("event_fetch_by_id (3.2)");
        rb_raise(rb_eRuntimeError, "Can't fetch event with id %s: %s", id, error.c_str());
        return Qnil;
    }
    VALUE rEvent = event_cast<VALUE>(jRet.get());
    RHO_TRACE("event_fetch_by_id (4)");
    return rEvent;
}
Пример #3
0
RHO_GLOBAL void* openPhonebookRecord(void* pb, char* id)
{
    if (logging_enable) RAWLOG_INFO("openPhonebookRecord() START");
    JNIEnv *env = jnienv();
    jobject obj = (jobject)pb;
    jclass cls = getJNIClass(RHODES_JAVA_CLASS_PHONEBOOK);
    if (!cls) return NULL;
    jmethodID mid = getJNIClassMethod(env, cls, "getRecord", "(Ljava/lang/String;)Lcom/rhomobile/rhodes/phonebook/Contact;");
    if (!mid) return NULL;

    jhstring objId = rho_cast<jhstring>(id);
    jhobject recordObj = jhobject(env->CallObjectMethod(obj, mid, objId.get()));
    if (!recordObj) return NULL;
    jhobject retval = jhobject(env->NewGlobalRef(recordObj.get()));
    if (logging_enable) RAWLOG_INFO("openPhonebookRecord() FINISH");
    if (!retval) return NULL;
    return retval.release();
}
Пример #4
0
jhobject RhoValueConverter::createObject(rho_param *p)
{
    if (!init || !p)
        return jhobject(0);

    switch (p->type) {
    case RHO_PARAM_STRING:
        return rho_cast<jhstring>(env, p->v.string);
        break;
    case RHO_PARAM_ARRAY:
        {
            jhobject v = env->NewObject(clsVector, midVectorConstructor);
            if (!v) return NULL;

            for (int i = 0, lim = p->v.array->size; i < lim; ++i) {
                jhobject value = jhobject(createObject(p->v.array->value[i]));
                env->CallVoidMethod(v.get(), midAddElement, value.get());
            }
            return v;
        }
        break;
    case RHO_PARAM_HASH:
        {
            jhobject v = env->NewObject(clsHashMap, midHashMapConstructor);
            if (!v) return NULL;

            for (int i = 0, lim = p->v.hash->size; i < lim; ++i) {
                jhstring key = rho_cast<jhstring>(p->v.hash->name[i]);
                jhobject value = jhobject(createObject(p->v.hash->value[i]));
                env->CallObjectMethod(v.get(), midPut, key.get(), value.get());
            }
            return v;
        }
        break;
    default:
        return NULL;
    }
}
Пример #5
0
rho::String rho_sysimpl_get_phone_id()
{
    JNIEnv *env = jnienv();

    jclass cls = getJNIClass(RHODES_JAVA_CLASS_RHODES_SERVICE);
    if (!cls) return 0;
    jmethodID mid = getJNIClassStaticMethod(env, cls, "getProperty", "(Ljava/lang/String;)Ljava/lang/Object;");
    if (!mid) return 0;

    jhstring propNameObj = rho_cast<jhstring>("phone_id");
    jhobject result = jhobject(env->CallStaticObjectMethod(cls, mid, propNameObj.get()));
    if (!result) return 0;

    jstring resStrObj = (jstring)result.get();
    return rho_cast<std::string>(resStrObj);
}
Пример #6
0
RHO_GLOBAL int rho_sysimpl_get_property(char* szPropName, VALUE* resValue)
{
    JNIEnv *env = jnienv();

    jclass cls = getJNIClass(RHODES_JAVA_CLASS_RHODES_SERVICE);
    if (!cls) return 0;
    jmethodID mid = getJNIClassStaticMethod(env, cls, "getProperty", "(Ljava/lang/String;)Ljava/lang/Object;");
    if (!mid) return 0;

    jhstring propNameObj = rho_cast<jhstring>(szPropName);
    jhobject result = jhobject(env->CallStaticObjectMethod(cls, mid, propNameObj.get()));
    if (!result) return 0;

    jclass clsBoolean = getJNIClass(RHODES_JAVA_CLASS_BOOLEAN);
    jclass clsInteger = getJNIClass(RHODES_JAVA_CLASS_INTEGER);
    jclass clsFloat = getJNIClass(RHODES_JAVA_CLASS_FLOAT);
    jclass clsDouble = getJNIClass(RHODES_JAVA_CLASS_DOUBLE);
    jclass clsString = getJNIClass(RHODES_JAVA_CLASS_STRING);

    if (env->IsInstanceOf(result.get(), clsBoolean)) {
        jmethodID midValue = getJNIClassMethod(env, clsBoolean, "booleanValue", "()Z");
        *resValue = rho_ruby_create_boolean((int)env->CallBooleanMethod(result.get(), midValue));
        return 1;
    }
    else if (env->IsInstanceOf(result.get(), clsInteger)) {
        jmethodID midValue = getJNIClassMethod(env, clsInteger, "intValue", "()I");
        *resValue = rho_ruby_create_integer((int)env->CallIntMethod(result.get(), midValue));
        return 1;
    }
    else if (env->IsInstanceOf(result.get(), clsFloat)) {
        jmethodID midValue = getJNIClassMethod(env, clsFloat, "floatValue", "()F");
        *resValue = rho_ruby_create_double((double)env->CallFloatMethod(result.get(), midValue));
        return 1;
    }
    else if (env->IsInstanceOf(result.get(), clsDouble)) {
        jmethodID midValue = getJNIClassMethod(env, clsDouble, "doubleValue", "()D");
        *resValue = rho_ruby_create_double((double)env->CallDoubleMethod(result.get(), midValue));
        return 1;
    }
    else if (env->IsInstanceOf(result.get(), clsString)) {
        jstring resStrObj = (jstring)result.get();
        *resValue = rho_ruby_create_string(rho_cast<std::string>(resStrObj).c_str());
        return 1;
    }

    return 0;
}
Пример #7
0
//#if 0
RHO_GLOBAL void google_mapview_create(rho_param *p)
{
#ifdef RHO_GOOGLE_API_KEY
    JNIEnv *env = jnienv();
    jclass clsMapView = getJNIClass(RHODES_JAVA_CLASS_GOOGLEMAPVIEW);
    if (!clsMapView) return;
    jmethodID midCreate = getJNIClassStaticMethod(env, clsMapView, "create", "(Ljava/lang/String;Ljava/util/Map;)V");
    if (!midCreate) return;

    if (p->type != RHO_PARAM_HASH) {
        RAWLOG_ERROR("create: wrong input parameter (expect Hash)");
        return;
    }

    jhobject paramsObj = jhobject(RhoValueConverter(env).createObject(p));
    jhstring keyObj = rho_cast<jhstring>(RHO_GOOGLE_API_KEY);
    env->CallStaticVoidMethod(clsMapView, midCreate, keyObj.get(), paramsObj.get());
#else
    RAWLOG_ERROR("MapView disabled at build time");
#endif
}
Пример #8
0
RHO_GLOBAL int open(const char *path, int oflag, ...)
{
    std::string fpath;
    if (path)
        fpath = path;

    if (fpath.empty())
    {
        RHO_LOG("open: path is empty");
        errno = EFAULT;
        return -1;
    }

    RHO_LOG("open: %s...", path);
    fpath = make_full_path(fpath);
    RHO_LOG("open: %s: fpath: %s", path, fpath.c_str());

    bool emulate = need_emulate(fpath);
    RHO_LOG("open: %s: emulate: %d", path, (int)emulate);
    if (emulate && has_pending_exception())
    {
        RHO_LOG("open: %s: has_pending_exception, return -1", path);
        errno = EFAULT;
        return -1;
    }
    if (emulate && (oflag & (O_WRONLY | O_RDWR)))
    {
        RHO_LOG("open: %s: copy from Android package", path);
        JNIEnv *env = jnienv();
        jhstring relPathObj = rho_cast<jhstring>(env, make_rel_path(fpath).c_str());
        env->CallStaticBooleanMethod(clsFileApi, midCopy, relPathObj.get());
        if (has_pending_exception())
        {
            RHO_LOG("open: %s: has_pending_exception, return -1", path);
            errno = EFAULT;
            return -1;
        }

        emulate = false;
    }
    RHO_LOG("open: %s: emulate: %d", path, (int)emulate);

    int fd;
    if (emulate)
    {
        RHO_LOG("open: %s: emulate", path);
        JNIEnv *env = jnienv();
        jhstring relPathObj = rho_cast<jhstring>(env, make_rel_path(fpath).c_str());
        jhobject is = jhobject(env->CallStaticObjectMethod(clsFileApi, midOpen, relPathObj.get()));

        if (!is)
        {
            errno = EFAULT;
            fd = -1;
        }
        else
        {
            scoped_lock_t guard(rho_file_mtx);
            if (!rho_fd_free.empty())
            {
                fd = rho_fd_free[0];
                rho_fd_free.erase(rho_fd_free.begin());
            }
            else
                fd = rho_fd_counter++;
            rho_fd_data_t d;
            d.type = rho_type_file;
            d.is = env->NewGlobalRef(is.get());
            d.dirp = NULL;
            d.fpath = fpath;
            d.pos = 0;
            rho_fd_map[fd] = d;
        }
    }
    else
    {
        RHO_LOG("open: %s: native", path);
        mode_t mode = 0;
        if (oflag & O_CREAT)
        {
            va_list vl;
            va_start(vl, oflag);
            mode = va_arg(vl, int);
            va_end(vl);
        }
        fd = real_open(path, oflag, mode);
    }