diff options
author | Robert Gustafsson <robg@student.chalmers.se> | 2017-09-28 13:41:45 +0200 |
---|---|---|
committer | Andreas Lindhé <andreas@lindhe.io> | 2017-10-09 12:14:35 +0200 |
commit | 2f5b120d90ce98676447df7fbf27397029d84ebd (patch) | |
tree | 3997b1cc0ba7ba94e53e6ddddbc5690982f26233 | |
parent | a5cfcd3770e0860013bef2e2fdfe2ec4969bdab7 (diff) | |
download | midbro-2f5b120d90ce98676447df7fbf27397029d84ebd.tar.gz midbro-2f5b120d90ce98676447df7fbf27397029d84ebd.tar.bz2 |
Add basic fifoqueue to buffer events
-rw-r--r-- | Makefile | 6 | ||||
-rw-r--r-- | fifoqueue.c | 82 | ||||
-rw-r--r-- | includes/fifoqueue.h | 15 | ||||
-rw-r--r-- | includes/types.h | 29 | ||||
-rw-r--r-- | pasad.c | 29 |
5 files changed, 148 insertions, 13 deletions
@@ -4,11 +4,13 @@ LDFLAGS = -L/usr/local/lib -lbroccoli all: pasad -pasad: pasad.o - $(CC) pasad.o -o pasad $(LDFLAGS) +pasad: pasad.o fifoqueue.o + $(CC) pasad.o fifoqueue.o -o pasad $(LDFLAGS) pasad.o: pasad.c $(CC) $(CFLAGS) pasad.c +fifoqueue.o: fifoqueue.c + $(CC) -c -Wall fifoqueue.c clean: rm *.o pasad diff --git a/fifoqueue.c b/fifoqueue.c new file mode 100644 index 0000000..56b1dda --- /dev/null +++ b/fifoqueue.c @@ -0,0 +1,82 @@ +#include "includes/fifoqueue.h" + + Fifo_q * +init_queue(int size) +{ + Fifo_q * q = (Fifo_q *) malloc(sizeof(Fifo_q)); + q->head = NULL; + q->tail = NULL; + q->maxSize = size; + q->currentSize = 0; + return q; +} + + boolean +is_full(Fifo_q * q) +{ + if(q->currentSize < q->maxSize) + return false; + else + return true; +} + + boolean +is_empty(Fifo_q * q) +{ + if(q->head==NULL) + return true; + else + return false; +} + + int +add_to_queue(Fifo_q * q, Sensor_t * sensor) +{ + /* TODO delete first one if full */ + if(q == NULL){ + return -1; + } + else if(is_full(q)){ + return -1; + } + Queue_t * new_elem = (Queue_t *) malloc(sizeof(Queue_t *)); + new_elem->next = NULL; + new_elem->sensor = sensor; + if(is_empty(q)) + q->head = new_elem; + else + q->tail->next = new_elem; + q->tail = new_elem; + q->currentSize++; + return 1; +} + + Sensor_t * +pop_from_queue(Fifo_q * q) +{ + if(is_empty(q)){ + perror("The queue is empty"); + exit(-1); + } + Queue_t * head = q->head; + q->head = q->head->next; + Sensor_t * sensor = head->sensor; + free(head); + q->currentSize--; + return sensor; +} + + void +print_queue(Fifo_q * q) +{ + Queue_t * current = q->head; + if(current == NULL){ + printf("The queue is empty!"); + return; + } + while(current != NULL){ + printf("sensor value=%d, sensor uid=%d\n", + current->sensor->value, current->sensor->uid); + current = current->next; + } +} diff --git a/includes/fifoqueue.h b/includes/fifoqueue.h new file mode 100644 index 0000000..9ee5c53 --- /dev/null +++ b/includes/fifoqueue.h @@ -0,0 +1,15 @@ +#include <stdlib.h> +#include <stdio.h> +#include "types.h" + +Fifo_q * init_queue(int size); + +boolean is_full(Fifo_q * q); + +boolean is_empty(Fifo_q * q); + +int add_to_queue(Fifo_q * q, Sensor_t * sensor); + +Sensor_t * pop_from_queue(Fifo_q * q); + +void print_queue(Fifo_q * q); diff --git a/includes/types.h b/includes/types.h new file mode 100644 index 0000000..a8fa1cf --- /dev/null +++ b/includes/types.h @@ -0,0 +1,29 @@ +#ifndef TYPES_H +#define TYPES_H + +#define true 1 +#define false 0 + +typedef int boolean; +typedef struct sensor_t Sensor_t; +typedef struct queue_t Queue_t; +typedef struct fifo_q Fifo_q; + +struct sensor_t{ + int uid; + int value; +}; + +struct queue_t{ + Sensor_t * sensor; + Queue_t * next; +}; + +struct fifo_q{ + Queue_t * head; + Queue_t * tail; + int maxSize; + int currentSize; +}; + +#endif @@ -1,6 +1,7 @@ #ifdef BROCCOLI #include <broccoli.h> #endif +#include "includes/fifoqueue.h" char *host_default = "127.0.0.1"; char *port_default = "47760"; @@ -14,25 +15,24 @@ bro_pasad_response(BroConn *conn, void *data, uint64* registers, uint64* uid) data = NULL; } - int -main(int argc, char **argv) + void +bro_event_listener() { - BroConn *bc; - char hostname[512]; - int fd = -1; + int fd = -1; + BroConn *bc = NULL; bro_init(NULL); - - bro_debug_calltrace = 0; - bro_debug_messages = 0; + char hostname[512]; snprintf(hostname, 512, "%s:%s", host_default, port_default); - if (! (bc = bro_conn_new_str(hostname, BRO_CFLAG_RECONNECT | BRO_CFLAG_ALWAYS_QUEUE))) { printf("Could not get Bro connection handle.\n"); exit(-1); } + bro_debug_calltrace = 0; + bro_debug_messages = 0; + bro_event_registry_add(bc, "response",(BroEventFunc) bro_pasad_response, NULL); if (! bro_conn_connect(bc)) @@ -42,11 +42,11 @@ main(int argc, char **argv) exit(-1); } - fd = bro_conn_get_fd(bc); + fd =bro_conn_get_fd(bc); fd_set rfds; setbuf(stdout,NULL); - while(1) + while(true) { FD_ZERO(&rfds); FD_SET(fd,&rfds); @@ -59,6 +59,13 @@ main(int argc, char **argv) } bro_conn_delete(bc); +} + + int +main(int argc, char **argv) +{ + Fifo_q * q = init_queue(5); + free(q); return 0; } |