...
authoralex <[email protected]>
Fri, 4 Mar 2022 22:56:28 +0000 (14:56 -0800)
committeralex <[email protected]>
Fri, 4 Mar 2022 22:56:28 +0000 (14:56 -0800)
Makefile.am
inc/pqueue.h [new file with mode: 0644]
src/peer/handshake.c
src/pqueue.c [new file with mode: 0644]

index 2af39890a193e3f8175123f339e747ca6912005e..65f17a33354a52d44c9417ec2de4aa93feff5fc8 100644 (file)
@@ -45,6 +45,7 @@ seederd_SOURCES = \
        src/opt/set.c \
        src/opt/watch.c \
        src/opt/worker.c \
+       src/pqueue.c \
        src/rss/entry.c \
        src/rss/footer.c \
        src/rss/free.c \
@@ -83,6 +84,7 @@ seederd_SOURCES += \
        inc/meta.h \
        inc/net.h \
        inc/opt.h \
+       inc/pqueue.h \
        inc/rss.h \
        inc/session.h \
        inc/setup.h \
diff --git a/inc/pqueue.h b/inc/pqueue.h
new file mode 100644 (file)
index 0000000..f442908
--- /dev/null
@@ -0,0 +1,25 @@
+#ifndef __PQUEUE_H_
+#define __PQUEUE_H_
+
+#include<hashmap.h>
+
+struct priority_queue_entry {
+       void *p;
+       int priority;
+       size_t index;
+};
+
+struct priority_queue {
+       struct priority_queue_entry **queue;
+       size_t size;
+       
+       struct hash_map *map;
+};
+
+int pqueue_access(struct priority_queue*,const void*,size_t);
+int pqueue_add(struct priority_queue*,const void*,size_t,void*);
+void *pqueue_find(struct priority_queue*,const void*,size_t);
+void pqueue_free(struct priority_queue*);
+int pqueue_init(struct priority_queue**,size_t);
+
+#endif
index 24d3135b5f6e7e9c15f58b921effda340caa74ae..6325b6214b8bcb5e5527df6409d43d4e35d6408d 100644 (file)
@@ -8,12 +8,12 @@ int peer_handshake(int sock, const uint8_t *infohash, size_t infohash_size const
 
        assert(HANDSHAKE_INFOHASH_SIZE==infohash_size);
        assert(PEER_ID_SIZE==peer_id_size);
+       
+       if(sock<0) { return -1; }
 
        memcpy(&(header[24]),infohash,HANDSHAKE_INFOHASH_SIZE);
        memcpy(&(header[44]),peer_id,PEER_ID_SIZE);
 
-       if(sock<0) { return -1; }
-
        // send header
 
        return -1;
diff --git a/src/pqueue.c b/src/pqueue.c
new file mode 100644 (file)
index 0000000..e749de0
--- /dev/null
@@ -0,0 +1,122 @@
+#include<pqueue.h>
+
+static int pqueue_swap(struct priority_queue*,size_t,size_t);
+
+int pqueue_access(struct priority_queue *queue, const void *key, size_t key_size) {
+       struct priority_queue_entry *p;
+       size_t index;
+       
+       if(NULL==queue) { return -1; }
+       if((NULL==key)||(0==key_size)) { return -1; }
+
+       p = hashmap_find(queue->map,key,key_size);
+       if(NULL==p) { return 0; }
+
+       p->priority++;
+
+       index = p->index;
+       while(index>0) {
+               if(queue->queue[index-1]->priority<p->priority) {
+                       index--;
+               }
+       }
+
+       if(p->index!=index) {
+               pqueue_swap(queue,index,p->index);
+       }
+
+       return 1;
+}
+
+int pqueue_add(struct priority_queue *queue, const void *key, size_t key_size, void *value) {
+       struct priority_queue_entry *p;
+
+       if(NULL==queue) { return -1; }
+       if((NULL==key)||(0==key_size)) { return -1; }
+
+       if(hashmap_find(queue->map,key,key_size)!=NULL) { return 0; }
+
+       if(priority_queue_entry_init(&p)<0) { return -1; }
+       p->p = value;
+
+       for(size_t i=0;i<queue->size;i++) {
+               if(queue->queue[i]!=NULL) {
+                       queue->queue[i] = p;
+                       p->index = i;
+                       return 1;
+               }
+       }
+
+       return -1;
+}
+
+void *pqueue_evict(struct priority_queue *queue, const void *key, size_t key_size) {
+       struct priority_queue_entry *entry;
+       void *p;
+
+       entry = hashmap_find(queue->map,key,key_size);
+       if(entry!=NULL) {
+               p = entry->p;
+
+               for(size_t i=entry->index;i<(queue->size-1);i++) {
+                       if(NULL==queue->queue[i+1]) {
+                               pqueue_swap(queue,entry->index,i+1);
+                               break;
+                       }
+               }
+
+               free(entry);
+               return p;
+       }
+
+       return NULL;
+}
+
+void *pqueue_find(struct priority_queue *queue, const void *key, size_t key_size) {
+       return NULL;
+}
+
+void pqueue_free(struct priority_queue *queue) {
+       if(queue->map!=NULL) { hashmap_free(queue->map); }
+       if(queue->queue!=NULL) { free(queue->queue); }
+
+       free(queue);
+}
+
+int pqueue_init(struct priority_queue **p, size_t size) {
+       if(p==NULL) { return -1; }
+       if(0==size) { return -1; }
+
+       *p = malloc(sizeof(struct priority_queue));
+       if(NULL==(*p)) {
+               perror("malloc");
+               return -1;
+       }
+
+       (*p)->queue = NULL;
+       (*p)->size = size;
+       (*p)->map = NULL;
+
+       if(hashmap_init(&((*p)->map),size)<0) { pqueue_free(*p); }
+
+       (*p)->queue = malloc(sizeof(void*)*size);
+       if(NULL==(*p)->queue) { pqueue_free(*p); }
+
+       for(size_t i=0;i<size;i++) {
+               (*p)->queue[i] = NULL;
+       }
+
+       return 1;
+}
+
+static void pqueue_swap(struct priority_queue *queue, size_t a, size_t b) {
+       struct priority_queue_entry *p;
+
+       queue->queue[a] = p;
+
+       queue->queue[a] = queue->queue[b];
+       queue->queue[a]->index = a;
+
+       queue->queue[b] = p;
+       queue->queue[b]->index = b;
+}