Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds uthash-based content store implementation #16

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ endif()

if (NOT DEFINED CCNL_LINUXKERNEL)
if (NOT DEFINED CCNL_RIOT)
add_dependencies(ccn-lite-relay ccnl-core ccnl-pkt ccnl-fwd ccnl-unix)
add_dependencies(ccn-lite-relay ccnl-core ccnl-pkt ccnl-fwd ccnl-unix ccnl-cs)
endif()
endif()

Expand Down
2 changes: 1 addition & 1 deletion src/ccnl-core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ project(ccnl-core)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

include_directories(include ../ccnl-pkt/include ../ccnl-unix/include ../ccnl-fwd/include ../ccnl-test/include ../ccnl-utils/include)
include_directories(include ../ccnl-cs/include ../ccnl-pkt/include ../ccnl-unix/include ../ccnl-fwd/include ../ccnl-test/include ../ccnl-utils/include)

if(DEFINED CCNL_RIOT)
include_directories(include ../ccnl-riot/include)
Expand Down
2 changes: 1 addition & 1 deletion src/ccnl-cs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 2.8)
project(ccnl-cs)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/../lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

include_directories(include)

Expand Down
46 changes: 46 additions & 0 deletions src/ccnl-cs/include/ccnl-cs-helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @f ccnl-cs-helper.h
* @b Helper functions for the content store
*
* Copyright (C) 2018 MSA Safety
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#ifndef CCNL_CS_HELPER
#define CCNL_CS_HELPER

#include "ccnl-cs.h"

/**
* Allocates a \ref ccnl_cs_name_t struct
*
* @param[in] prefix The prefix to set in the name
* @param[in] size The length of the \p name
*
* @return Upon success an allocated and initialized \ref ccnl_cs_name_t struct, NULL otherwise
*/
ccnl_cs_name_t *ccnl_cs_create_name(const char* prefix, size_t length);

/**
* Allocates a \ref ccnl_cs_content_t struct
*
* @param[in] content The content to set
* @param[in] size The size of the \p content
*
* @return Upon success an allocated and initialized \ref ccnl_cs_content_t struct, NULL otherwise
*/
ccnl_cs_content_t *ccnl_cs_create_content(uint8_t *content,
size_t size);

#endif //CCNL_CS_HELPER
61 changes: 61 additions & 0 deletions src/ccnl-cs/include/ccnl-cs-uthash.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* @f ccnl-cs-uthash.h
* @b uthash-based content store
*
* Copyright (C) 2018 MSA Safety
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#ifndef CCNL_CS_SIMPLE
#define CCNL_CS_SIMPLE

#include "ccnl-cs.h"
#include "uthash.h"

#include <stdbool.h>

/**
* @brief An entry in the uthash-based implementation of the
* content store.
*/
typedef struct {
ccnl_cs_name_t *key; /**< key */
ccnl_cs_content_t *value; /**< value */
UT_hash_handle hh; /**< makes structure hashable */
} ccnl_cs_uthash_t;

/**
* @brief Sets the \p ops struct to the uthash-powered content store
*
* @param[in] ops The struct to set the functions points to the internal
* uthash-powered add/lookup/remove functions
*/
void ccnl_cs_init_uthash(ccnl_cs_ops_t *ops);

/**
* @brief Checks if a given \p name is already stored in the content store
*
* @param[in] name The name to lookup in the content store
*
* @return true if the \p name is in the content store, false otherwise
*/
bool ccnl_cs_uthash_exists(ccnl_cs_name_t *name);

/**
* @brief Prints the content of the content store to the command line
*/
void ccnl_cs_uthash_dump(void);


#endif //CCNL_CS_UTHASH
10 changes: 7 additions & 3 deletions src/ccnl-cs/include/ccnl-cs.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@
* @brief Provides status codes for content store operations
*/
typedef enum {
CS_OPERATION_WAS_SUCCESSFUL = 0, /**< operation was successfull */
CS_OPERATION_UNSUCCESSFUL = 0, /**< operation was not successfull */
CS_OPERATION_WAS_SUCCESSFUL = 1, /**< operation was successfull */
CS_OPTIONS_ARE_NULL = -1, /**< \ref ccnl_cs_ops_t are NULL */
CS_NAME_IS_INVALID = -2, /**< name is invalid or NULL */
CS_CONTENT_IS_INVALID = -3, /**< content is invalid or NULL */

CS_NAME_COULD_NOT_BE_FOUND = -4, /**< a name could not be found */

CS_DO_NOT_USE = INT_MAX /**< set the enum to a fixed width, do not use! */
} ccnl_cs_status_t;
Expand Down Expand Up @@ -115,10 +118,11 @@ ccnl_cs_add(ccnl_cs_ops_t *ops,
* @param[in] name The name of the content which is about to searched in the content store
* @param[out] content If the lookup was successfull, the variable contains the result
*
* @return 0 The content was found in the content store
* @return 0 The content could not be found in the content store
* @return 1 The content was found in the content store
* @return -1 An invalid \ref ccnl_cs_op_t struct was passed to the function (e.g. \p ops is NULL)
* @return -2 An invalid \ref ccnl_cs_name_t struct was passed to the function (e.g. \p name is NULL)
* @return -2 An invalid \ref ccnl_cs_content_t struct was passed to the function (e.g. \p content is NULL)
* @return -3 An invalid \ref ccnl_cs_content_t struct was passed to the function (e.g. \p content is NULL)
*/
ccnl_cs_status_t
ccnl_cs_lookup(ccnl_cs_ops_t *ops,
Expand Down
67 changes: 67 additions & 0 deletions src/ccnl-cs/src/ccnl-cs-helper.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* @f ccnl-cs.c
* @b Implementation of helper functions for the content store
*
* Copyright (C) 2018 MSA Safety
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#include "ccnl-cs-helper.h"

#include <string.h>

ccnl_cs_name_t *ccnl_cs_create_name(const char* name, size_t length) {
/** allocate the size of the structure + the size of the string we'll store*/
size_t size = sizeof(ccnl_cs_name_t); // + (sizeof(uint8_t) * length);
ccnl_cs_name_t *result = malloc(size);

if (result) {
size = (sizeof(uint8_t) * length);
result->name = malloc(size);

if (result->name) {
/* set members of 'result' struct */
memcpy(result->name, name, length);
result->length = length;
} else {
free(result);
result = NULL;
}
}

return result;
}


ccnl_cs_content_t *ccnl_cs_create_content(uint8_t *content, size_t size) {
ccnl_cs_content_t *result = malloc(sizeof(ccnl_cs_content_t));

if (result) {
result->content = malloc(sizeof(uint8_t) * size);

if (result->content) {
/* set members of 'result' struct */
memcpy(result->content, content, size);
result->length = size;
/* could not allocate memory for member 'content' */
} else {
free(result);
result = NULL;
}
}

return result;
}


145 changes: 145 additions & 0 deletions src/ccnl-cs/src/ccnl-cs-uthash.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/**
* @f ccnl-cs-uthash.c
* @b Content store implementation based on uthash
*
* Copyright (C) 2018 MSA Safety
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#include <stdio.h>
#include <stdbool.h>
#include <string.h>

#include "ccnl-cs-helper.h"
#include "ccnl-cs-uthash.h"

/***
* The actual hashmap of the uthash-based content store implementation
*/
static ccnl_cs_uthash_t *hashmap = NULL;


static ccnl_cs_status_t ccnl_cs_uthash_add(const ccnl_cs_name_t *name, const ccnl_cs_content_t *content);
static ccnl_cs_status_t ccnl_cs_uthash_lookup(const ccnl_cs_name_t *name, ccnl_cs_content_t *content);
static ccnl_cs_status_t ccnl_cs_uthash_remove(const ccnl_cs_name_t *name);


void ccnl_cs_init_uthash(ccnl_cs_ops_t *ccnl_cs_ops_uthash) {
/* set the function pointers */
ccnl_cs_init(ccnl_cs_ops_uthash, ccnl_cs_uthash_add, ccnl_cs_uthash_lookup, ccnl_cs_uthash_remove);
}

static ccnl_cs_status_t ccnl_cs_uthash_add(const ccnl_cs_name_t *name, const ccnl_cs_content_t *content) {
/* name is not already stored in the content store */
if (!ccnl_cs_uthash_exists((ccnl_cs_name_t*)name)) {
ccnl_cs_uthash_t *entry = malloc(sizeof(ccnl_cs_uthash_t));

if (entry) {
entry->key = ccnl_cs_create_name((const char*)name->name, name->length);

if (entry->key) {
/* copy over the name */
memcpy(entry->key->name, name->name, name->length);
entry->key->length = name->length;

/* create value member */
entry->value = ccnl_cs_create_content(content->content, content->length);

if (entry->value) {
/* copy over the content */
memcpy(entry->value->content, content->content, content->length);
entry->value->length = content->length;

HASH_ADD_KEYPTR(hh, hashmap, entry->key->name, entry->key->length, entry);

return CS_OPERATION_WAS_SUCCESSFUL;
} else {
free(entry);
}
} else {
free(entry);
}
}
}

return CS_OPERATION_UNSUCCESSFUL;
}

static ccnl_cs_status_t ccnl_cs_uthash_lookup(const ccnl_cs_name_t *name, ccnl_cs_content_t *content) {
ccnl_cs_uthash_t *entry = NULL;
/* lookup if an entry for the given name exists */
HASH_FIND(hh, hashmap, name->name, name->length, entry);

if (entry) {
// TODO: should be caught in ccnl_cs_lookup
if (content) {
content->content = entry->value->content;
content->length = entry->value->length;

return CS_OPERATION_WAS_SUCCESSFUL;
}
}

return CS_NAME_COULD_NOT_BE_FOUND;
}

static ccnl_cs_status_t ccnl_cs_uthash_remove(const ccnl_cs_name_t *name) {
ccnl_cs_uthash_t *entry = NULL;
/* lookup if an entry for the given name exists */
HASH_FIND(hh, hashmap, name->name, name->length, entry);

/* check if entry was filled by the lookup */
if (entry) {
/* free the entry */
free(entry);

return CS_OPERATION_WAS_SUCCESSFUL;
}

return CS_NAME_COULD_NOT_BE_FOUND;
}

bool ccnl_cs_uthash_exists(ccnl_cs_name_t *name) {
ccnl_cs_uthash_t *entry = NULL;

/* check that name is a valid pointer */
if (name) {
HASH_FIND(hh, hashmap, name->name, name->length, entry);
}

/* if no entry could be found the, the temporary element 'entry' points to NULL */
return (entry != NULL);
}

void ccnl_cs_uthash_dump(void) {
ccnl_cs_uthash_t *entry;

for (entry = hashmap; entry != NULL; entry = entry->hh.next) {
printf("name: %s length: %d\n", entry->key->name, entry->key->length);
printf("content: \n");
printf("\t");

for(uint32_t i = 0; i < entry->value->length; i++) {
printf("%04x ", entry->value->content[i]);

if ((i != 0) && (i % 8 == 0)) {
printf("\n");
printf("\t");
}
}

printf("\nlength: %d\n", entry->value->length);
printf("\n");
}
}
Loading