dataPacketStore.c 3.01 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <stdlib.h>
#include <pthread.h>
#include "../../util/common.h"
#include "../../util/bptree.h"
#include "../../util/dbg.h"
#include "dataPacketStore.h"


PrrtDataPacketStore *PrrtDataPacketStore_create(void)
{
    PrrtDataPacketStore* store = calloc(1, sizeof(PrrtDataPacketStore));
    check_mem(store);
    check(pthread_mutex_init(&store->lock, NULL) == EXIT_SUCCESS, "Init mutex failed.")

15 16
    store->dataStore = NULL;

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
    return store;
    error:
        PERROR("Out of memory%s.","");
        return NULL;
}

uint32_t PrrtDataPacketStore_size(PrrtDataPacketStore *store)
{
    check(pthread_mutex_lock(&store->lock) == EXIT_SUCCESS, "Lock failed.");
    uint32_t res = BPTree_size(store->dataStore);
    check(pthread_mutex_unlock(&store->lock) == EXIT_SUCCESS, "Unlock failed.");
    return res;
    error:
    PERROR("Getting size failed%s.", "");
    return 0;
}

bool PrrtDataPacketStore_destroy(PrrtDataPacketStore *store)
{
    List *dataList = List_create();
    BPTree_get_range(store->dataStore, dataList, 0, SEQNO_SPACE - 1);
    while(List_count(dataList) > 0) {
        PrrtPacket *packet = List_shift(dataList);
40
        store->dataStore = BPTree_delete(store->dataStore, packet->sequenceNumber);
41 42 43 44 45 46 47
        PrrtPacket_destroy(packet);
    }

    List_destroy(dataList);

    check(pthread_mutex_destroy(&store->lock) == EXIT_SUCCESS, "Destroy failed.");

48 49 50
    if(store->dataStore != NULL) {
        store->dataStore = BPTree_destroy(store->dataStore);
    }
51 52 53 54 55 56 57 58 59 60

    free(store);
    return true;
    error:
        PERROR("Could not destroy%s.","");
        return false;
}

void PrrtDataPacketStore_remove_range(PrrtDataPacketStore *store, List *res, prrtSequenceNumber_t start, prrtSequenceNumber_t stop)
{
61 62 63 64 65 66 67 68 69
    check(pthread_mutex_lock(&store->lock) == EXIT_SUCCESS, "Lock failed.");

    if(start > stop) {
        BPTree_get_range(store->dataStore, res, start, SEQNO_SPACE-1);
        BPTree_get_range(store->dataStore, res, 0, stop);
    } else {
        BPTree_get_range(store->dataStore, res, start, stop);
    }

70 71 72 73
    LIST_FOREACH(res, first, next, current) {
        PrrtPacket *packet = current->value;
        store->dataStore = BPTree_delete(store->dataStore, packet->sequenceNumber);
    }
74 75 76 77
    check(pthread_mutex_unlock(&store->lock) == EXIT_SUCCESS, "Unlock failed.");
    return;
    error:
        PERROR("Failed to remove range%s.","");
78 79 80 81
}

bool PrrtDataStore_insert(PrrtDataPacketStore *store, PrrtPacket *packet)
{
82
    check(pthread_mutex_lock(&store->lock) == EXIT_SUCCESS, "Lock failed.");
Andreas Schmidt's avatar
Andreas Schmidt committed
83
    debug(DEBUG_BLOCK, "PrrtDataStore_insert: %d", packet->sequenceNumber);
84 85
    if(BPTree_get(store->dataStore, packet->sequenceNumber) == NULL) {
        store->dataStore = BPTree_insert(store->dataStore, packet->sequenceNumber, packet);
86
        check(pthread_mutex_unlock(&store->lock) == EXIT_SUCCESS, "Unlock failed.");
Andreas Schmidt's avatar
Andreas Schmidt committed
87
        debug(DEBUG_BLOCK, "PrrtDataStore_size: %d", PrrtDataPacketStore_size(store));
88 89
        return true;
    }
90 91
    check(pthread_mutex_unlock(&store->lock) == EXIT_SUCCESS, "Unlock failed.");
    error:
92
    return false;
93
}