Commit 6d326efb authored by Ivan Tyagov's avatar Ivan Tyagov

WIP: initial implementation of a dictionary.

parent af08348a
......@@ -63,3 +63,64 @@ char *randomString(size_t length)
}
return randomString;
}
// XXX: dictionary implementation based on https://gist.github.com/kylef/86784/fe97567ec9baf5c0dce3c7fcbec948e21dfcce09
typedef struct dict_t_struct {
char *key;
void *value;
struct dict_t_struct *next;
} dict_t;
dict_t **dictAlloc(void) {
return malloc(sizeof(dict_t));
}
void dictDealloc(dict_t **dict) {
free(dict);
}
void *getItem(dict_t *dict, char *key) {
dict_t *ptr;
for (ptr = dict; ptr != NULL; ptr = ptr->next) {
if (strcmp(ptr->key, key) == 0) {
return ptr->value;
}
}
return NULL;
}
void delItem(dict_t **dict, char *key) {
dict_t *ptr, *prev;
for (ptr = *dict, prev = NULL; ptr != NULL; prev = ptr, ptr = ptr->next) {
if (strcmp(ptr->key, key) == 0) {
if (ptr->next != NULL) {
if (prev == NULL) {
*dict = ptr->next;
} else {
prev->next = ptr->next;
}
} else if (prev != NULL) {
prev->next = NULL;
} else {
*dict = NULL;
}
free(ptr->key);
free(ptr);
return;
}
}
}
void addItem(dict_t **dict, char *key, void *value) {
delItem(dict, key); /* If we already have a item with this key, delete it. */
dict_t *d = malloc(sizeof(struct dict_t_struct));
d->key = malloc(strlen(key)+1);
strcpy(d->key, key);
d->value = value;
d->next = *dict;
*dict = d;
}
......@@ -35,6 +35,7 @@ static void dataChangeNotificationCallback(UA_Server *server, UA_UInt32 monitore
unsigned int coupler_id = *(UA_UInt32*) var->value.data;
if (coupler_id!=COUPLER_ID) {
// care for other coupler_id NOT ourselves
addItem(&SUBSCRIBER_DICT, "foo", "bar");
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "ID = %d, microseconds=%ld", coupler_id, micro_seconds);
}
}
......
......@@ -39,6 +39,9 @@ static int COUPLER_ID = 0;
// global server
UA_Server *server;
// global dictionary of subscribers
dict_t *SUBSCRIBER_DICT;
// The default port of OPC-UA server
const int DEFAULT_OPC_UA_PORT = 4840;
const int DEFAULT_MODE = 0;
......@@ -156,6 +159,11 @@ int main(int argc, char **argv)
long result;
char *eptr;
// init dictionary only once$
if (SUBSCRIBER_DICT==NULL){
SUBSCRIBER_DICT = *dictAlloc();
}
// handle command line arguments
struct arguments arguments;
arguments.port = DEFAULT_OPC_UA_PORT;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment