controller-legacy/models/controller_save.c
2020-03-12 00:35:16 +01:00

89 lines
2.1 KiB
C

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <uuid/uuid.h>
#include <models/controller.h>
#include <macros.h>
int
controller_save_single(MDB_txn *mdb_txn, MDB_dbi mdb_dbi, controller_db_key key_meta, MDB_val value)
{
int err;
MDB_val key;
key.mv_size = sizeof(controller_db_key);
key.mv_data = &key_meta;
if((err = mdb_put(mdb_txn, mdb_dbi, &key, &value, 0)) != 0)
{
fprintf(stderr, "mdb_put error %s\n", mdb_strerror(err));
mdb_txn_abort(mdb_txn);
return 1;
}
return 0;
}
int
controller_save(controller *cntrlr, MDB_env *mdb_env)
{
int err;
MDB_txn *mdb_txn;
MDB_dbi mdb_dbi;
MDB_val value;
controller_debug(cntrlr);
if((err = mdb_txn_begin(mdb_env, NULL, 0, &mdb_txn)) != 0)
{
fprintf(stderr, "mdb_txn_begin error %s\n", mdb_strerror(err));
exit(1);
}
if((err = mdb_dbi_open(mdb_txn, "meta", MDB_CREATE, &mdb_dbi)) != 0)
{
fprintf(stderr, "mdb_dbi_open error %s\n", mdb_strerror(err));
exit(1);
}
value.mv_size = sizeof(cntrlr->id);
value.mv_data = cntrlr->id;
if(controller_save_single(mdb_txn, mdb_dbi, KEY_META_ID, value))
{
return 1;
}
value.mv_size = sizeof(char) * (strlen(cntrlr->name) + 1);
value.mv_data = cntrlr->name;
if(controller_save_single(mdb_txn, mdb_dbi, KEY_META_NAME, value))
{
return 1;
}
value.mv_size = sizeof(cntrlr->command_port);
value.mv_data = &cntrlr->command_port;
if(controller_save_single(mdb_txn, mdb_dbi, KEY_META_COMMAND_PORT, value))
{
return 1;
}
value.mv_size = sizeof(cntrlr->discovery_port);
value.mv_data = &cntrlr->discovery_port;
if(controller_save_single(mdb_txn, mdb_dbi, KEY_META_DISCOVERY_PORT, value))
{
return 1;
}
value.mv_size = sizeof(cntrlr->relay_count);
value.mv_data = &cntrlr->relay_count;
if(controller_save_single(mdb_txn, mdb_dbi, KEY_META_RELAY_COUNT, value))
{
return 1;
}
mdb_txn_commit(mdb_txn);
return 0;
}