Fixed binary file access
authorSam Moore <[email protected]>
Fri, 16 Aug 2013 00:28:37 +0000 (08:28 +0800)
committerSam Moore <[email protected]>
Fri, 16 Aug 2013 00:28:37 +0000 (08:28 +0800)
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.

server/Makefile
server/main.c
server/query.c
server/sensor.c

index 95cd03e..009128b 100644 (file)
@@ -2,7 +2,7 @@
 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
index 524f550..5e2c6c8 100644 (file)
@@ -9,7 +9,7 @@
 #include <signal.h> // for signal handling
 
 // --- Custom headers --- //
-
+#include "query.h"
 #include "log.h"
 #include "options.h"
 #include "sensor.h"
@@ -74,7 +74,7 @@ int main(int argc, char ** argv)
        }
 
        // 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;
 }
 
index 7a9ab16..8499e37 100644 (file)
@@ -6,7 +6,7 @@
 
 
 
-
+#include "query.h"
 
 #include "sensor.h"
 #include "log.h"
@@ -32,8 +32,7 @@ void QuerySensor(int id) //TODO: This code will form the SensorHandler FastCGI f
                }
                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);
index f1ec5dc..a3d3f0b 100644 (file)
@@ -105,13 +105,12 @@ void * Sensor_Main(void * arg)
                // 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)
                        {

UCC git Repository :: git.ucc.asn.au