First cut of peer request queue complete

This commit is contained in:
John Jones 2017-07-24 10:26:07 -05:00
parent 5507937bff
commit 4368e052e2
3 changed files with 82 additions and 7 deletions

View file

@ -256,6 +256,13 @@ int ipfs_cid_cast(const unsigned char* incoming, size_t incoming_size, struct Ci
* @returns < 0 if side A is greater, > 0 if side B is greater, or 0 if equal
*/
int ipfs_cid_compare(struct Cid* a, struct Cid* b) {
if (a == NULL && b == NULL)
return 0;
if (a != NULL && b == NULL)
return -1;
if (a == NULL && b != NULL)
return 1;
if (a->version != b->version) {
return b->version - a->version;
}

View file

@ -35,8 +35,11 @@ int ipfs_bitswap_peer_request_free(struct PeerRequest* request) {
*/
struct PeerRequestQueue* ipfs_bitswap_peer_request_queue_new() {
struct PeerRequestQueue* queue = malloc(sizeof(struct PeerRequestQueue));
queue->first = NULL;
queue->last = NULL;
if (queue != NULL) {
pthread_mutex_init(&queue->queue_mutex, NULL);
queue->first = NULL;
queue->last = NULL;
}
return queue;
}
@ -49,10 +52,12 @@ int ipfs_bitswap_peer_request_queue_free(struct PeerRequestQueue* queue) {
pthread_mutex_lock(&queue->queue_mutex);
struct PeerRequestEntry* current = queue->last;
while (current != NULL) {
struct PeerRequestEntry* prior = current->prior;
ipfs_bitswap_peer_request_entry_free(current);
current = current->prior;
current = prior;
}
pthread_mutex_unlock(&queue->queue_mutex);
free(queue);
return 1;
}
@ -69,6 +74,9 @@ int ipfs_bitswap_peer_request_queue_add(struct PeerRequestQueue* queue, struct P
pthread_mutex_lock(&queue->queue_mutex);
entry->prior = queue->last;
queue->last = entry;
if (queue->first == NULL) {
queue->first = entry;
}
pthread_mutex_unlock(&queue->queue_mutex);
return 1;
}
@ -151,13 +159,13 @@ struct PeerRequestEntry* ipfs_bitswap_peer_request_entry_new() {
/**
* Frees resources allocated
* NOTE: This does not free the embedded PeerRequest (should it?)
* @param entry the PeerRequestEntry to free
* @returns true(1)
*/
int ipfs_bitswap_peer_request_entry_free(struct PeerRequestEntry* entry) {
entry->next = NULL;
entry->prior = NULL;
ipfs_bitswap_peer_request_free(entry->current);
entry->current = NULL;
free(entry);
return 1;

View file

@ -5,11 +5,71 @@
* Create a queue, do some work, free the queue, make sure valgrind likes it.
*/
int test_bitswap_peer_request_queue_new() {
int retVal = 0;
struct PeerRequestQueue* queue = NULL;
struct PeerRequest* request = NULL;
// create a queue
struct PeerRequestQueue* queue = ipfs_bitswap_peer_request_queue_new();
struct PeerRequest* request = ipfs_bitswap_peer_request_new();
queue = ipfs_bitswap_peer_request_queue_new();
if (queue == NULL)
goto exit;
// add a request
request = ipfs_bitswap_peer_request_new();
if (request == NULL)
goto exit;
ipfs_bitswap_peer_request_queue_add(queue, request);
retVal = 1;
exit:
// clean up
ipfs_bitswap_peer_request_queue_free(queue);
return 1;
return retVal;
}
int test_bitswap_peer_request_queue_find() {
int retVal = 0;
struct PeerRequestQueue* queue = NULL;
struct PeerRequest* request1 = NULL;
struct PeerRequest* request2 = NULL;
struct PeerRequestEntry* foundRequestEntry = NULL;
// create a queue
queue = ipfs_bitswap_peer_request_queue_new();
if (queue == NULL)
goto exit;
// add a request
request1 = ipfs_bitswap_peer_request_new();
if (request1 == NULL)
goto exit;
request1->peer_id = 1;
ipfs_bitswap_peer_request_queue_add(queue, request1);
// add a second request
request2 = ipfs_bitswap_peer_request_new();
if (request2 == NULL)
goto exit;
request2->peer_id = 2;
ipfs_bitswap_peer_request_queue_add(queue, request2);
// find the first
foundRequestEntry = ipfs_bitswap_peer_request_queue_find_entry(queue, request1);
if (foundRequestEntry == NULL)
goto exit;
if (foundRequestEntry->current->peer_id != 1)
goto exit;
// find the second
foundRequestEntry = ipfs_bitswap_peer_request_queue_find_entry(queue, request2);
if (foundRequestEntry == NULL)
goto exit;
if (foundRequestEntry->current->peer_id != 2)
goto exit;
retVal = 1;
exit:
// clean up
ipfs_bitswap_peer_request_queue_free(queue);
return retVal;
}