From 3d93df54d024f49895db6277e873dccd10b5baec Mon Sep 17 00:00:00 2001 From: "Suren A. Chilingaryan" Date: Sun, 17 Nov 2019 09:16:57 +0100 Subject: The first test (file file-base simmulation) --- src/ufo-roof-read-file.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/ufo-roof-read-file.c (limited to 'src/ufo-roof-read-file.c') diff --git a/src/ufo-roof-read-file.c b/src/ufo-roof-read-file.c new file mode 100644 index 0000000..de8391e --- /dev/null +++ b/src/ufo-roof-read-file.c @@ -0,0 +1,84 @@ +#include +#include +#include + +#include "glib.h" + +#include "ufo-roof.h" +#include "ufo-roof-read-file.h" + +typedef struct { + UfoRoofReadInterface iface; + + UfoRoofConfig *cfg; + + gchar *fname; + FILE *fd; +} UfoRoofReadFile; + +static void ufo_roof_read_file_free(UfoRoofReadInterface *iface) { + UfoRoofReadFile *reader = (UfoRoofReadFile*)iface; + + if (reader) { + if (reader->fname) + g_free(reader->fname); + + if (reader->fd >= 0) + fclose(reader->fd); + + free(reader); + } +} + +static guint ufo_roof_read_file(UfoRoofReadInterface *iface, void *buffers, GError **error) { + UfoRoofReadFile *reader = (UfoRoofReadFile*)iface; + UfoRoofConfig *cfg = reader->cfg; + + size_t bytes = 0; + size_t packet_size = cfg->header_size + cfg->payload_size; + size_t expected = cfg->max_packets * packet_size; + + while ((!feof(reader->fd))&&(!ferror(reader->fd))&&(bytes < expected)) { + size_t ret = fread(buffers + bytes, 1, expected - bytes, reader->fd); + bytes += ret; + } + + guint packets = bytes / packet_size; + + if (ferror(reader->fd)) { + roof_network_error_with_retval(error, 0, "read failed, error %i", ferror(reader->fd)); + } else if ((feof(reader->fd))&&(bytes % packet_size)) { + roof_network_error_with_retval(error, packets, "extra data in the end of input"); + } + + return packets; +} + + +UfoRoofReadInterface *ufo_roof_read_file_new(UfoRoofConfig *cfg, guint id, GError **error) { + UfoRoofReadFile *reader = (UfoRoofReadFile*)calloc(1, sizeof(UfoRoofReadFile)); + if (!reader) roof_new_error(error, "Can't allocate UfoRoofReadFile"); + + // FIXME: Shall we jump if max_packet_size > header+payload (or will be extra data included in the data files)? Report error for now. + if ((cfg->header_size + cfg->payload_size) != cfg->max_packet_size) + roof_new_error(error, "packet_size (%u) should be equal to max_packet_size (%u) if UfoRoofReadFile is used", cfg->header_size + cfg->payload_size, cfg->max_packet_size); + + reader->cfg = cfg; + reader->iface.close = ufo_roof_read_file_free; + reader->iface.read =ufo_roof_read_file; + + reader->fname = g_strdup_printf(cfg->path, id + cfg->first_file_number); + if (!reader->fname) { + free(reader); + roof_new_error(error, "Can't build file name"); + } + + reader->fd = fopen(reader->fname, "rb"); + if (!reader->fd) { + g_free(reader->fname); + g_free(reader); + roof_new_error(error, "Can't open file %s", reader->fname); + } + + return (UfoRoofReadInterface*)reader; +} -- cgit v1.2.3