Hi All, I am a new bee to confd.I am writing my application of reading a string from a file and storing it in the cdb database.Here is my yang module:
module myown {
namespace “http://tail-f.com/ns/example/if”;
prefix myown;
import tailf-common {
prefix tailf;
}
revision “2016-02-02” {
description “latest”;
}
container Surabhi{
config false;
tailf:cdb-oper;
list new_entry{
key name;
leaf name {
type string;
}
}
}
}
my text file contains only one string “hello!” from where i will be storing it to the database.
Here is my application for that:-
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/poll.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <stdio.h>
#include <confd_lib.h>
#include <confd_cdb.h>
#include “myown.h”
#define INTERVAL 10
static int update_status(int sock)
{
FILE *proc;
int ret = 0, res = 0;
char buf[BUFSIZ], ch;
char *p;
confd_value_t val[2];
confd_value_t * ptr = val;
int i = 0;
printf(“update_status\n”);
if ((ret = cdb_start_session(sock, CDB_OPERATIONAL)) != CONFD_OK)
return ret;
if ((ret = cdb_set_namespace(sock, myown__ns)) != CONFD_OK)
return ret;
printf(“update_status : opening file\n”);
if ((proc = fopen("/home/surabhi/Confd/hello.txt", “r”)) == NULL)
return CONFD_ERR;
while (ret == CONFD_OK && fgets(buf, sizeof(buf), proc) != NULL) {
printf("value of buf = %s\n",buf);
p = strchr(buf,(ch = fgetc(proc)));
CONFD_SET_XMLTAG(&val[i], myown_Surabhi, myown__ns);
i++;
CONFD_SET_STR(&val[i],buf);
res = cdb_set_object(sock,ptr,i,"/Surabhi/new_entry{%s}", p);
printf("value of res is: %d\n", res);
}
fclose(proc);
printf("update_status value buffer at the end : buf = %s\n",buf);
cdb_end_session(sock);
return ret;
}
int main(int argc, char **argv)
{
int interval = 0;
struct sockaddr_in addr;
int sock;
if (argc > 1)
interval = atoi(argv[1]);
if (interval == 0)
interval = INTERVAL;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_family = AF_INET;
addr.sin_port = htons(CONFD_PORT);
confd_init(argv[0], stderr, CONFD_SILENT);
if (confd_load_schemas((struct sockaddr*)&addr,
sizeof (struct sockaddr_in)) != CONFD_OK)
confd_fatal("%s: Failed to load schemas from confd\n", argv[0]);
if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0)
confd_fatal("%s: Failed to create socket", argv[0]);
if (cdb_connect(sock, CDB_DATA_SOCKET, (struct sockaddr *)&addr,
sizeof(struct sockaddr_in)) < 0)
confd_fatal("%s: Failed to connect to ConfD", argv[0]);
while (1) {
if (update_status(sock) != CONFD_OK)
confd_fatal("%s: Failed to update status\n", argv[0]);
sleep(interval);
}
}
Now, the problem is with the cdb_set_object() function which is giving -1 in res, due to which when I check through cli then it doesnt show any value stored in the leaf: name (type string) in the container Surabhi.
Can anybody please tell where is the mistake in my application??
Thanks in advance.
Please help.