|
|
| version 1.2.2.5, 2012/04/25 12:04:30 | version 1.2.2.7, 2012/05/08 13:04:02 |
|---|---|
| Line 40 mqtt_rtlm_open(cfg_root_t *cfg) | Line 40 mqtt_rtlm_open(cfg_root_t *cfg) |
| if (!cfg) | if (!cfg) |
| return NULL; | return NULL; |
| sqlite3_config(SQLITE_CONFIG_SERIALIZED); | |
| if (!sqlite3_threadsafe()) | |
| return NULL; | |
| str = cfg_getAttribute(cfg, "mqtt_pub", "name"); | str = cfg_getAttribute(cfg, "mqtt_pub", "name"); |
| if (!str) { | if (!str) { |
| mqtt_rtlm_log("Error:: Unknown database name ...\n"); | mqtt_rtlm_log("Error:: Unknown database name ...\n"); |
| Line 56 mqtt_rtlm_open(cfg_root_t *cfg) | Line 52 mqtt_rtlm_open(cfg_root_t *cfg) |
| return NULL; | return NULL; |
| } | } |
| sqlite3_mutex_enter(sqlite3_db_mutex(sql)); | |
| if (sqlite3_exec(sql, sql_schema, NULL, NULL, NULL)) { | if (sqlite3_exec(sql, sql_schema, NULL, NULL, NULL)) { |
| MQTT_RTLM_LOG(sql); | MQTT_RTLM_LOG(sql); |
| sqlite3_mutex_leave(sqlite3_db_mutex(sql)); | |
| sqlite3_close(sql); | sqlite3_close(sql); |
| return NULL; | return NULL; |
| } | } |
| sqlite3_mutex_leave(sqlite3_db_mutex(sql)); | |
| return sql; | return sql; |
| } | } |
| Line 121 mqtt_rtlm_init_session(cfg_root_t *cfg, sqlite3 *sql, | Line 114 mqtt_rtlm_init_session(cfg_root_t *cfg, sqlite3 *sql, |
| va_end(lst); | va_end(lst); |
| } | } |
| sqlite3_mutex_enter(sqlite3_db_mutex(sql)); | |
| if (sqlite3_prepare_v2(sql, szStmt, strlen(szStmt), &stmt, NULL)) { | if (sqlite3_prepare_v2(sql, szStmt, strlen(szStmt), &stmt, NULL)) { |
| MQTT_RTLM_LOG(sql); | MQTT_RTLM_LOG(sql); |
| sqlite3_mutex_leave(sqlite3_db_mutex(sql)); | |
| return -1; | return -1; |
| } | } |
| if ((ret = sqlite3_step(stmt)) == SQLITE_DONE) | if ((ret = sqlite3_step(stmt)) == SQLITE_DONE) |
| Line 135 mqtt_rtlm_init_session(cfg_root_t *cfg, sqlite3 *sql, | Line 126 mqtt_rtlm_init_session(cfg_root_t *cfg, sqlite3 *sql, |
| ret = 0; | ret = 0; |
| } | } |
| sqlite3_finalize(stmt); | sqlite3_finalize(stmt); |
| sqlite3_mutex_leave(sqlite3_db_mutex(sql)); | |
| return ret; | return ret; |
| } | } |
| Line 168 mqtt_rtlm_fini_session(cfg_root_t *cfg, sqlite3 *sql, | Line 158 mqtt_rtlm_fini_session(cfg_root_t *cfg, sqlite3 *sql, |
| snprintf(szStmt, sizeof szStmt, "DELETE FROM %s WHERE ConnID = '%s' AND Username = '%s' " | snprintf(szStmt, sizeof szStmt, "DELETE FROM %s WHERE ConnID = '%s' AND Username = '%s' " |
| "AND RemoteHost LIKE '%s';", str, connid, user, host); | "AND RemoteHost LIKE '%s';", str, connid, user, host); |
| sqlite3_mutex_enter(sqlite3_db_mutex(sql)); | |
| if (sqlite3_prepare_v2(sql, szStmt, strlen(szStmt), &stmt, NULL)) { | if (sqlite3_prepare_v2(sql, szStmt, strlen(szStmt), &stmt, NULL)) { |
| MQTT_RTLM_LOG(sql); | MQTT_RTLM_LOG(sql); |
| sqlite3_mutex_leave(sqlite3_db_mutex(sql)); | |
| return -1; | return -1; |
| } | } |
| if ((ret = sqlite3_step(stmt)) == SQLITE_DONE) | if ((ret = sqlite3_step(stmt)) == SQLITE_DONE) |
| Line 182 mqtt_rtlm_fini_session(cfg_root_t *cfg, sqlite3 *sql, | Line 170 mqtt_rtlm_fini_session(cfg_root_t *cfg, sqlite3 *sql, |
| ret = 0; | ret = 0; |
| } | } |
| sqlite3_finalize(stmt); | sqlite3_finalize(stmt); |
| sqlite3_mutex_leave(sqlite3_db_mutex(sql)); | |
| return ret; | return ret; |
| } | } |
| Line 216 mqtt_rtlm_chk_session(cfg_root_t *cfg, sqlite3 *sql, c | Line 203 mqtt_rtlm_chk_session(cfg_root_t *cfg, sqlite3 *sql, c |
| "ConnID = '%s' AND Username = '%s' AND RemoteHost LIKE '%s';", | "ConnID = '%s' AND Username = '%s' AND RemoteHost LIKE '%s';", |
| str, connid, user, host); | str, connid, user, host); |
| sqlite3_mutex_enter(sqlite3_db_mutex(sql)); | |
| if (sqlite3_prepare_v2(sql, szStmt, strlen(szStmt), &stmt, NULL)) { | if (sqlite3_prepare_v2(sql, szStmt, strlen(szStmt), &stmt, NULL)) { |
| MQTT_RTLM_LOG(sql); | MQTT_RTLM_LOG(sql); |
| sqlite3_mutex_leave(sqlite3_db_mutex(sql)); | |
| return -1; | return -1; |
| } | } |
| if (sqlite3_step(stmt) == SQLITE_ROW) | if (sqlite3_step(stmt) == SQLITE_ROW) |
| Line 227 mqtt_rtlm_chk_session(cfg_root_t *cfg, sqlite3 *sql, c | Line 212 mqtt_rtlm_chk_session(cfg_root_t *cfg, sqlite3 *sql, c |
| else | else |
| ret = 0; | ret = 0; |
| sqlite3_finalize(stmt); | sqlite3_finalize(stmt); |
| sqlite3_mutex_leave(sqlite3_db_mutex(sql)); | |
| return ret; | return ret; |
| } | } |
| Line 266 mqtt_rtlm_write_topic(cfg_root_t *cfg, sqlite3 *sql, u | Line 250 mqtt_rtlm_write_topic(cfg_root_t *cfg, sqlite3 *sql, u |
| "datetime('now', 'localtime'), '%s');", | "datetime('now', 'localtime'), '%s');", |
| str, retain, msgid, topic, txt, user, host); | str, retain, msgid, topic, txt, user, host); |
| sqlite3_mutex_enter(sqlite3_db_mutex(sql)); | |
| if (sqlite3_prepare_v2(sql, szStmt, strlen(szStmt), &stmt, NULL)) { | if (sqlite3_prepare_v2(sql, szStmt, strlen(szStmt), &stmt, NULL)) { |
| MQTT_RTLM_LOG(sql); | MQTT_RTLM_LOG(sql); |
| sqlite3_mutex_leave(sqlite3_db_mutex(sql)); | |
| return -1; | return -1; |
| } | } |
| if ((ret = sqlite3_step(stmt)) == SQLITE_DONE) | if ((ret = sqlite3_step(stmt)) == SQLITE_DONE) |
| Line 280 mqtt_rtlm_write_topic(cfg_root_t *cfg, sqlite3 *sql, u | Line 262 mqtt_rtlm_write_topic(cfg_root_t *cfg, sqlite3 *sql, u |
| ret = 0; | ret = 0; |
| } | } |
| sqlite3_finalize(stmt); | sqlite3_finalize(stmt); |
| sqlite3_mutex_leave(sqlite3_db_mutex(sql)); | |
| return ret; | return ret; |
| } | } |
| Line 328 mqtt_rtlm_delete_topic(cfg_root_t *cfg, sqlite3 *sql, | Line 309 mqtt_rtlm_delete_topic(cfg_root_t *cfg, sqlite3 *sql, |
| "PubUser LIKE '%s' AND PubHost LIKE '%s' %s;", str, | "PubUser LIKE '%s' AND PubHost LIKE '%s' %s;", str, |
| msgid, topic, user, host, rtn); | msgid, topic, user, host, rtn); |
| sqlite3_mutex_enter(sqlite3_db_mutex(sql)); | |
| if (sqlite3_prepare_v2(sql, szStmt, strlen(szStmt), &stmt, NULL)) { | if (sqlite3_prepare_v2(sql, szStmt, strlen(szStmt), &stmt, NULL)) { |
| MQTT_RTLM_LOG(sql); | MQTT_RTLM_LOG(sql); |
| sqlite3_mutex_leave(sqlite3_db_mutex(sql)); | |
| return -1; | return -1; |
| } | } |
| if ((ret = sqlite3_step(stmt)) == SQLITE_DONE) | if ((ret = sqlite3_step(stmt)) == SQLITE_DONE) |
| Line 342 mqtt_rtlm_delete_topic(cfg_root_t *cfg, sqlite3 *sql, | Line 321 mqtt_rtlm_delete_topic(cfg_root_t *cfg, sqlite3 *sql, |
| ret = 0; | ret = 0; |
| } | } |
| sqlite3_finalize(stmt); | sqlite3_finalize(stmt); |
| sqlite3_mutex_leave(sqlite3_db_mutex(sql)); | |
| return ret; | return ret; |
| } | } |
| Line 390 mqtt_rtlm_read_topic(cfg_root_t *cfg, sqlite3 *sql, u_ | Line 368 mqtt_rtlm_read_topic(cfg_root_t *cfg, sqlite3 *sql, u_ |
| "MsgID = %d AND Topic LIKE '%s' %s;", | "MsgID = %d AND Topic LIKE '%s' %s;", |
| str, msgid, topic, szStr); | str, msgid, topic, szStr); |
| sqlite3_mutex_enter(sqlite3_db_mutex(sql)); | |
| if (sqlite3_prepare_v2(sql, szStmt, strlen(szStmt), &stmt, NULL)) { | if (sqlite3_prepare_v2(sql, szStmt, strlen(szStmt), &stmt, NULL)) { |
| MQTT_RTLM_LOG(sql); | MQTT_RTLM_LOG(sql); |
| sqlite3_mutex_leave(sqlite3_db_mutex(sql)); | |
| return NULL; | return NULL; |
| } | } |
| Line 417 mqtt_rtlm_read_topic(cfg_root_t *cfg, sqlite3 *sql, u_ | Line 393 mqtt_rtlm_read_topic(cfg_root_t *cfg, sqlite3 *sql, u_ |
| } | } |
| end: | end: |
| sqlite3_finalize(stmt); | sqlite3_finalize(stmt); |
| sqlite3_mutex_leave(sqlite3_db_mutex(sql)); | |
| return s; | return s; |
| } | } |
| Line 455 mqtt_rtlm_write_subscribe(cfg_root_t *cfg, sqlite3 *sq | Line 430 mqtt_rtlm_write_subscribe(cfg_root_t *cfg, sqlite3 *sq |
| "datetime('now', 'localtime'), '%s');", str, | "datetime('now', 'localtime'), '%s');", str, |
| msgid, qos, topic, user, host); | msgid, qos, topic, user, host); |
| sqlite3_mutex_enter(sqlite3_db_mutex(sql)); | |
| if (sqlite3_prepare_v2(sql, szStmt, strlen(szStmt), &stmt, NULL)) { | if (sqlite3_prepare_v2(sql, szStmt, strlen(szStmt), &stmt, NULL)) { |
| MQTT_RTLM_LOG(sql); | MQTT_RTLM_LOG(sql); |
| sqlite3_mutex_leave(sqlite3_db_mutex(sql)); | |
| return -1; | return -1; |
| } | } |
| if ((ret = sqlite3_step(stmt)) == SQLITE_DONE) | if ((ret = sqlite3_step(stmt)) == SQLITE_DONE) |
| Line 469 mqtt_rtlm_write_subscribe(cfg_root_t *cfg, sqlite3 *sq | Line 442 mqtt_rtlm_write_subscribe(cfg_root_t *cfg, sqlite3 *sq |
| ret = 0; | ret = 0; |
| } | } |
| sqlite3_finalize(stmt); | sqlite3_finalize(stmt); |
| sqlite3_mutex_leave(sqlite3_db_mutex(sql)); | |
| return ret; | return ret; |
| } | } |
| Line 482 mqtt_rtlm_write_subscribe(cfg_root_t *cfg, sqlite3 *sq | Line 454 mqtt_rtlm_write_subscribe(cfg_root_t *cfg, sqlite3 *sq |
| * @topic = topic | * @topic = topic |
| * @user = username | * @user = username |
| * @host = hostname | * @host = hostname |
| * @qos = Subscribe QoS if -1 no matter | |
| * return: -1 error, 0 no changes or >0 deleted rows | * return: -1 error, 0 no changes or >0 deleted rows |
| */ | */ |
| int | int |
| mqtt_rtlm_delete_subscribe(cfg_root_t *cfg, sqlite3 *sql, const char *topic, | mqtt_rtlm_delete_subscribe(cfg_root_t *cfg, sqlite3 *sql, const char *topic, |
| const char *user, const char *host, char qos) | const char *user, const char *host) |
| { | { |
| int ret = 0; | int ret = 0; |
| char *str, szStr[STRSIZ] = { 0 }, szStmt[BUFSIZ] = { 0 }; | char *str, szStmt[BUFSIZ] = { 0 }; |
| sqlite3_stmt *stmt; | sqlite3_stmt *stmt; |
| if (!cfg || !sql || !topic) | if (!cfg || !sql || !topic) |
| Line 501 mqtt_rtlm_delete_subscribe(cfg_root_t *cfg, sqlite3 *s | Line 472 mqtt_rtlm_delete_subscribe(cfg_root_t *cfg, sqlite3 *s |
| mqtt_rtlm_log("Error:: not found subscribes table name"); | mqtt_rtlm_log("Error:: not found subscribes table name"); |
| return -1; | return -1; |
| } | } |
| if (qos > -1 && qos < 3) | |
| snprintf(szStr, sizeof szStr, "AND QoS = %d", qos); | |
| snprintf(szStmt, sizeof szStmt, "DELETE FROM %s WHERE Topic LIKE '%s' AND " | snprintf(szStmt, sizeof szStmt, "DELETE FROM %s WHERE Topic LIKE '%s' AND " |
| "PubUser LIKE '%s' AND PubHost LIKE '%s' %s;", str, | "PubUser LIKE '%s' AND PubHost LIKE '%s';", str, |
| topic, user, host, szStr); | topic, user, host); |
| sqlite3_mutex_enter(sqlite3_db_mutex(sql)); | |
| if (sqlite3_prepare_v2(sql, szStmt, strlen(szStmt), &stmt, NULL)) { | if (sqlite3_prepare_v2(sql, szStmt, strlen(szStmt), &stmt, NULL)) { |
| MQTT_RTLM_LOG(sql); | MQTT_RTLM_LOG(sql); |
| sqlite3_mutex_leave(sqlite3_db_mutex(sql)); | |
| return -1; | return -1; |
| } | } |
| if ((ret = sqlite3_step(stmt)) == SQLITE_DONE) | if ((ret = sqlite3_step(stmt)) == SQLITE_DONE) |
| Line 521 mqtt_rtlm_delete_subscribe(cfg_root_t *cfg, sqlite3 *s | Line 488 mqtt_rtlm_delete_subscribe(cfg_root_t *cfg, sqlite3 *s |
| ret = 0; | ret = 0; |
| } | } |
| sqlite3_finalize(stmt); | sqlite3_finalize(stmt); |
| sqlite3_mutex_leave(sqlite3_db_mutex(sql)); | |
| return ret; | return ret; |
| } | } |
| Line 553 mqtt_rtlm_read_subscribe(cfg_root_t *cfg, sqlite3 *sql | Line 519 mqtt_rtlm_read_subscribe(cfg_root_t *cfg, sqlite3 *sql |
| } | } |
| snprintf(szStmt, sizeof szStmt, "SELECT QoS, Topic FROM %s WHERE Topic LIKE '%s';", str, topic); | snprintf(szStmt, sizeof szStmt, "SELECT QoS, Topic FROM %s WHERE Topic LIKE '%s';", str, topic); |
| sqlite3_mutex_enter(sqlite3_db_mutex(sql)); | |
| if (sqlite3_prepare_v2(sql, szStmt, strlen(szStmt), &stmt, NULL)) { | if (sqlite3_prepare_v2(sql, szStmt, strlen(szStmt), &stmt, NULL)) { |
| MQTT_RTLM_LOG(sql); | MQTT_RTLM_LOG(sql); |
| sqlite3_mutex_leave(sqlite3_db_mutex(sql)); | |
| return NULL; | return NULL; |
| } | } |
| Line 580 mqtt_rtlm_read_subscribe(cfg_root_t *cfg, sqlite3 *sql | Line 544 mqtt_rtlm_read_subscribe(cfg_root_t *cfg, sqlite3 *sql |
| } | } |
| end: | end: |
| sqlite3_finalize(stmt); | sqlite3_finalize(stmt); |
| sqlite3_mutex_leave(sqlite3_db_mutex(sql)); | |
| return s; | return s; |
| } | } |