You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

154 lines
5.4 KiB

// db_sync_stub.c — Minimal db_sync replacement for chatgui (no P2P sync, local only)
//
// Назначение: заменяет полноценный db_sync.c когда синхронизация через ETCP не нужна.
// Сохраняет API-совместимость: chat_core.c использует db_sync_instance_add / db_sync_set_insert_cb /
// db_sync_insert_signed — стаб обрабатывает их локально, без сетевого обмена.
//
// utun_instance.c вызывает db_sync_init/db_sync_destroy — стаб создаёт минимальную инфраструктуру
// без SQLite и без регистрации ETCP-сервисов.
#include "db_sync.h"
#include "utun_instance.h"
#include "../lib/mem.h"
#include "../lib/debug_config.h"
#include <string.h>
#include <stdlib.h>
#define DEBUG_CATEGORY_DB_SYNC DEBUG_CATEGORY_DEBUG
/* ── Internal structs (match db_sync.h opaque types) ── */
struct DB_SYNC_INSTANCE {
struct DB_SYNC* db_sync;
uint8_t enabled;
db_sync_insert_cb on_insert;
void* on_insert_arg;
};
struct DB_SYNC {
struct UTUN_INSTANCE* inst;
struct DB_SYNC_INSTANCE* instances;
int instance_count, instance_capacity;
uint8_t enabled;
};
/* ── Init / Destroy ── */
int db_sync_init(struct UTUN_INSTANCE* inst) {
if (!inst) {
DEBUG_ERROR(DEBUG_CATEGORY_DB_SYNC, "db_sync stub: NULL instance");
return -1;
}
if (inst->db_sync) {
DEBUG_INFO(DEBUG_CATEGORY_DB_SYNC, "db_sync stub: already initialized");
return 0;
}
struct DB_SYNC* db = u_calloc(1, sizeof(struct DB_SYNC));
if (!db) {
DEBUG_ERROR(DEBUG_CATEGORY_DB_SYNC, "db_sync stub: u_calloc failed");
return -1;
}
db->inst = inst;
db->enabled = 1;
db->instance_count = 0;
db->instance_capacity = 0;
db->instances = NULL;
inst->db_sync = db;
DEBUG_INFO(DEBUG_CATEGORY_DB_SYNC, "db_sync: stub initialized (local-only, no P2P sync)");
return 0;
}
void db_sync_destroy(struct UTUN_INSTANCE* inst) {
if (!inst || !inst->db_sync) return;
struct DB_SYNC* db = inst->db_sync;
inst->db_sync = NULL;
for (int i = 0; i < db->instance_count; i++) {
struct DB_SYNC_INSTANCE* si = &db->instances[i];
if (si->on_insert_arg) u_free(si->on_insert_arg);
}
if (db->instances) u_free(db->instances);
u_free(db);
DEBUG_INFO(DEBUG_CATEGORY_DB_SYNC, "db_sync: stub destroyed");
}
/* ── Instance management ── */
struct DB_SYNC_INSTANCE* db_sync_instance_add(struct UTUN_INSTANCE* inst,
const char* name, uint64_t id) {
if (!inst || !inst->db_sync || !name || !name[0]) {
DEBUG_ERROR(DEBUG_CATEGORY_DB_SYNC, "db_sync stub: instance_add invalid args");
return NULL;
}
struct DB_SYNC* db = inst->db_sync;
if (db->instance_count >= db->instance_capacity) {
int nc = db->instance_capacity ? db->instance_capacity * 2 : 4;
struct DB_SYNC_INSTANCE* np = u_realloc(db->instances, nc * sizeof(*db->instances));
if (!np) return NULL;
db->instances = np;
db->instance_capacity = nc;
}
struct DB_SYNC_INSTANCE* si = &db->instances[db->instance_count++];
memset(si, 0, sizeof(*si));
si->db_sync = db;
si->enabled = 1;
DEBUG_INFO(DEBUG_CATEGORY_DB_SYNC, "db_sync stub: instance added name=%s id=0x%016llx idx=%d",
name, (unsigned long long)id, db->instance_count - 1);
return si;
}
void db_sync_instance_remove(struct DB_SYNC_INSTANCE* si) {
if (!si) return;
struct DB_SYNC* db = si->db_sync;
si->enabled = 0;
if (si->on_insert_arg) { u_free(si->on_insert_arg); si->on_insert_arg = NULL; }
int idx = (int)(si - db->instances);
if (idx >= 0 && idx < db->instance_count) {
memmove(&db->instances[idx], &db->instances[idx + 1],
(db->instance_count - idx - 1) * sizeof(*db->instances));
db->instance_count--;
}
DEBUG_INFO(DEBUG_CATEGORY_DB_SYNC, "db_sync stub: instance removed idx=%d", idx);
}
/* ── Insert ── */
int db_sync_insert_signed(struct DB_SYNC_INSTANCE* si, const char* json_data,
size_t len, const uint8_t* sig, size_t sig_len) {
(void)sig; (void)sig_len;
if (!si || !si->enabled || !json_data || len == 0) return -1;
if (si->on_insert)
si->on_insert(si, json_data, len, si->db_sync->inst->node_id, si->on_insert_arg);
return 0;
}
int db_sync_insert_len(struct DB_SYNC_INSTANCE* si, const char* json_data, size_t len) {
return db_sync_insert_signed(si, json_data, len, NULL, 0);
}
int db_sync_insert(struct DB_SYNC_INSTANCE* si, const char* json_data) {
if (!json_data) return -1;
return db_sync_insert_len(si, json_data, strlen(json_data));
}
/* ── Count / Select ── */
uint32_t db_sync_count(struct DB_SYNC_INSTANCE* si) {
(void)si;
return 0;
}
int db_sync_select(struct DB_SYNC_INSTANCE* si, uint32_t offset, uint32_t limit,
db_sync_select_cb cb, void* arg) {
(void)si; (void)offset; (void)limit; (void)cb; (void)arg;
return 0;
}
/* ── Callbacks ── */
void db_sync_set_insert_cb(struct DB_SYNC_INSTANCE* si, db_sync_insert_cb cb, void* arg) {
if (!si) return;
si->on_insert = cb;
if (si->on_insert_arg) u_free(si->on_insert_arg);
si->on_insert_arg = arg;
}