Teonet library  0.4.7
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
teodb_ex.c

Using the Teonet DB function

Module: teodb.c and teodb_com.c This is example application to test connection to teo-db application set and get DB data.

Created on February 15, 2016, 2:11 AM

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ev_mgr.h"
#define TDB_VERSION "0.0.1"
#define APPNAME _ANSI_MAGENTA "teodb_ex" _ANSI_NONE
#define TEODB_PEER ke->teo_cfg.app_argv[1]
#define TEODB_EX_KEY "teo_db_ex"
#define TEST_KEY "test"
#define TEST_VALUE "{ \"name\": \"1\" }"
typedef struct get_cq_data {
teo_db_data **tdd;
void list_cb(uint32_t id, int type, void *data) {
get_cq_data *cqd = data;
#define ke cqd->ke
#define NUM_RECORDS_TO_SHOW "25"
void *ar_data = (*cqd->tdd)->key_data + (*cqd->tdd)->key_length;
uint32_t ar_data_num = *(uint32_t*)ar_data;
size_t ptr = sizeof(ar_data_num);
printf("7) Got Callback Queue callback with id: %o, type: %d => %s\n"
"Key: \"%s\"\n",
id, type, type ? "success" : "timeout",
(*cqd->tdd)->key_data);
printf("Number of records in list: %o\n", ar_data_num);
int i = 0; for(i = 0; i < ar_data_num; i++) {
size_t len = strlen((char*)ar_data + ptr) + 1;
printf("%d %s\n", i+1, (char*)ar_data + ptr);
ptr += len;
if(i >= atoi(NUM_RECORDS_TO_SHOW) - 1) {
puts("(the first " NUM_RECORDS_TO_SHOW " entries shows)");
break;
}
}
puts("");
printf("Test finished ...\n");
free(cqd);
#undef NUM_RECORDS_TO_SHOW
#undef ke
}
void get_cb(uint32_t id, int type, void *data) {
get_cq_data *cqd = data;
#define ke cqd->ke
printf("5) Got Callback Queue callback with id: %o, type: %d => %s\n"
"Key: \"%s\", Value: \"%s\"\n",
id, type, type ? "success" : "timeout",
(*cqd->tdd)->key_data, (*cqd->tdd)->key_data + (*cqd->tdd)->key_length);
// 6) Send LIST request to the TeoDB using cQueue
printf("6) Send LIST request to the TeoDB using cQueue\n");
// Add callback to queue and wait timeout after 5 sec ...
ksnCQueData *cq = ksnCQueAdd(ke->kq, list_cb, 5.000, memdup(cqd, sizeof(*cqd)));
printf("6.1) Register callback id %o\n", cq->id);
// Prepare data
size_t tdd_len;
#define KEY "teo_db_ex."
teo_db_data *tdd = prepare_request_data(KEY, sizeof(KEY), NULL, 0, cq->id, &tdd_len);
// Send CMD_D_LIST command to DB peer
ksnCoreSendCmdto(ke->kc, TEODB_PEER, CMD_D_LIST, tdd, tdd_len);
free(tdd);
free(cqd);
#undef ke
}
void event_cb(ksnetEvMgrClass *ke, ksnetEvMgrEvents event, void *data,
size_t data_len, void *user_data) {
// Check teonet event
switch(event) {
{
// Client send subscribe command to server
char *peer = ((ksnCorePacketData*)data)->from;
if(!strcmp(peer, TEODB_PEER)) {
printf("The TeoDB peer: \"%s\" was connected\n",
const char *key = TEODB_EX_KEY "." TEST_KEY;
const char *value = TEST_VALUE;
// 1) Add test key to the TeoDB
printf("1) Add test key to the TeoDB, key: \"%s\", value: \"%s\"\n",
key, value);
// Prepare data
size_t tdd_len;
size_t key_len = strlen(key) + 1;
teo_db_data *tdd = prepare_request_data(key, key_len, value, strlen(value) + 1, 0, &tdd_len);
// Send SET command to DB peer
ksnCoreSendCmdto(ke->kc, TEODB_PEER, CMD_D_SET, tdd, tdd_len);
free(tdd);
// 2) Send GET request to the TeoDB
printf("2) Send GET request to the TeoDB, key: \"%s\"\n",
key);
// Prepare data
tdd = prepare_request_data(key, key_len, NULL, 0, 0, &tdd_len);
// Send GET command to DB peer
ksnCoreSendCmdto(ke->kc, TEODB_PEER, CMD_D_GET, tdd, tdd_len);
free(tdd);
}
}
break;
{
// DATA event
static teo_db_data *tdd;
switch(rd->cmd) {
// Get data response #132
{
tdd = rd->data;
char *key = tdd->key_data;
char *value = tdd->key_data + tdd->key_length;
if(!tdd->id) {
// 3) Got test key value from the TeoDB
printf("3) Got test key value from the TeoDB, "
"key: \"%s\", value: \"%s\"\n",
key, value);
// 4) \todo Send GET request to the TeoDB using cQueue
printf("4) Send GET request to the TeoDB using cQueue, "
"key: \"%s\"\n",
key);
// Add callback to queue and wait timeout after 5 sec ...
get_cq_data *cqd = malloc(sizeof(get_cq_data));
cqd->ke = ke;
cqd->tdd = &tdd;
ksnCQueData *cq = ksnCQueAdd(ke->kq, get_cb, 5.000, cqd);
printf("4.1) Register callback id %d\n", cq->id);
// Prepare data
size_t tdd_len;
tdd = prepare_request_data(key, tdd->key_length, NULL, 0, cq->id, &tdd_len);
// Send GET command to DB peer
ksnCoreSendCmdto(ke->kc, TEODB_PEER, CMD_D_GET, tdd, tdd_len);
free(tdd);
}
else {
// Execute callback queue to make success result
ksnCQueExec(ke->kq, tdd->id);
}
}
break;
// Get list response #133
{
// Parse response
tdd = rd->data;
if(tdd->id) {
// Execute callback queue to make success result
ksnCQueExec(ke->kq, tdd->id);
}
}
break;
}
}
break;
// Other events
default: break;
}
}
int main(int argc, char** argv) {
printf("Teodb example ver " TDB_VERSION ", "
"based on teonet ver. " VERSION "\n");
// Application parameters
const char *app_argv[] = { "", "teodb_peer" };
ksnetEvMgrAppParam app_param;
app_param.app_argc = 2;
app_param.app_argv = app_argv;
app_param.app_descr = NULL;
// Initialize teonet event manager and Read configuration
// Set application type
teoSetAppType(ke, "teo-db-ex");
// Show Hello message
ksn_puts(ke, APPNAME, MESSAGE, "started ...\n");
// Start teonet
return (EXIT_SUCCESS);
}