Really dumb mistakes.
If we can open the file for reading and writing, and remove the need to open/close
in both threads each time it is accessed, the efficiency might be better.
We'd still have to close the file to save changes to disk though,
might be a problem if the program crashes.
CXX = gcc
FLAGS = -std=c99 -Wall -Werror -pedantic -g
LIB = -lpthread
-OBJ = log.o sensor.o test_request.o main.o
+OBJ = log.o sensor.o query.o main.o
RM = rm -f
BIN = server
#include <signal.h> // for signal handling
// --- Custom headers --- //
-
+#include "query.h"
#include "log.h"
#include "options.h"
#include "sensor.h"
}
// run request thread in the main thread
- Query_Request(NULL); //TODO: Replace with FastCGI code
+ Query_Main(NULL); //TODO: Replace with FastCGI code
return 0;
}
-
+#include "query.h"
#include "sensor.h"
#include "log.h"
}
else
{
- fseek(file, 0, SEEK_SET);
- rewind(file);
+ fseek(file, (s->read_offset)*sizeof(DataPoint), SEEK_SET);
amount_read = fread(&buffer, sizeof(DataPoint), QUERY_BUFSIZ, file);
s->read_offset += amount_read;
Log(LOGDEBUG, "Read %d data points; offset now at %d", amount_read, s->read_offset);
// CRITICAL SECTION (no threads should be able to read/write the file at the same time)
pthread_mutex_lock(&(s->mutex));
- // Open binary file and dump buffer into it
- FILE * file = fopen(s->filename, "wb");
+ // Open binary file in append mode and dump buffer into it
+ FILE * file = fopen(s->filename, "ab");
if (file == NULL)
{
- Fatal("Couldn't open file \"%s\" mode wb - %s", s->filename, strerror(errno));
+ Fatal("Couldn't open file \"%s\" mode ab - %s", s->filename, strerror(errno));
}
- fseek(file, 0, SEEK_END);
int amount_written = fwrite(s->buffer, sizeof(DataPoint), SENSOR_DATABUFSIZ, file);
if (amount_written != SENSOR_DATABUFSIZ)
{