controller-legacy/models/controller_save.c

83 lines
1.8 KiB
C
Raw Normal View History

2020-02-08 14:09:34 +00:00
#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->uuid);
value.mv_data = cntrlr->uuid;
if(controller_save_single(mdb_txn, mdb_dbi, KEY_META_UUID, value))
{
return 1;
}
value.mv_size = sizeof(char) * strlen(cntrlr->name);
value.mv_data = cntrlr->name;
if(controller_save_single(mdb_txn, mdb_dbi, KEY_META_NAME, value))
{
return 1;
}
value.mv_size = sizeof(cntrlr->port);
value.mv_data = &cntrlr->port;
if(controller_save_single(mdb_txn, mdb_dbi, KEY_META_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;
}