Beispiel #1
0
/*
 * Connects to MySQL.
 */
static int connect (lua_State *L) {
    mysql_rec *m;
    const char *host, *user, *passwd, *db, *unix_socket, *charset;
    int port;

    luaL_checktype(L, 1, LUA_TTABLE);
    host = get_string_field(L, 1, "host", NULL);
    user = get_string_field(L, 1, "user", NULL);
    passwd = get_string_field(L, 1, "password", NULL);
    db = get_string_field(L, 1, "database", NULL);
    port = get_int_field(L, 1, "port", 0);
    unix_socket = get_string_field(L, 1, "unix_socket", NULL);
    charset = get_string_field(L, 1, "charset", NULL);

    m = (mysql_rec *) lua_newuserdata(L, sizeof(mysql_rec));
    memset(m, 0, sizeof(mysql_rec));
#ifdef _REENTRANT
    if (pthread_mutex_lock(&lock) != 0) {
        lua_pushliteral(L, "Error acquiring MySQL lock");
        lua_error(L);
    }
#endif
    m->mysql = mysql_init(NULL);
#ifdef _REENTRANT
    if (pthread_mutex_unlock(&lock) != 0) {
        lua_pushliteral(L, "Error releasing MySQL lock");
        lua_error(L);
    }
#endif
    if (!m->mysql) {
        lua_pushliteral(L, "MySQL error: out of memory");
        lua_error(L);
    }
    if (!mysql_real_connect(m->mysql, host, user, passwd, db, port,
                            unix_socket, 0)) {
        lua_pushfstring(L, "MySQL error %d (%s): %s",
                        mysql_errno(m->mysql), mysql_sqlstate(m->mysql),
                        mysql_error(m->mysql));
        mysql_close(m->mysql);
        lua_error(L);
    }
    if (charset) {
        if (mysql_set_character_set(m->mysql, charset)) {
            error(L, m);
        }
    }

    luaL_getmetatable(L, IS_MYSQL_METATABLE);
    lua_setmetatable(L, -2);

    return 1;
}
Beispiel #2
0
	/*! Extract targetcompid from a session entity.
	  \param from xml entity to search
	  \return target target_comp_id */
	target_comp_id get_target_comp_id(const XmlElement *from) const
		{ target_comp_id to; return get_string_field(from, "target_comp_id", to); }
Beispiel #3
0
	/*! Extract sendercompid from a session entity.
	  \param from xml entity to search
	  \return target sender_comp_id */
	sender_comp_id get_sender_comp_id(const XmlElement *from) const
		{ sender_comp_id to; return get_string_field(from, "sender_comp_id", to); }
Beispiel #4
0
	/*! Extract default_appl_ver_id from a session entity.
	  \param from xml entity to search
	  \return target default_appl_ver_id */
	default_appl_ver_id get_default_appl_ver_id(const XmlElement *from) const
		{ default_appl_ver_id to; return get_string_field(from, "default_appl_ver_id", to); }
Beispiel #5
0
 std::string strategy_class() const {
     return get_string_field("strategy_class");
 }
Beispiel #6
0
JNIEXPORT jstring JNICALL SUBMIT_MESSAGE (JNIEnv *env, jobject obj) {

    const char FUNCNAME[] = "submitMessage";
    MsgObject       the_msg;
    char            *tmpstr;
    char            *msg_id;
    int             check_flag;
    int             actionCode;
    int             retry_count;
    char            *user_id;
    char            *subject;
    char            *valid_time;
    long            valid_time_long;
    char            *timeout_time;
    long            timeout_time_long;
    int             priority;
    int             type;
    char            *product;
    char            *body_file;
    char            *bodybuf	= NULL;
    size_t          bodybufsiz;
    jclass          msg_class;
    jstring         jmsg_id;
    char            buf[200];

    /* Start code ----------------------------------------------------------------------------------- */
    dwbNETsetEnv(0, (char **) NULL);

    msg_class = (*env)->GetObjectClass(env, obj);
    sprintf(buf, "%s/MhsSubmitException", PACKAGE_CLASS);
    if ((newExcCls = (*env)->FindClass(env, buf)) == NULL) {
	sprintf(statbuf, "Could not find Exception Class \"%s/MhsSubmitException\"\n", PACKAGE_CLASS);
        printf(statbuf);
        return((*env)->NewStringUTF(env, statbuf));
    }

    /* Note: To get the various signatures, use this command: javap -s -p com.raytheon.messaging.mhs.MhsMessage */

    /* Get the showTrace flag ----------------------------------------------------------------------- */
    if (!get_boolean_field(env, obj, msg_class, "showTrace", &show_trace)) {
        strcpy(statbuf, "Could not get boolean field \"showTrace\"");
        throw_submit_exception(env, FAIL_JNI, statbuf);
        return((*env)->NewStringUTF(env, statbuf));
    }

    /* Get the message type ------------------------------------------------------------------------- */
    if (!get_message_type(env, obj, msg_class, &type)) {
        strcpy(statbuf, "Could not get message type field");
        throw_submit_exception(env, FAIL_JNI, statbuf);
        return((*env)->NewStringUTF(env, statbuf));
    }

    /* Create the message object -------------------------------------------------------------------- */
    if (!(the_msg = coDDM_createMsg(type))) {
        sprintf (statbuf, "Failed createMsg type=%d\n", type);
        throw_submit_exception(env, FAIL_CREATE, statbuf);
        return((*env)->NewStringUTF(env, statbuf));
    }

    /* Get the productID ---------------------------------------------------------------------------- */
    if (!get_string_field(env, obj, msg_class, "productId", &product)) {
        strcpy(statbuf, "Could not get product ID");
        throw_submit_exception(env, FAIL_JNI, statbuf);
        return((*env)->NewStringUTF(env, statbuf));
    }

    if (strlen(product) > 0) {
        if (show_trace) printf ("Product ID: %s\n", product);
        if (coDDM_setProdID(the_msg, product) < 0) {
            sprintf (statbuf, "Failed setProdID '%s'\n", product);
            free(product);
            throw_submit_exception(env, FAIL_PRODID, statbuf);
            return((*env)->NewStringUTF(env, statbuf));
        }
    }
    free(product);

    /* Get the verifyAddressees flag ---------------------------------------------------------------- */
    if (!get_boolean_field(env, obj, msg_class, "verifyAddressees", &check_flag)) {
        strcpy(statbuf, "Could not get verifyAddressees field");
        throw_submit_exception(env, FAIL_JNI, statbuf);
        return((*env)->NewStringUTF(env, statbuf));
    }
    if (show_trace) printf ("Verify addressees flag: %d\n", check_flag);

    /* Get the Addressees --------------------------------------------------------------------------- */
    if (!set_msg_addressees(the_msg, env, obj, msg_class)) {
        return((*env)->NewStringUTF(env, statbuf));
    }

    if (show_trace) {
        AddrObject  addr;
        for (addr = addrListGetFirstItem(the_msg->addr_list); addr; addr = addrListGetNextItem(the_msg->addr_list)) {
            printf ("Addressee from msg object: %s %s\n", addr->addressee_name, addr->ack_required ? "(A)" : "");
        }
    }

    /* If check flag is set, don't send the message -- only verify the addressees ------------------- */
    if (check_flag) {
        int     bufsiz = 0;
        char    *addrbuf;
        char    *p_addrlist_out;

        for (tmpstr = coDDM_getFirstAddrName(the_msg); tmpstr; tmpstr = coDDM_getNextAddrName(the_msg)) {
            bufsiz += strlen(tmpstr)+1;
        }

        if (!(addrbuf = malloc(bufsiz+1))) {
            sprintf (statbuf, "Could not allocate %d bytes in %s\n", bufsiz+1, FUNCNAME);
            coDDM_destroyMsg(&the_msg);
            throw_submit_exception(env, FAIL_JNI, statbuf);
            return((*env)->NewStringUTF(env, statbuf));
        }

        addrbuf[0] = '\0';
        for (tmpstr = coDDM_getFirstAddrName(the_msg); tmpstr; tmpstr = coDDM_getNextAddrName(the_msg)) {
            if (strlen(addrbuf)) {
                strcat (addrbuf, ",");
            }
            strcat(addrbuf, tmpstr);
        }

        if (show_trace) printf ("check string: %s\n", addrbuf);
        p_addrlist_out = NULL;
        if (mqc_check_addr(the_msg->prodid, addrbuf, &p_addrlist_out) < 0) {
            sprintf (statbuf, "Failed check addr list '%s'\n", addrbuf);
            free (addrbuf);
            coDDM_destroyMsg(&the_msg);
            throw_submit_exception(env, FAIL_ADDR, statbuf);
            return((*env)->NewStringUTF(env, statbuf));
        }

        if (p_addrlist_out) {
            printf("Addr list: %s\n", p_addrlist_out);
        } else {
            printf("\n");
        }
        free (addrbuf);
        coDDM_destroyMsg(&the_msg);
        return((*env)->NewStringUTF(env, strlen(p_addrlist_out) ? p_addrlist_out : "\n"));
    }

    /* Get the message action code ------------------------------------------------------------------ */
    if (!get_int_field(env, obj, msg_class, "actionCode", &actionCode)) {
        coDDM_destroyMsg(&the_msg);
        strcpy(statbuf, "Could not get actionCode");
        throw_submit_exception(env, FAIL_JNI, statbuf);
        return((*env)->NewStringUTF(env, statbuf));
    }

    if (show_trace) printf ("Action Code: %d\n", actionCode);

    if (coDDM_setMsgCode(the_msg, actionCode) < 0) {
        sprintf (statbuf, "Failed setMsgCode '%d'\n", actionCode);
        coDDM_destroyMsg(&the_msg);
        throw_submit_exception(env, FAIL_CODE, statbuf);
        return((*env)->NewStringUTF(env, statbuf));
    }

    /* Get the subject string ----------------------------------------------------------------------- */
    if (!get_string_field(env, obj, msg_class, "subject", &subject)) {
        coDDM_destroyMsg(&the_msg);
        strcpy(statbuf, "Could not get subject");
        throw_submit_exception(env, FAIL_JNI, statbuf);
        return((*env)->NewStringUTF(env, statbuf));
    }

    if (strlen(subject) > 0) {
        if (show_trace) printf ("Message subject: %s\n", subject);
        if (coDDM_setSubject(the_msg, subject) < 0) {
            sprintf (statbuf, "Failed setSubject '%s'\n", subject);
            coDDM_destroyMsg(&the_msg);
            free(subject);
            throw_submit_exception(env, FAIL_SUBJECT, statbuf);
            return((*env)->NewStringUTF(env, statbuf));
        }
    }
    free(subject);

    /* Get the message priority code ---------------------------------------------------------------- */
    if (!get_priority(env, obj, msg_class, &priority)) {
        coDDM_destroyMsg(&the_msg);
        strcpy(statbuf, "Could not get priority");
        throw_submit_exception(env, FAIL_JNI, statbuf);
        return((*env)->NewStringUTF(env, statbuf));
    }

    if (show_trace) printf ("Priority: %d\n", priority);

    if (coDDM_setPriority(the_msg, priority) < 0) {
        sprintf (statbuf, "Failed setPriority '%d'\n", priority);
        coDDM_destroyMsg(&the_msg);
        throw_submit_exception(env, FAIL_PRIORITY, statbuf);
        return((*env)->NewStringUTF(env, statbuf));
    }

    /* Get the body file name ----------------------------------------------------------------------- */
    if (!get_string_field(env, obj, msg_class, "bodyFile", &body_file)) {
        coDDM_destroyMsg(&the_msg);
        strcpy(statbuf, "Could not get bodyFile");
        throw_submit_exception(env, FAIL_JNI, statbuf);
        return((*env)->NewStringUTF(env, statbuf));
    }

    if (strlen(body_file) > 0) {
        if (show_trace) printf ("Message body file name: %s\n", body_file);
        if (buffer_file((char *) body_file, &bodybuf, &bodybufsiz) < 0) {
            free(bodybuf);
            free(body_file);
            coDDM_destroyMsg(&the_msg);
            throw_submit_exception(env, FAIL_BODY, statbuf);
            return((*env)->NewStringUTF(env, statbuf));
        }
        if (show_trace) printf ("Message body: %s", bodybuf);

        if (coDDM_setMsgBody(the_msg, bodybuf, bodybufsiz) < 0) {
            sprintf (statbuf, "Failed setMsgBody '%s'\n", body_file);
            free(bodybuf);
            free(body_file);
            coDDM_destroyMsg(&the_msg);
            throw_submit_exception(env, FAIL_BODY, statbuf);
            return((*env)->NewStringUTF(env, statbuf));
        }
        free(bodybuf);
    }

    free(body_file);

    /* Set the Enclosure list ----------------------------------------------------------------------- */
    if (!set_enclosures(the_msg, env, obj, msg_class)) {
        coDDM_destroyMsg(&the_msg);
        return((*env)->NewStringUTF(env, statbuf));
    }

    if (show_trace) {
        Enclosure   encl;
        for (encl = encListGetFirstItem(the_msg->enc_list); encl; encl = encListGetNextItem(the_msg->enc_list)) {
            printf ("Enclosure file from msg object: %s\n", encl->filename);
        }
    }

    /* Get the valid time string -------------------------------------------------------------------- */
    if (!get_string_field(env, obj, msg_class, "validTimeString", &valid_time)) {
        coDDM_destroyMsg(&the_msg);
        strcpy(statbuf, "Could not get validTimeString");
        throw_submit_exception(env, FAIL_JNI, statbuf);
        return((*env)->NewStringUTF(env, statbuf));
    }

    if (strlen(valid_time) > 0) {
        if (show_trace) printf ("Valid Time String: %s\n", valid_time);
        valid_time_long = hhmmToUtime((char *) valid_time);
        if (coDDM_setValidTime(the_msg, valid_time_long) < 0) {
            sprintf (statbuf, "Failed setValidTime: %s", ctime((const time_t *)&valid_time_long));
            coDDM_destroyMsg(&the_msg);
            free(valid_time);
            throw_submit_exception(env, FAIL_VALID_TIME, statbuf);
            return((*env)->NewStringUTF(env, statbuf));
        }
    }

    free(valid_time);

    /* Get the timeout time string ------------------------------------------------------------------ */
    if (!get_string_field(env, obj, msg_class, "timeoutTimeString", &timeout_time)) {
        coDDM_destroyMsg(&the_msg);
        strcpy(statbuf, "Could not get timeoutTimeString");
        throw_submit_exception(env, FAIL_JNI, statbuf);
        return((*env)->NewStringUTF(env, statbuf));
    }

    if (strlen(timeout_time) > 0) {
        if (show_trace) printf ("Time Out String: %s\n", timeout_time);
        timeout_time_long = hhmmToUtime((char *) timeout_time);
        if (coDDM_setTimeoutTime(the_msg, timeout_time_long) < 0) {
            sprintf (statbuf, "Failed setTimeoutTime: %s", ctime((const time_t *)&timeout_time_long));
            coDDM_destroyMsg(&the_msg);
            free(timeout_time);
            throw_submit_exception(env, FAIL_TIMEOUT_TIME, statbuf);
            return((*env)->NewStringUTF(env, statbuf));
        }
    }

    free(timeout_time);

    /* Get the retry count -------------------------------------------------------------------------- */
    if (!get_int_field(env, obj, msg_class, "retryCount", &retry_count)) {
        coDDM_destroyMsg(&the_msg);
        strcpy(statbuf, "Could not get retryCount");
        throw_submit_exception(env, FAIL_JNI, statbuf);
        return((*env)->NewStringUTF(env, statbuf));
    }

    if (retry_count > 0) {
        if (show_trace) printf ("Retry count = %d\n", retry_count);
        if (coDDM_setRetryCount(the_msg, retry_count) < 0) {
            sprintf (statbuf, "Failed setRetryCount '%d'\n", retry_count);
            coDDM_destroyMsg(&the_msg);
            throw_submit_exception(env, FAIL_RETRY_COUNT, statbuf);
            return((*env)->NewStringUTF(env, statbuf));
        }
    }

    /* Get user ID string --------------------------------------------------------------------------- */
    if (!get_string_field(env, obj, msg_class, "userId", &user_id)) {
        coDDM_destroyMsg(&the_msg);
        strcpy(statbuf, "Could not get userId");
        throw_submit_exception(env, FAIL_JNI, statbuf);
        return((*env)->NewStringUTF(env, statbuf));
    }

    if (strlen(user_id) > 0) {
        if (show_trace) printf ("User ID: %s\n", user_id);
        if (coDDM_setUserID(the_msg, (char *) user_id) < 0) {
            sprintf (statbuf, "Failed setUserID '%s'\n", user_id);
            coDDM_destroyMsg(&the_msg);
            free(user_id);
            throw_submit_exception(env, FAIL_USER_ID, statbuf);
            return((*env)->NewStringUTF(env, statbuf));
        }
    }

    free(user_id);

    /* Submit the message  -------------------------------------------------------------------------- */
    if (coDDM_submitMsg(the_msg) < 0) {
        sprintf (statbuf, "Failed submitMsg\n");
        coDDM_destroyMsg(&the_msg);
        throw_submit_exception(env, FAIL_SUBMIT, statbuf);
        return((*env)->NewStringUTF(env, statbuf));
    }

    if ((msg_id = coDDM_getMsgID(the_msg))) {
        if (show_trace) printf ("Message successfully submitted.  Message ID: %s\n", msg_id);
        jmsg_id = (*env)->NewStringUTF(env, msg_id);
        coDDM_destroyMsg(&the_msg);
        return(jmsg_id);
    } else {
        sprintf (statbuf, "Failed getMsgID\n");
        coDDM_destroyMsg(&the_msg);
        throw_submit_exception(env, FAIL_MSGID, statbuf);
        return((*env)->NewStringUTF(env, statbuf));
    }
}