From f01e882c28f5da48b969668310a43215c0e37b38 Mon Sep 17 00:00:00 2001 From: "Adrien Bourmault (neox)" Date: Sun, 10 Mar 2024 22:19:12 +0200 Subject: [PATCH 01/24] WIP: rebooting server design Signed-off-by: Adrien Bourmault (neox) --- Makefile | 176 +++---- bin/.placeholder | 0 bin/tests/.placeholder | 0 debian/conffiles | 1 - debian/control | 10 - debian/copyright | 13 - debian/etc/server.conf | 2 - debian/var/models/.placeholder | 0 debian/var/models/example_0.2.xml | 111 ----- debian/var/models/schemas/.placeholder | 0 debian/var/models/schemas/model_0.2.xmls | 199 -------- debian/var/users/.placeholder | 0 include/arrows.h | 29 -- include/base.h | 159 ------- include/centers.h | 8 - include/cmds.h | 17 +- include/model.h | 22 +- include/parsing.h | 34 -- include/scheduler.h | 51 +-- include/server.h | 10 +- include/supervisor.h | 13 +- include/terminal.h | 22 +- include/worker.h | 12 +- include/xml.h | 35 -- manifest.scm | 26 ++ src/arrows.c | 4 - src/centers.c | 27 -- src/{ => cli}/cli.c | 0 src/cmds.c | 308 ++++++------- src/main.c | 54 +-- src/model.c | 282 ------------ src/parsing.c | 556 ----------------------- src/scheduler.c | 198 -------- src/server.c | 27 +- src/supervisor.c | 18 - src/tests/getchar | Bin 17096 -> 0 bytes src/tests/test_realloc | Bin 16720 -> 0 bytes src/tests/xml | Bin 17128 -> 0 bytes src/worker.c | 36 -- 39 files changed, 367 insertions(+), 2093 deletions(-) delete mode 100644 bin/.placeholder delete mode 100644 bin/tests/.placeholder delete mode 100644 debian/conffiles delete mode 100644 debian/control delete mode 100644 debian/copyright delete mode 100644 debian/etc/server.conf delete mode 100644 debian/var/models/.placeholder delete mode 100644 debian/var/models/example_0.2.xml delete mode 100644 debian/var/models/schemas/.placeholder delete mode 100644 debian/var/models/schemas/model_0.2.xmls delete mode 100644 debian/var/users/.placeholder delete mode 100644 include/xml.h create mode 100644 manifest.scm rename src/{ => cli}/cli.c (100%) delete mode 100755 src/tests/getchar delete mode 100755 src/tests/test_realloc delete mode 100755 src/tests/xml diff --git a/Makefile b/Makefile index 1d5cc68..a566609 100644 --- a/Makefile +++ b/Makefile @@ -20,120 +20,124 @@ # along with this program. If not, see . # #=----------------------------------------------------------------------------=# -CCOPTS=-pthread -Wall -g -Os -I/usr/include/libxml2 -LDFLAGS= -lc -lpthread -lxml2 +.PHONY: all clean install run build_system +.DELETE_ON_ERROR: $(BINDIR)/gem-graph-server +.DEFAULT_GOAL := all + +# +# Color codes +# +CL='\033[0;32m' +CL2='\033[1;36m' +CL3='\033[0m' +NC='\033[1;37m' + +# +# Variables & constants +# +NTHREADS= $(shell nproc) + +CC=gcc +WARNINGS= -Wall +DEBUG= -ggdb -fno-omit-frame-pointer -fdiagnostics-color=always -fsanitize=bounds -fstack-check + #-fsanitize=address \ + #-fsanitize=pointer-compare -fsanitize=pointer-subtract \ + #-fsanitize=leak -fsanitize=undefined -fsanitize=null -fsanitize=bounds +OPTIMIZE= -O3 +INCLUDE= $(shell pkg-config --cflags glib-2.0 libxml-2.0 gtk4) +LIBS= $(shell pkg-config --libs glib-2.0 libxml-2.0 gtk4) -lGL -lGLU -lm -lepoxy -lX11 -lGLEW BINDIR=bin -INCDIR=include +BUILDDIR=build SRCDIR=src -DEBDIR=debian +#vpath %.c $(SRCDIR) -SERVEROBJ= $(BINDIR)/scheduler.o $(BINDIR)/server.o $(BINDIR)/worker.o \ - $(BINDIR)/centers.o $(BINDIR)/cmds.o $(BINDIR)/model.o \ - $(BINDIR)/main.o $(BINDIR)/arrows.o $(BINDIR)/parsing.o -CLIOBJ= $(BINDIR)/cli.o - -SRCS=$(patsubst $(BINDIR)/%.o,$(SRCDIR)/%.c,$(SERVEROBJ)) \ - $(patsubst $(BINDIR)/%.o,$(SRCDIR)/%.c,$(CLIOBJ)) - -DEPS=$(patsubst $(BINDIR)/%.o,$(BINDIR)/%.d,$(SERVEROBJ)) \ - $(patsubst $(BINDIR)/%.o,$(BINDIR)/%.d,$(CLIOBJ)) +SOURCES= $(shell find $(SRCDIR) -maxdepth 1 -type f -name "*.c") +BUILDBINS=$(patsubst %.c,$(BUILDDIR)/%.o,$(SOURCES)) +BUILDDEPS=$(patsubst %.c,$(BUILDDIR)/%.d,$(SOURCES)) -.DEFAULT_GOAL:= all -.PHONY: all clean deb tests install run run-cli run-tests dependencies +-include /etc/os-release -# ---- Dependencies enumeration ---------------------------------------------- # +# +# Directories +# +$(BUILDDIR): + @mkdir -p $@ + @echo -e ${CL2}[$@] ${CL}folder generated.${CL3} -dependencies: $(DEPS) +$(BINDIR): + @mkdir -p $@ + @echo -e ${CL2}[$@] ${CL}folder generated.${CL3} -$(BINDIR)/%.d: $(BINDIR)/%.o $(SRCDIR)/%.c - @$(CC) $(CFLAGS) -MM -MT $< $(word 2,$^) -MF $@ +# +# Dependencies +# +-include $(BUILDDEPS) -# ---- Tests enumeration ----------------------------------------------------- # -TEST_SCHEDULER=$(BINDIR)/tests/centers -TESTS=$(TEST_SCHEDULER) +$(BUILDDIR)/%.d: %.c | $(BUILDDIR) + @mkdir -p $(shell dirname $@) + @$(CC) -MM -MT $(@:%.d=%.o) -MF $@ $< + @echo -e ${CL2}[$@] ${CL}dependencies generated.${CL3} -$(BINDIR)/tests/centers: $(SRCDIR)/tests/centers.c - @echo "Compiling $<" - @$(CC) $(CCINCLUDES) $(CCOPTS) $(CCFLAGS) $(LDFLAGS) -o $@ $< +# +# Compilation +# +$(BINDIR)/gem-graph-server: $(BUILDBINS) | $(BINDIR) + @$(CC) -o $@ $(WARNINGS) $(DEBUG) $(OPTIMIZE) $^ $(INCLUDE) $(LIBS) + @echo -e ${CL2}[$@] ${CL}built.${CL3} -# ---- General recipes ------------------------------------------------------- # -$(BINDIR)/%.o: $(SRCDIR)/%.c - @echo "Compiling $<" - @$(CC) $(CCINCLUDES) $(CCOPTS) $(CCFLAGS) -c -o $@ $< +$(BUILDDIR)/%.o: %.c | $(BUILDDIR) + @mkdir -p $(shell dirname $@) + @$(CC) $(WARNINGS) $(DEBUG) $(OPTIMIZE) $(INCLUDE) -c $< -o $@ + @echo -e ${CL2}[$@] ${CL}compiled.${CL3} -# ---- Main recipe ----------------------------------------------------------- # -$(BINDIR)/main.o: $(SRCDIR)/main.c - @echo "Compiling $<" - @$(CC) $(CCINCLUDES) $(CCOPTS) $(CCFLAGS) -c -o $@ $< -$(BINDIR)/gem-graph-server: $(SERVEROBJ) - @echo "Building program to $@" - @$(CC) -o $@ $(SERVEROBJ) $(LDFLAGS) - @echo "Success!" - -$(BINDIR)/gem-graph-ctl: $(CLIOBJ) - @echo "Building program to $@" - @$(CC) -o $@ $(CLIOBJ) $(LDFLAGS) - @echo "Success!" - -# ---- Misc recipes ---------------------------------------------------------- # +# +# Virtual recipes +# clean: - -rm -f $(SRCDIR)/*.o $(BINDIR)/* $(BINDIR)/tests/* *.deb + @rm -rf $(BINDIR) + @rm -rf $(BUILDDIR) + @echo -e ${CL2}[$@] ${CL}done.${CL3} -all: dependencies $(BINDIR)/gem-graph-server $(BINDIR)/gem-graph-ctl - -tests: $(TESTS) - -# ---- Build debian package -------------------------------------------------- # -$(BINDIR)/gem-graph-server.deb: all $(DEBDIR)/control \ - $(DEBDIR)/conffiles \ - $(DEBDIR)/etc/server.conf \ - $(DEBDIR)/copyright - mkdir -p gem-graph-server/usr/bin - mkdir -p gem-graph-server/var/lib/gem-graph-server/models - mkdir -p gem-graph-server/var/lib/gem-graph-server/users - mkdir -p gem-graph-server/etc/gem-graph-server - mkdir -p gem-graph-server/usr/share/doc/gem-graph-server - mkdir -p gem-graph-server/DEBIAN - cp $(DEBDIR)/control gem-graph-server/DEBIAN/control - cp $(DEBDIR)/conffiles gem-graph-server/DEBIAN/conffiles - cp $(DEBDIR)/etc/* gem-graph-server/etc/gem-graph-server - cp $(BINDIR)/gem-graph-server gem-graph-server/usr/bin - cp $(BINDIR)/gem-graph-ctl gem-graph-server/usr/bin - cp $(DEBDIR)/copyright \ - gem-graph-server/usr/share/doc/gem-graph-server/copyright - dpkg-deb --build gem-graph-server - rm -rf gem-graph-server - -deb: $(BINDIR)/gem-graph-server.deb - -# ---- Builder will call this to install the application before running ------ # install: echo "Installing is not supported" -# ---- Builder uses this target to run the application ----------------------- # -run: all - @bin/gem-graph-server -C debian/etc -M debian/var/models \ - -U debian/var/users -run-gdb: all +build_system: + @echo -e ${CL2}[$@] ${CL}building...${CL3} + @make $(BINDIR)/gem-graph-server -j $(NTHREADS) + @echo -e ${CL2}[$@] ${CL}done.${CL3} + +run: build_system + @echo -e ${CL2}[$@] ${CL}executing...${CL3} @gdb bin/gem-graph-server \ -ex "set args -C debian/etc -M debian/var/models -U debian/var/users" + @echo -e ${CL2}[$@] ${CL}done.${CL3} -run-valgrind: all +debug: build_system + @echo -e ${CL2}[$@] ${CL}executing...${CL3} + @gdb $(BINDIR)/gem-graph-server + @echo -e ${CL2}[$@] ${CL}done.${CL3} + +valgrind: build_system + @echo -e ${CL2}[$@] ${CL}executing...${CL3} @valgrind --leak-check=full --show-leak-kinds=all -s \ bin/gem-graph-server -C debian/etc -M debian/var/models \ -U debian/var/users + @echo -e ${CL2}[$@] ${CL}done.${CL3} -run-both: all +run-both: build_system + @echo -e ${CL2}[$@] ${CL}executing...${CL3} @bin/gem-graph-server -C debian/etc -M debian/var/models \ -U debian/var/users & sleep 1 && bin/gem-graph-ctl -run-server: all + @echo -e ${CL2}[$@] ${CL}done.${CL3} +run-server: build_system + @echo -e ${CL2}[$@] ${CL}executing...${CL3} @bin/gem-graph-server -C debian/etc -M debian/var/models \ -U debian/var/users -run-cli: all - @bin/gem-graph-ctl -run-tests: tests - @bin/tests/scheduler + @echo -e ${CL2}[$@] ${CL}done.${CL3} + +all: build_system + @echo -e ${CL2}[$@] ${CL}done.${CL3} diff --git a/bin/.placeholder b/bin/.placeholder deleted file mode 100644 index e69de29..0000000 diff --git a/bin/tests/.placeholder b/bin/tests/.placeholder deleted file mode 100644 index e69de29..0000000 diff --git a/debian/conffiles b/debian/conffiles deleted file mode 100644 index 39f0001..0000000 --- a/debian/conffiles +++ /dev/null @@ -1 +0,0 @@ -/etc/gem-graph-server/server.conf diff --git a/debian/control b/debian/control deleted file mode 100644 index 8341870..0000000 --- a/debian/control +++ /dev/null @@ -1,10 +0,0 @@ -Package: gem-graph-server -Version: 0.0.1 -Section: custom -Priority: optional -Depends: libxml2 -Architecture: amd64 -Essential: no -Installed-Size: 112760 -Maintainer: Libre en Communs (contact@a-lec.org) -Description: The wonderful gem-graph computation server diff --git a/debian/copyright b/debian/copyright deleted file mode 100644 index 60bafbd..0000000 --- a/debian/copyright +++ /dev/null @@ -1,13 +0,0 @@ -This is a packacked upstream version of gem-graph-server - -The sources may be found at . - -Copyright © 2021 Libre en Communs (contact@a-lec.org) - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as - published by the Free Software Foundation, either version 3 of the - License, or (at your option) any later version. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . diff --git a/debian/etc/server.conf b/debian/etc/server.conf deleted file mode 100644 index 7cb97ca..0000000 --- a/debian/etc/server.conf +++ /dev/null @@ -1,2 +0,0 @@ -# CECI EST UN FICHIER DE CONFIGURATION -server_enabled=true diff --git a/debian/var/models/.placeholder b/debian/var/models/.placeholder deleted file mode 100644 index e69de29..0000000 diff --git a/debian/var/models/example_0.2.xml b/debian/var/models/example_0.2.xml deleted file mode 100644 index 849e357..0000000 --- a/debian/var/models/example_0.2.xml +++ /dev/null @@ -1,111 +0,0 @@ - - - - - - Modèle de test - - Gaston Lagaffe - - 2 - - 1629830000 - - 1.0 - - Ref - - - - - - 0 - 9 - - - - - 1 - - 3 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/debian/var/models/schemas/.placeholder b/debian/var/models/schemas/.placeholder deleted file mode 100644 index e69de29..0000000 diff --git a/debian/var/models/schemas/model_0.2.xmls b/debian/var/models/schemas/model_0.2.xmls deleted file mode 100644 index a4f255d..0000000 --- a/debian/var/models/schemas/model_0.2.xmls +++ /dev/null @@ -1,199 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/debian/var/users/.placeholder b/debian/var/users/.placeholder deleted file mode 100644 index e69de29..0000000 diff --git a/include/arrows.h b/include/arrows.h index ef887ff..27e9e74 100644 --- a/include/arrows.h +++ b/include/arrows.h @@ -27,33 +27,4 @@ /* -------------------------------------------------------------------------- */ -static inline int ArrowsInitLock(ArrowArray_t *arrowArray) -{ - return pthread_spin_init(&arrowArray->lock, PTHREAD_PROCESS_SHARED); -} -static inline int ArrowsDestroyLock(ArrowArray_t *arrowArray) -{ - return pthread_spin_destroy(&arrowArray->lock); -} - -static inline int ArrowsAcquireLock(ArrowArray_t *arrowArray) -{ - return pthread_spin_lock(&arrowArray->lock); -} - -static inline int ArrowsAcquireNonBlockingLock(ArrowArray_t *arrowArray) -{ - return pthread_spin_trylock(&arrowArray->lock); -} - -static inline int ArrowsReleaseLock(ArrowArray_t *arrowArray) -{ - return pthread_spin_unlock(&arrowArray->lock); -} - -Arrow_t *ArrowAdd(Scheduler_t *scheduler, int x, int y, int z, int siteId, - int weight); - -bool ArrowRemove(Scheduler_t *scheduler, int x, int y, int z, int siteId, - int weight); diff --git a/include/base.h b/include/base.h index c348dcf..8dec1f9 100644 --- a/include/base.h +++ b/include/base.h @@ -44,163 +44,4 @@ /* -------------------------------------------------------------------------- */ -struct IntArray_t { - size_t size; - int *space; -} typedef IntArray_t; -struct Arrow_t { - int x; - int y; - int z; - int siteId; - int weight; -} typedef Arrow_t; - -struct ArrowArray_t { - size_t size; - unsigned int freeSlotCount; - Arrow_t **freeSlots; - pthread_spinlock_t lock; - Arrow_t *array; -} typedef ArrowArray_t; - -struct Site_t { - char *label; - int nArrow; - Arrow_t *arrowsPtr; -} typedef Site_t; - -struct SpaceUnit_t { - int nSite; - char *label; - Site_t *sites; -} typedef SpaceUnit_t; - -struct Space_t { - int xMax; - int yMax; - int zMax; - size_t size; - SpaceUnit_t *space; -} typedef Space_t; - -/* -------------------------------------------------------------------------- */ - -// -// Scheduler -// -struct Scheduler_t { - pthread_t id; - bool pleaseStop; - Space_t *globalDrawingSpace; - IntArray_t *conditionTree; - ArrowArray_t *arrowArray; - int nMaxThread; - int nMaxCycles; - int ruleRadius; -} typedef Scheduler_t; - -/* -------------------------------------------------------------------------- */ - -// -// Local threads -// -struct Center_t { - int x; - int y; - int z; - struct Center_t *next; - struct Center_t *prev; -} typedef Center_t; - -struct Worker_t { - pthread_t id; - Center_t *localWorkAreaCenter; - Space_t *globalDrawingSpace; - IntArray_t *conditionTree; // XXX - ArrowArray_t *arrowArray; - bool pleaseStop; - bool terminated; - int returnValue; -} typedef Worker_t; - -/* -------------------------------------------------------------------------- */ - -// -// Server -// -struct Server_t { - pthread_t id; - bool pleaseStop; - int returnValue; - int sockfd; // Socket file descriptor -} typedef Server_t; - -struct ServerCommunication_t { - pthread_t id; - bool pleaseStop; - Server_t *associatedServer; - int sockfd; // Socket file descriptor - struct sockaddr_in clientAddr; - socklen_t sockLen; -} typedef ServerCommunication_t; - -/* -------------------------------------------------------------------------- */ - -// -// Supervisor -// -struct Supervisor_t { - pthread_t id; - bool pleaseStop; - int *workerCreationOccurency; // XXX ??? -} typedef Supervisor_t; - -/* -------------------------------------------------------------------------- */ - -// -// Model -// -struct Model_t { - // Identity - int id; - bool validated; // true if model has been correctly parsed - char *name; - char *owner; - char *owner_id; - time_t date; - char *version; - char *filename; - // Parameters - int space_xMax; // TODO more than 3 dimension ? - int space_yMax; - int space_zMax; - int nmaxThread; - int nmaxCycles; - int siteNumber; - // Model instance - bool isRunning; - Scheduler_t *scheduler; - Supervisor_t *supervisor; -} typedef Model_t; - -struct ModelField_t { - bool mandatory; // true if field must exist in file - char tag[25]; // XML tag attached to that field - char *destination; // data destination of that field - size_t size; // max size of data allowed - struct ModelField_t *son; // son in XML tree - struct ModelField_t *next; // brother in XML tree -} typedef ModelField_t; - -/* -------------------------------------------------------------------------- */ - -// -// Parameters -// -struct Parameters_t { - char *configDir; - char *modelDir; - char *userDir; -} typedef Parameters_t; diff --git a/include/centers.h b/include/centers.h index be2c188..27e9e74 100644 --- a/include/centers.h +++ b/include/centers.h @@ -27,12 +27,4 @@ /* -------------------------------------------------------------------------- */ -// -------------------------------------------------------------------------- // -// Add a center to the center list // -// -------------------------------------------------------------------------- // -Center_t *CenterAdd(Center_t *anyCenter, Center_t *newCenter); -// -------------------------------------------------------------------------- // -// Remove a center from the center list // -// -------------------------------------------------------------------------- // -void CenterRemove(Center_t *oldCenter); diff --git a/include/cmds.h b/include/cmds.h index 705f42b..f5e6293 100644 --- a/include/cmds.h +++ b/include/cmds.h @@ -32,19 +32,26 @@ /* -------------------------------------------------------------------------- */ -struct Command_t { +struct command_t { const char *name; char* (*execute)(char*, char**, Server_t*); const char *help; -} typedef Command_t; +} typedef command_t; /* -------------------------------------------------------------------------- */ // // Existing commands // -char *CmdModel(char*, char**, Server_t*); -char *CmdShutdown(char*, char**, Server_t*); -char *CmdHelp(char*, char**, Server_t*); +char *Cmd_model(char*, char**, Server_t*); +char *cmd_shutdown(char*, char**, Server_t*); +char *cmd_help(char*, char**, Server_t*); + +static command_t cmdList[] = +{ + {"help", cmd_help, "Help command"}, + {"model", cmd_model, "Model command"}, + {"shutdown", cmd_shutdown, "Shutdown command"}, +}; /* -------------------------------------------------------------------------- */ diff --git a/include/model.h b/include/model.h index 845b657..42d2946 100644 --- a/include/model.h +++ b/include/model.h @@ -27,6 +27,10 @@ #include #include +#include +#include +#include +#include /* -------------------------------------------------------------------------- */ @@ -47,44 +51,44 @@ // -------------------------------------------------------------------------- // // Model init function (and model discovery) // // -------------------------------------------------------------------------- // -void ModelSystemInit(Parameters_t *parameters); +void model_system_init (parameters_t *parameters); // -------------------------------------------------------------------------- // // Model stopping function // // -------------------------------------------------------------------------- // -void ModelSystemDestroy(void); +void model_system_shutdown (void); // -------------------------------------------------------------------------- // // Load a model ready to execute // // -------------------------------------------------------------------------- // -int ModelLoad(int id); +int model_load (int id); // -------------------------------------------------------------------------- // // Unload a model // // -------------------------------------------------------------------------- // -int ModelUnload(int id); +int model_unload (int id); // -------------------------------------------------------------------------- // // Add a model to the known model list // // -------------------------------------------------------------------------- // -void ModelAddToKnown(Model_t **newModel); +void model_add_to_known (model_t **newModel); // -------------------------------------------------------------------------- // // Print informations about all models (known and loaded) to the client // // -------------------------------------------------------------------------- // -void printModels(char *buf); +void print_models (char *buf); // -------------------------------------------------------------------------- // // Launch a model execution // // -------------------------------------------------------------------------- // -int ModelRun(int id); +int model_run (int id); // -------------------------------------------------------------------------- // // Stop a model execution // // -------------------------------------------------------------------------- // -int ModelStop(int id); +int model_stop (int id); // -------------------------------------------------------------------------- // // Stop and unload all loaded or running model // // -------------------------------------------------------------------------- // -void ModelShutdown(void); +void model_shutdown (void); diff --git a/include/parsing.h b/include/parsing.h index 128a1f9..06e9ec0 100644 --- a/include/parsing.h +++ b/include/parsing.h @@ -36,38 +36,4 @@ /* -------------------------------------------------------------------------- */ -struct ModelParserTableXML_t; -struct ParserTableXML_t -{ - const xmlChar *tag; - int (*parse) (xmlDoc*, - struct ModelParserTableXML_t*, - int currentParser, - xmlNode*); - void *destination; -} typedef ParserTableXML_t; - -struct ModelParserTableXML_t -{ - size_t len; - ParserTableXML_t *table; -} typedef ModelParserTableXML_t; - -/* -------------------------------------------------------------------------- */ - -int parseParentFieldXML(xmlDocPtr, - ModelParserTableXML_t*, - int, - xmlNodePtr); - -int parseTextField(xmlDocPtr, - ModelParserTableXML_t*, - int, - xmlNodePtr); - -int ParseModelXML(Model_t*); - -int ParseModelIdentityXML(Model_t*, Parameters_t*); - -/* -------------------------------------------------------------------------- */ diff --git a/include/scheduler.h b/include/scheduler.h index 83e1a95..0985ce1 100644 --- a/include/scheduler.h +++ b/include/scheduler.h @@ -32,47 +32,40 @@ // -------------------------------------------------------------------------- // // Scheduler init function // // -------------------------------------------------------------------------- // -void SchedInit(Scheduler_t *scheduler); +void sched_init(scheduler_t *scheduler); // -------------------------------------------------------------------------- // // Scheduler content destructor function // // -------------------------------------------------------------------------- // -static inline void SchedContentDestroy(Scheduler_t *scheduler) +static inline void sched_clean (scheduler_t *scheduler) { - if (scheduler) { - if (scheduler->globalDrawingSpace) { - if (scheduler->globalDrawingSpace->space) { - free(scheduler->globalDrawingSpace->space); - scheduler->globalDrawingSpace->space = NULL; - } - free(scheduler->globalDrawingSpace); - scheduler->globalDrawingSpace = NULL; + assert(scheduler); + + if (scheduler->globalDrawingSpace) { + if (scheduler->globalDrawingSpace->space) { + free(scheduler->globalDrawingSpace->space); + scheduler->globalDrawingSpace->space = NULL; } - if (scheduler->arrowArray) { - if (scheduler->arrowArray->array) { - free(scheduler->arrowArray->array); - scheduler->arrowArray->array = NULL; - } - free(scheduler->arrowArray); - scheduler->arrowArray = NULL; + free(scheduler->globalDrawingSpace); + scheduler->globalDrawingSpace = NULL; + } + + if (scheduler->arrowArray) { + if (scheduler->arrowArray->array) { + free(scheduler->arrowArray->array); + scheduler->arrowArray->array = NULL; } + free(scheduler->arrowArray); + scheduler->arrowArray = NULL; } } // -------------------------------------------------------------------------- // // Scheduler destructor function // // -------------------------------------------------------------------------- // -static inline void SchedDestroy(Scheduler_t *scheduler) +static inline void sched_shutdown (scheduler_t *scheduler) { - if (scheduler) { - free(scheduler); - } -} - -// -------------------------------------------------------------------------- // -// Scheduler wait function // -// -------------------------------------------------------------------------- // -static inline void SchedWait(Scheduler_t *scheduler) -{ - pthread_join(scheduler->id, NULL); + assert(scheduler); + free(scheduler); + scheduler = NULL; } diff --git a/include/server.h b/include/server.h index 203938d..92e53a3 100644 --- a/include/server.h +++ b/include/server.h @@ -37,12 +37,4 @@ // -------------------------------------------------------------------------- // // Server init function // // -------------------------------------------------------------------------- // -void ServerInit(Server_t *server); - -// -------------------------------------------------------------------------- // -// Server wait function // -// -------------------------------------------------------------------------- // -static inline void ServerWait(Server_t *server) -{ - pthread_join(server->id, NULL); -} +void server_init (server_t *server); diff --git a/include/supervisor.h b/include/supervisor.h index 6605c87..144d296 100644 --- a/include/supervisor.h +++ b/include/supervisor.h @@ -30,19 +30,12 @@ // -------------------------------------------------------------------------- // // Supervisor init function // // -------------------------------------------------------------------------- // -void SupervisorInit(Supervisor_t *supervisor); +void superv_init (supervisor_t *supervisor); // -------------------------------------------------------------------------- // // Supervisor destructor function // // -------------------------------------------------------------------------- // -static inline void SupervisorDestroy(Supervisor_t *supervisor) +static inline void superv_destroy (supervisor_t *supervisor) { -} - -// -------------------------------------------------------------------------- // -// Supervisor wait function // -// -------------------------------------------------------------------------- // -static inline void SupervisorWait(Supervisor_t *supervisor) -{ - pthread_join(supervisor->id, NULL); + ; } diff --git a/include/terminal.h b/include/terminal.h index 436e6b1..c897e49 100644 --- a/include/terminal.h +++ b/include/terminal.h @@ -54,14 +54,14 @@ /* -------------------------------------------------------------------------- */ -volatile struct winsize TermWindowSize; +volatile struct winsize termWinSize; /* -------------------------------------------------------------------------- */ // -------------------------------------------------------------------------- // // Get a character code from the keyboard // // -------------------------------------------------------------------------- // -static inline int TermGetch(bool nonBlocking) +static inline int term_getch(bool nonBlocking) { int buf = 0; // old terminal @@ -123,16 +123,16 @@ static inline int TermGetch(bool nonBlocking) // -------------------------------------------------------------------------- // // Get the screen size // // -------------------------------------------------------------------------- // -static inline void TermGetScreenSize(int signum) +static inline void term_get_screensize(int signum) { // Get current terminal size - ioctl(STDOUT_FILENO, TIOCGWINSZ, &TermWindowSize); + ioctl(STDOUT_FILENO, TIOCGWINSZ, &termWinSize); } // -------------------------------------------------------------------------- // // Set cursor location // // -------------------------------------------------------------------------- // -static inline void TermSetCursorLocation(char x, char y) +static inline void term_set_cursor_location(char x, char y) { printf("\x1b[%d;%dH", y, x); } @@ -140,7 +140,7 @@ static inline void TermSetCursorLocation(char x, char y) // -------------------------------------------------------------------------- // // Save cursor location // // -------------------------------------------------------------------------- // -static inline void TermSaveCursorLocation(void) +static inline void term_save_cursor_location(void) { printf(C_SAVE_CURSORPOS); } @@ -148,7 +148,7 @@ static inline void TermSaveCursorLocation(void) // -------------------------------------------------------------------------- // // Restore cursor location // // -------------------------------------------------------------------------- // -static inline void TermRestoreCursorLocation(void) +static inline void term_restore_cursor_location(void) { printf(C_RESTORE_CURSORPOS); } @@ -156,7 +156,7 @@ static inline void TermRestoreCursorLocation(void) // -------------------------------------------------------------------------- // // Move cursor location to the right // // -------------------------------------------------------------------------- // -static inline void TermMoveCursorForward(void) +static inline void term_move_cursor_forward(void) { printf(C_CURSORRIGHT); } @@ -164,7 +164,7 @@ static inline void TermMoveCursorForward(void) // -------------------------------------------------------------------------- // // Move cursor location to the left // // -------------------------------------------------------------------------- // -static inline void TermMoveCursorBackward(void) +static inline void term_move_cursor_backward(void) { printf(C_CURSORLEFT); } @@ -172,10 +172,10 @@ static inline void TermMoveCursorBackward(void) // -------------------------------------------------------------------------- // // Clear screen // // -------------------------------------------------------------------------- // -static inline void TermClearScreen(void) +static inline void term_clear_screen(void) { printf(C_CLEARSCREEN); - TermSetCursorLocation(1,1); + term_set_cursor_location(1,1); fflush(stdout); } diff --git a/include/worker.h b/include/worker.h index c80a144..bee79c6 100644 --- a/include/worker.h +++ b/include/worker.h @@ -30,12 +30,12 @@ // -------------------------------------------------------------------------- // // Worker init function // // -------------------------------------------------------------------------- // -void WorkerInit(Worker_t *worker); +void worker_init(worker_t *worker); // -------------------------------------------------------------------------- // // Worker destructor function // // -------------------------------------------------------------------------- // -static inline void WorkerDestroy(Worker_t *worker) +static inline void worker_destroy(worker_t *worker) { worker->pleaseStop = false; worker->terminated = false; @@ -45,11 +45,3 @@ static inline void WorkerDestroy(Worker_t *worker) free(worker->localWorkAreaCenter); worker->localWorkAreaCenter = NULL; } - -// -------------------------------------------------------------------------- // -// Worker wait function // -// -------------------------------------------------------------------------- // -static inline void WorkerWait(Worker_t *worker) -{ - pthread_join(worker->id, NULL); -} diff --git a/include/xml.h b/include/xml.h deleted file mode 100644 index 4809a57..0000000 --- a/include/xml.h +++ /dev/null @@ -1,35 +0,0 @@ -//=-------------------------------------------------------------------------=// -// XML management tools // -// // -// Copyright © 2021 Libre en Communs (contact@a-lec.org) // -// Copyright © 2021 Adrien Bourmault (neox@a-lec.org) // -// // -// This file is part of gem-graph. // -// // -// This program is free software: you can redistribute it and/or modify // -// it under the terms of the GNU Affero General Public License as // -// published by the Free Software Foundation, either version 3 of the // -// License, or (at your option) any later version. // -// // -// This program is distributed in the hope that it will be useful, // -// but WITHOUT ANY WARRANTY; without even the implied warranty of // -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // -// GNU Affero General Public License for more details. // -// // -// You should have received a copy of the GNU Affero General Public License // -// along with this program. If not, see . // -//=-------------------------------------------------------------------------=// - -#pragma once -#ifndef BASE_H - #include "../include/base.h" -#endif - -/* -------------------------------------------------------------------------- */ - -#include -#include -#include -#include - - diff --git a/manifest.scm b/manifest.scm new file mode 100644 index 0000000..01a3a6d --- /dev/null +++ b/manifest.scm @@ -0,0 +1,26 @@ +;; +;; Dépendances sous GNU Guix +;; + + +(specifications->manifest + (list + "bash" + "coreutils" + "gcc-toolchain" + "pkg-config" + "valgrind" + "findutils" + "gdb" + "make" + "libxml2" + "glu" + "glew" + "glfw" + "cglm" + "libepoxy" + "glib" + "mesa-headers" + "mesa" + ) +) diff --git a/src/arrows.c b/src/arrows.c index d24e219..79f8f7f 100644 --- a/src/arrows.c +++ b/src/arrows.c @@ -25,7 +25,3 @@ /* -------------------------------------------------------------------------- */ - -static inline int location(Space_t *space, int x, int y, int z) { - return x + y * (space->xMax+1) + z * (space->xMax+1) * (space->zMax+1); -} diff --git a/src/centers.c b/src/centers.c index 2eba517..f9fdcdb 100644 --- a/src/centers.c +++ b/src/centers.c @@ -24,31 +24,4 @@ /* -------------------------------------------------------------------------- */ -Center_t *CenterAdd(Center_t *anyCenter, Center_t *newCenter) -{ - if (!newCenter) return NULL; - if (anyCenter->next) { - anyCenter->next->prev = newCenter; - } - newCenter->next = anyCenter->next; - anyCenter->next = newCenter; - newCenter->prev = anyCenter; - return newCenter; -} - -void CenterRemove(Center_t *oldCenter) -{ - register Center_t *prev; - register Center_t *next; - - //printLog("Removing center %p\n", oldCenter); - if (!oldCenter) return; - - prev = oldCenter->prev; - next = oldCenter->next; - - - if (prev) prev->next = oldCenter->next; - if (next) next->prev = oldCenter->prev; -} diff --git a/src/cli.c b/src/cli/cli.c similarity index 100% rename from src/cli.c rename to src/cli/cli.c diff --git a/src/cmds.c b/src/cmds.c index 4940869..c4ea45f 100644 --- a/src/cmds.c +++ b/src/cmds.c @@ -28,195 +28,195 @@ /* -------------------------------------------------------------------------- */ -char *CmdModel(char *buf, char **argv, Server_t *args) -{ - int id, eid; - Model_t *newModel; +/* char *cmd_model(char *buf, char **argv, server_t *args) */ +/* { */ +/* int id, eid; */ +/* model_t *newModel; */ - // invalid use - if (!argv[1]) { - strcat(buf, "{create | delete | load | unload | run | stop | list |" - "info}\n"); - goto CmdModelEnd; - } +/* // invalid use */ +/* if (!argv[1]) { */ +/* strcat(buf, "{create | delete | load | unload | run | stop | list |" */ +/* "info}\n"); */ +/* goto cmd_ModelEnd; */ +/* } */ - if (strcmp(argv[1], "create") == 0) { +/* if (strcmp(argv[1], "create") == 0) { */ - if (!argv[2] || !argv[3]) { - goto createEnd; - } +/* if (!argv[2] || !argv[3]) { */ +/* goto createEnd; */ +/* } */ - if (strncmp(argv[2], "name=", 5) == 0) { +/* if (strncmp(argv[2], "name=", 5) == 0) { */ - if (strncmp(argv[3], "file=", 5) == 0) { - // TODO get the file content (sent by the client) from args +/* if (strncmp(argv[3], "file=", 5) == 0) { */ +/* // TODO get the file content (sent by the client) from args */ - // Creating model - ModelAddToKnown(&newModel); +/* // Creating model */ +/* //XXX ModelAddToKnown(&newModel); */ - // TODO modify model according to things in file +/* // TODO modify model according to things in file */ - // Write name - strcpy(newModel->name, argv[2] + 5); +/* // Write name */ +/* strcpy(newModel->name, argv[2] + 5); */ - // Write filename - strcpy(newModel->filename, argv[3] + 5); +/* // Write filename */ +/* strcpy(newModel->filename, argv[3] + 5); */ - sprintf(buf + strlen(buf), "Model %s created with id %d\n", - newModel->name, newModel->id); - goto CmdModelEnd; +/* sprintf(buf + strlen(buf), "Model %s created with id %d\n", */ +/* newModel->name, newModel->id); */ +/* goto cmd_ModelEnd; */ - } else { - goto createEnd; - } - } else { - goto createEnd; - } +/* } else { */ +/* goto createEnd; */ +/* } */ +/* } else { */ +/* goto createEnd; */ +/* } */ - createEnd: - // invalid use - strcat(buf, "Creates a model structure\n"); - strcat(buf, "Usage: model create name=NAME file=FILENAME\n"); - goto CmdModelEnd; - } +/* createEnd: */ +/* // invalid use */ +/* strcat(buf, "Creates a model structure\n"); */ +/* strcat(buf, "Usage: model create name=NAME file=FILENAME\n"); */ +/* goto cmd_ModelEnd; */ +/* } */ - else if (strcmp(argv[1], "load") == 0) { - if (!argv[2]) { - goto loadEnd; - } +/* else if (strcmp(argv[1], "load") == 0) { */ +/* if (!argv[2]) { */ +/* goto loadEnd; */ +/* } */ - id = (int) strtol(argv[2] + 3, NULL, 10); +/* id = (int) strtol(argv[2] + 3, NULL, 10); */ - if (id == 0 || (eid = ModelLoad(id)) <= 0) { - sprintf(buf + strlen(buf), "Failed to load model id %d\n", id); - goto CmdModelEnd; - } +/* if (id == 0 || (eid = ModelLoad(id)) <= 0) { */ +/* sprintf(buf + strlen(buf), "Failed to load model id %d\n", id); */ +/* goto cmd_ModelEnd; */ +/* } */ - sprintf(buf + strlen(buf), "Model id %d loaded with effective " - "id %d\n", id, eid); - goto CmdModelEnd; - loadEnd: - // invalid use - strcat(buf, "Loads a model structure\n"); - strcat(buf, "Usage: model load id=ID\n"); - goto CmdModelEnd; - } +/* sprintf(buf + strlen(buf), "Model id %d loaded with effective " */ +/* "id %d\n", id, eid); */ +/* goto cmd_ModelEnd; */ +/* loadEnd: */ +/* // invalid use */ +/* strcat(buf, "Loads a model structure\n"); */ +/* strcat(buf, "Usage: model load id=ID\n"); */ +/* goto cmd_ModelEnd; */ +/* } */ - else if (strcmp(argv[1], "unload") == 0) { - if (!argv[2]) { - goto unloadEnd; - } +/* else if (strcmp(argv[1], "unload") == 0) { */ +/* if (!argv[2]) { */ +/* goto unloadEnd; */ +/* } */ - id = (int) strtol(argv[2] + 3, NULL, 10); +/* id = (int) strtol(argv[2] + 3, NULL, 10); */ - if (id == 0 || ModelUnload(id) < 0) { - printErr("Failed to unload model id %d\n", id); - sprintf(buf + strlen(buf), "Failed to unload model id %d\n", id); - goto CmdModelEnd; - } +/* if (id == 0 || ModelUnload(id) < 0) { */ +/* printErr("Failed to unload model id %d\n", id); */ +/* sprintf(buf + strlen(buf), "Failed to unload model id %d\n", id); */ +/* goto cmd_ModelEnd; */ +/* } */ - sprintf(buf + strlen(buf), "Model id %d unloaded\n", id); - goto CmdModelEnd; - unloadEnd: - // invalid use - strcat(buf, "Unloads a model structure\n"); - strcat(buf, "Usage: model unload id=ID\n"); - goto CmdModelEnd; - } +/* sprintf(buf + strlen(buf), "Model id %d unloaded\n", id); */ +/* goto cmd_ModelEnd; */ +/* unloadEnd: */ +/* // invalid use */ +/* strcat(buf, "Unloads a model structure\n"); */ +/* strcat(buf, "Usage: model unload id=ID\n"); */ +/* goto cmd_ModelEnd; */ +/* } */ - else if (strcmp(argv[1], "delete") == 0) { - if (!argv[2]) { - goto deleteEnd; - } - // TODO delete model - deleteEnd: - // invalid use - strcat(buf, "Deletes a model structure\n"); - strcat(buf, "Usage: model delete id=ID\n"); - goto CmdModelEnd; - } +/* else if (strcmp(argv[1], "delete") == 0) { */ +/* if (!argv[2]) { */ +/* goto deleteEnd; */ +/* } */ +/* // TODO delete model */ +/* deleteEnd: */ +/* // invalid use */ +/* strcat(buf, "Deletes a model structure\n"); */ +/* strcat(buf, "Usage: model delete id=ID\n"); */ +/* goto cmd_ModelEnd; */ +/* } */ - else if (strcmp(argv[1], "run") == 0) { - if (!argv[2]) { - goto runEnd; - } +/* else if (strcmp(argv[1], "run") == 0) { */ +/* if (!argv[2]) { */ +/* goto runEnd; */ +/* } */ - id = (int) strtol(argv[2] + 3, NULL, 10); +/* id = (int) strtol(argv[2] + 3, NULL, 10); */ - if (id == 0 || (!ModelRun(id))) { - sprintf(buf + strlen(buf), "Failed to run model id %d\n", id); - goto CmdModelEnd; - } +/* if (id == 0 || (!ModelRun(id))) { */ +/* sprintf(buf + strlen(buf), "Failed to run model id %d\n", id); */ +/* goto cmd_ModelEnd; */ +/* } */ - sprintf(buf + strlen(buf), "Model id %d is running\n", id); - goto CmdModelEnd; - runEnd: - // invalid use - strcat(buf, "Run a model simulation\n"); - strcat(buf, "Usage: model run id=ID\n"); - goto CmdModelEnd; - } +/* sprintf(buf + strlen(buf), "Model id %d is running\n", id); */ +/* goto cmd_ModelEnd; */ +/* runEnd: */ +/* // invalid use */ +/* strcat(buf, "Run a model simulation\n"); */ +/* strcat(buf, "Usage: model run id=ID\n"); */ +/* goto cmd_ModelEnd; */ +/* } */ - else if (strcmp(argv[1], "stop") == 0) { - if (!argv[2]) { - goto stopEnd; - } +/* else if (strcmp(argv[1], "stop") == 0) { */ +/* if (!argv[2]) { */ +/* goto stopEnd; */ +/* } */ - id = (int) strtol(argv[2] + 3, NULL, 10); +/* id = (int) strtol(argv[2] + 3, NULL, 10); */ - if (id == 0 || (!ModelStop(id))) { - sprintf(buf + strlen(buf), "Failed to stop model id %d\n", id); - goto CmdModelEnd; - } +/* if (id == 0 || (!ModelStop(id))) { */ +/* sprintf(buf + strlen(buf), "Failed to stop model id %d\n", id); */ +/* goto cmd_ModelEnd; */ +/* } */ - sprintf(buf + strlen(buf), "Model id %d is running\n", id); - goto CmdModelEnd; - stopEnd: - // invalid use - strcat(buf, "Stop a model simulation\n"); - strcat(buf, "Usage: model stop id=ID\n"); - goto CmdModelEnd; - } +/* sprintf(buf + strlen(buf), "Model id %d is running\n", id); */ +/* goto cmd_ModelEnd; */ +/* stopEnd: */ +/* // invalid use */ +/* strcat(buf, "Stop a model simulation\n"); */ +/* strcat(buf, "Usage: model stop id=ID\n"); */ +/* goto cmd_ModelEnd; */ +/* } */ - else if (strcmp(argv[1], "list") == 0) { - strcat(buf, "You asked for us to list models\n"); - printModels(buf); - } +/* else if (strcmp(argv[1], "list") == 0) { */ +/* strcat(buf, "You asked for us to list models\n"); */ +/* printModels(buf); */ +/* } */ - else if (strcmp(argv[1], "info") == 0) { - if (!argv[2]) { - goto infoEnd; - } - // TODO info model - infoEnd: - // invalid use - strcat(buf, "Print info about a model\n"); - strcat(buf, "Usage: model info id=ID\n"); - goto CmdModelEnd; - } +/* else if (strcmp(argv[1], "info") == 0) { */ +/* if (!argv[2]) { */ +/* goto infoEnd; */ +/* } */ +/* // TODO info model */ +/* infoEnd: */ +/* // invalid use */ +/* strcat(buf, "Print info about a model\n"); */ +/* strcat(buf, "Usage: model info id=ID\n"); */ +/* goto cmd_ModelEnd; */ +/* } */ - // invalid use - else strcat(buf, "{create | delete | load | unload | run | stop | list |" - "info}\n"); +/* // invalid use */ +/* else strcat(buf, "{create | delete | load | unload | run | stop | list |" */ +/* "info}\n"); */ -CmdModelEnd: - return buf; -} +/* cmd_ModelEnd: */ +/* return buf; */ +/* } */ -char *CmdShutdown(char *buf, char **argv, Server_t *args) -{ - args->pleaseStop = true; - strcat(buf, "Server stopping\n"); +/* char *cmd_shutdown(char *buf, char **argv, server_t *args) */ +/* { */ +/* args->pleaseStop = true; */ +/* strcat(buf, "Server stopping\n"); */ - ModelShutdown(); - strcat(buf, "All model shutted down\n"); +/* //XXX ModelShutdown(); */ +/* strcat(buf, "All model shutted down\n"); */ - return buf; -} +/* return buf; */ +/* } */ -char *CmdHelp(char *buf, char **argv, Server_t *args) -{ - strcat(buf, "List of known commands:\n"); +/* char *cmd_help(char *buf, char **argv, server_t *args) */ +/* { */ +/* strcat(buf, "List of known commands:\n"); */ - return buf; -} +/* return buf; */ +/* } */ diff --git a/src/main.c b/src/main.c index 42ded6c..2c004dd 100644 --- a/src/main.c +++ b/src/main.c @@ -26,23 +26,23 @@ /* -------------------------------------------------------------------------- */ -static Server_t *server; +static server_t *server; -static void sigtermHandler(int signum) -{ - server->pleaseStop = true; - printLog("Server stopping\n"); +/* static void sig_term_handler(int signum) */ +/* { */ +/* server->pleaseStop = true; */ +/* printLog("Server stopping\n"); */ - ModelShutdown(); - printLog("All model shutted down\n"); -} +/* //XXX ModelShutdown(); */ +/* printLog("All model shutted down\n"); */ +/* } */ int main(int argc, char **argv) { int options; time_t t; int returnValue = 0; - Parameters_t parameters; + parameters_t parameters; while ((options = getopt(argc, argv, ":C:M:U:")) != -1) { switch (options) { @@ -108,30 +108,30 @@ int main(int argc, char **argv) // Go! printLog("Starting gem-graph-server...\n"); - // Register new interrupt handler - signal(SIGINT, sigtermHandler); - signal(SIGTERM, sigtermHandler); + /* // Register new interrupt handler */ + /* signal(SIGINT, sig_term_handler); */ + /* signal(SIGTERM, sig_term_handler); */ - // Initializing random generator - t = time(&t); - srand((unsigned) t); + /* // Initializing random generator */ + /* t = time(&t); */ + /* srand((unsigned) t); */ - server = calloc(1, sizeof(*server)); + /* server = calloc(1, sizeof(*server)); */ - // Initializing model system - ModelSystemInit(¶meters); + /* // Initializing model system */ + /* model_system_init(¶meters); */ - // Launching server - ServerInit(server); + /* // Launching server */ + /* server_init(server); */ - // Waiting for termination - ServerWait(server); + /* // Waiting for termination */ + /* server_wait(server); */ - // Exiting - returnValue |= server->returnValue; - ModelSystemDestroy(); - free(server); - server = NULL; + /* // Exiting */ + /* returnValue |= server->returnValue; */ + /* ModelSystemDestroy(); */ + /* free(server); */ + /* server = NULL; */ free(parameters.userDir); free(parameters.modelDir); diff --git a/src/model.c b/src/model.c index db08dbd..0070ade 100644 --- a/src/model.c +++ b/src/model.c @@ -25,286 +25,4 @@ #include "../include/arrows.h" #include "../include/scheduler.h" -static Model_t **loadedModel; -static int loadedModelSize; // begins to 1 -static Model_t **knownModel; -static int knownModelSize; // begins to 1 - -Model_t *lastModel; -Model_t **lastModelAddr; - -/* -------------------------------------------------------------------------- */ - -void printModels(char *buf) -{ - sprintf(buf + strlen(buf),"Known models\n"); - for (int i = 0; i <= knownModelSize-1; i++) { - sprintf(buf + strlen(buf), "id: %d, addr: %p, name: %s, date: %lu, " - "owner: %s\n", - knownModel[i]->id, - knownModel[i], - knownModel[i]->name, - knownModel[i]->date, - knownModel[i]->owner - ); - } - - sprintf(buf + strlen(buf), "\nLoaded models\n"); - for (int i = 0; i <= loadedModelSize-1; i++) { - sprintf(buf + strlen(buf), "id: %d, addr: %p, name: %s\n", - loadedModel[i]->id, loadedModel[i], loadedModel[i]->name); - } -} - -int ModelLoad(int id) -{ - if (id <= 0 || id > knownModelSize) { - return -1; - } - - printLog("Loading model id %d (/%d models)...\n", id, knownModelSize); - - - // Creating structure for the Scheduler - knownModel[id-1]->scheduler = - calloc(1, sizeof(*knownModel[id-1]->scheduler)); - - loadedModelSize++; - - loadedModel = - (Model_t**) realloc(loadedModel, loadedModelSize * sizeof(Model_t*)); - - loadedModel[loadedModelSize-1] = knownModel[id-1]; - - // Parse model - ParseModelXML(loadedModel[loadedModelSize-1]); - - return loadedModelSize; -} - -int ModelUnload(int id) -{ - // Destroy scheduler - if (id > loadedModelSize) - return -1; - - SchedContentDestroy(loadedModel[id-1]->scheduler); - - SchedDestroy(loadedModel[id-1]->scheduler); - loadedModel[id-1]->scheduler = NULL; - - // Prevent fragmentation by moving data in the newly freed slot - if (id-1 < loadedModelSize) { - memmove(&loadedModel[id-1], - &loadedModel[id-1] + sizeof(Model_t*), - loadedModelSize - id); - } - - // Decrement loaded model index - loadedModelSize--; - - // Resize loaded model list - loadedModel = - (Model_t**) realloc(loadedModel, loadedModelSize * sizeof(Model_t*)); - - return 0; -} -/* -------------------------------------------------------------------------- */ - -int ModelRun(int id) -{ - if (id <= 0 || id > loadedModelSize) - return -1; - - if (!loadedModel[id-1]->scheduler) - return -2; - - if (loadedModel[id-1]->isRunning) - return -3; - - loadedModel[id-1]->scheduler->nMaxThread = knownModel[id-1]->nmaxThread; - loadedModel[id-1]->scheduler->nMaxCycles = knownModel[id-1]->nmaxCycles; - loadedModel[id-1]->scheduler->pleaseStop = false; - loadedModel[id-1]->isRunning = true; - - SchedInit(loadedModel[id-1]->scheduler); - - printLog("Model %d launched\n", id); - return 0; -} - -int ModelStop(int id) -{ - if (id <= 0 || id > loadedModelSize) { - return -1; - } - if (!loadedModel[id-1]->scheduler) { - return -2; - } - if (!loadedModel[id-1]->isRunning) { - return -3; - } - - // Stop model scheduler - loadedModel[id-1]->scheduler->pleaseStop = true; - printLog("Model %d stop bit set\n", id); - - // Wait for Shceduler to stop - SchedWait(loadedModel[id-1]->scheduler); - - // Disable running bit - loadedModel[id-1]->isRunning = false; - return 0; -} - -/* -------------------------------------------------------------------------- */ - -void ModelAddToKnown(Model_t **newModel) -{ - // increment index - knownModelSize++; - // create socket - knownModel = - (Model_t**) realloc(knownModel, knownModelSize * sizeof(*knownModel)); - // populate socket - knownModel[knownModelSize-1] = calloc(1, sizeof(**knownModel)); - // populate model - knownModel[knownModelSize-1]->id = knownModelSize; - // return value - *newModel = knownModel[knownModelSize-1]; - lastModel = knownModel[knownModelSize-1]; - lastModelAddr = &knownModel[knownModelSize-1]; - - // continue. model population - knownModel[knownModelSize-1]->name = - calloc(1, sizeof(char) * MODEL_STRING_SIZE); - - knownModel[knownModelSize-1]->filename = - calloc(1, sizeof(char) * MODEL_STRING_SIZE); - - knownModel[knownModelSize-1]->owner = - calloc(1, sizeof(char) * MODEL_STRING_SIZE); - - knownModel[knownModelSize-1]->version = - calloc(1, sizeof(char) * MODEL_STRING_SIZE); - - knownModel[knownModelSize-1]->space_xMax = XMAX; - knownModel[knownModelSize-1]->space_yMax = YMAX; - knownModel[knownModelSize-1]->space_zMax = ZMAX; - knownModel[knownModelSize-1]->nmaxThread = MAX_THREAD; - knownModel[knownModelSize-1]->nmaxCycles = MAX_CYCLES; - knownModel[knownModelSize-1]->siteNumber = 0; -} - -void ModelDelete(int id) //XXX fragmentation -{ - // Free a model structure - free(knownModel[id-1]->name); - knownModel[id-1]->name = NULL; - - free(knownModel[id-1]->filename); - knownModel[id-1]->filename = NULL; - - free(knownModel[id-1]->owner); - knownModel[id-1]->owner = NULL; - - free(knownModel[id-1]->version); - knownModel[id-1]->version = NULL; - - free(knownModel[id-1]); - knownModel[id-1] = NULL; -} - -void ModelShutdown(void) -{ - // Stop each model from running - for (int i = 0; i < loadedModelSize; i++) { - ModelStop(i); - ModelUnload(i); - } -} - -/* -------------------------------------------------------------------------- */ - -void ModelSystemInit(Parameters_t *parameters) -{ - struct dirent *modelDirEntry = NULL; - DIR *modelDir = NULL; - Model_t *newModel; - char *extensionPosition; - - loadedModel = calloc(1, sizeof(*loadedModel)); - - knownModel = calloc(1, sizeof(*knownModel)); - - knownModelSize = 0; - loadedModelSize = 0; - - printLog("Model system initiated with folder : %s\n", parameters->modelDir); - - // Open model directory - if ((modelDir = opendir(parameters->modelDir)) <= 0) { - printErr("Could not open %s\n", parameters->modelDir); - ModelSystemDestroy(); - kill(getpid(), SIGTERM); - return; - } - - while ((modelDirEntry = readdir(modelDir)) != NULL) { - if ((extensionPosition = strstr(modelDirEntry->d_name, ".xml"))) { - - // Creating model - ModelAddToKnown(&newModel); - - // Write file path in filename - strncpy(newModel->filename, parameters->modelDir, - strlen(parameters->modelDir)); - // Add a / separator - strcat(newModel->filename - + strlen(parameters->modelDir), - "/"); - // Add the file relative name - strncpy(newModel->filename - + strlen(parameters->modelDir) - + 1, - modelDirEntry->d_name, - strlen(modelDirEntry->d_name)); - - // Write model name - strncpy(newModel->name, modelDirEntry->d_name, - extensionPosition - modelDirEntry->d_name); - - // Ask to parse the new model - if (ParseModelIdentityXML(newModel, parameters) != 0) { - printErr("Deleting invalid model %s from known list\n", - newModel->name); - ModelDelete(newModel->id); - continue; - }; - - // Check model is valid and/or parsed - if (newModel->validated == false) { - printErr("Deleting invalid model %s from known list\n", - newModel->name); - ModelDelete(newModel->id); - continue; - } - - // Succeeded ! - printLog("Loaded model %s\n", newModel->name); - } - } - free(modelDir); -} - -void ModelSystemDestroy(void) -{ - for (int i = 0; i < loadedModelSize; i++) { - ModelDelete(i); - } - free(loadedModel); - loadedModel = NULL; - free(knownModel); - knownModel = NULL; -} diff --git a/src/parsing.c b/src/parsing.c index 35839cb..ec88c89 100644 --- a/src/parsing.c +++ b/src/parsing.c @@ -24,560 +24,4 @@ #include "../include/arrows.h" #include "../include/model.h" -// -------------------------------------------------------------------------- // -// Parsing NOTHING (but yeah that prints) // -// -------------------------------------------------------------------------- // -int parseStubFieldXML (xmlDocPtr doc, - ModelParserTableXML_t *ModelTable, - int currentParser, - xmlNodePtr currentNode) -{ - xmlChar *content, *contentText, *contentValueAttribute; - contentText = xmlNodeListGetString(doc, currentNode->xmlChildrenNode, 1); - contentValueAttribute = xmlGetProp(currentNode, - (xmlChar*)"value"); - - if (contentText) { - content = contentText; - } else if (contentValueAttribute) { - content = contentValueAttribute; - - // Detect children - if (currentNode->xmlChildrenNode) { - printLog("%s (stub) has children\n", - ModelTable->table[currentParser].tag); - } - } - - printLog("%s (stub): %s\n", ModelTable->table[currentParser].tag, - content); - - if (!content) { - xmlFree(contentText); - xmlFree(contentValueAttribute); - return -1; - } - - xmlFree(contentText); - xmlFree(contentValueAttribute); - - return 0; -} - -// -------------------------------------------------------------------------- // -// Parsing an arrow (but yeah that prints) // -// -------------------------------------------------------------------------- // -int parseArrowFieldXML (xmlDocPtr doc, - ModelParserTableXML_t *ModelTable, - int currentParser, - xmlNodePtr currentNode) -{ - Model_t *destination = ModelTable->table[currentParser].destination; - xmlChar *x, *y, *z, *siteId, *weight; - - x = xmlGetProp(currentNode, (xmlChar*)"x"); - y = xmlGetProp(currentNode, (xmlChar*)"y"); - z = xmlGetProp(currentNode, (xmlChar*)"z"); - siteId = xmlGetProp(currentNode, (xmlChar*)"siteId"); - weight = xmlGetProp(currentNode, (xmlChar*)"weight"); - - // Detect children - if (currentNode->xmlChildrenNode) { - printLog("%s (stub) has children\n", - ModelTable->table[currentParser].tag); - } - - printLog("%s: x:%s, y:%s, z:%s\n", ModelTable->table[currentParser].tag, - x, y, z); - - if (!x || !y || !z) { - xmlFree(x); - xmlFree(y); - xmlFree(z); - return -1; - } - - // Create arrow in model - if (destination->scheduler) { - ArrowAdd(destination->scheduler, - atoi((char*)x), atoi((char*)y), atoi((char*)z), - atoi((char*)siteId), atoi((char*)weight)); - } else { - xmlFree(x); - xmlFree(y); - xmlFree(z); - - return -1; - } - - xmlFree(x); - xmlFree(y); - xmlFree(z); - - return 0; -} - -int parseSpaceSizeFieldXML (xmlDocPtr doc, - ModelParserTableXML_t *ModelTable, - int currentParser, - xmlNodePtr currentNode) -{ - xmlChar *x, *y, *z; - Model_t *destination = - (Model_t*)ModelTable->table[currentParser].destination; - - x = xmlGetProp(currentNode, (xmlChar*)"x"); - y = xmlGetProp(currentNode, (xmlChar*)"y"); - z = xmlGetProp(currentNode, (xmlChar*)"z"); - - // Detect children - if (currentNode->xmlChildrenNode) { - printLog("%s (stub) has children\n", - ModelTable->table[currentParser].tag); - } - - printLog("%s: x:%s, y:%s, z:%s\n", ModelTable->table[currentParser].tag, - x, y, z); - - if (!x || !y || !z) { - xmlFree(x); - xmlFree(y); - xmlFree(z); - return -1; - } - - // Store space size - destination->space_xMax = atoi((const char*)x); - destination->space_yMax = atoi((const char*)y); - destination->space_zMax = atoi((const char*)z); - - xmlFree(x); - xmlFree(y); - xmlFree(z); - - return 0; -} - -// -------------------------------------------------------------------------- // -// Parsing a text field // -// -------------------------------------------------------------------------- // -int parseTextFieldXML (xmlDocPtr doc, - ModelParserTableXML_t *ModelTable, - int currentParser, - xmlNodePtr currentNode) -{ - xmlChar *content, *contentText, *contentValueAttribute; - char *destination = (char*)ModelTable->table[currentParser].destination; - - contentText = xmlNodeListGetString(doc, currentNode->xmlChildrenNode, 1); - contentValueAttribute = xmlGetProp(currentNode, - (xmlChar*)"value"); - - if (contentText) { - content = contentText; - } else if (contentValueAttribute) { - content = contentValueAttribute; - - // Detect children - if (currentNode->xmlChildrenNode) { - printLog("%s has children\n", ModelTable->table[currentParser].tag); - } - } - - printLog("%s: %s\n", ModelTable->table[currentParser].tag, - content); - - if (!content) { - xmlFree(contentText); - xmlFree(contentValueAttribute); - return -1; - } - - strcat(destination, " "); - strncpy(destination + strlen(destination), - (char *)content, MODEL_STRING_SIZE - strlen(destination)); - - xmlFree(contentText); - xmlFree(contentValueAttribute); - - return 0; -} - -// -------------------------------------------------------------------------- // -// Parsing an integer field // -// -------------------------------------------------------------------------- // -int parseIntFieldXML (xmlDocPtr doc, - ModelParserTableXML_t *ModelTable, - int currentParser, - xmlNodePtr currentNode) -{ - xmlChar *content, *contentText, *contentValueAttribute; - int *destination = (int*)ModelTable->table[currentParser].destination; - - contentText = xmlNodeListGetString(doc, currentNode->xmlChildrenNode, 1); - contentValueAttribute = xmlGetProp(currentNode, - (xmlChar*)"value"); - - if (contentText) { - content = contentText; - } else if (contentValueAttribute) { - content = contentValueAttribute; - - // Detect children - if (currentNode->xmlChildrenNode) { - printLog("%s has children\n", ModelTable->table[currentParser].tag); - } - } - - printLog("%s: %s\n", ModelTable->table[currentParser].tag, - content); - - if (!content) { - xmlFree(contentText); - xmlFree(contentValueAttribute); - return -1; - } - - *destination = (int)atoi((char*)content); - - xmlFree(content); - return 0; -} - -// -------------------------------------------------------------------------- // -// Parsing a field that contains children fields // -// -------------------------------------------------------------------------- // -int parseParentFieldXML (xmlDocPtr doc, - ModelParserTableXML_t *ModelTable, - int currentParser, - xmlNodePtr currentNode) -{ - printLog("%s parsed\n", - ModelTable->table[currentParser].tag); - - // Getting children content - currentNode = currentNode->xmlChildrenNode; - while (currentNode != NULL) { - for (int i = 0; i < ModelTable->len; i++) { - if ((!xmlStrcmp(currentNode->name, - ModelTable->table[i].tag))) { - ModelTable->table[i].parse(doc, - ModelTable, - i, - currentNode); - break; - } - } - currentNode = currentNode->next; - } - return 0; -} - -// -------------------------------------------------------------------------- // -// Parsing a field that contains children fields with props // -// -------------------------------------------------------------------------- // -int parseParentFieldPropsXML (xmlDocPtr doc, - ModelParserTableXML_t *ModelTable, - int currentParser, - xmlNodePtr currentNode) -{ - xmlChar *id, *date, *author; - // (truc*)destination = (truc*)ModelTable->table[currentParser].destination; - - // Getting field identity - id = xmlGetProp(currentNode, (xmlChar*)"id"); - date = xmlGetProp(currentNode, (xmlChar*)"date"); - author = xmlGetProp(currentNode, (xmlChar*)"author"); - - printLog("%s parsed (id=%s, date=%s, author=%s)\n", - ModelTable->table[currentParser].tag, id, date, author); - - // Getting children content - currentNode = currentNode->xmlChildrenNode; - while (currentNode != NULL) { - for (int i = 0; i < ModelTable->len; i++) { - if ((!xmlStrcmp(currentNode->name, - ModelTable->table[i].tag))) { - ModelTable->table[i].parse(doc, - ModelTable, - i, - currentNode); - break; - } - } - currentNode = currentNode->next; - } - - free(id); - free(date); - free(author); - - return 0; -} - -// -------------------------------------------------------------------------- // -// Preparsing a model file // -// -------------------------------------------------------------------------- // -int ParseModelIdentityXML(Model_t *model, Parameters_t *params) -{ - xmlDocPtr xmlDocument; - xmlNodePtr currentNode; - xmlChar *version = NULL; - xmlSchemaPtr schemPtr; - xmlSchemaValidCtxtPtr schemValidator; - xmlSchemaParserCtxtPtr schemFile; - char *schemPath; - - ParserTableXML_t table[] = - { - // IDENTITY - {(const xmlChar *)"identity", parseParentFieldXML, NULL}, - {(const xmlChar *)"name", parseTextFieldXML, model->name}, - {(const xmlChar *)"owner", parseTextFieldXML, model->owner}, - - // TODO lacking implementation (model side) - {(const xmlChar *)"owner_id", parseStubFieldXML, model->owner_id}, - - {(const xmlChar *)"date", parseIntFieldXML, &model->date}, - {(const xmlChar *)"version", parseTextFieldXML, model->version}, - - // PARAMETERS - {(const xmlChar *)"parameters", parseParentFieldPropsXML, model}, - // MODELIZATION - {(const xmlChar *)"modelization", parseParentFieldXML, model}, - {(const xmlChar *)"max_thread", parseStubFieldXML, model}, - {(const xmlChar *)"max_cycles", parseStubFieldXML, model}, - // SPACE - {(const xmlChar *)"space_param", parseParentFieldXML, model}, - {(const xmlChar *)"dimension", parseStubFieldXML, model}, - {(const xmlChar *)"size", parseSpaceSizeFieldXML, model}, - {(const xmlChar *)"site_multiplicity", parseIntFieldXML, - &model->siteNumber}, - - // TODO lacking implementation (model side) - {(const xmlChar *)"boundaries", parseStubFieldXML, model}, - }; - - ModelParserTableXML_t identityParserTable = - { - LEN(table), - &table[0] - }; - - // Allocating space for schema file path - schemPath = calloc(1, strlen(params->modelDir) - + strlen("/schemas/model_ .xmls")); - - printLog("Preparsing model %s\n", model->name); - - // Opening document - xmlDocument = xmlReadFile(model->filename, NULL, 0); - - if (xmlDocument == NULL) { - printErr("Can't parse model file at '%s'.\n", model->filename); - return -1; - } - - // Getting root from XML model file - currentNode = xmlDocGetRootElement(xmlDocument); - - if (currentNode == NULL) { - printErr("Invalid model file at '%s', document empty !\n", - model->filename); - xmlFreeDoc(xmlDocument); - free(schemPath); - return -2; - } - - // Checking that XML file is actually a model - if (xmlStrcmp(currentNode->name, (const xmlChar *) "gem-graph-model")) { - printErr("Invalid model file at '%s', " - "root node is not !\n", - model->filename); - xmlFreeDoc(xmlDocument); - free(schemPath); - return -3; - } - - // Get gem-graph model version to parse - version = xmlGetProp(currentNode, (xmlChar*)"version"); - // Check version is present - if (!version) { - printErr("Missing version for model %s \n", model->name); - xmlFreeDoc(xmlDocument); - free(schemPath); - return -4; - } else if (strlen((char*)version) > MODEL_STRING_SIZE) { - printErr("Invalid version number for model %s \n", model->name); - free(version); - xmlFreeDoc(xmlDocument); - free(schemPath); - } - printLog("Gem-graph model version %s detected\n", version); - - // Retrieving schema file - sprintf(schemPath, "%s/schemas/model_%s.xmls", params->modelDir, version); - printLog("Loading schema %s\n", schemPath); - - // Loading schema file - schemFile = xmlSchemaNewParserCtxt(schemPath); - if (schemFile == NULL) { - printErr("Invalid gem-graph version %s in model %s: no schema present", - version, model->name); - free(version); - xmlFreeDoc(xmlDocument); - free(schemPath); - return -5; - } - - // Loading schema file content - schemPtr = xmlSchemaParse(schemFile); - if (schemPtr == NULL) { - printErr("Invalid schema file, version %s\n", version); - xmlSchemaFreeParserCtxt(schemFile); - free(version); - xmlFreeDoc(xmlDocument); - free(schemPath); - return -6; - } - - // Creating validating context - schemValidator = xmlSchemaNewValidCtxt(schemPtr); - if (schemValidator == NULL) { - xmlSchemaFreeParserCtxt(schemFile); - xmlSchemaFree(schemPtr); - free(version); - xmlFreeDoc(xmlDocument); - free(schemPath); - printErr("An error occured preparing schema file, version %s\n", version); - return -7; - } - - // Validate (and print errors) ! - xmlSchemaSetValidErrors(schemValidator, - (xmlSchemaValidityErrorFunc) fprintf, - (xmlSchemaValidityWarningFunc) fprintf, stderr); - - // Get the validation status - model->validated = - xmlSchemaValidateDoc(schemValidator, xmlDocument) == 0 ? true : false; - - // Reset node - currentNode = xmlDocGetRootElement(xmlDocument); - - // Parsing identity - currentNode = currentNode->xmlChildrenNode; - while (currentNode != NULL) { - for (int i = 0; i < identityParserTable.len; i++) { - if ((!xmlStrcmp(currentNode->name, - identityParserTable.table[i].tag))) { - identityParserTable.table[i].parse(xmlDocument, - &identityParserTable, - i, - currentNode); - } - } - currentNode = currentNode->next; - } - - xmlSchemaFreeParserCtxt(schemFile); - xmlSchemaFreeValidCtxt(schemValidator); - xmlSchemaFree(schemPtr); - free(version); - xmlFreeDoc(xmlDocument); - free(schemPath); - xmlCleanupParser(); - - return 0; -} - -// -------------------------------------------------------------------------- // -// Parsing a model file // -// -------------------------------------------------------------------------- // -int ParseModelXML(Model_t *model) -{ - xmlDocPtr xmlDocument; - xmlNodePtr currentNode; - - ParserTableXML_t table[] = - { - // OBJECTS - // {(const xmlChar *)"objects", parseParentFieldXML, model}, - // {(const xmlChar *)"object", parseParentFieldPropsXML, model}, - - // SAVESTATES - {(const xmlChar *)"savestates", parseParentFieldPropsXML, model}, - // SPACE - {(const xmlChar *)"space", parseParentFieldPropsXML, model}, - - // TRANSITIONS - {(const xmlChar *)"transitions", parseParentFieldPropsXML, model}, - // TRANSITION - // TODO probability - {(const xmlChar *)"transition", parseParentFieldPropsXML, model}, - {(const xmlChar *)"if", parseParentFieldXML, model}, - {(const xmlChar *)"then", parseParentFieldXML, model}, - - // ARROW - {(const xmlChar *)"arrow", parseArrowFieldXML, model}, - - // REF - {(const xmlChar *)"ref", parseStubFieldXML, model}, - - // QUOTE - {(const xmlChar *)"quote", parseStubFieldXML, model}, - }; - - ModelParserTableXML_t modelParserTable = - { - LEN(table), - &table[0] - }; - - printLog("Parsing model %s\n", model->name); - - xmlDocument = xmlReadFile(model->filename, NULL, 0); - - if (xmlDocument == NULL) { - printErr("Can't parse model file at '%s'.\n", model->filename); - return -1; - } - - currentNode = xmlDocGetRootElement(xmlDocument); - - if (currentNode == NULL) { - printErr("Invalid model file at '%s', document empty !\n", - model->filename); - xmlFreeDoc(xmlDocument); - return -2; - } - - if (xmlStrcmp(currentNode->name, (const xmlChar *) "gem-graph-model")) { - printErr("Invalid model file at '%s', " - "root node is not !\n", - model->filename); - xmlFreeDoc(xmlDocument); - return -3; - } - - currentNode = currentNode->xmlChildrenNode; - while (currentNode != NULL) { - for (int i = 0; i < modelParserTable.len; i++) { - if ((!xmlStrcmp(currentNode->name, - modelParserTable.table[i].tag))) { - modelParserTable.table[i].parse(xmlDocument, - &modelParserTable, - i, - currentNode); - } - } - - currentNode = currentNode->next; - } - - xmlFreeDoc(xmlDocument); - xmlCleanupParser(); - - return 0; -} diff --git a/src/scheduler.c b/src/scheduler.c index 1b317bd..402e744 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -27,202 +27,4 @@ #include -/* -------------------------------------------------------------------------- */ -static void *schedulerMain(void *scheduler); - -/* -------------------------------------------------------------------------- */ - -// -------------------------------------------------------------------------- // -// Scheduler init function // -// -------------------------------------------------------------------------- // -void SchedInit(Scheduler_t *scheduler) -{ - pthread_create(&scheduler->id, NULL, schedulerMain, scheduler); -} - -/* -------------------------------------------------------------------------- */ - -// -------------------------------------------------------------------------- // -// Scheduler area finder function // -// -------------------------------------------------------------------------- // -static Center_t *findWorkArea(Center_t *centersList, Arrow_t *electedArrow, - int ruleRadius, int xmax, int ymax, int zmax) -{ - register Center_t *currentCenter, *newCenter; - - currentCenter = centersList->next; - newCenter = malloc(sizeof(Center_t)); - - while (currentCenter){ - if ( (xmax && (abs(electedArrow->x - currentCenter->x) <= ruleRadius)) - || (ymax && (abs(electedArrow->y - currentCenter->y) <= ruleRadius)) - || (zmax && (abs(electedArrow->z - currentCenter->z) <= ruleRadius)) - ){ - free(newCenter); - newCenter = NULL; - //printLog("Can't find a free area\n"); - return NULL; - } - currentCenter = currentCenter->next; - } - - newCenter->x = electedArrow->x; - newCenter->y = electedArrow->y; - newCenter->z = electedArrow->z; - return newCenter; -} - -/* -------------------------------------------------------------------------- */ - -// -------------------------------------------------------------------------- // -// Scheduler thread main function // -// -------------------------------------------------------------------------- // -static void *schedulerMain(void *scheduler) -{ - Scheduler_t *args; - Worker_t *workerArray; - // A center is a structure that defines, with a radius, a cubic preempted - // area for a worker to operate on (as an approximation of a spheric one) - Center_t *centersList, *workArea; - Arrow_t *electedArrow; - - int ncpu, nworker, err; - - // Getting scheduler argument structure - args = (Scheduler_t*) scheduler; - printLog("Scheduler #%lu online\n", args->id); - - if (!args->nMaxThread) { // nmaxthread = 0 => no minimum - ncpu = get_nprocs(); // allocating all the cpus available - } else { // n thread = min(cpu, nmaxthread) - ncpu = MIN(get_nprocs(), args->nMaxThread); - } - printLog("%d threads available.\n", ncpu); - - // Data structures - workerArray = calloc(ncpu, sizeof(*workerArray)); - nworker = 0; - centersList = calloc(1, sizeof(*centersList)); - - // Initiate the arrowArray lock - if (err = ArrowsInitLock(args->arrowArray), err != 0) { - printErr("Impossible to create the arrow array lock (error %d)\n", err); - return NULL; - } - // - // MAIN LOOP - // - while (!args->pleaseStop && 0 <= args->nMaxCycles) { //XXX count cycles - //printLog("Scheduler #%lu online: cycle %d\n", *args->id, ncycles); - // - - // TODO statistics here - - // Create a new thread - if (nworker < ncpu) { - - workArea = NULL; - - // Acquiring lock to consistently read the arrowArray - if (ArrowsAcquireNonBlockingLock(args->arrowArray)) { - // Random choice of an arrow - electedArrow = - &args->arrowArray->array[rand() % args->arrowArray->size]; // XXX - - // Find a local area - workArea = findWorkArea(centersList, - electedArrow, - args->ruleRadius, - args->globalDrawingSpace->xMax, - args->globalDrawingSpace->yMax, - args->globalDrawingSpace->zMax - ); - ArrowsReleaseLock(args->arrowArray); - } - - // If a free area exists, - if (workArea) { - // preempt it, - CenterAdd(centersList, workArea); - // find a worker socket, - for (int i = 0; i < ncpu; i++) { - // if a location is empty - if (workerArray[i].id == 0) { - // prepare the worker for the area, - workerArray[i].localWorkAreaCenter = workArea; - workerArray[i].globalDrawingSpace = - args->globalDrawingSpace; - workerArray[i].conditionTree = - args->conditionTree; - workerArray[i].arrowArray = - args->arrowArray; - // create the worker, - WorkerInit(&workerArray[i]); - // and increment worker count. - nworker++; - printLog("Added worker at rank %d with center %p, now %d worker(s)\n", - i, - workArea, - nworker - ); - // Increment partial cycle TODO - break; - } - } - } - } - - // Delete finished workers - for (int i = 0; i < ncpu; i++) { - if (workerArray[i].id) { - //printLog("Checking termination of worker #%lu\n", *workerArray[i].id); - // Check worker termination - if (workerArray[i].terminated) { - // Join the thread to act his termination - WorkerWait(&workerArray[i]); - // Remove preemption on space - CenterRemove(workerArray[i].localWorkAreaCenter); - printLog("Worker #%lu terminated with return %d. Cleaning...\n", - workerArray[i].id, - workerArray[i].returnValue - ); - // Remove Worker - WorkerDestroy(&workerArray[i]); - nworker--; - } - } - } - } - - // Exiting scheduler properly - printLog("Stopping scheduler... (waiting for workers)\n"); - // Waiting for remaining workers - for (int i = 0; i < ncpu; i++) { - if (workerArray[i].id) { - printLog("Waiting for termination of worker #%lu\n", workerArray[i].id); - // Join the thread to wait for his termination - WorkerWait(&workerArray[i]); - // Remove preemption on space - CenterRemove(workerArray[i].localWorkAreaCenter); - printLog("Worker #%lu terminated with return %d. Cleaning...\n", - workerArray[i].id, - workerArray[i].returnValue - ); - // Remove Worker - WorkerDestroy(&workerArray[i]); - } - } - - printLog("Scheduler #%lu offline\n", args->id); - - - ArrowsDestroyLock(args->arrowArray); - - free(workerArray); - workerArray = NULL; - free(centersList); - centersList = NULL; - - return NULL; -} diff --git a/src/server.c b/src/server.c index ff738a8..a82fc68 100644 --- a/src/server.c +++ b/src/server.c @@ -25,37 +25,22 @@ /* -------------------------------------------------------------------------- */ -static Command_t cmdList[] = -{ - {"help", CmdHelp, "Help command"}, - {"model", CmdModel, "Model command"}, - {"shutdown", CmdShutdown, "Shutdown command"}, -}; - -static void *serverMain(void *server); +static void *server_main(void *server); /* -------------------------------------------------------------------------- */ -// -------------------------------------------------------------------------- // -// Server init function // -// -------------------------------------------------------------------------- // -void ServerInit(Server_t *server) -{ - pthread_create(&server->id, NULL, serverMain, server); -} - #define SEND_BUFFER_SIZE 80 * 24 #define RECEIVE_BUFFER_SIZE 80 -void *serverCommunicationInstance(void *serverCom) +void *server_communication_instance(void *serverCom) { - ServerCommunication_t *args; + servercom_t *args; char **argv = NULL; char receiveBuff[RECEIVE_BUFFER_SIZE]; char sendBuff[SEND_BUFFER_SIZE]; int tokenIndex, bytesReceived, clientPort; char clientIP[16]; - args = (ServerCommunication_t*) serverCom; + args = (servercom_t*) serverCom; // Get ip address from client inet_ntop(AF_INET, @@ -132,7 +117,7 @@ void *serverCommunicationInstance(void *serverCom) #define PORT 9000 #define MAX_CONNECTION 100 -static void *serverMain(void *server) +static void *server_main(void *server) { Server_t *args; ServerCommunication_t serverSlots[MAX_CONNECTION] = {0}; @@ -235,7 +220,7 @@ static void *serverMain(void *server) // Create thread threadStatus = pthread_create(&serverSlots[serverSlotIndex].id, NULL, - serverCommunicationInstance, + server_communication_instance, (void*)&serverSlots[serverSlotIndex]); if(threadStatus != 0) { printErr("Error from pthread: %d (%s)\n", diff --git a/src/supervisor.c b/src/supervisor.c index 03cf758..3d8fae5 100644 --- a/src/supervisor.c +++ b/src/supervisor.c @@ -24,22 +24,4 @@ /* -------------------------------------------------------------------------- */ -static void *supervisorMain(void *supervisor); -/* -------------------------------------------------------------------------- */ - -// -------------------------------------------------------------------------- // -// Supervisor init function // -// -------------------------------------------------------------------------- // -void SupervisorInit(Supervisor_t *supervisor) -{ - pthread_create(&supervisor->id, NULL, supervisorMain, supervisor); -} - -// -------------------------------------------------------------------------- // -// Supervisor thread main function // -// -------------------------------------------------------------------------- // -static void *supervisorMain(void *supervisor) -{ - return NULL; -} diff --git a/src/tests/getchar b/src/tests/getchar deleted file mode 100755 index 79b5550f461522eae1012f175b3bdf6eb7fd6005..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 17096 zcmeHOdu&_P89$EuNMBA$TNYZNZfUi2UEHM5QrczMN!&V9`dCS!6O>!mb`mo`f_)8b zH%7M5s-_K8+C%)o1l81up$T;_4Y3MRirRu0D??1x7$Pvx99~OdKoD)c-*?XW+vMe1xjWdgw#wxaT-@U8g1Et2h4{4~v_@Az{9=`uhR-X- z6=D+j2@+Fszao&T(o?V>(^G=%7 zPMNa26wgb>C|>zlqP@EPOs#g5n?i%qFEra}pSC-wDcccCcB9&ERNFBfq6VQb<$O}} z>Hit+&q>?J2!$!>QrYyD8KwG!1(F>@S)VD}eG_)n&d*yo?VEMH@$yoq+s{;YS1e1! zcdWc-St8nyh^KNp8+NW**|72&UpDPqF8e(j4WTi$Zo?Mw)I&!eynoAMw?F#XRl8>` z-M2Kh^s(PiyQvN2k94SzKBu4P4E&Q^_3Qg!Y9$6p)L(Jn@)u(-B&lAmd>G@$W1y0OkB&Hx6Du4*px@DRsiMvF9$wTTqb(_ z%7VvjE7*k!vVV4UC34vw(VK~<%r4O#Gb26Wj4+H?CX-4ViF71v#?vWbMzS$8Y?>L7 zHKXaA2>`khFpS8~u+bGyg%k1JG0_!CnF(lR;2}+(Frp$x)(mG%BN>jT1o1SWR*H2U z?Jcdwa^G^_3SqQ&t~a8wOsqSeHDj61^{t6?D%Kg^k%+-ZcQT#QJ`AN=R#A zg3ITm78ESFezZ!-hy|a)}Q3{)~u$v`Co zl?+reP|3jmTLwb??^cKQPkeQm5TQMTW>xWYsQ;PjW)epyZz;o~CfaM;e(R@HSsis z8}5_*qr}s6Y`9nQE#SKjf|sS~z4rWzw}$d>g!vDkOlQDI*8guoQ}NxC*qkGEVQwE9SaHZhr1|&Yl#{oh`Z!&_;dTs_{lVw^htB7N z&&$dusHgx>KS>c)ln-Gcquk3;&Z45CY*bO!s3`eTl)Fn&{!T6L1Dg+?>%Z%q*tMz9 zBK`D_PRmV|Z5pOHg|A8!efS|N%KKT&@6m@>Wsv4b$@>Cms6On0B{lt2mF~l-YD*vJ zUQm5l1`G7z+QNx*I#Ug$DEn2EJ5`j1Qj|YHRYrMdAzu{ub6%7^)ILQ~ z3opUF?(z5DgFJr%S7A!Yw*jknUheYM%)bZPbVG~4O&tTxuPqTGaASM^FM%!X`Hupf zf&95Gp@D|efN$tna-NlQ%EUZu2hA2I+m77jM$AqoB8d9vUeYpC|rVzCpH-ak`u?cOY0J8NC@ zrcbUu=$c$hIPGEB3O`FpL6`FSLM(!u^!L4AEXp4_Jhf{*b>FC&c4u{;xPI=MuUx%o zA-N!X{G1_1poc|!sgNzT_m7A-kA8&-cxv}nwa%F2zO)KWlD@wS_|(~A@oLbLoa!j# z^FjOLPfs&-G^eeR&gJ1`;WsXs8W&C|GdQpmHafATty|2EI6fTuCwsc-Rk zTRe5pZ}C*i_n2>zJ@Q0L(@Lpippt<~1}Yh-WT29PN(L$!sAQm$fiErtykC&_2lAYp z_8d}~u1IMEBHkIv64eWp%QVmX5a(&0*B)~<&-)Q+9YTfWA6+P>C6Cv#RCtf!0}~X_ zd-fW&ocG?npv2g3u1cMzb2Me5&9JdgA++?NGAKVUuFq(VM}_w5Qt8uy8Gl4KjN5%i zsR`aA$no(uO!fz2sfstClHaH8@v2kuoM$W_CEumxpK89;ZXM@~CcLlQ8Fyu^Le^?} zqo!S&-l6GUO@E^4&oyO#+x@DxuC;Zwx4td5BOXq9n;NmTwy9y|5?%8yZ(QEg*woa> zR^_Q1J4E@MfK|hd-!OQ;yB&9n0uKa>z>S@w)nyXz#;D}+XHm!g(@BEIsU4pz_TWT! zbUwf;bz{eOXn_@B{aJ#a7gnhozh3Zj!j8`o<;J61Tq=0qvRxl{vEPcQx?9wVt!43x z`_HXbZoyBlizwZ<6U5o#MMTx;Iawt9RkC%rd z&~f3qCl*R|+ef*$a%YE_HRihiN%|Rk-PLncn=xUHA#%lw_~M_1C(Xg#1&Ucmum46_#u5~-d5yeC^HFI5EzKFPZ%ET~}Mug6pWO2qMnl`!< z=^f#O5jE49tP#%b6p?hYHxV;qQD0+u4LbJ{H^P}rc$X1NnVDUpD-%w}jA$;I+yxU0 zXF$`msp``j?g=D_-(5!}#b;KYmFHY$v^Z*2&yZ*R3rAFr|K~lQqmBgG)a5B4 z8)C$wVKb}`8rdbzBSH=-*{Q=nd_KvcU_TvXM6+q5C!C7X$)Wa*u*3@vBbSXKZ5Mk? z7elDSPY!Q8vRNHX9#<+Ibh4?H&O_Ny!@#*EzGifwsq85wJ{#pQwVtA~iG(k^D`|#z zfSMUad$@*ETd_>9@TJmb%-5aD`F7;uIJ_5+YADds-e88ig_QM#anj2d-IanrMRA@> zO74hdvUJSL${3JkVu>&r=vr^W6h4_bAF96YG*~mX6Q6V&Kq)P+u`kx62Yyc!PMA}s z>JCsATt|d(G9E$rv`L=PI2tQHTngdCJ($F;#W(W*Mrl2TM;yMZRN;9pQ(jMLvDD={ z(W~%JYZ$iY`7qN(q+}`ld}e4RDpq@*H#7CJqNYy!HsBv)O~dhde$BLA+v(*`Xecqn_G?(|Mul?9_PkDD%Ii>$@9h6Q+P+0Q?9&rCrh^(M zds_QC?e7CdIpKL9eU;0iDcM+y^ZMr@wAy>M9n*tWW7Hk)#g82JypCbY?PWL2GySQ< zeozNsddRWf`6Ms8Igc-gzFo zjTJR@#-|O(R(oELF|8+|r8wh13mIlPJQN?z6{$Mi#@OJMj8O3u%6;B z@{GR@0gXMrOuP={bMQmD{X9O|j<4ebuq73?=k?;5WdKp?6)AnPJ=3=#vDyp2R&7*t zt7e(79n%jXu-fxHd#kqRb~^2u{}dJS#qH<$X3yueSL+{~gJp-Bvi+AGxK5{RGl_`j zv1e61s2z^z#MAc>rIh>4&tJMfxb0T^K3eFa9CbM7_)1D^hw}DcTdg!EI}9AG_%}DV B3331c diff --git a/src/tests/test_realloc b/src/tests/test_realloc deleted file mode 100755 index 03248fc77c4135b2b61aea905a96301596c465da..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16720 zcmeHOeQX>@6`#9{V<%0{7bgKXfo2nuCV}{Tb`#??adYRdtdZk{#10^uY|g&5edRu^ zyS>y7NCdf5oKm+DP*D&_k?0>F0iuXM`Uh$gIi#wDP=Z7isG^!GRh=pnw-DrtHphE2 z@11woX9P$fA$FwQH}Cg8=Iz_ry_wsYFL(8J`+YvaB_JLW$c=>*5+%Xay;K1b6|LeP zINv8eDwYGkOkzqNRRpA}jQX9lhVTGTwEF?c-Si`*$CM*Pigrtt>s3m^RM2&xofHwz zIMW7-gDKipErO zuw{2+D$$TiW(wmCp?Kr;=w(VHj{5 zhK#%w&sjz~p3Dg3@h-H8gS|cN9Y%AwIlM<0J^hD_gqbr(l6lL_^&jd;Wiw`fd@zMR zM$*{~`7o4fMMW8Z5#AbA za@J@5`T>PwU%(u5nX%#6f4I!raDGNmHfO`DSsCcO4d=E|Tpz`=5U9(26L~F)XNA)% zPVzgVl1Tl3<#|+A-r385mjN#WUIx4jcp30A;APFC$A3l!; zSepLWQ@nOkFTSo%-kdwq-#bRzDy~+a=H`t?ve#ztnE(L`mDXfzhX`(2u@% z-dYdg-iLJvcSUKYcGFq(J53bylTv@rAd;PT_2Qg<>FophrTKvFyP{vaYpnwZcf!g+ zO=)IWw!`*k+jfGTC^Qb}lWnhnme7lDSgZ7NZ7TtvZGcOf9fi}CDj)E^6Ku!y))zrI z05*`=(2Mx*#q96ymP&fDYhFJGNAU_o_*77c=RqW9e~dD~U(kTli!aUoLMhJyIH!~c z%gSd#Ce2=+hM*rq2O{>F-l?`tyP$Hsr}#>2pr?2z)*marKcG)FgaAI)yYmCQ$FpnE z@ud#}*2bn+Yd4AB;=8@Yw>yh(#!Bm6(MoE_dHs2Rvg%{@8b__-GVUMcfv6adiL*ZX!|f60}$^ZP!S7;9`|>w zUhY2u5hp{OgSR=nVV;-IdKXCb8@iO3Lz{`M_0WSky2D}V-8SpaTWx&h8e>?-c z4wBbB@>~SxaadLxb$A)d41(Z;(1QBfp}hLiSsKgEWdNNl$AU!n&dT% zHOr(Lt+^E?=Y!}L$`-zE8b#Fyii-v9e2URUmJ zxrG*NcN0BMl=XO>?7@zX2eloY=3p|O(V8N#`nRc}Whd3N=16l>q^T*wR+VW0R@n0T zf4fEi{+r-+>`pu&X88u&1OZqHUQ;2V0a*FZ{mP~e>%W%^?tf0aTJU{x;x&TruM=M( zxZgPOmF52B#GyC4@`>^QJcU|Ce6=`L5f7I4X*m7Y3Vv>#I5^Slh^hy~n)3R8yEFhR z%f~9}uPv{$cIvMa{QNud^`cTg3y2R1UT5vpho>`YM^rrkPhX@Wey@mD#QCkX;0vF} zW#XOE0-|d8{_~5VxZuh&EWVKVRTsXQaDJaEFG740Dn79g4?q7M5?`DjTV$;>yGkWLKa)CZ7v0BczY<{^n)8&A_~L%D8}JbHA9p{g1H8_^_<7lf zxYM=d`j)^`c9&n9x z?4-`W#D0{aoRzld9InYRquC*_c9 zW9_|N=vD5x8Oj%Av9~I%mLk`&6k+J}Y3R%L$Lim%!!0g*9_KODSdpmPz7z1j!q}SQ=kXv@ZofSLaQW*4 z9_Jct&*Mg>*GZrEh4DOr=LN?>jzdtk=kX;|p3jh*Mfp1k0-QUrJ&!|~@^2Ey&w5P1 z0QNW+VV=jUOf_oQ9Y4Y13{)_uY|rCbrWZ+(+jn38V`Se>dOW{ks@v>w4(7Ih5-{w4 z<-Ke?9w3S~_JXn6OA#2my+(FSqjqJeyWER!y6jJp0#lBc-7wGedoFwaon`tQ24^eW zK11J!iamZF$1~-5DC@hg|A%DH_fKe~Nwk?*CLA~O&p`#-XZzink}%z`*z(!k{)-^7 z+w=T^DURpt#ohkPAjB~@K0{b|T*>=|-TKTk{Vmwk!ol`DADN^5)U1cTu`ti{4}f9c zV|$*r@P6zWil6%@+wpyT25eDNvIB0H`S+lL7{?!tDoK>=-TIUcr3B}@a3JQ6lkK;cHC2V}?UehTU3HWk zMkw+4JA}8C+C1D^(@I;cO-QU$!c(Y7Uf)T&(yzwbx+w diff --git a/src/tests/xml b/src/tests/xml deleted file mode 100755 index b0826726346e8356badc37d0491a90faf21a9e8a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 17128 zcmeHOdu$xV8K3jz;RJGaNYWZo$P$8@P<*jX;)WoTJHN7qO9*+;RFG`WzB~KieCOWn zvL795^ALzYxkS)_dRCj+u8BX&CIEx#6`GY zAuboQz|WAFk_QxlRHeSEQd+HXFDTjlSj*k?OInX9*N`aLO_xSyD+yDf+d+0SNu+r< zt=CSOvbdiyccPMu0K&?kRF66tgJiO$1Ca@j7=eaML_!hE<+MqlZ_{@g+CjwCBd-vf6^ z38I)S)8Kv6;NP7F-#-oBISo#2HE_Ap4glrq`HN|Aiid&AovsC7xIyrb<0|L>r@)t% z(SH=!d~vB53@9P@XJJ{r{i&ptvBPQGvV_&Ob%Pa&rlY;_j2%sH-O!duC8Jxz-HE7@ zm6h!6PizXOGttgC)WB@9(>?tIlM)d5Q>5X zM)72?7|7ZgF@UTc6B!#-NsHK?blgVyR?ptB6^kdsiTK^bqObPon)ip}NdX=%n^bQl zA?2DysH=7RhES8$~LHRk49Sqfw z)@d?DBKhl}{N80Ri_amSA4n<2B|WZiydRsd1Y<6Im&SJml$`aMKebNb^v=+`!fDKb z>mgZ+@(vsoDy9hsKDUSpG3mfBa^Qmj#n6~e?7DR=sPvc&*B!C?8eIy?r zD07I zAC&wP#M5Wh_<-b>f{%@Wm#Oi+uH4C;X6`L>==8*aSi)WW{+iH(?$> zyUsj5=`}qsm?zKM3*q2r+Cg<;EGElg`@xngVJEUHx0ypNF=z=h_qKhpd9dXzF!_2k zDc^_N3o}0f@7ck2Ovk?o;WpUdLDibM{gY-cJ7FH|z!j)7a~%_BZq{6_IlQ{rNB&>T z$IcfD_m5_mDUn$RI(dwqk(qmiBx3**BujX72d0tKj;V(*JPFF-!yU9qbrpE07<5j|!)f0G#@aO7ECNX_F#Lem<^O zerg5fVYHJd`9nhegR4_kALR17dQU7 zW{C*p&V_Pk+jD;p78br?4xR9r*Z(E^4!!%g?+D%z+!5Rvw9p3yjRbOxC>a%G`ITEG z0+k3hQ#z@t|~!dbg7mCgt(hGl6D%t)qeV}RDqB1R_L z(-X~PV%bDupWm1(qWuH*J|m(v=VCQ1mENb@&8U%z8FpXP*pp5rdkuTvK=fK8jU_ht zjYN$rTMT6+@9|_TCAMY4z0tMC)fsY}4EIOp3eRHi4JQ23y2*im6bcW4E}SS7o&{Y7 z8Uda2exdLO(0f2X1byyYp|B7WsbwD&3ada5fZhX2>%){hcW)M+y|tdj7tgL9@yxCz zoECMD!v9xDL8tQi@C!NV=g(q}N4mb+PG8+kH5cuv9u(^?yWyJamMtZi>@htPo8Z^E z?nAbB;HpRcGoT{qt9_uVZQiV^BxtL?`@z(dDX4b zp2~a{R~~X&>lLlO+V50#`RX3@cKD3p86Cdm-=5j-Yj|Lm>02{2d%Z7kr*BQr*AVnA zZ}l0izB=f)`l{u+;S4Gt@g!17l?YTKP>Db#0+k3qd9 zc<sx4LsMfz zV`Bqbm8V|p=;U((P7T@{%lq9+aj(ep18@ku*wI;CCh%VLO74FSb?ghBCAgoK;s|3AxfgG%Q5K&ko+^v`isv~>{nrS7ze{mA zF`S6Xd&T_XIRU5Ciyhzjvib{(`;1HV7YZIHO7TUaTz~Y6F9_aeT&j<^IN(H7-Ye?F z_OkeeedbOpui(+^0!riK4Dmh>KScqJlU3MpJmHEnHeN{F(EFv?;7X12IL!E0k?~;P z`?mrL;qkal;#1>uCvYnFF_-_{Qh#ck9|m5o{@>Sr>hvak`5Tk;QC|OtQg(^Bb}2Hh zRL=i1QfF#EIg18XmFdsFhQ3EkeeSPFKMSVxlQ%TZ|A!=t+c{hw8r1`gC|ZqjbyG_O zi-FgopY*x(4*@sI^q-}`=hMq*C^!1wmS_ZCCNv@h{1QB`+ZQN(`CA>bx00W#IR4w#J0vF1p z+!3jN>6Ezr9C*1n|9%?$w8Swk>G;UsD&hY_jX$pYx%`b3IQuH>#K`tpjej zFk)|)`aWUk4+6NA>8@$~+&>L|WE%g^OoRV+8b6{ZZD;IkEavYK7ml}BcE8m_2V62Z zO%h32y@^zJIAKNbLwm*wXZMPpRR2ICYDXjfhVmM893^gr)9LU&3qR-M_xV^FKdM`i zY=8egm^e5Kns$jQ4y&L#^4EW?2SB@Xb8th4)v>XiPU2WNKV@~88Z_HC3#8-p9V z+8`_O6)6SeSsr1n58cul3|Y5yc5dm|YHba+hB_>LuqNO<17k%aVLOb|J-6U2O(dSQ zvRI6ON9SRi5(9bG2JyP-1r`qx0pC z3%PyMK_Qnnc_64Kla=97f!!h(P0F1#a{Gf$@eq?+_HU0xsdGqFEOiFQsi!Gk+$WY? z)t`D2tL$kjbsVb1R`@gf`t5KxsGU}{k8?Ol6-^HaKUSKe{@!HP-<^%)d|y1Gpf-D_PgvmhX1`@XLQ|}ahWc|G6Gkk{%D_mH~J!Q!kjWyFNw0?JW2@n$9qtG$|ldK9Q9g1`i$`7HI)a-^dR$J z5Snk%ScPFkCjJg)%JVHPmb#oLdKE635~S*rUW cO@xu&a5?94m6YZ#id, NULL, WorkerMain, worker)) { - printLog("Worker #%lu can't be initialized!\n", worker->id); - return; - } -} - -// -------------------------------------------------------------------------- // -// Worker thread main function // -// -------------------------------------------------------------------------- // -static void *WorkerMain(void *worker) -{ - Worker_t *args; - int a = rand()%__INT_MAX__; - - args = (Worker_t*) worker; - printLog("Worker #%lu online\n", args->id); - - int size = args->globalDrawingSpace->size; - - // TODO execute rule comparison - - args->returnValue = a; - args->terminated = true; - - printLog("Worker #%lu offline\n", args->id); - - return NULL; -} -- 2.39.5 From efd547c85da8bfee0eebd8e0605f6c45a98d8002 Mon Sep 17 00:00:00 2001 From: "Adrien Bourmault (neox)" Date: Mon, 11 Mar 2024 22:06:10 +0200 Subject: [PATCH 02/24] WIP: reworked parsing --- src/model.c | 445 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/parsing.c | 27 --- 2 files changed, 445 insertions(+), 27 deletions(-) delete mode 100644 src/parsing.c diff --git a/src/model.c b/src/model.c index 0070ade..fb2ddae 100644 --- a/src/model.c +++ b/src/model.c @@ -25,4 +25,449 @@ #include "../include/arrows.h" #include "../include/scheduler.h" +#define READ_SITE 1 << 0 +#define READ_WEIGHT 1 << 1 +#define READ_X 1 << 2 +#define READ_Y 1 << 3 +#define READ_Z 1 << 4 +#define SUCCESSFUL_READ_ARROW_X (READ_SITE | READ_WEIGHT | READ_X) +#define SUCCESSFUL_READ_ARROW_XY (READ_SITE | READ_WEIGHT | READ_X | READ_Y) +#define SUCCESSFUL_READ_ARROW_XYZ (READ_SITE | READ_WEIGHT | READ_X | READ_Y | READ_Z) +static xmlDocPtr model; +static xmlHashTablePtr model_hashtable; + +bool model_init(const char *content, size_t length, const char *basename) +{ + xmlNode *node; + + /* + * this initialize the library and check potential ABI mismatches + * between the version it was compiled for and the actual shared + * library used. + */ + LIBXML_TEST_VERSION + + model = xmlReadMemory(content, length, basename, NULL, 0); + + if (model == NULL ) { + return false; + } + + node = xmlDocGetRootElement(model); + + if (node == NULL) { + g_printerr("Empty XML model !\n"); + xmlFreeDoc(model); + return false; + } + + if (xmlStrcmp(node->name, (xmlChar *) "gem-graph-model")) { + g_printerr("document of the wrong type, root node != gem-graph-model\n"); + xmlFreeDoc(model); + return false; + } + + model_hashtable = xmlHashCreate(0); + + if (model_hashtable == NULL) { + g_printerr("Can't create model hash table !\n"); + xmlFreeDoc(model); + return false; + } + + return true; +} + +bool model_shutdown(void) +{ + xmlFreeDoc(model); + xmlHashFree(model_hashtable, NULL); + + // Cleanup function for the XML library + xmlCleanupParser(); + + // This is to debug memory for regression tests + xmlMemoryDump(); + + return true; +} + +/******************************************************************************/ + +static inline xmlNodePtr getNextChild(xmlNodePtr node, xmlChar *last) +{ + while (node != NULL && xmlStrcmp(node->name, last)) { + // //printf(" <>--- line n°%lu <%s>\n", xmlGetLineNo(node), node->name); + node = node->next; + } + return node; +} + +static inline xmlChar* splitStrAtSlash(xmlChar *toSplit) +{ + toSplit = (xmlChar *)xmlStrchr(toSplit, '/'); + toSplit = xmlStrsub (toSplit, 1, xmlStrlen(toSplit)); + return toSplit; +} + +static inline xmlChar* getFirstTag(xmlChar *path) +{ + xmlChar *preop = path; + path = (xmlChar *)xmlStrchr(path, '/'); + path = xmlStrsub (path, 1, xmlStrlen(path)); + + //printf("%s = %s + / + %s\n", preop,\ + xmlStrsub (preop, 0, xmlStrlen(preop) - xmlStrlen(path) - 1), path); + + return xmlStrsub (preop, 0, xmlStrlen(preop) - xmlStrlen(path) - 1); +} + +static inline xmlChar* getLastTag(xmlChar *path) +{ + while ((ulong)xmlStrchr (path, '/')) + path = splitStrAtSlash((xmlChar *)path); + + // //printf("last tag in the path = <%s>\n", path); + return path; // which is no more the given path but only its last tag ! +} + +/******************************************************************************/ + +static xmlNodePtr model_get_node(xmlChar *path) +{ + xmlNodePtr node; + xmlChar *extrait; + xmlChar *reste, *last, *affich; + + // Lookup for node from path in hash table + node = xmlHashLookup(model_hashtable, path); + + // Found a node in hash table + if (node) { + return node; + + // no node found in hash table + } else { + reste = path; + affich = reste; + last = getLastTag(reste); + node = xmlDocGetRootElement(model); + + while (xmlStrchr (reste, '/')) { + extrait = getFirstTag(reste); + reste = splitStrAtSlash((xmlChar *)reste); + node = node->xmlChildrenNode; + node = getNextChild(node, extrait); + } + + if(node && xmlStrcmp(node->name, last)) { + node = node->xmlChildrenNode; + + while (node && xmlStrcmp(node->name, last)) { + node = node->next; + } + xmlHashAddEntry (model_hashtable, path, node); + } + + return node; + + } + + return NULL; +} + +/******************************************************************************/ + +static inline long model_get_node_long_attrib(xmlNodePtr node, char *id) +{ + xmlAttr *attribute; + xmlChar* value; + long ret_value; + + if (node && node->properties) { + attribute = node->properties; + while(attribute && attribute->name && attribute->children) { + if (!xmlStrcmp(attribute->name, (const xmlChar *)id)) { + value = xmlNodeListGetString(node->doc, attribute->children, 1); + ret_value = strtol((char *)value, NULL, 0); + xmlFree(value); + return ret_value; + } + attribute = attribute->next; + } + } + return 0; +} + +static inline bool model_get_node_str_attrib(xmlNodePtr node, + char *id, + char *dest) +{ + xmlAttr *attribute; + xmlChar* value; + + if (node && node->properties) { + attribute = node->properties; + while(attribute && attribute->name && attribute->children) { + if (!xmlStrcmp(attribute->name, (const xmlChar *)id)) { + value = xmlNodeListGetString(node->doc, attribute->children, 1); + strcpy(dest, value); + xmlFree(value); + return true; + } + attribute = attribute->next; + } + } + return false; +} + +/******************************************************************************/ + +char model_get_dim(void) +{ + xmlAttr *attribute; + xmlChar* value; + xmlNodePtr node = model_get_node( + (xmlChar *)"parameters/space-param/dimension"); + + if (xmlHasProp (node, (xmlChar *) "z")) return 3; + if (xmlHasProp (node, (xmlChar *) "y")) return 2; + if (xmlHasProp (node, (xmlChar *) "x")) return 1; + return 0; +} + +long model_get_dim_value(const char *axis) +{ + xmlAttr *attribute; + xmlChar *value; + long ret_value; + xmlNodePtr node = model_get_node( + (xmlChar *)"parameters/space-param/dimension"); + + return model_get_node_long_attrib(node, axis); +} + +char model_get_multiplicity(void) +{ + xmlAttr *attribute; + xmlChar* value; + xmlNodePtr node = model_get_node( + (xmlChar *)"parameters/space-param/site_multiplicity"); + + if (node->children) + if (node->children->content) + return (char)strtol((char *)node->children->content, NULL, 0); + + return 0; +} + +bool model_get_next_state(char *new_state_id) +{ + static xmlNodePtr cur_node = NULL; + xmlAttr *attribute; + xmlChar *value; + + if (cur_node == NULL) { + // Get first state + cur_node = model_get_node((xmlChar *)"savedstates/state"); + + } else { + // Get next state + if (cur_node->next) + cur_node = cur_node->next; + else + return false; + } + + // Lookup in properties + if (model_get_node_str_attrib(cur_node, "id", new_state_id)) + return true; + + cur_node = NULL; + return false; +} + +long model_get_state_arrows_count(const char *state_id) +{ + xmlNodePtr cur_node = NULL; + xmlAttr *attribute; + long value = 0; + bool found = false; + char temp_char[25]; + uint check = 0; // bit field checker + + //printf("NEW CALL : cur_node = %p\n", cur_node); + + assert(state_id); + + // Get first state node + cur_node = model_get_node((xmlChar *)"savedstates/state"); + + // Lookup in properties + while (cur_node && cur_node->properties) { + attribute = cur_node->properties; + + // Look for the id attribute + if (model_get_node_str_attrib(cur_node, "id", &temp_char)) { + if (!xmlStrcmp(temp_char, (const xmlChar *)state_id)) { + found = true; + break; + } + } + cur_node = cur_node->next; + } + + // Check if the state has been found + if (!found) { + cur_node = NULL; + return -1; + } + + + // Count arrows + if (cur_node->children) { + cur_node = cur_node->children; + while (cur_node) { + if (!xmlStrcmp(cur_node->name, (const xmlChar *)"arrow")) + value++; + cur_node = cur_node->next; + } + } else { + return -1; + } + return value; +} + +bool model_get_next_arrow(struct arrow_t *new_arrow, + const char *state_id, + char dimension) +{ + static xmlNodePtr cur_node = NULL; + xmlAttr *attribute; + xmlChar *value; + bool found = false; + char temp_char[25]; + uint check = 0; // bit field checker + + //printf("NEW CALL : cur_node = %p\n", cur_node); + + assert(new_arrow); + assert(state_id); + + if (cur_node == NULL) { + // Get first state node + cur_node = model_get_node((xmlChar *)"savedstates/state"); + + // Lookup in properties + while (cur_node && cur_node->properties) { + attribute = cur_node->properties; + + // Look for the id attribute + if (model_get_node_str_attrib(cur_node, "id", &temp_char)) { + if (!xmlStrcmp(temp_char, (const xmlChar *)state_id)) { + found = true; + break; + } + } + cur_node = cur_node->next; + } + + // Check if the state has been found + if (!found) { + cur_node = NULL; + return false; + } + + // Get first arrow + if (cur_node->children) { + cur_node = cur_node->children; + + found = false; + while (cur_node && cur_node->name) { + if (!xmlStrcmp(cur_node->name, (const xmlChar *)"arrow")) { + found = true; + break; + } + cur_node = cur_node->next; + } + } + + // Check if the state has been found + if (!found) { + cur_node = NULL; + return false; + } + + } else { + // Get next arrow + found = false; + while (cur_node->next) { + cur_node = cur_node->next; + if (!xmlStrcmp(cur_node->name, (const xmlChar *)"arrow")) { + found = true; + break; + } + } + + // Check if the state has been found + if (!found) { + cur_node = NULL; + return false; + } + } + + //printf("DURING CALL : cur_node = %p\n", cur_node); + //printf("DURING CALL : cur_node->name = %s\n", cur_node->name); + + // Lookup in properties + if (cur_node && cur_node->properties) { + attribute = cur_node->properties; + + while(attribute && attribute->name && attribute->children) { + //printf("attr name : %s\n", attribute->name); + if (!xmlStrcmp(attribute->name, (const xmlChar *)"site")) { + value = xmlNodeListGetString(cur_node->doc, attribute->children, 1); + new_arrow->site = strtol((char *)value, NULL, 0); + xmlFree(value); + check |= READ_SITE; + } + if (!xmlStrcmp(attribute->name, (const xmlChar *)"weight")) { + value = xmlNodeListGetString(cur_node->doc, attribute->children, 1); + new_arrow->load = strtol((char *)value, NULL, 0); + xmlFree(value); + check |= READ_WEIGHT; + } + if (!xmlStrcmp(attribute->name, (const xmlChar *)"x")) { + value = xmlNodeListGetString(cur_node->doc, attribute->children, 1); + new_arrow->x = strtol((char *)value, NULL, 0); + xmlFree(value); + check |= READ_X; + } + if (!xmlStrcmp(attribute->name, (const xmlChar *)"y")) { + value = xmlNodeListGetString(cur_node->doc, attribute->children, 1); + new_arrow->y = strtol((char *)value, NULL, 0); + xmlFree(value); + check |= READ_Y; + } + if (!xmlStrcmp(attribute->name, (const xmlChar *)"z")) { + value = xmlNodeListGetString(cur_node->doc, attribute->children, 1); + new_arrow->z = strtol((char *)value, NULL, 0); + xmlFree(value); + check |= READ_Z; + } + attribute = attribute->next; + } + + switch(dimension) { + case 3: + return (bool)(check & SUCCESSFUL_READ_ARROW_XYZ); + case 2: + return (bool)(check & SUCCESSFUL_READ_ARROW_XY); + case 1: + return (bool)(check & SUCCESSFUL_READ_ARROW_X); + } + } + cur_node = NULL; + return false; +} diff --git a/src/parsing.c b/src/parsing.c deleted file mode 100644 index ec88c89..0000000 --- a/src/parsing.c +++ /dev/null @@ -1,27 +0,0 @@ -//=-------------------------------------------------------------------------=// -// XML parsing module // -// // -// Copyright © 2021 Libre en Communs (contact@a-lec.org) // -// Copyright © 2021 Adrien Bourmault (neox@a-lec.org) // -// // -// This file is part of gem-graph. // -// // -// This program is free software: you can redistribute it and/or modify // -// it under the terms of the GNU Affero General Public License as // -// published by the Free Software Foundation, either version 3 of the // -// License, or (at your option) any later version. // -// // -// This program is distributed in the hope that it will be useful, // -// but WITHOUT ANY WARRANTY; without even the implied warranty of // -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // -// GNU Affero General Public License for more details. // -// // -// You should have received a copy of the GNU Affero General Public License // -// along with this program. If not, see . // -//=-------------------------------------------------------------------------=// - -#include "../include/parsing.h" -#include "../include/arrows.h" -#include "../include/model.h" - - -- 2.39.5 From 4214fc2502fa3b4b4027a4e9eeab2af1a30dd8b7 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 13 Mar 2024 20:10:10 +0200 Subject: [PATCH 03/24] WIP: ready to work --- .gitignore | 2 + Makefile | 22 +- data/models/dimers random walk.xml | 118 +++++++ data/models/dimers_random_walk_recursif.xml | 145 ++++++++ data/schemas/models_0.2.1.xmls | 194 +++++++++++ data/schemas/models_0_recursif.2.1.xmls | 235 +++++++++++++ include/base.h | 27 +- include/cmds.h | 28 +- include/model.h | 22 +- include/parsing.h | 39 --- include/scheduler.h | 54 +-- include/server.h | 2 +- include/supervisor.h | 10 +- include/worker.h | 20 +- src/main.c | 28 +- src/model.c | 10 +- src/server.c | 368 ++++++++++---------- 17 files changed, 995 insertions(+), 329 deletions(-) create mode 100644 data/models/dimers random walk.xml create mode 100644 data/models/dimers_random_walk_recursif.xml create mode 100644 data/schemas/models_0.2.1.xmls create mode 100644 data/schemas/models_0_recursif.2.1.xmls delete mode 100644 include/parsing.h diff --git a/.gitignore b/.gitignore index 53d923c..8925ba7 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ *.o *.deb bin/** +build/** + diff --git a/Makefile b/Makefile index a566609..71ecf5b 100644 --- a/Makefile +++ b/Makefile @@ -39,10 +39,11 @@ NTHREADS= $(shell nproc) CC=gcc WARNINGS= -Wall -DEBUG= -ggdb -fno-omit-frame-pointer -fdiagnostics-color=always -fsanitize=bounds -fstack-check - #-fsanitize=address \ - #-fsanitize=pointer-compare -fsanitize=pointer-subtract \ - #-fsanitize=leak -fsanitize=undefined -fsanitize=null -fsanitize=bounds +DEBUG= -ggdb -fno-omit-frame-pointer -fdiagnostics-color=always \ + -fsanitize=bounds -fstack-check -fopenmp \ + -fsanitize=address \ + -fsanitize=pointer-compare -fsanitize=pointer-subtract \ + -fsanitize=leak -fsanitize=undefined -fsanitize=null -fsanitize=bounds OPTIMIZE= -O3 INCLUDE= $(shell pkg-config --cflags glib-2.0 libxml-2.0 gtk4) LIBS= $(shell pkg-config --libs glib-2.0 libxml-2.0 gtk4) -lGL -lGLU -lm -lepoxy -lX11 -lGLEW @@ -112,8 +113,7 @@ build_system: run: build_system @echo -e ${CL2}[$@] ${CL}executing...${CL3} - @gdb bin/gem-graph-server \ - -ex "set args -C debian/etc -M debian/var/models -U debian/var/users" + @bin/gem-graph-server -C data/config -M data/models -U data/users @echo -e ${CL2}[$@] ${CL}done.${CL3} debug: build_system @@ -124,19 +124,17 @@ debug: build_system valgrind: build_system @echo -e ${CL2}[$@] ${CL}executing...${CL3} @valgrind --leak-check=full --show-leak-kinds=all -s \ - bin/gem-graph-server -C debian/etc -M debian/var/models \ - -U debian/var/users + bin/gem-graph-server -C data/config -M data/models -U data/users @echo -e ${CL2}[$@] ${CL}done.${CL3} run-both: build_system @echo -e ${CL2}[$@] ${CL}executing...${CL3} - @bin/gem-graph-server -C debian/etc -M debian/var/models \ - -U debian/var/users & sleep 1 && bin/gem-graph-ctl + @bin/gem-graph-server -C data/config -M data/models -U data/users\ + & sleep 1 && bin/gem-graph-ctl @echo -e ${CL2}[$@] ${CL}done.${CL3} run-server: build_system @echo -e ${CL2}[$@] ${CL}executing...${CL3} - @bin/gem-graph-server -C debian/etc -M debian/var/models \ - -U debian/var/users + @bin/gem-graph-server -C data/config -M data/models -U data/users @echo -e ${CL2}[$@] ${CL}done.${CL3} all: build_system diff --git a/data/models/dimers random walk.xml b/data/models/dimers random walk.xml new file mode 100644 index 0000000..12ae465 --- /dev/null +++ b/data/models/dimers random walk.xml @@ -0,0 +1,118 @@ + + + + + Modèle de test + + Léontine Patinette + + 2 + + 1630000000 + + 1.0 + + Ref + + + + + 0 + 9 + + + + + + + + + + + + + + + + 3 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/data/models/dimers_random_walk_recursif.xml b/data/models/dimers_random_walk_recursif.xml new file mode 100644 index 0000000..0bd387c --- /dev/null +++ b/data/models/dimers_random_walk_recursif.xml @@ -0,0 +1,145 @@ + + + + + Test 21-03-2023 + + J + + J + + time_stamp + + 1.0 + + Ref + + + + + 0 + 9 + + + + + + + + + + + + + + + + 3 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/data/schemas/models_0.2.1.xmls b/data/schemas/models_0.2.1.xmls new file mode 100644 index 0000000..3a623f5 --- /dev/null +++ b/data/schemas/models_0.2.1.xmls @@ -0,0 +1,194 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/data/schemas/models_0_recursif.2.1.xmls b/data/schemas/models_0_recursif.2.1.xmls new file mode 100644 index 0000000..3714f41 --- /dev/null +++ b/data/schemas/models_0_recursif.2.1.xmls @@ -0,0 +1,235 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/include/base.h b/include/base.h index 8dec1f9..4302918 100644 --- a/include/base.h +++ b/include/base.h @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -35,13 +36,29 @@ /* -------------------------------------------------------------------------- */ -#define LOGMSG "[%s]" -#define printLog(FORMAT, ...) printf("\e[0m" LOGMSG " " FORMAT "\e[0m", \ - __func__, ##__VA_ARGS__) -#define printErr(FORMAT,...) fprintf(stderr, "\e[0m" LOGMSG " " FORMAT "\e[0m",\ - __func__, ##__VA_ARGS__) +#define LOGMSG "<%s:%s()>" +#define printlog(FORMAT, ...) printf("\e[0m" LOGMSG " " FORMAT "\e[0m", \ + __FILE__,__func__, ##__VA_ARGS__) +#define printerr(FORMAT,...) fprintf(stderr, "\e[0m" LOGMSG " " FORMAT "\e[0m",\ + __FILE__,__func__, ##__VA_ARGS__) #define LEN(x) (sizeof(x) / sizeof((x)[0])) /* -------------------------------------------------------------------------- */ +struct parameters_t +{ + char *configDir; + char *userDir; + char *modelDir; +}; +/* + * Structure describing an arrow + */ +struct arrow_t { + uint load; + uint site; + uint x; + uint y; + uint z; +}; diff --git a/include/cmds.h b/include/cmds.h index f5e6293..8c0fe70 100644 --- a/include/cmds.h +++ b/include/cmds.h @@ -32,26 +32,26 @@ /* -------------------------------------------------------------------------- */ -struct command_t { - const char *name; - char* (*execute)(char*, char**, Server_t*); - const char *help; -} typedef command_t; +// struct command_t { +// const char *name; +// char* (*execute)(char*, char**, Server_t*); +// const char *help; +// } typedef command_t; /* -------------------------------------------------------------------------- */ // // Existing commands // -char *Cmd_model(char*, char**, Server_t*); -char *cmd_shutdown(char*, char**, Server_t*); -char *cmd_help(char*, char**, Server_t*); +// char *Cmd_model(char*, char**, Server_t*); +// char *cmd_shutdown(char*, char**, Server_t*); +// char *cmd_help(char*, char**, Server_t*); -static command_t cmdList[] = -{ - {"help", cmd_help, "Help command"}, - {"model", cmd_model, "Model command"}, - {"shutdown", cmd_shutdown, "Shutdown command"}, -}; +// static command_t cmdList[] = +// { +// {"help", cmd_help, "Help command"}, +// {"model", cmd_model, "Model command"}, +// {"shutdown", cmd_shutdown, "Shutdown command"}, +// }; /* -------------------------------------------------------------------------- */ diff --git a/include/model.h b/include/model.h index 42d2946..372387a 100644 --- a/include/model.h +++ b/include/model.h @@ -29,8 +29,8 @@ #include #include #include -#include -#include +#include +#include /* -------------------------------------------------------------------------- */ @@ -51,44 +51,44 @@ // -------------------------------------------------------------------------- // // Model init function (and model discovery) // // -------------------------------------------------------------------------- // -void model_system_init (parameters_t *parameters); +// void model_system_init (parameters_t *parameters); // -------------------------------------------------------------------------- // // Model stopping function // // -------------------------------------------------------------------------- // -void model_system_shutdown (void); +// void model_system_shutdown (void); // -------------------------------------------------------------------------- // // Load a model ready to execute // // -------------------------------------------------------------------------- // -int model_load (int id); +// int model_load (int id); // -------------------------------------------------------------------------- // // Unload a model // // -------------------------------------------------------------------------- // -int model_unload (int id); +// int model_unload (int id); // -------------------------------------------------------------------------- // // Add a model to the known model list // // -------------------------------------------------------------------------- // -void model_add_to_known (model_t **newModel); +// void model_add_to_known (model_t **newModel); // -------------------------------------------------------------------------- // // Print informations about all models (known and loaded) to the client // // -------------------------------------------------------------------------- // -void print_models (char *buf); +// void print_models (char *buf); // -------------------------------------------------------------------------- // // Launch a model execution // // -------------------------------------------------------------------------- // -int model_run (int id); +// int model_run (int id); // -------------------------------------------------------------------------- // // Stop a model execution // // -------------------------------------------------------------------------- // -int model_stop (int id); +// int model_stop (int id); // -------------------------------------------------------------------------- // // Stop and unload all loaded or running model // // -------------------------------------------------------------------------- // -void model_shutdown (void); +// void model_shutdown (void); diff --git a/include/parsing.h b/include/parsing.h deleted file mode 100644 index 06e9ec0..0000000 --- a/include/parsing.h +++ /dev/null @@ -1,39 +0,0 @@ -//=-------------------------------------------------------------------------=// -// XML parsing module // -// // -// Copyright © 2021 Libre en Communs (contact@a-lec.org) // -// Copyright © 2021 Adrien Bourmault (neox@a-lec.org) // -// // -// This file is part of gem-graph. // -// // -// This program is free software: you can redistribute it and/or modify // -// it under the terms of the GNU Affero General Public License as // -// published by the Free Software Foundation, either version 3 of the // -// License, or (at your option) any later version. // -// // -// This program is distributed in the hope that it will be useful, // -// but WITHOUT ANY WARRANTY; without even the implied warranty of // -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // -// GNU Affero General Public License for more details. // -// // -// You should have received a copy of the GNU Affero General Public License // -// along with this program. If not, see . // -//=-------------------------------------------------------------------------=// - -#pragma once -#ifndef BASE_H - #include "../include/base.h" -#endif - -#include -#include -#include -#include - -/* -------------------------------------------------------------------------- */ - -#define NO_DESTINATION NULL - -/* -------------------------------------------------------------------------- */ - - diff --git a/include/scheduler.h b/include/scheduler.h index 0985ce1..449a5e8 100644 --- a/include/scheduler.h +++ b/include/scheduler.h @@ -32,40 +32,40 @@ // -------------------------------------------------------------------------- // // Scheduler init function // // -------------------------------------------------------------------------- // -void sched_init(scheduler_t *scheduler); +//void sched_init(scheduler_t *scheduler); // -------------------------------------------------------------------------- // // Scheduler content destructor function // // -------------------------------------------------------------------------- // -static inline void sched_clean (scheduler_t *scheduler) -{ - assert(scheduler); +// static inline void sched_clean (scheduler_t *scheduler) +// { +// assert(scheduler); - if (scheduler->globalDrawingSpace) { - if (scheduler->globalDrawingSpace->space) { - free(scheduler->globalDrawingSpace->space); - scheduler->globalDrawingSpace->space = NULL; - } - free(scheduler->globalDrawingSpace); - scheduler->globalDrawingSpace = NULL; - } +// if (scheduler->globalDrawingSpace) { +// if (scheduler->globalDrawingSpace->space) { +// free(scheduler->globalDrawingSpace->space); +// scheduler->globalDrawingSpace->space = NULL; +// } +// free(scheduler->globalDrawingSpace); +// scheduler->globalDrawingSpace = NULL; +// } - if (scheduler->arrowArray) { - if (scheduler->arrowArray->array) { - free(scheduler->arrowArray->array); - scheduler->arrowArray->array = NULL; - } - free(scheduler->arrowArray); - scheduler->arrowArray = NULL; - } -} +// if (scheduler->arrowArray) { +// if (scheduler->arrowArray->array) { +// free(scheduler->arrowArray->array); +// scheduler->arrowArray->array = NULL; +// } +// free(scheduler->arrowArray); +// scheduler->arrowArray = NULL; +// } +// } // -------------------------------------------------------------------------- // // Scheduler destructor function // // -------------------------------------------------------------------------- // -static inline void sched_shutdown (scheduler_t *scheduler) -{ - assert(scheduler); - free(scheduler); - scheduler = NULL; -} +// static inline void sched_shutdown (scheduler_t *scheduler) +// { +// assert(scheduler); +// free(scheduler); +// scheduler = NULL; +// } diff --git a/include/server.h b/include/server.h index 92e53a3..6fb2137 100644 --- a/include/server.h +++ b/include/server.h @@ -37,4 +37,4 @@ // -------------------------------------------------------------------------- // // Server init function // // -------------------------------------------------------------------------- // -void server_init (server_t *server); +//void server_init (server_t *server); diff --git a/include/supervisor.h b/include/supervisor.h index 144d296..8a9630b 100644 --- a/include/supervisor.h +++ b/include/supervisor.h @@ -30,12 +30,12 @@ // -------------------------------------------------------------------------- // // Supervisor init function // // -------------------------------------------------------------------------- // -void superv_init (supervisor_t *supervisor); +//void superv_init (supervisor_t *supervisor); // -------------------------------------------------------------------------- // // Supervisor destructor function // // -------------------------------------------------------------------------- // -static inline void superv_destroy (supervisor_t *supervisor) -{ - ; -} +// static inline void superv_destroy (supervisor_t *supervisor) +// { +// ; +// } diff --git a/include/worker.h b/include/worker.h index bee79c6..1bdd01b 100644 --- a/include/worker.h +++ b/include/worker.h @@ -30,18 +30,18 @@ // -------------------------------------------------------------------------- // // Worker init function // // -------------------------------------------------------------------------- // -void worker_init(worker_t *worker); +//void worker_init(worker_t *worker); // -------------------------------------------------------------------------- // // Worker destructor function // // -------------------------------------------------------------------------- // -static inline void worker_destroy(worker_t *worker) -{ - worker->pleaseStop = false; - worker->terminated = false; - worker->returnValue = false; +// static inline void worker_destroy(worker_t *worker) +// { +// worker->pleaseStop = false; +// worker->terminated = false; +// worker->returnValue = false; - worker->id = 0; - free(worker->localWorkAreaCenter); - worker->localWorkAreaCenter = NULL; -} +// worker->id = 0; +// free(worker->localWorkAreaCenter); +// worker->localWorkAreaCenter = NULL; +// } diff --git a/src/main.c b/src/main.c index 2c004dd..272f55e 100644 --- a/src/main.c +++ b/src/main.c @@ -26,15 +26,15 @@ /* -------------------------------------------------------------------------- */ -static server_t *server; +/* static server_t *server; */ /* static void sig_term_handler(int signum) */ /* { */ /* server->pleaseStop = true; */ -/* printLog("Server stopping\n"); */ +/* printlog("Server stopping\n"); */ /* //XXX ModelShutdown(); */ -/* printLog("All model shutted down\n"); */ +/* printlog("All model shutted down\n"); */ /* } */ int main(int argc, char **argv) @@ -42,27 +42,27 @@ int main(int argc, char **argv) int options; time_t t; int returnValue = 0; - parameters_t parameters; + struct parameters_t parameters = {0}; while ((options = getopt(argc, argv, ":C:M:U:")) != -1) { switch (options) { case 'C': - printLog("Config : %s\n", optarg); + printlog("Config : %s\n", optarg); parameters.configDir = calloc(1, strlen(optarg) + 1); strcpy(parameters.configDir, optarg); break; case 'M': - printLog("Models : %s\n", optarg); + printlog("Models : %s\n", optarg); parameters.modelDir = calloc(1, strlen(optarg) + 1); strcpy(parameters.modelDir, optarg); break; case 'U': - printLog("Users : %s\n", optarg); + printlog("Users : %s\n", optarg); parameters.userDir = calloc(1, strlen(optarg) + 1); strcpy(parameters.userDir, optarg); break; case ':': - printErr("Option missing argument : %c\n", optopt); + printerr("Option missing argument : %c\n", optopt); if (parameters.configDir) free(parameters.configDir); @@ -75,7 +75,7 @@ int main(int argc, char **argv) return ENOSYS; break; case '?': - printErr("Unknown option : %c\n", optopt); + printerr("Unknown option : %c\n", optopt); if (parameters.configDir) free(parameters.configDir); @@ -91,7 +91,7 @@ int main(int argc, char **argv) } if (!parameters.configDir | !parameters.modelDir | !parameters.userDir) { - printErr("Missing arguments\n"); + printerr("Missing arguments\n"); if (parameters.configDir) free(parameters.configDir); @@ -106,15 +106,15 @@ int main(int argc, char **argv) } // Go! - printLog("Starting gem-graph-server...\n"); + printlog("Starting gem-graph-server...\n"); /* // Register new interrupt handler */ /* signal(SIGINT, sig_term_handler); */ /* signal(SIGTERM, sig_term_handler); */ - /* // Initializing random generator */ - /* t = time(&t); */ - /* srand((unsigned) t); */ + // Initializing random generator + t = time(&t); + srand((unsigned)t); /* server = calloc(1, sizeof(*server)); */ diff --git a/src/model.c b/src/model.c index fb2ddae..ecd50ae 100644 --- a/src/model.c +++ b/src/model.c @@ -21,7 +21,6 @@ //=-------------------------------------------------------------------------=// #include "../include/model.h" -#include "../include/parsing.h" #include "../include/arrows.h" #include "../include/scheduler.h" @@ -57,13 +56,13 @@ bool model_init(const char *content, size_t length, const char *basename) node = xmlDocGetRootElement(model); if (node == NULL) { - g_printerr("Empty XML model !\n"); + fprintf(stderr, "Empty XML model !\n"); xmlFreeDoc(model); return false; } if (xmlStrcmp(node->name, (xmlChar *) "gem-graph-model")) { - g_printerr("document of the wrong type, root node != gem-graph-model\n"); + fprintf(stderr, "document of the wrong type, root node != gem-graph-model\n"); xmlFreeDoc(model); return false; } @@ -71,7 +70,7 @@ bool model_init(const char *content, size_t length, const char *basename) model_hashtable = xmlHashCreate(0); if (model_hashtable == NULL) { - g_printerr("Can't create model hash table !\n"); + fprintf(stderr, "Can't create model hash table !\n"); xmlFreeDoc(model); return false; } @@ -117,9 +116,6 @@ static inline xmlChar* getFirstTag(xmlChar *path) path = (xmlChar *)xmlStrchr(path, '/'); path = xmlStrsub (path, 1, xmlStrlen(path)); - //printf("%s = %s + / + %s\n", preop,\ - xmlStrsub (preop, 0, xmlStrlen(preop) - xmlStrlen(path) - 1), path); - return xmlStrsub (preop, 0, xmlStrlen(preop) - xmlStrlen(path) - 1); } diff --git a/src/server.c b/src/server.c index a82fc68..9b89ffa 100644 --- a/src/server.c +++ b/src/server.c @@ -25,223 +25,223 @@ /* -------------------------------------------------------------------------- */ -static void *server_main(void *server); +/* static void *server_main(void *server); */ /* -------------------------------------------------------------------------- */ -#define SEND_BUFFER_SIZE 80 * 24 -#define RECEIVE_BUFFER_SIZE 80 -void *server_communication_instance(void *serverCom) -{ - servercom_t *args; - char **argv = NULL; - char receiveBuff[RECEIVE_BUFFER_SIZE]; - char sendBuff[SEND_BUFFER_SIZE]; - int tokenIndex, bytesReceived, clientPort; - char clientIP[16]; +/* #define SEND_BUFFER_SIZE 80 * 24 */ +/* #define RECEIVE_BUFFER_SIZE 80 */ +/* void *server_communication_instance(void *serverCom) */ +/* { */ +/* servercom_t *args; */ +/* char **argv = NULL; */ +/* char receiveBuff[RECEIVE_BUFFER_SIZE]; */ +/* char sendBuff[SEND_BUFFER_SIZE]; */ +/* int tokenIndex, bytesReceived, clientPort; */ +/* char clientIP[16]; */ - args = (servercom_t*) serverCom; +/* args = (servercom_t*) serverCom; */ - // Get ip address from client - inet_ntop(AF_INET, - &args->clientAddr.sin_addr, - clientIP, - args->sockLen); +/* // Get ip address from client */ +/* inet_ntop(AF_INET, */ +/* &args->clientAddr.sin_addr, */ +/* clientIP, */ +/* args->sockLen); */ - // Get port number from client - clientPort = ntohs(args->clientAddr.sin_port); +/* // Get port number from client */ +/* clientPort = ntohs(args->clientAddr.sin_port); */ - //Accept and incoming connection - while(!args->pleaseStop) { +/* //Accept and incoming connection */ +/* while(!args->pleaseStop) { */ - // Zeroing buffer - bzero(receiveBuff, RECEIVE_BUFFER_SIZE); - //printLog("Waiting for commands...\n"); +/* // Zeroing buffer */ +/* bzero(receiveBuff, RECEIVE_BUFFER_SIZE); */ +/* //printLog("Waiting for commands...\n"); */ - // Read the message from client and copy it in buffer - bytesReceived = recv(args->sockfd, receiveBuff, RECEIVE_BUFFER_SIZE, 0); - if (bytesReceived == -1) { - printErr("Could not receive data! (%s)\n", strerror(errno)); - break; - }; +/* // Read the message from client and copy it in buffer */ +/* bytesReceived = recv(args->sockfd, receiveBuff, RECEIVE_BUFFER_SIZE, 0); */ +/* if (bytesReceived == -1) { */ +/* printErr("Could not receive data! (%s)\n", strerror(errno)); */ +/* break; */ +/* }; */ - // Ignore null-sized request - if (bytesReceived == 0) - break; +/* // Ignore null-sized request */ +/* if (bytesReceived == 0) */ +/* break; */ - // Print buffer which contains the client request - printLog("Client %s:%d request : '%s'\n", - clientIP, - clientPort, - receiveBuff); +/* // Print buffer which contains the client request */ +/* printLog("Client %s:%d request : '%s'\n", */ +/* clientIP, */ +/* clientPort, */ +/* receiveBuff); */ - if (receiveBuff[0] == '\0') //XXX - break; +/* if (receiveBuff[0] == '\0') //XXX */ +/* break; */ - // get args in an array - tokenIndex = 0; - argv = (char**) realloc(argv, 1 * sizeof(char*)); - argv[0] = strtok(receiveBuff, " "); - while (argv[tokenIndex]) { - tokenIndex++; - argv = (char**) realloc(argv, (tokenIndex+1) * sizeof(char*)); - argv[tokenIndex] = strtok(NULL, " "); - } +/* // get args in an array */ +/* tokenIndex = 0; */ +/* argv = (char**) realloc(argv, 1 * sizeof(char*)); */ +/* argv[0] = strtok(receiveBuff, " "); */ +/* while (argv[tokenIndex]) { */ +/* tokenIndex++; */ +/* argv = (char**) realloc(argv, (tokenIndex+1) * sizeof(char*)); */ +/* argv[tokenIndex] = strtok(NULL, " "); */ +/* } */ - // Zeroing buffer - bzero(sendBuff, RECEIVE_BUFFER_SIZE); +/* // Zeroing buffer */ +/* bzero(sendBuff, RECEIVE_BUFFER_SIZE); */ - // Execute command by first arg in cmdList - for (int i = 0; i < LEN(cmdList); i++) { - if (strcmp(cmdList[i].name, argv[0]) == 0) { - cmdList[i].execute(sendBuff, argv, args->associatedServer); - } - } +/* // Execute command by first arg in cmdList */ +/* for (int i = 0; i < LEN(cmdList); i++) { */ +/* if (strcmp(cmdList[i].name, argv[0]) == 0) { */ +/* cmdList[i].execute(sendBuff, argv, args->associatedServer); */ +/* } */ +/* } */ - // and send that buffer to client - send(args->sockfd, sendBuff, SEND_BUFFER_SIZE, 0); - } +/* // and send that buffer to client */ +/* send(args->sockfd, sendBuff, SEND_BUFFER_SIZE, 0); */ +/* } */ - close(args->sockfd); - if (argv) { - free(argv); - argv = NULL; - } - printLog("Disconnected from %s:%d\n", clientIP, clientPort); - return NULL; -} +/* close(args->sockfd); */ +/* if (argv) { */ +/* free(argv); */ +/* argv = NULL; */ +/* } */ +/* printLog("Disconnected from %s:%d\n", clientIP, clientPort); */ +/* return NULL; */ +/* } */ -// -------------------------------------------------------------------------- // -// Server main function // -// -------------------------------------------------------------------------- // -#define PORT 9000 -#define MAX_CONNECTION 100 +/* // -------------------------------------------------------------------------- // */ +/* // Server main function // */ +/* // -------------------------------------------------------------------------- // */ +/* #define PORT 9000 */ +/* #define MAX_CONNECTION 100 */ -static void *server_main(void *server) -{ - Server_t *args; - ServerCommunication_t serverSlots[MAX_CONNECTION] = {0}; - int connfd, flags, threadStatus, serverSlotIndex = 0; - uint socklen; - struct sockaddr_in servaddr; - char clientIP[16]; +/* static void *server_main(void *server) */ +/* { */ +/* Server_t *args; */ +/* ServerCommunication_t serverSlots[MAX_CONNECTION] = {0}; */ +/* int connfd, flags, threadStatus, serverSlotIndex = 0; */ +/* uint socklen; */ +/* struct sockaddr_in servaddr; */ +/* char clientIP[16]; */ - // Get args - args = (Server_t*) server; - args->returnValue = 0; - printLog("Server #%lu online\n", args->id); +/* // Get args */ +/* args = (Server_t*) server; */ +/* args->returnValue = 0; */ +/* printLog("Server #%lu online\n", args->id); */ - // Create socket - args->sockfd = socket(AF_INET, SOCK_STREAM, 0); - if (args->sockfd == -1) { - printErr("Socket creation failed! (%s)\n", strerror(errno)); - args->returnValue = 1; - goto serverExiting; - } +/* // Create socket */ +/* args->sockfd = socket(AF_INET, SOCK_STREAM, 0); */ +/* if (args->sockfd == -1) { */ +/* printErr("Socket creation failed! (%s)\n", strerror(errno)); */ +/* args->returnValue = 1; */ +/* goto serverExiting; */ +/* } */ - // Get socket flags - flags = fcntl(args->sockfd, F_GETFL); - if (flags == -1) { - printErr("Socket parameters getting failed! (%s)\n", strerror(errno)); - args->returnValue = 1; - goto serverExiting; - } +/* // Get socket flags */ +/* flags = fcntl(args->sockfd, F_GETFL); */ +/* if (flags == -1) { */ +/* printErr("Socket parameters getting failed! (%s)\n", strerror(errno)); */ +/* args->returnValue = 1; */ +/* goto serverExiting; */ +/* } */ - // Change socket flags to non-blocking - if (fcntl(args->sockfd, F_SETFL, flags | O_NONBLOCK) < 0) { - printErr("Socket non-blocking setting failed! (%s)\n", strerror(errno)); - args->returnValue = 1; - goto serverExiting; - } +/* // Change socket flags to non-blocking */ +/* if (fcntl(args->sockfd, F_SETFL, flags | O_NONBLOCK) < 0) { */ +/* printErr("Socket non-blocking setting failed! (%s)\n", strerror(errno)); */ +/* args->returnValue = 1; */ +/* goto serverExiting; */ +/* } */ - // Prepare binding structure - bzero(&servaddr, sizeof(servaddr)); +/* // Prepare binding structure */ +/* bzero(&servaddr, sizeof(servaddr)); */ - // Assign IP and PORT - servaddr.sin_family = AF_INET; - servaddr.sin_addr.s_addr = htonl(INADDR_ANY); - servaddr.sin_port = htons(PORT); +/* // Assign IP and PORT */ +/* servaddr.sin_family = AF_INET; */ +/* servaddr.sin_addr.s_addr = htonl(INADDR_ANY); */ +/* servaddr.sin_port = htons(PORT); */ - // Binding newly created socket - if ((bind(args->sockfd, (struct sockaddr*)&servaddr, - sizeof(servaddr))) == -1) { - printErr("Socket bind failed! (%s)\n", strerror(errno)); - args->returnValue = 1; - goto serverExiting; - } +/* // Binding newly created socket */ +/* if ((bind(args->sockfd, (struct sockaddr*)&servaddr, */ +/* sizeof(servaddr))) == -1) { */ +/* printErr("Socket bind failed! (%s)\n", strerror(errno)); */ +/* args->returnValue = 1; */ +/* goto serverExiting; */ +/* } */ - // Now server is ready to listen and verification - if (listen(args->sockfd, MAX_CONNECTION) == -1) { - printErr("Socket listening failed! (%s)\n", strerror(errno)); - args->returnValue = 1; - goto serverExiting; - } +/* // Now server is ready to listen and verification */ +/* if (listen(args->sockfd, MAX_CONNECTION) == -1) { */ +/* printErr("Socket listening failed! (%s)\n", strerror(errno)); */ +/* args->returnValue = 1; */ +/* goto serverExiting; */ +/* } */ - socklen = sizeof(struct sockaddr_in); +/* socklen = sizeof(struct sockaddr_in); */ - // Get server socket address structure - if (getsockname(args->sockfd, (struct sockaddr *)&servaddr, &socklen) - == -1) { - printErr("Could not get socket structure! (%s)\n", strerror(errno)); - args->returnValue = 1; - goto serverExiting; - } +/* // Get server socket address structure */ +/* if (getsockname(args->sockfd, (struct sockaddr *)&servaddr, &socklen) */ +/* == -1) { */ +/* printErr("Could not get socket structure! (%s)\n", strerror(errno)); */ +/* args->returnValue = 1; */ +/* goto serverExiting; */ +/* } */ - printLog("Server listening...\n"); +/* printLog("Server listening...\n"); */ - while (!args->pleaseStop) { +/* while (!args->pleaseStop) { */ - // Accept the data packet from client - connfd = accept(args->sockfd, - (struct sockaddr*) &serverSlots[serverSlotIndex].clientAddr, - &socklen); - if (connfd < 0) { - // If error is not due to lack of clients connecting, this is error - if (errno != EWOULDBLOCK && errno != EAGAIN) { - printErr("Server acccept failed! (%s)\n", strerror(errno)); - goto serverExiting; - } - sleep(1); - } else { - // Client connected - // get IP addr from client - inet_ntop(AF_INET, - &(serverSlots[serverSlotIndex].clientAddr.sin_addr), - clientIP, socklen); - printLog("Client accepted from %s:%d\n", - clientIP, - ntohs(serverSlots[serverSlotIndex].clientAddr.sin_port)); // TODO envisager déplacement dans thread +/* // Accept the data packet from client */ +/* connfd = accept(args->sockfd, */ +/* (struct sockaddr*) &serverSlots[serverSlotIndex].clientAddr, */ +/* &socklen); */ +/* if (connfd < 0) { */ +/* // If error is not due to lack of clients connecting, this is error */ +/* if (errno != EWOULDBLOCK && errno != EAGAIN) { */ +/* printErr("Server acccept failed! (%s)\n", strerror(errno)); */ +/* goto serverExiting; */ +/* } */ +/* sleep(1); */ +/* } else { */ +/* // Client connected */ +/* // get IP addr from client */ +/* inet_ntop(AF_INET, */ +/* &(serverSlots[serverSlotIndex].clientAddr.sin_addr), */ +/* clientIP, socklen); */ +/* printLog("Client accepted from %s:%d\n", */ +/* clientIP, */ +/* ntohs(serverSlots[serverSlotIndex].clientAddr.sin_port)); // TODO envisager déplacement dans thread */ - // Populate communicator slot - serverSlots[serverSlotIndex].sockLen = socklen; // XXX - serverSlots[serverSlotIndex].sockfd = connfd; - serverSlots[serverSlotIndex].associatedServer = args; +/* // Populate communicator slot */ +/* serverSlots[serverSlotIndex].sockLen = socklen; // XXX */ +/* serverSlots[serverSlotIndex].sockfd = connfd; */ +/* serverSlots[serverSlotIndex].associatedServer = args; */ - // Create thread - threadStatus = pthread_create(&serverSlots[serverSlotIndex].id, - NULL, - server_communication_instance, - (void*)&serverSlots[serverSlotIndex]); - if(threadStatus != 0) { - printErr("Error from pthread: %d (%s)\n", - threadStatus, strerror(errno)); - args->returnValue = 1; - goto serverExiting; - } +/* // Create thread */ +/* threadStatus = pthread_create(&serverSlots[serverSlotIndex].id, */ +/* NULL, */ +/* server_communication_instance, */ +/* (void*)&serverSlots[serverSlotIndex]); */ +/* if(threadStatus != 0) { */ +/* printErr("Error from pthread: %d (%s)\n", */ +/* threadStatus, strerror(errno)); */ +/* args->returnValue = 1; */ +/* goto serverExiting; */ +/* } */ - serverSlotIndex++; - //printLog("Accepted connection. Server will now listen...\n"); - } - } +/* serverSlotIndex++; */ +/* //printLog("Accepted connection. Server will now listen...\n"); */ +/* } */ +/* } */ -serverExiting: - for (int i; i < serverSlotIndex; i++) { - serverSlots[i].pleaseStop = true; - usleep(10000); - pthread_cancel(serverSlots[i].id); - pthread_join(serverSlots[i].id, NULL); - } - close(args->sockfd); - printLog("Server #%lu offline\n", args->id); - return NULL; -} +/* serverExiting: */ +/* for (int i; i < serverSlotIndex; i++) { */ +/* serverSlots[i].pleaseStop = true; */ +/* usleep(10000); */ +/* pthread_cancel(serverSlots[i].id); */ +/* pthread_join(serverSlots[i].id, NULL); */ +/* } */ +/* close(args->sockfd); */ +/* printLog("Server #%lu offline\n", args->id); */ +/* return NULL; */ +/* } */ -- 2.39.5 From f4a730109d2ed15f1d4a4b522e85b683cfdf8119 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Thu, 14 Mar 2024 23:15:18 +0200 Subject: [PATCH 04/24] WIP: ready to work (2) --- include/base.h | 10 ++++++++++ include/model.h | 11 +++++++++++ include/scheduler.h | 2 +- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/include/base.h b/include/base.h index 4302918..eec6641 100644 --- a/include/base.h +++ b/include/base.h @@ -62,3 +62,13 @@ struct arrow_t { uint y; uint z; }; + +struct model_t { + int id; +} + +struct scheduler_t +{ + int id; + struct model_t *models; +}; diff --git a/include/model.h b/include/model.h index 372387a..a991392 100644 --- a/include/model.h +++ b/include/model.h @@ -48,6 +48,17 @@ /* -------------------------------------------------------------------------- */ +bool model_init(const char *content, size_t length, const char *basename); +bool model_shutdown(void); + +char model_get_dim(void); +long model_get_dim_value(const char *axis); +char model_get_multiplicity(void); +bool model_get_next_state(char *new_state_id); +bool model_get_next_arrow(struct arrow_t *new_arrow, + const char *state_id, + char dimension); + // -------------------------------------------------------------------------- // // Model init function (and model discovery) // // -------------------------------------------------------------------------- // diff --git a/include/scheduler.h b/include/scheduler.h index 449a5e8..1a4b057 100644 --- a/include/scheduler.h +++ b/include/scheduler.h @@ -32,7 +32,7 @@ // -------------------------------------------------------------------------- // // Scheduler init function // // -------------------------------------------------------------------------- // -//void sched_init(scheduler_t *scheduler); +void sched_init(struct scheduler_t *scheduler); // -------------------------------------------------------------------------- // // Scheduler content destructor function // -- 2.39.5 From cb434fc4dcf55b63482f75cf094e7558b593ebca Mon Sep 17 00:00:00 2001 From: "Adrien Bourmault (neox)" Date: Fri, 15 Mar 2024 23:10:10 +0200 Subject: [PATCH 05/24] WIP: thread structure ready ! --- include/base.h | 87 ++++++++++++++++++++++++++++++++++++++++----- include/centers.h | 30 ---------------- include/model.h | 31 ++++++++++------ include/scheduler.h | 18 ++++++++-- include/worker.h | 16 ++++++++- manifest.scm | 1 + src/centers.c | 27 -------------- src/cmds.c | 1 + src/main.c | 10 ++++-- src/model.c | 3 ++ src/scheduler.c | 58 ++++++++++++++++++++++++++++-- src/server.c | 1 + src/supervisor.c | 1 + src/worker.c | 23 ++++++++++++ 14 files changed, 222 insertions(+), 85 deletions(-) delete mode 100644 include/centers.h delete mode 100644 src/centers.c diff --git a/include/base.h b/include/base.h index eec6641..45f0157 100644 --- a/include/base.h +++ b/include/base.h @@ -22,7 +22,6 @@ #include #include -#include #include #include #include @@ -31,16 +30,21 @@ #include #include #include +#include #define BASE_H /* -------------------------------------------------------------------------- */ #define LOGMSG "<%s:%s()>" -#define printlog(FORMAT, ...) printf("\e[0m" LOGMSG " " FORMAT "\e[0m", \ - __FILE__,__func__, ##__VA_ARGS__) -#define printerr(FORMAT,...) fprintf(stderr, "\e[0m" LOGMSG " " FORMAT "\e[0m",\ - __FILE__,__func__, ##__VA_ARGS__) + +#define printlog(FORMAT, ...) printf("\e[0;36m" LOGMSG "\e[0m" " " FORMAT \ + "\e[0m", __FILE__,__func__, ##__VA_ARGS__) + +#define printerr(FORMAT,...) fprintf(stderr, "\e[0;31m" LOGMSG "\e[0m" " " \ + FORMAT "\e[0m", __FILE__,__func__, \ + ##__VA_ARGS__) + #define LEN(x) (sizeof(x) / sizeof((x)[0])) /* -------------------------------------------------------------------------- */ @@ -63,12 +67,79 @@ struct arrow_t { uint z; }; -struct model_t { +struct space_unit_t +{ + bool lock; + struct arrow_t **sites; // Array of struct arrow_t* elements + // - lenght is multiplicity +}; + +struct space_t +{ + // Dimensions of space. + // Note that a value 0 is not allowed, minimum is 1 + int x_dim; + int y_dim; + int z_dim; + + struct space_unit_t *units; // (flat) arraw of space_unit_t elements : + // - lenght is x_dim * y_dim * z_dim + // - access to the (x,y,z) is done by + // units[ x + // + dim_x * y + // + dim_x * dim_y * z ] +}; + +struct state_t +{ + // Metadata int id; -} + int owner_id; + time_t date; + + struct space_t *space; +}; + +struct model_t { + + // Metadata + int id; + int owner_id; + time_t date; + union version + { + int major; + int minor; + }; + + // User friendly metadata + char *owner_name; + char *model_name; + + // Model parameters + int multiplicity; // number of sites in a space_unit + int dimension; // number of space dimensions + + // Simulation parameters + int max_thread; + int max_cycles; + + // Handler to the current space of the model + struct space_t *space; + + // Handler to the saved states of the model + struct state_t **states; + +}; + +struct worker_t +{ + int id; + int status; +}; struct scheduler_t { int id; - struct model_t *models; + struct model_t **model; // Queue (array) of waiting models }; diff --git a/include/centers.h b/include/centers.h deleted file mode 100644 index 27e9e74..0000000 --- a/include/centers.h +++ /dev/null @@ -1,30 +0,0 @@ -//=-------------------------------------------------------------------------=// -// Local workers definition // -// // -// Copyright © 2021 Libre en Communs (contact@a-lec.org) // -// Copyright © 2021 Adrien Bourmault (neox@a-lec.org) // -// // -// This file is part of gem-graph. // -// // -// This program is free software: you can redistribute it and/or modify // -// it under the terms of the GNU Affero General Public License as // -// published by the Free Software Foundation, either version 3 of the // -// License, or (at your option) any later version. // -// // -// This program is distributed in the hope that it will be useful, // -// but WITHOUT ANY WARRANTY; without even the implied warranty of // -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // -// GNU Affero General Public License for more details. // -// // -// You should have received a copy of the GNU Affero General Public License // -// along with this program. If not, see . // -//=-------------------------------------------------------------------------=// - -#pragma once -#ifndef BASE_H - #include "../include/base.h" -#endif - -/* -------------------------------------------------------------------------- */ - - diff --git a/include/model.h b/include/model.h index a991392..154aa9b 100644 --- a/include/model.h +++ b/include/model.h @@ -48,17 +48,6 @@ /* -------------------------------------------------------------------------- */ -bool model_init(const char *content, size_t length, const char *basename); -bool model_shutdown(void); - -char model_get_dim(void); -long model_get_dim_value(const char *axis); -char model_get_multiplicity(void); -bool model_get_next_state(char *new_state_id); -bool model_get_next_arrow(struct arrow_t *new_arrow, - const char *state_id, - char dimension); - // -------------------------------------------------------------------------- // // Model init function (and model discovery) // // -------------------------------------------------------------------------- // @@ -74,11 +63,15 @@ bool model_get_next_arrow(struct arrow_t *new_arrow, // -------------------------------------------------------------------------- // // int model_load (int id); +bool model_init(const char *content, size_t length, const char *basename); + // -------------------------------------------------------------------------- // // Unload a model // // -------------------------------------------------------------------------- // // int model_unload (int id); +bool model_shutdown(void); + // -------------------------------------------------------------------------- // // Add a model to the known model list // // -------------------------------------------------------------------------- // @@ -103,3 +96,19 @@ bool model_get_next_arrow(struct arrow_t *new_arrow, // Stop and unload all loaded or running model // // -------------------------------------------------------------------------- // // void model_shutdown (void); + +// -------------------------------------------------------------------------- // +// Parsing primitives // +// -------------------------------------------------------------------------- // + +char model_get_dim(void); + +long model_get_dim_value(const char *axis); + +char model_get_multiplicity(void); + +bool model_get_next_state(char *new_state_id); + +bool model_get_next_arrow(struct arrow_t *new_arrow, + const char *state_id, + char dimension); diff --git a/include/scheduler.h b/include/scheduler.h index 1a4b057..bc36f8c 100644 --- a/include/scheduler.h +++ b/include/scheduler.h @@ -29,10 +29,24 @@ /* -------------------------------------------------------------------------- */ +enum +{ + // quitting values + SCHED_NORMAL_EXIT, + SCHED_OMP_ERROR, + SCHED_UNKNOWN_ERROR, + + // non-quitting / non-fatal errors + SCHED_NON_FATAL_ERRORS, //meta-code + SCHED_MODEL_ERROR, + + SCHED_CONTINUE +}; + // -------------------------------------------------------------------------- // -// Scheduler init function // +// Scheduler main function // // -------------------------------------------------------------------------- // -void sched_init(struct scheduler_t *scheduler); +int sched_start (struct scheduler_t *scheduler, struct parameters_t *parameters); // -------------------------------------------------------------------------- // // Scheduler content destructor function // diff --git a/include/worker.h b/include/worker.h index 1bdd01b..a95ab3c 100644 --- a/include/worker.h +++ b/include/worker.h @@ -27,10 +27,24 @@ /* -------------------------------------------------------------------------- */ +enum +{ + // quitting values + WORKER_NORMAL_EXIT, + WORKER_OMP_ERROR, + WORKER_UNKNOWN_ERROR, + + // non-quitting / non-fatal errors + WORKER_NON_FATAL_ERRORS, //meta-code + WORKER_MODEL_ERROR, + + WORKER_CONTINUE +}; + // -------------------------------------------------------------------------- // // Worker init function // // -------------------------------------------------------------------------- // -//void worker_init(worker_t *worker); +void worker_start(struct worker_t *worker, struct scheduler_t *scheduler); // -------------------------------------------------------------------------- // // Worker destructor function // diff --git a/manifest.scm b/manifest.scm index 01a3a6d..ffdc2c5 100644 --- a/manifest.scm +++ b/manifest.scm @@ -22,5 +22,6 @@ "glib" "mesa-headers" "mesa" + "libomp" ) ) diff --git a/src/centers.c b/src/centers.c deleted file mode 100644 index f9fdcdb..0000000 --- a/src/centers.c +++ /dev/null @@ -1,27 +0,0 @@ -//=-------------------------------------------------------------------------=// -// Centers management module // -// // -// Copyright © 2021 Libre en Communs (contact@a-lec.org) // -// Copyright © 2021 Adrien Bourmault (neox@a-lec.org) // -// // -// This file is part of gem-graph. // -// // -// This program is free software: you can redistribute it and/or modify // -// it under the terms of the GNU Affero General Public License as // -// published by the Free Software Foundation, either version 3 of the // -// License, or (at your option) any later version. // -// // -// This program is distributed in the hope that it will be useful, // -// but WITHOUT ANY WARRANTY; without even the implied warranty of // -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // -// GNU Affero General Public License for more details. // -// // -// You should have received a copy of the GNU Affero General Public License // -// along with this program. If not, see . // -//=-------------------------------------------------------------------------=// - -#include "../include/centers.h" - -/* -------------------------------------------------------------------------- */ - - diff --git a/src/cmds.c b/src/cmds.c index c4ea45f..306d737 100644 --- a/src/cmds.c +++ b/src/cmds.c @@ -20,6 +20,7 @@ // along with this program. If not, see . // //=-------------------------------------------------------------------------=// +#include "../include/base.h" #include "../include/cmds.h" #include "../include/scheduler.h" #include "../include/model.h" diff --git a/src/main.c b/src/main.c index 272f55e..3bd8ad7 100644 --- a/src/main.c +++ b/src/main.c @@ -21,8 +21,7 @@ //=-------------------------------------------------------------------------=// #include "../include/base.h" -#include "../include/server.h" -#include "../include/model.h" +#include "../include/scheduler.h" /* -------------------------------------------------------------------------- */ @@ -41,8 +40,9 @@ int main(int argc, char **argv) { int options; time_t t; - int returnValue = 0; + int returnValue = SCHED_CONTINUE; struct parameters_t parameters = {0}; + struct scheduler_t scheduler = {0}; while ((options = getopt(argc, argv, ":C:M:U:")) != -1) { switch (options) { @@ -133,6 +133,10 @@ int main(int argc, char **argv) /* free(server); */ /* server = NULL; */ + while (returnValue > SCHED_NON_FATAL_ERRORS) { + returnValue = sched_start (&scheduler, ¶meters); + } + free(parameters.userDir); free(parameters.modelDir); free(parameters.configDir); diff --git a/src/model.c b/src/model.c index ecd50ae..5e6e4d7 100644 --- a/src/model.c +++ b/src/model.c @@ -20,6 +20,7 @@ // along with this program. If not, see . // //=-------------------------------------------------------------------------=// +#include "../include/base.h" #include "../include/model.h" #include "../include/arrows.h" #include "../include/scheduler.h" @@ -33,6 +34,8 @@ #define SUCCESSFUL_READ_ARROW_XY (READ_SITE | READ_WEIGHT | READ_X | READ_Y) #define SUCCESSFUL_READ_ARROW_XYZ (READ_SITE | READ_WEIGHT | READ_X | READ_Y | READ_Z) +/* -------------------------------------------------------------------------- */ + static xmlDocPtr model; static xmlHashTablePtr model_hashtable; diff --git a/src/scheduler.c b/src/scheduler.c index 402e744..5f0b488 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -20,11 +20,63 @@ // along with this program. If not, see . // //=-------------------------------------------------------------------------=// +#include "../include/base.h" #include "../include/scheduler.h" -#include "../include/centers.h" #include "../include/worker.h" -#include "../include/arrows.h" -#include +/* -------------------------------------------------------------------------- */ + +static int sched_new_id (void) +{ + static int id = 0; + return id; +} + +int sched_start (struct scheduler_t *self, struct parameters_t *parameters) +{ + int n_threads = omp_get_max_threads(); + int n_arrows; + int iter_per_cycle; + struct worker_t **workers; + + self->id = sched_new_id(); + + printlog("Hey, I'm the scheduler %d and I can work with %d threads !\n", + self->id, + n_threads); + + workers = calloc(n_threads, sizeof(struct worker_t*)); + + //XXX + n_arrows = 1000; + iter_per_cycle = (n_arrows / n_threads) + 1; + + printlog("Needed iteration per cycle (%d arrows to work on) : %d\n", + n_arrows, + iter_per_cycle); + + printlog("Start of the simulation cycle\n"); + for (int iter = 0; iter < iter_per_cycle; iter++) { + printlog("Start of the thread cycle (iter %d)\n", iter); + + #pragma omp parallel + { + int thread_num; + thread_num = omp_get_thread_num(); + workers[thread_num] = calloc(1, sizeof(struct worker_t)); + + workers[thread_num]->id = thread_num; + worker_start(workers[thread_num], self); + + free(workers[thread_num]); + } + + printlog("End of the thread cycle\n"); + } + + free(workers); + + return SCHED_NORMAL_EXIT; +} diff --git a/src/server.c b/src/server.c index 9b89ffa..b2e0981 100644 --- a/src/server.c +++ b/src/server.c @@ -20,6 +20,7 @@ // along with this program. If not, see . // //=-------------------------------------------------------------------------=// +#include "../include/base.h" #include "../include/server.h" #include "../include/cmds.h" diff --git a/src/supervisor.c b/src/supervisor.c index 3d8fae5..0ff04ec 100644 --- a/src/supervisor.c +++ b/src/supervisor.c @@ -20,6 +20,7 @@ // along with this program. If not, see . // //=-------------------------------------------------------------------------=// +#include "../include/base.h" #include "../include/supervisor.h" /* -------------------------------------------------------------------------- */ diff --git a/src/worker.c b/src/worker.c index f758ed5..0ab7186 100644 --- a/src/worker.c +++ b/src/worker.c @@ -20,8 +20,31 @@ // along with this program. If not, see . // //=-------------------------------------------------------------------------=// +#include "../include/base.h" #include "../include/worker.h" /* -------------------------------------------------------------------------- */ +static int worker_new_id (void) +{ + static int id = 0; + return id; +} +void worker_start(struct worker_t *self, struct scheduler_t *scheduler) +{ + unsigned int random_time; + + random_time = (unsigned int)(rand() % 2); + + printlog("Coucou, c'est le worker %d (et je vais dormir %d s)\n", + self->id, + random_time); + + sleep(random_time); + + printlog("Fin du worker %d\n", + self->id); + + self->status = WORKER_NORMAL_EXIT; +} -- 2.39.5 From 5b4d9f0e2ea24a6d4a2893697eb7bcdc54211790 Mon Sep 17 00:00:00 2001 From: "Adrien Bourmault (neox)" Date: Fri, 15 Mar 2024 23:58:36 +0200 Subject: [PATCH 06/24] WIP: trying loop performances --- include/base.h | 1 + src/scheduler.c | 52 ++++++++++++++++++++++++++++--------------------- src/worker.c | 6 +++++- 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/include/base.h b/include/base.h index 45f0157..d6e24e6 100644 --- a/include/base.h +++ b/include/base.h @@ -135,6 +135,7 @@ struct model_t { struct worker_t { int id; + int thread_num; int status; }; diff --git a/src/scheduler.c b/src/scheduler.c index 5f0b488..43ac7df 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -36,7 +36,11 @@ int sched_start (struct scheduler_t *self, struct parameters_t *parameters) { int n_threads = omp_get_max_threads(); int n_arrows; - int iter_per_cycle; + int n_workers; + int workers_per_cycle; + int max_cycles; + int max_workers; + bool pleaseStop; struct worker_t **workers; self->id = sched_new_id(); @@ -48,33 +52,37 @@ int sched_start (struct scheduler_t *self, struct parameters_t *parameters) workers = calloc(n_threads, sizeof(struct worker_t*)); //XXX - n_arrows = 1000; - iter_per_cycle = (n_arrows / n_threads) + 1; + n_arrows = 10000000000; + max_cycles = 1; + workers_per_cycle = (n_arrows / n_threads) + 1; + max_workers = workers_per_cycle * max_cycles; - printlog("Needed iteration per cycle (%d arrows to work on) : %d\n", - n_arrows, - iter_per_cycle); + printlog("Need %d workers per cycle (%d arrows to work on) : %d\n", + workers_per_cycle, + n_arrows); - printlog("Start of the simulation cycle\n"); + pleaseStop = false; + n_workers = -1; + #pragma omp parallel + { + int thread_num; + thread_num = omp_get_thread_num(); - for (int iter = 0; iter < iter_per_cycle; iter++) { - printlog("Start of the thread cycle (iter %d)\n", iter); + while (!pleaseStop) { + /* n_workers++; */ + /* if (n_workers > max_workers) { */ + /* pleaseStop = true; */ + /* } */ - #pragma omp parallel - { - int thread_num; - thread_num = omp_get_thread_num(); - workers[thread_num] = calloc(1, sizeof(struct worker_t)); - - workers[thread_num]->id = thread_num; - worker_start(workers[thread_num], self); - - free(workers[thread_num]); - } - - printlog("End of the thread cycle\n"); + workers[thread_num] = calloc(1, sizeof(struct worker_t)); + workers[thread_num]->id = n_workers; + worker_start(workers[thread_num], self); + free(workers[thread_num]); } + } + + printlog("End of simulation\n"); free(workers); diff --git a/src/worker.c b/src/worker.c index 0ab7186..77a1d7f 100644 --- a/src/worker.c +++ b/src/worker.c @@ -25,6 +25,8 @@ /* -------------------------------------------------------------------------- */ +double truc; + static int worker_new_id (void) { static int id = 0; @@ -41,7 +43,9 @@ void worker_start(struct worker_t *self, struct scheduler_t *scheduler) self->id, random_time); - sleep(random_time); + double a = (double)self->id * 42 / ((double)self->id + 2); + truc = a; + //sleep(random_time); printlog("Fin du worker %d\n", self->id); -- 2.39.5 From 8b2f383409e0c1e54e346318f221c96a09655b29 Mon Sep 17 00:00:00 2001 From: "Adrien Bourmault (neox)" Date: Sat, 16 Mar 2024 22:59:30 +0200 Subject: [PATCH 07/24] WIP: test on modularity of scheduler --- src/scheduler.c | 49 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/src/scheduler.c b/src/scheduler.c index 43ac7df..d2a82f0 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -32,7 +32,10 @@ static int sched_new_id (void) return id; } -int sched_start (struct scheduler_t *self, struct parameters_t *parameters) + +static int sched_run(struct scheduler_t *self, + struct parameters_t *parameters, + struct model_t *model) { int n_threads = omp_get_max_threads(); int n_arrows; @@ -43,14 +46,6 @@ int sched_start (struct scheduler_t *self, struct parameters_t *parameters) bool pleaseStop; struct worker_t **workers; - self->id = sched_new_id(); - - printlog("Hey, I'm the scheduler %d and I can work with %d threads !\n", - self->id, - n_threads); - - workers = calloc(n_threads, sizeof(struct worker_t*)); - //XXX n_arrows = 10000000000; max_cycles = 1; @@ -70,15 +65,8 @@ int sched_start (struct scheduler_t *self, struct parameters_t *parameters) thread_num = omp_get_thread_num(); while (!pleaseStop) { - /* n_workers++; */ - /* if (n_workers > max_workers) { */ - /* pleaseStop = true; */ - /* } */ - - workers[thread_num] = calloc(1, sizeof(struct worker_t)); workers[thread_num]->id = n_workers; worker_start(workers[thread_num], self); - free(workers[thread_num]); } } @@ -88,3 +76,32 @@ int sched_start (struct scheduler_t *self, struct parameters_t *parameters) return SCHED_NORMAL_EXIT; } + +int sched_start (struct scheduler_t *self, struct parameters_t *parameters) +{ + int n_threads = omp_get_max_threads(); + int n_arrows; + int n_workers; + int workers_per_cycle; + int max_cycles; + int max_workers; + bool pleaseStop; + struct worker_t **workers; + + self->id = sched_new_id(); + + printlog("Hey, I'm the scheduler %d and I can work with %d threads !\n", + self->id, + n_threads); + + workers = calloc(n_threads, sizeof(struct worker_t*)); + for (int i = 0; i < n_threads; i++) { + workers[thread_num] = calloc(1, sizeof(struct worker_t)); + } + + sched_run(self, parameters, NULL); + + for (int i = 0; i < n_threads; i++) { + free(workers[thread_num]); + } +} -- 2.39.5 From 376062563276c478315d9cca1b1cdcc158a22f21 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Mon, 18 Mar 2024 23:59:00 +0200 Subject: [PATCH 08/24] WIP: working on scheduler and worker --- include/base.h | 62 ++++++++++++++++++++++--------- src/scheduler.c | 99 ++++++++++++++++++++++++++++++++----------------- src/worker.c | 21 +++-------- 3 files changed, 116 insertions(+), 66 deletions(-) diff --git a/include/base.h b/include/base.h index d6e24e6..b24d844 100644 --- a/include/base.h +++ b/include/base.h @@ -67,6 +67,24 @@ struct arrow_t { uint z; }; +/* + * Structure describing a transition + */ +struct transition_t { + uint id; + struct condition_t *parent; + struct arrow_t *arrows; +}; + +/* + * Structure describing a condition + */ +struct condition_t { + uint id; + struct condition_t *parent; + struct arrow_t *arrows; +}; + struct space_unit_t { bool lock; @@ -78,9 +96,9 @@ struct space_t { // Dimensions of space. // Note that a value 0 is not allowed, minimum is 1 - int x_dim; - int y_dim; - int z_dim; + uint x_dim; + uint y_dim; + uint z_dim; struct space_unit_t *units; // (flat) arraw of space_unit_t elements : // - lenght is x_dim * y_dim * z_dim @@ -93,8 +111,8 @@ struct space_t struct state_t { // Metadata - int id; - int owner_id; + uint id; + uint owner_id; time_t date; struct space_t *space; @@ -103,13 +121,13 @@ struct state_t struct model_t { // Metadata - int id; - int owner_id; + uint id; + uint owner_id; time_t date; union version { - int major; - int minor; + uint major; + uint minor; }; // User friendly metadata @@ -117,12 +135,12 @@ struct model_t { char *model_name; // Model parameters - int multiplicity; // number of sites in a space_unit - int dimension; // number of space dimensions + uint multiplicity; // number of sites in a space_unit + uint dimension; // number of space dimensions // Simulation parameters - int max_thread; - int max_cycles; + uint max_thread; + uint max_cycles; // Handler to the current space of the model struct space_t *space; @@ -130,17 +148,25 @@ struct model_t { // Handler to the saved states of the model struct state_t **states; + // Array of conditions + struct condition_t *conditions; + + // Array of transitions + struct transition_t *transitions; }; struct worker_t { - int id; - int thread_num; - int status; + uint id; + uint status; + struct arrow_t *elected_arrow; }; struct scheduler_t { - int id; - struct model_t **model; // Queue (array) of waiting models + uint id; + uint n_workers; + bool pleaseStop; + struct model_t **models; // Queue (array) of waiting models + struct worker_t **workers; // Workers array }; diff --git a/src/scheduler.c b/src/scheduler.c index d2a82f0..5ab624d 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -26,82 +26,115 @@ /* -------------------------------------------------------------------------- */ +// +// +// +// static int sched_new_id (void) { static int id = 0; return id; } - +// +// +// +// static int sched_run(struct scheduler_t *self, struct parameters_t *parameters, struct model_t *model) { int n_threads = omp_get_max_threads(); int n_arrows; - int n_workers; int workers_per_cycle; int max_cycles; int max_workers; - bool pleaseStop; - struct worker_t **workers; - //XXX - n_arrows = 10000000000; + self->pleaseStop = false; + self->n_workers = 0; + n_arrows = 2000; max_cycles = 1; - workers_per_cycle = (n_arrows / n_threads) + 1; - max_workers = workers_per_cycle * max_cycles; + workers_per_cycle = (int) round((double)n_arrows / (double)n_threads); + max_workers = workers_per_cycle * n_threads * max_cycles; - printlog("Need %d workers per cycle (%d arrows to work on) : %d\n", + printlog("Begin simulation of model %d with %d workers per cycle" + " (%d arrows to work on)\n", + model->id, workers_per_cycle, n_arrows); - pleaseStop = false; - n_workers = -1; - #pragma omp parallel { int thread_num; thread_num = omp_get_thread_num(); - while (!pleaseStop) { - workers[thread_num]->id = n_workers; - worker_start(workers[thread_num], self); + while (!self->pleaseStop) { + // Get an ID; + self->workers[thread_num]->id = self->n_workers; + + // Elect an arrow (XXX TODO) + self->workers[thread_num]->elected_arrow = NULL; + + // Launch evaluation + worker_start(self->workers[thread_num], self); + + // Check if this is the end + if (self->n_workers > max_workers) { + self->pleaseStop = true; + } } } - printlog("End of simulation\n"); - - free(workers); + printlog("End of simulation (%d workers executed)\n", + self->n_workers); return SCHED_NORMAL_EXIT; } +// +// +// +// int sched_start (struct scheduler_t *self, struct parameters_t *parameters) { int n_threads = omp_get_max_threads(); - int n_arrows; - int n_workers; - int workers_per_cycle; - int max_cycles; - int max_workers; - bool pleaseStop; - struct worker_t **workers; - + int n_models = 20; + int returnValue; self->id = sched_new_id(); - printlog("Hey, I'm the scheduler %d and I can work with %d threads !\n", + printlog("Scheduler initialized with %d threads\n", self->id, n_threads); - workers = calloc(n_threads, sizeof(struct worker_t*)); - for (int i = 0; i < n_threads; i++) { - workers[thread_num] = calloc(1, sizeof(struct worker_t)); + // Allocating models + self->models = calloc(n_models, sizeof(struct model_t*)); + for (int i = 0; i < n_models; i++) { + self->models[i] = calloc(1, sizeof(struct model_t)); + //XXX populate model } - sched_run(self, parameters, NULL); - + // Allocating workers + self->workers = calloc(n_threads, sizeof(struct worker_t*)); for (int i = 0; i < n_threads; i++) { - free(workers[thread_num]); + self->workers[i] = calloc(1, sizeof(struct worker_t)); } + + // Run + #pragma omp for + for (int i = 0; i < n_models; i++) + returnValue = sched_run(self, parameters, self->models[i]); + + // Freeing workers + for (int i = 0; i < n_threads; i++) { + free(self->workers[i]); + } + free(self->workers); + + // Freeing models + for (int i = 0; i < n_models; i++) { + free(self->models[i]); + } + free(self->models); + + return returnValue; } diff --git a/src/worker.c b/src/worker.c index 77a1d7f..00985e2 100644 --- a/src/worker.c +++ b/src/worker.c @@ -35,20 +35,11 @@ static int worker_new_id (void) void worker_start(struct worker_t *self, struct scheduler_t *scheduler) { - unsigned int random_time; - - random_time = (unsigned int)(rand() % 2); - - printlog("Coucou, c'est le worker %d (et je vais dormir %d s)\n", - self->id, - random_time); - - double a = (double)self->id * 42 / ((double)self->id + 2); - truc = a; - //sleep(random_time); - - printlog("Fin du worker %d\n", - self->id); - + // Locking ressources + #pragma omp critical + { + scheduler->n_workers++; + //XXX lock space units + } self->status = WORKER_NORMAL_EXIT; } -- 2.39.5 From 1625658b45b1558a7b62c769960a1208ab0d0f42 Mon Sep 17 00:00:00 2001 From: "Adrien Bourmault (neox)" Date: Wed, 20 Mar 2024 23:59:00 +0200 Subject: [PATCH 09/24] WIP: integrating with model ? --- include/arrows.h | 11 ++++- include/base.h | 111 +------------------------------------------- include/model.h | 88 ++++++++++++++++++++++++++++++++--- include/scheduler.h | 13 ++++++ include/worker.h | 16 ++++++- src/model.c | 76 ++++++++++++++++-------------- src/worker.c | 10 ++-- 7 files changed, 165 insertions(+), 160 deletions(-) diff --git a/include/arrows.h b/include/arrows.h index 27e9e74..9096a92 100644 --- a/include/arrows.h +++ b/include/arrows.h @@ -27,4 +27,13 @@ /* -------------------------------------------------------------------------- */ - +/* + * Structure describing an arrow + */ +struct arrow_t { + uint load; + uint site; + uint x; + uint y; + uint z; +}; diff --git a/include/base.h b/include/base.h index b24d844..abf3e9a 100644 --- a/include/base.h +++ b/include/base.h @@ -27,10 +27,12 @@ #include #include #include +#include #include #include #include #include +#include #define BASE_H @@ -56,117 +58,8 @@ struct parameters_t char *modelDir; }; -/* - * Structure describing an arrow - */ -struct arrow_t { - uint load; - uint site; - uint x; - uint y; - uint z; -}; -/* - * Structure describing a transition - */ -struct transition_t { - uint id; - struct condition_t *parent; - struct arrow_t *arrows; -}; -/* - * Structure describing a condition - */ -struct condition_t { - uint id; - struct condition_t *parent; - struct arrow_t *arrows; -}; -struct space_unit_t -{ - bool lock; - struct arrow_t **sites; // Array of struct arrow_t* elements - // - lenght is multiplicity -}; -struct space_t -{ - // Dimensions of space. - // Note that a value 0 is not allowed, minimum is 1 - uint x_dim; - uint y_dim; - uint z_dim; - struct space_unit_t *units; // (flat) arraw of space_unit_t elements : - // - lenght is x_dim * y_dim * z_dim - // - access to the (x,y,z) is done by - // units[ x - // + dim_x * y - // + dim_x * dim_y * z ] -}; - -struct state_t -{ - // Metadata - uint id; - uint owner_id; - time_t date; - - struct space_t *space; -}; - -struct model_t { - - // Metadata - uint id; - uint owner_id; - time_t date; - union version - { - uint major; - uint minor; - }; - - // User friendly metadata - char *owner_name; - char *model_name; - - // Model parameters - uint multiplicity; // number of sites in a space_unit - uint dimension; // number of space dimensions - - // Simulation parameters - uint max_thread; - uint max_cycles; - - // Handler to the current space of the model - struct space_t *space; - - // Handler to the saved states of the model - struct state_t **states; - - // Array of conditions - struct condition_t *conditions; - - // Array of transitions - struct transition_t *transitions; -}; - -struct worker_t -{ - uint id; - uint status; - struct arrow_t *elected_arrow; -}; - -struct scheduler_t -{ - uint id; - uint n_workers; - bool pleaseStop; - struct model_t **models; // Queue (array) of waiting models - struct worker_t **workers; // Workers array -}; diff --git a/include/model.h b/include/model.h index 154aa9b..fc1ebd7 100644 --- a/include/model.h +++ b/include/model.h @@ -32,6 +32,8 @@ #include #include +#include "../include/arrows.h" + /* -------------------------------------------------------------------------- */ #define MODEL_STRING_SIZE 64 @@ -48,6 +50,74 @@ /* -------------------------------------------------------------------------- */ +struct space_unit_t +{ + bool lock; + struct arrow_t **sites; // Array of struct arrow_t* elements + // - lenght is multiplicity +}; + +struct space_t +{ + // Dimensions of space. + // Note that a value 0 is not allowed, minimum is 1 + int x_dim; + int y_dim; + int z_dim; + + struct space_unit_t *units; // (flat) arraw of space_unit_t elements : + // - lenght is x_dim * y_dim * z_dim + // - access to the (x,y,z) is done by + // units[ x + // + dim_x * y + // + dim_x * dim_y * z ] +}; + +struct state_t +{ + // Metadata + int id; + int owner_id; + time_t date; + + struct space_t *space; +}; + +struct model_t { + + // Metadata + int id; + int owner_id; + time_t date; + union version + { + int major; + int minor; + }; + + // XML handlers + xmlDocPtr model; + xmlHashTablePtr model_hashtable; + + // User friendly metadata + char *owner_name; + char *model_name; + + // Model parameters + int multiplicity; // number of sites in a space_unit + int dimension; // number of space dimensions + + // Simulation parameters + int max_thread; + int max_cycles; + + // Handler to the current space of the model + struct space_t *space; + + // Handler to the saved states of the model + struct state_t **states; +}; + // -------------------------------------------------------------------------- // // Model init function (and model discovery) // // -------------------------------------------------------------------------- // @@ -63,14 +133,17 @@ // -------------------------------------------------------------------------- // // int model_load (int id); -bool model_init(const char *content, size_t length, const char *basename); +bool model_init(struct model_t *self, + const char *content, + size_t length, + const char *basename); // -------------------------------------------------------------------------- // // Unload a model // // -------------------------------------------------------------------------- // // int model_unload (int id); -bool model_shutdown(void); +bool model_shutdown(struct model_t *self); // -------------------------------------------------------------------------- // // Add a model to the known model list // @@ -101,14 +174,15 @@ bool model_shutdown(void); // Parsing primitives // // -------------------------------------------------------------------------- // -char model_get_dim(void); +char model_get_dim(struct model_t *self); -long model_get_dim_value(const char *axis); +long model_get_dim_value(struct model_t *self, const char *axis); -char model_get_multiplicity(void); +char model_get_multiplicity(struct model_t *self); -bool model_get_next_state(char *new_state_id); +bool model_get_next_state(struct model_t *self, char *new_state_id); -bool model_get_next_arrow(struct arrow_t *new_arrow, +bool model_get_next_arrow(struct model_t *self, + struct arrow_t *new_arrow, const char *state_id, char dimension); diff --git a/include/scheduler.h b/include/scheduler.h index bc36f8c..6306b9f 100644 --- a/include/scheduler.h +++ b/include/scheduler.h @@ -27,6 +27,9 @@ #include +#include "../include/worker.h" +#include "../include/model.h" + /* -------------------------------------------------------------------------- */ enum @@ -43,6 +46,16 @@ enum SCHED_CONTINUE }; +struct scheduler_t +{ + int id; + int n_workers; + int pleaseStop; + + struct model_t **models; + struct worker_t **workers; +}; + // -------------------------------------------------------------------------- // // Scheduler main function // // -------------------------------------------------------------------------- // diff --git a/include/worker.h b/include/worker.h index a95ab3c..978912c 100644 --- a/include/worker.h +++ b/include/worker.h @@ -25,6 +25,9 @@ #include "../include/base.h" #endif +#include "../include/scheduler.h" +#include "../include/arrows.h" + /* -------------------------------------------------------------------------- */ enum @@ -41,10 +44,21 @@ enum WORKER_CONTINUE }; +struct scheduler_t; + +struct worker_t +{ + int id; + int thread_num; + int status; + + struct arrow_t *elected_arrow; +}; + // -------------------------------------------------------------------------- // // Worker init function // // -------------------------------------------------------------------------- // -void worker_start(struct worker_t *worker, struct scheduler_t *scheduler); +void worker_start(struct worker_t *self, struct scheduler_t *scheduler); // -------------------------------------------------------------------------- // // Worker destructor function // diff --git a/src/model.c b/src/model.c index 5e6e4d7..06b8f80 100644 --- a/src/model.c +++ b/src/model.c @@ -23,7 +23,6 @@ #include "../include/base.h" #include "../include/model.h" #include "../include/arrows.h" -#include "../include/scheduler.h" #define READ_SITE 1 << 0 #define READ_WEIGHT 1 << 1 @@ -36,10 +35,10 @@ /* -------------------------------------------------------------------------- */ -static xmlDocPtr model; -static xmlHashTablePtr model_hashtable; - -bool model_init(const char *content, size_t length, const char *basename) +bool model_init(struct model_t *self, + const char *content, + size_t length, + const char *basename) { xmlNode *node; @@ -50,41 +49,41 @@ bool model_init(const char *content, size_t length, const char *basename) */ LIBXML_TEST_VERSION - model = xmlReadMemory(content, length, basename, NULL, 0); + self->model = xmlReadMemory(content, length, basename, NULL, 0); - if (model == NULL ) { + if (self->model == NULL ) { return false; } - node = xmlDocGetRootElement(model); + node = xmlDocGetRootElement(self->model); if (node == NULL) { fprintf(stderr, "Empty XML model !\n"); - xmlFreeDoc(model); + xmlFreeDoc(self->model); return false; } if (xmlStrcmp(node->name, (xmlChar *) "gem-graph-model")) { fprintf(stderr, "document of the wrong type, root node != gem-graph-model\n"); - xmlFreeDoc(model); + xmlFreeDoc(self->model); return false; } - model_hashtable = xmlHashCreate(0); + self->model_hashtable = xmlHashCreate(0); - if (model_hashtable == NULL) { + if (self->model_hashtable == NULL) { fprintf(stderr, "Can't create model hash table !\n"); - xmlFreeDoc(model); + xmlFreeDoc(self->model); return false; } return true; } -bool model_shutdown(void) +bool model_shutdown(struct model_t *self) { - xmlFreeDoc(model); - xmlHashFree(model_hashtable, NULL); + xmlFreeDoc(self->model); + xmlHashFree(self->model_hashtable, NULL); // Cleanup function for the XML library xmlCleanupParser(); @@ -95,7 +94,7 @@ bool model_shutdown(void) return true; } -/******************************************************************************/ +/* -------------------------------------------------------------------------- */ static inline xmlNodePtr getNextChild(xmlNodePtr node, xmlChar *last) { @@ -131,16 +130,16 @@ static inline xmlChar* getLastTag(xmlChar *path) return path; // which is no more the given path but only its last tag ! } -/******************************************************************************/ +/* -------------------------------------------------------------------------- */ -static xmlNodePtr model_get_node(xmlChar *path) +static xmlNodePtr model_get_node(struct model_t *self, xmlChar *path) { xmlNodePtr node; xmlChar *extrait; xmlChar *reste, *last, *affich; // Lookup for node from path in hash table - node = xmlHashLookup(model_hashtable, path); + node = xmlHashLookup(self->model_hashtable, path); // Found a node in hash table if (node) { @@ -151,7 +150,7 @@ static xmlNodePtr model_get_node(xmlChar *path) reste = path; affich = reste; last = getLastTag(reste); - node = xmlDocGetRootElement(model); + node = xmlDocGetRootElement(self->model); while (xmlStrchr (reste, '/')) { extrait = getFirstTag(reste); @@ -166,7 +165,7 @@ static xmlNodePtr model_get_node(xmlChar *path) while (node && xmlStrcmp(node->name, last)) { node = node->next; } - xmlHashAddEntry (model_hashtable, path, node); + xmlHashAddEntry (self->model_hashtable, path, node); } return node; @@ -176,7 +175,7 @@ static xmlNodePtr model_get_node(xmlChar *path) return NULL; } -/******************************************************************************/ +/* -------------------------------------------------------------------------- */ static inline long model_get_node_long_attrib(xmlNodePtr node, char *id) { @@ -221,13 +220,13 @@ static inline bool model_get_node_str_attrib(xmlNodePtr node, return false; } -/******************************************************************************/ +/* -------------------------------------------------------------------------- */ -char model_get_dim(void) +char model_get_dim(struct model_t *self) { xmlAttr *attribute; xmlChar* value; - xmlNodePtr node = model_get_node( + xmlNodePtr node = model_get_node(self, (xmlChar *)"parameters/space-param/dimension"); if (xmlHasProp (node, (xmlChar *) "z")) return 3; @@ -236,22 +235,22 @@ char model_get_dim(void) return 0; } -long model_get_dim_value(const char *axis) +long model_get_dim_value(struct model_t *self, const char *axis) { xmlAttr *attribute; xmlChar *value; long ret_value; - xmlNodePtr node = model_get_node( + xmlNodePtr node = model_get_node(self, (xmlChar *)"parameters/space-param/dimension"); return model_get_node_long_attrib(node, axis); } -char model_get_multiplicity(void) +char model_get_multiplicity(struct model_t *self) { xmlAttr *attribute; xmlChar* value; - xmlNodePtr node = model_get_node( + xmlNodePtr node = model_get_node(self, (xmlChar *)"parameters/space-param/site_multiplicity"); if (node->children) @@ -261,7 +260,7 @@ char model_get_multiplicity(void) return 0; } -bool model_get_next_state(char *new_state_id) +bool model_get_next_state(struct model_t *self, char *new_state_id) { static xmlNodePtr cur_node = NULL; xmlAttr *attribute; @@ -269,7 +268,7 @@ bool model_get_next_state(char *new_state_id) if (cur_node == NULL) { // Get first state - cur_node = model_get_node((xmlChar *)"savedstates/state"); + cur_node = model_get_node(self, (xmlChar *)"savedstates/state"); } else { // Get next state @@ -287,7 +286,7 @@ bool model_get_next_state(char *new_state_id) return false; } -long model_get_state_arrows_count(const char *state_id) +long model_get_state_arrows_count(struct model_t *self, const char *state_id) { xmlNodePtr cur_node = NULL; xmlAttr *attribute; @@ -301,7 +300,7 @@ long model_get_state_arrows_count(const char *state_id) assert(state_id); // Get first state node - cur_node = model_get_node((xmlChar *)"savedstates/state"); + cur_node = model_get_node(self, (xmlChar *)"savedstates/state"); // Lookup in properties while (cur_node && cur_node->properties) { @@ -338,7 +337,8 @@ long model_get_state_arrows_count(const char *state_id) return value; } -bool model_get_next_arrow(struct arrow_t *new_arrow, +bool model_get_next_arrow(struct model_t *self, + struct arrow_t *new_arrow, const char *state_id, char dimension) { @@ -356,7 +356,7 @@ bool model_get_next_arrow(struct arrow_t *new_arrow, if (cur_node == NULL) { // Get first state node - cur_node = model_get_node((xmlChar *)"savedstates/state"); + cur_node = model_get_node(self, (xmlChar *)"savedstates/state"); // Lookup in properties while (cur_node && cur_node->properties) { @@ -470,3 +470,7 @@ bool model_get_next_arrow(struct arrow_t *new_arrow, cur_node = NULL; return false; } + +/* -------------------------------------------------------------------------- */ + + diff --git a/src/worker.c b/src/worker.c index 00985e2..a54a34f 100644 --- a/src/worker.c +++ b/src/worker.c @@ -27,12 +27,10 @@ double truc; -static int worker_new_id (void) -{ - static int id = 0; - return id; -} - +// +// +// +// void worker_start(struct worker_t *self, struct scheduler_t *scheduler) { // Locking ressources -- 2.39.5 From 575612a77df7701c0d4effaf43b080f4ffda6161 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Thu, 21 Mar 2024 02:09:00 +0200 Subject: [PATCH 10/24] WIP: loading arrows successfully --- data/models/dimers random walk.xml | 14 +- data/models/dimers_random_walk_recursif.xml | 2 +- include/model.h | 40 +++-- src/main.c | 6 +- src/model.c | 178 +++++++++++++++++--- src/scheduler.c | 2 +- 6 files changed, 192 insertions(+), 50 deletions(-) diff --git a/data/models/dimers random walk.xml b/data/models/dimers random walk.xml index 12ae465..277f9ef 100644 --- a/data/models/dimers random walk.xml +++ b/data/models/dimers random walk.xml @@ -34,7 +34,7 @@ - 3 + 2 @@ -75,19 +75,19 @@ - - + + - + + and the dimer identified by conditions (0,1) can be moved to East. --> - + + and the dimer identified by conditions (0,1) can be moved to West. --> diff --git a/data/models/dimers_random_walk_recursif.xml b/data/models/dimers_random_walk_recursif.xml index 0bd387c..976512c 100644 --- a/data/models/dimers_random_walk_recursif.xml +++ b/data/models/dimers_random_walk_recursif.xml @@ -34,7 +34,7 @@ - 3 + 2 diff --git a/include/model.h b/include/model.h index fc1ebd7..1056233 100644 --- a/include/model.h +++ b/include/model.h @@ -76,17 +76,17 @@ struct space_t struct state_t { // Metadata - int id; + char id[25]; int owner_id; time_t date; struct space_t *space; }; -struct model_t { - +struct model_t +{ // Metadata - int id; + char id; int owner_id; time_t date; union version @@ -96,12 +96,13 @@ struct model_t { }; // XML handlers - xmlDocPtr model; - xmlHashTablePtr model_hashtable; + xmlDocPtr doc; + xmlHashTablePtr hashtable; // User friendly metadata char *owner_name; char *model_name; + char *filename; // Model parameters int multiplicity; // number of sites in a space_unit @@ -114,14 +115,26 @@ struct model_t { // Handler to the current space of the model struct space_t *space; - // Handler to the saved states of the model - struct state_t **states; + // Handler to the arrows + struct arrow_t *arrows; + size_t n_arrows; + + // Handler to the saved states array + struct state_t *states; + size_t n_states; +}; + + +struct model_array_t +{ + struct model_t *models; + size_t size; }; // -------------------------------------------------------------------------- // // Model init function (and model discovery) // // -------------------------------------------------------------------------- // -// void model_system_init (parameters_t *parameters); +void model_system_init (struct parameters_t *parameters); // -------------------------------------------------------------------------- // // Model stopping function // @@ -131,20 +144,13 @@ struct model_t { // -------------------------------------------------------------------------- // // Load a model ready to execute // // -------------------------------------------------------------------------- // -// int model_load (int id); - -bool model_init(struct model_t *self, - const char *content, - size_t length, - const char *basename); +bool model_load (struct model_t *self); // -------------------------------------------------------------------------- // // Unload a model // // -------------------------------------------------------------------------- // // int model_unload (int id); -bool model_shutdown(struct model_t *self); - // -------------------------------------------------------------------------- // // Add a model to the known model list // // -------------------------------------------------------------------------- // diff --git a/src/main.c b/src/main.c index 3bd8ad7..05743ea 100644 --- a/src/main.c +++ b/src/main.c @@ -114,12 +114,12 @@ int main(int argc, char **argv) // Initializing random generator t = time(&t); - srand((unsigned)t); + srand ((unsigned)t); /* server = calloc(1, sizeof(*server)); */ - /* // Initializing model system */ - /* model_system_init(¶meters); */ + // Initializing model system + model_system_init (¶meters); /* // Launching server */ /* server_init(server); */ diff --git a/src/model.c b/src/model.c index 06b8f80..b631363 100644 --- a/src/model.c +++ b/src/model.c @@ -35,10 +35,11 @@ /* -------------------------------------------------------------------------- */ -bool model_init(struct model_t *self, - const char *content, - size_t length, - const char *basename) +struct model_array_t *knownModels; + +/* -------------------------------------------------------------------------- */ + +static bool model_init(struct model_t *self) { xmlNode *node; @@ -49,41 +50,42 @@ bool model_init(struct model_t *self, */ LIBXML_TEST_VERSION - self->model = xmlReadMemory(content, length, basename, NULL, 0); + self->doc = xmlReadFile(self->filename, NULL, 0); - if (self->model == NULL ) { + if (self->doc == NULL ) { + printerr("Error trying to open the XML model !\n"); return false; } - node = xmlDocGetRootElement(self->model); + node = xmlDocGetRootElement(self->doc); if (node == NULL) { - fprintf(stderr, "Empty XML model !\n"); - xmlFreeDoc(self->model); + printerr("Empty XML model !\n"); + xmlFreeDoc(self->doc); return false; } if (xmlStrcmp(node->name, (xmlChar *) "gem-graph-model")) { - fprintf(stderr, "document of the wrong type, root node != gem-graph-model\n"); - xmlFreeDoc(self->model); + printerr("document of the wrong type, root node != gem-graph-model\n"); + xmlFreeDoc(self->doc); return false; } - self->model_hashtable = xmlHashCreate(0); + self->hashtable = xmlHashCreate(0); - if (self->model_hashtable == NULL) { - fprintf(stderr, "Can't create model hash table !\n"); - xmlFreeDoc(self->model); + if (self->hashtable == NULL) { + printerr("Can't create model hash table !\n"); + xmlFreeDoc(self->doc); return false; } return true; } -bool model_shutdown(struct model_t *self) +static bool model_shutdown(struct model_t *self) { - xmlFreeDoc(self->model); - xmlHashFree(self->model_hashtable, NULL); + xmlFreeDoc(self->doc); + xmlHashFree(self->hashtable, NULL); // Cleanup function for the XML library xmlCleanupParser(); @@ -139,7 +141,7 @@ static xmlNodePtr model_get_node(struct model_t *self, xmlChar *path) xmlChar *reste, *last, *affich; // Lookup for node from path in hash table - node = xmlHashLookup(self->model_hashtable, path); + node = xmlHashLookup(self->hashtable, path); // Found a node in hash table if (node) { @@ -150,7 +152,7 @@ static xmlNodePtr model_get_node(struct model_t *self, xmlChar *path) reste = path; affich = reste; last = getLastTag(reste); - node = xmlDocGetRootElement(self->model); + node = xmlDocGetRootElement(self->doc); while (xmlStrchr (reste, '/')) { extrait = getFirstTag(reste); @@ -165,7 +167,7 @@ static xmlNodePtr model_get_node(struct model_t *self, xmlChar *path) while (node && xmlStrcmp(node->name, last)) { node = node->next; } - xmlHashAddEntry (self->model_hashtable, path, node); + xmlHashAddEntry (self->hashtable, path, node); } return node; @@ -473,4 +475,138 @@ bool model_get_next_arrow(struct model_t *self, /* -------------------------------------------------------------------------- */ +bool model_load (struct model_t *self) +{ + char state_id[30] = {0}; + if (!model_init (self)) + return false; + + printlog ("Loading %s...\n", self->filename); + + // Dimension + self->dimension = model_get_dim (self); + printlog ("Dimension : %d\n", self->dimension); + + // Dimensions + self->space = calloc (1, sizeof(struct space_t)); + self->space->x_dim = 1; + self->space->y_dim = 1; + self->space->z_dim = 1; + + switch(self->dimension) { + case 3: + // even in 1D, we must be able to see a grid, so prevent to be 0 + if (self->space->z_dim) + self->space->z_dim = model_get_dim_value (self, "z"); + case 2: + // even in 1D, we must be able to see a grid, so prevent to be 0 + if (self->space->y_dim) + self->space->y_dim = model_get_dim_value (self, "y"); + case 1: + // even in 1D, we must be able to see a grid, so prevent to be 0 + if (self->space->x_dim) + self->space->x_dim = model_get_dim_value (self, "x"); + + printlog("x_dim=%d, y_dim=%d, z_dim=%d\n", + self->space->x_dim, + self->space->y_dim, + self->space->z_dim); + break; + default: + printerr ("Invalid dimension value : %d\n", self->dimension); + return false; + } + + // Space allocation + self->space->units = calloc (self->space->x_dim + * self->space->y_dim + * self->space->z_dim, + sizeof(struct space_unit_t)); + + // Multiplicity + self->multiplicity = model_get_multiplicity(self); + printlog("Multiplicity : %d\n", self->multiplicity); + + if (!model_get_next_state(self, &state_id)) { + printerr("No valid state to load\n"); + return false; + } + + // States and initial state + self->states = calloc (1, sizeof(struct state_t)); + self->n_states = 1; + memcpy(&self->states[0].id, &state_id, 25); + self->states[0].space = self->space; + + printlog("Initial state : %s\n", &self->states[0].id); + + // Initial state arrows + self->n_arrows = model_get_state_arrows_count(self, state_id); + self->arrows = calloc (self->n_arrows, sizeof(struct arrow_t)); + + for (int i = 0; i < self->n_arrows; i++) { + model_get_next_arrow(self, + &self->arrows[i], + &self->states[0].id, + self->dimension); + } + + printlog("Loaded %d arrows\n", self->n_arrows); + + return true; +} + +void model_system_init (struct parameters_t *parameters) +{ + struct dirent *modelDirEntry = NULL; + DIR *modelDir = NULL; + char *extensionPosition; + int i; + + // Allocate model storage + knownModels = calloc (1, sizeof(struct model_array_t)); + + // Open model directory + if ((modelDir = opendir (parameters->modelDir)) <= 0) { + printerr ("Could not open %s\n", parameters->modelDir); + } + + while ((modelDirEntry = readdir (modelDir)) != NULL) { + if ((extensionPosition = strstr (modelDirEntry->d_name, ".xml"))) { + + i = knownModels->size; + + // Creating model + knownModels->models = reallocarray (knownModels->models, + ++knownModels->size, + sizeof(struct model_t)); + + // Allocate filename with trailing '\0' + knownModels->models[i].filename = + calloc (1, strlen (parameters->modelDir) + + sizeof((char)'/') + + strlen (modelDirEntry->d_name) + + sizeof((char)'\0')); + + memcpy (knownModels->models[i].filename, + parameters->modelDir, + strlen (parameters->modelDir)); + + knownModels->models[i].filename[ + strlen (parameters->modelDir)] = '/'; + + memcpy (knownModels->models[i].filename + + strlen (parameters->modelDir) + + sizeof((char)'/'), + modelDirEntry->d_name, + strlen (modelDirEntry->d_name)); + + // Ask to parse the new model + if (model_load (&knownModels->models[i])) + // XXX Check model is valid and/or parsed + printlog ("Loaded %s\n", knownModels->models[i].filename); + } + } + free(modelDir); +} diff --git a/src/scheduler.c b/src/scheduler.c index 5ab624d..d2affad 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -102,7 +102,7 @@ int sched_start (struct scheduler_t *self, struct parameters_t *parameters) int returnValue; self->id = sched_new_id(); - printlog("Scheduler initialized with %d threads\n", + printlog("Scheduler %d initialized with %d threads\n", self->id, n_threads); -- 2.39.5 From e9ba5643bbcbca4ade85212477ebf7e0d3c17652 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Thu, 21 Mar 2024 03:05:04 +0200 Subject: [PATCH 11/24] WIP: working on conditions --- Makefile | 3 +- include/arrows.h | 25 +++ include/model.h | 14 +- src/arrows.c | 64 ++++++ src/main.c | 2 + src/model.c | 515 ++++++++++++++++++++++++++++++++++++----------- src/scheduler.c | 27 +-- 7 files changed, 512 insertions(+), 138 deletions(-) diff --git a/Makefile b/Makefile index 71ecf5b..6c94108 100644 --- a/Makefile +++ b/Makefile @@ -118,7 +118,8 @@ run: build_system debug: build_system @echo -e ${CL2}[$@] ${CL}executing...${CL3} - @gdb $(BINDIR)/gem-graph-server + @gdb --args $(BINDIR)/gem-graph-server -C data/config -M data/models \ + -U data/users @echo -e ${CL2}[$@] ${CL}done.${CL3} valgrind: build_system diff --git a/include/arrows.h b/include/arrows.h index 9096a92..4d70404 100644 --- a/include/arrows.h +++ b/include/arrows.h @@ -36,4 +36,29 @@ struct arrow_t { uint x; uint y; uint z; + + struct arrow_t *next; + struct arrow_t *prev; }; + +struct condition_t { + uint id; + uint load; + uint site; + uint x; + uint y; + uint z; + + struct condition_t *child; + struct condition_t *parent; +}; + +/* -------------------------------------------------------------------------- */ + +struct arrow_t *arrow_new(struct arrow_t *self); + +struct arrow_t *arrow_destroy(struct arrow_t *self); + +struct condition_t *condition_new(struct condition_t *self); + +struct condition_t *condition_destroy(struct condition_t *self); diff --git a/include/model.h b/include/model.h index 1056233..1891e3a 100644 --- a/include/model.h +++ b/include/model.h @@ -81,6 +81,7 @@ struct state_t time_t date; struct space_t *space; + struct arrow_t *arrows; }; struct model_t @@ -115,6 +116,9 @@ struct model_t // Handler to the current space of the model struct space_t *space; + // Handler to the conditions + struct condition_t *conditions; + // Handler to the arrows struct arrow_t *arrows; size_t n_arrows; @@ -122,13 +126,9 @@ struct model_t // Handler to the saved states array struct state_t *states; size_t n_states; -}; - -struct model_array_t -{ - struct model_t *models; - size_t size; + struct model_t *next; + struct model_t *prev; }; // -------------------------------------------------------------------------- // @@ -139,7 +139,7 @@ void model_system_init (struct parameters_t *parameters); // -------------------------------------------------------------------------- // // Model stopping function // // -------------------------------------------------------------------------- // -// void model_system_shutdown (void); +void model_system_shutdown (void); // -------------------------------------------------------------------------- // // Load a model ready to execute // diff --git a/src/arrows.c b/src/arrows.c index 79f8f7f..3fa91ff 100644 --- a/src/arrows.c +++ b/src/arrows.c @@ -25,3 +25,67 @@ /* -------------------------------------------------------------------------- */ +struct arrow_t *arrow_new(struct arrow_t *self) +{ + if (self == NULL) { + return calloc (1, sizeof(struct arrow_t)); + } + + self->next = calloc (1, sizeof(struct arrow_t)); + self->next->prev = self; + + return self->next; +} + +struct arrow_t *arrow_destroy(struct arrow_t *self) +{ + struct arrow_t *res = NULL; + + if (self->next) + if (self->prev) { + self->prev->next = self->next; + } else { + res = self->next; + self->next->prev = NULL; + } + + if (self->prev) + if (self->next) { + self->next->prev = self->prev; + } else { + res = self->prev; + self->prev->next = NULL; + } + + free(self); + + return res; +} + +struct condition_t *condition_new(struct condition_t *self) +{ + if (self == NULL) { + return calloc (1, sizeof(struct condition_t)); + } + + self->child = calloc (1, sizeof(struct condition_t)); + self->child->parent = self; + + return self->child; +} + +struct condition_t *condition_destroy(struct condition_t *self) +{ + struct condition_t *res = NULL; + + if (self->child) { + condition_destroy(self->child); + } + + if (self->parent) + self->parent->child = NULL; + + free(self); + + return res; +} diff --git a/src/main.c b/src/main.c index 05743ea..cef1e09 100644 --- a/src/main.c +++ b/src/main.c @@ -137,6 +137,8 @@ int main(int argc, char **argv) returnValue = sched_start (&scheduler, ¶meters); } + model_system_shutdown(); + free(parameters.userDir); free(parameters.modelDir); free(parameters.configDir); diff --git a/src/model.c b/src/model.c index b631363..09b96ea 100644 --- a/src/model.c +++ b/src/model.c @@ -24,6 +24,7 @@ #include "../include/model.h" #include "../include/arrows.h" +#define ALLOWED_RADIUS 10 #define READ_SITE 1 << 0 #define READ_WEIGHT 1 << 1 #define READ_X 1 << 2 @@ -35,70 +36,11 @@ /* -------------------------------------------------------------------------- */ -struct model_array_t *knownModels; +struct model_t *knownModels = NULL; /* -------------------------------------------------------------------------- */ -static bool model_init(struct model_t *self) -{ - xmlNode *node; - - /* - * this initialize the library and check potential ABI mismatches - * between the version it was compiled for and the actual shared - * library used. - */ - LIBXML_TEST_VERSION - - self->doc = xmlReadFile(self->filename, NULL, 0); - - if (self->doc == NULL ) { - printerr("Error trying to open the XML model !\n"); - return false; - } - - node = xmlDocGetRootElement(self->doc); - - if (node == NULL) { - printerr("Empty XML model !\n"); - xmlFreeDoc(self->doc); - return false; - } - - if (xmlStrcmp(node->name, (xmlChar *) "gem-graph-model")) { - printerr("document of the wrong type, root node != gem-graph-model\n"); - xmlFreeDoc(self->doc); - return false; - } - - self->hashtable = xmlHashCreate(0); - - if (self->hashtable == NULL) { - printerr("Can't create model hash table !\n"); - xmlFreeDoc(self->doc); - return false; - } - - return true; -} - -static bool model_shutdown(struct model_t *self) -{ - xmlFreeDoc(self->doc); - xmlHashFree(self->hashtable, NULL); - - // Cleanup function for the XML library - xmlCleanupParser(); - - // This is to debug memory for regression tests - xmlMemoryDump(); - - return true; -} - -/* -------------------------------------------------------------------------- */ - -static inline xmlNodePtr getNextChild(xmlNodePtr node, xmlChar *last) +static inline xmlNodePtr getNextChild (xmlNodePtr node, xmlChar *last) { while (node != NULL && xmlStrcmp(node->name, last)) { // //printf(" <>--- line n°%lu <%s>\n", xmlGetLineNo(node), node->name); @@ -107,14 +49,14 @@ static inline xmlNodePtr getNextChild(xmlNodePtr node, xmlChar *last) return node; } -static inline xmlChar* splitStrAtSlash(xmlChar *toSplit) +static inline xmlChar* splitStrAtSlash (xmlChar *toSplit) { toSplit = (xmlChar *)xmlStrchr(toSplit, '/'); toSplit = xmlStrsub (toSplit, 1, xmlStrlen(toSplit)); return toSplit; } -static inline xmlChar* getFirstTag(xmlChar *path) +static inline xmlChar* getFirstTag (xmlChar *path) { xmlChar *preop = path; path = (xmlChar *)xmlStrchr(path, '/'); @@ -123,10 +65,10 @@ static inline xmlChar* getFirstTag(xmlChar *path) return xmlStrsub (preop, 0, xmlStrlen(preop) - xmlStrlen(path) - 1); } -static inline xmlChar* getLastTag(xmlChar *path) +static inline xmlChar* getLastTag (xmlChar *path) { while ((ulong)xmlStrchr (path, '/')) - path = splitStrAtSlash((xmlChar *)path); + path = splitStrAtSlash ((xmlChar *)path); // //printf("last tag in the path = <%s>\n", path); return path; // which is no more the given path but only its last tag ! @@ -134,7 +76,7 @@ static inline xmlChar* getLastTag(xmlChar *path) /* -------------------------------------------------------------------------- */ -static xmlNodePtr model_get_node(struct model_t *self, xmlChar *path) +static xmlNodePtr model_get_node (struct model_t *self, xmlChar *path) { xmlNodePtr node; xmlChar *extrait; @@ -179,7 +121,7 @@ static xmlNodePtr model_get_node(struct model_t *self, xmlChar *path) /* -------------------------------------------------------------------------- */ -static inline long model_get_node_long_attrib(xmlNodePtr node, char *id) +static inline long model_get_node_long_attrib (xmlNodePtr node, char *id) { xmlAttr *attribute; xmlChar* value; @@ -200,7 +142,7 @@ static inline long model_get_node_long_attrib(xmlNodePtr node, char *id) return 0; } -static inline bool model_get_node_str_attrib(xmlNodePtr node, +static inline bool model_get_node_str_attrib (xmlNodePtr node, char *id, char *dest) { @@ -224,7 +166,7 @@ static inline bool model_get_node_str_attrib(xmlNodePtr node, /* -------------------------------------------------------------------------- */ -char model_get_dim(struct model_t *self) +char model_get_dim (struct model_t *self) { xmlAttr *attribute; xmlChar* value; @@ -237,7 +179,7 @@ char model_get_dim(struct model_t *self) return 0; } -long model_get_dim_value(struct model_t *self, const char *axis) +long model_get_dim_value (struct model_t *self, const char *axis) { xmlAttr *attribute; xmlChar *value; @@ -248,7 +190,7 @@ long model_get_dim_value(struct model_t *self, const char *axis) return model_get_node_long_attrib(node, axis); } -char model_get_multiplicity(struct model_t *self) +char model_get_multiplicity (struct model_t *self) { xmlAttr *attribute; xmlChar* value; @@ -262,7 +204,7 @@ char model_get_multiplicity(struct model_t *self) return 0; } -bool model_get_next_state(struct model_t *self, char *new_state_id) +bool model_get_next_state (struct model_t *self, char *new_state_id) { static xmlNodePtr cur_node = NULL; xmlAttr *attribute; @@ -288,7 +230,7 @@ bool model_get_next_state(struct model_t *self, char *new_state_id) return false; } -long model_get_state_arrows_count(struct model_t *self, const char *state_id) +long model_get_state_arrows_count (struct model_t *self, const char *state_id) { xmlNodePtr cur_node = NULL; xmlAttr *attribute; @@ -339,7 +281,7 @@ long model_get_state_arrows_count(struct model_t *self, const char *state_id) return value; } -bool model_get_next_arrow(struct model_t *self, +bool model_get_next_arrow (struct model_t *self, struct arrow_t *new_arrow, const char *state_id, char dimension) @@ -473,11 +415,232 @@ bool model_get_next_arrow(struct model_t *self, return false; } +bool model_get_next_conditions (struct model_t *self, char *new_cond_id) +{ + static xmlNodePtr cur_node = NULL; + xmlAttr *attribute; + xmlChar *value; + + if (cur_node == NULL) { + // Get first state + cur_node = model_get_node(self, (xmlChar *)"conditions"); + + } else { + // Get next state + if (cur_node->next) + cur_node = cur_node->next; + else + return false; + } + + // Lookup in properties + if (model_get_node_str_attrib(cur_node, "id", new_cond_id)) + return true; + + cur_node = NULL; + return false; +} + +bool model_get_next_condition (struct model_t *self, + struct condition_t *new_condition, + const char *cond_id, + char dimension) +{ + static xmlNodePtr cur_node = NULL; + xmlAttr *attribute; + xmlChar *value; + bool found = false; + char temp_char[25]; + uint check = 0; // bit field checker + + //printf("NEW CALL : cur_node = %p\n", cur_node); + + assert(new_condition); + assert(cond_id); + + if (cur_node == NULL) { + // Get first state node + cur_node = model_get_node(self, (xmlChar *)"conditions"); + + // Lookup in properties + while (cur_node && cur_node->properties) { + attribute = cur_node->properties; + + // Look for the id attribute + if (model_get_node_str_attrib(cur_node, "id", &temp_char)) { + if (!xmlStrcmp(temp_char, (const xmlChar *)cond_id)) { + found = true; + break; + } + } + cur_node = cur_node->next; + } + + // Check if the state has been found + if (!found) { + cur_node = NULL; + return false; + } + + // Get first arrow + if (cur_node->children) { + cur_node = cur_node->children; + + found = false; + while (cur_node && cur_node->name) { + if (!xmlStrcmp(cur_node->name, (const xmlChar *)"arrow")) { + found = true; + break; + } + cur_node = cur_node->next; + } + } + + // Check if the state has been found + if (!found) { + cur_node = NULL; + return false; + } + + } else { + // Get next condition + found = false; + while (cur_node->next) { + cur_node = cur_node->next; + if (!xmlStrcmp(cur_node->name, (const xmlChar *)"arrow")) { + found = true; + break; + } + } + + // Check if the state has been found + if (!found) { + cur_node = NULL; + return false; + } + } + + //printf("DURING CALL : cur_node = %p\n", cur_node); + //printf("DURING CALL : cur_node->name = %s\n", cur_node->name); + + // Lookup in properties + if (cur_node && cur_node->properties) { + attribute = cur_node->properties; + + while(attribute && attribute->name && attribute->children) { + //printf("attr name : %s\n", attribute->name); + if (!xmlStrcmp(attribute->name, (const xmlChar *)"site")) { + value = xmlNodeListGetString(cur_node->doc, attribute->children, 1); + new_condition->site = strtol((char *)value, NULL, 0); + xmlFree(value); + check |= READ_SITE; + } + if (!xmlStrcmp(attribute->name, (const xmlChar *)"weight")) { + value = xmlNodeListGetString(cur_node->doc, attribute->children, 1); + new_condition->load = strtol((char *)value, NULL, 0); + xmlFree(value); + check |= READ_WEIGHT; + } + if (!xmlStrcmp(attribute->name, (const xmlChar *)"x")) { + value = xmlNodeListGetString(cur_node->doc, attribute->children, 1); + new_condition->x = strtol((char *)value, NULL, 0); + xmlFree(value); + check |= READ_X; + } + if (!xmlStrcmp(attribute->name, (const xmlChar *)"y")) { + value = xmlNodeListGetString(cur_node->doc, attribute->children, 1); + new_condition->y = strtol((char *)value, NULL, 0); + xmlFree(value); + check |= READ_Y; + } + if (!xmlStrcmp(attribute->name, (const xmlChar *)"z")) { + value = xmlNodeListGetString(cur_node->doc, attribute->children, 1); + new_condition->z = strtol((char *)value, NULL, 0); + xmlFree(value); + check |= READ_Z; + } + attribute = attribute->next; + } + + switch(dimension) { + case 3: + return (bool)(check & SUCCESSFUL_READ_ARROW_XYZ); + case 2: + return (bool)(check & SUCCESSFUL_READ_ARROW_XY); + case 1: + return (bool)(check & SUCCESSFUL_READ_ARROW_X); + } + } + cur_node = NULL; + return false; +} + +/* -------------------------------------------------------------------------- */ + +static bool model_init(struct model_t *self) +{ + xmlNode *node; + + /* + * this initialize the library and check potential ABI mismatches + * between the version it was compiled for and the actual shared + * library used. + */ + LIBXML_TEST_VERSION + + self->doc = xmlReadFile(self->filename, NULL, 0); + + if (self->doc == NULL ) { + printerr("Error trying to open the XML model !\n"); + return false; + } + + node = xmlDocGetRootElement(self->doc); + + if (node == NULL) { + printerr("Empty XML model !\n"); + xmlFreeDoc(self->doc); + return false; + } + + if (xmlStrcmp(node->name, (xmlChar *) "gem-graph-model")) { + printerr("document of the wrong type, root node != gem-graph-model\n"); + xmlFreeDoc(self->doc); + return false; + } + + self->hashtable = xmlHashCreate(0); + + if (self->hashtable == NULL) { + printerr("Can't create model hash table !\n"); + xmlFreeDoc(self->doc); + return false; + } + + return true; +} + +static bool model_shutdown(struct model_t *self) +{ + xmlFreeDoc(self->doc); + xmlHashFree(self->hashtable, NULL); + // This is to debug memory for regression tests + //xmlMemoryDump(); + + return true; +} + /* -------------------------------------------------------------------------- */ bool model_load (struct model_t *self) { char state_id[30] = {0}; + char conditions_id[30] = {0}; + struct arrow_t *arrow; + struct arrow_t *arrow_next; + struct arrow_t *arrow_prev; + int n_conditions; + if (!model_init (self)) return false; @@ -528,12 +691,12 @@ bool model_load (struct model_t *self) self->multiplicity = model_get_multiplicity(self); printlog("Multiplicity : %d\n", self->multiplicity); + // States and initial state if (!model_get_next_state(self, &state_id)) { printerr("No valid state to load\n"); return false; } - // States and initial state self->states = calloc (1, sizeof(struct state_t)); self->n_states = 1; memcpy(&self->states[0].id, &state_id, 25); @@ -542,31 +705,155 @@ bool model_load (struct model_t *self) printlog("Initial state : %s\n", &self->states[0].id); // Initial state arrows - self->n_arrows = model_get_state_arrows_count(self, state_id); - self->arrows = calloc (self->n_arrows, sizeof(struct arrow_t)); + self->arrows = arrow_new (self->arrows); - for (int i = 0; i < self->n_arrows; i++) { - model_get_next_arrow(self, - &self->arrows[i], - &self->states[0].id, - self->dimension); + if (model_get_next_arrow(self, + self->arrows, + &self->states[0].id, + self->dimension)) { + self->n_arrows++; + + while (model_get_next_arrow(self, + arrow_new (self->arrows), + &self->states[0].id, + self->dimension)) { + self->n_arrows++; + } } printlog("Loaded %d arrows\n", self->n_arrows); + self->states[0].arrows = self->arrows; + + // Conditions + if (!model_get_next_conditions(self, &conditions_id)) { + printerr("No valid conditions to load\n"); + return false; + } + + // Loading each condition + self->conditions = condition_new (self->conditions); + + if (model_get_next_condition(self, + self->conditions, + &conditions_id, + self->dimension)) { + n_conditions = 1; + while (model_get_next_arrow(self, + condition_new (self->conditions), + &conditions_id, + self->dimension)) { + n_conditions++; + } + } + + printlog("Loaded %d conditions\n", n_conditions); + return true; } +bool model_unload (struct model_t *self) +{ + if (!model_shutdown (self)) + return false; + + if (self->owner_name) { + free(self->owner_name); + self->owner_name = NULL; + } + if (self->model_name) { + free(self->model_name); + self->model_name = NULL; + } + if (self->space) { + if (self->space->units) { + free(self->space->units); + self->space->units = NULL; + } + free(self->space); + self->space = NULL; + } + if (self->arrows) { + while (self->arrows) { + self->arrows = arrow_destroy (self->arrows); + } + } + if (self->states) { + free(self->states); + self->states = NULL; + } + + return true; +} + +struct model_t *model_new(const char *path, const char *basename) +{ + struct model_t *self = NULL; + struct model_t *prev = NULL; + + // Allocate and insert in the list + self = calloc (1, sizeof(struct model_t)); + + // We want a cyclic chain + // This is the first model + if (!knownModels) { + knownModels = self; + self->next = self; + self->prev = self; + + // This is not the first model + } else { + self->next = knownModels->next; + knownModels->next = self; + self->prev = knownModels; + } + + // Allocate filename with trailing '\0' + self->filename = + calloc (1, strlen (path) + + sizeof((char)'/') + + strlen (basename) + + sizeof((char)'\0')); + + memcpy (self->filename, + path, + strlen (path)); + + self->filename[ + strlen (path)] = '/'; + + memcpy (self->filename + + strlen (path) + + sizeof((char)'/'), + basename, + strlen (basename)); + + return self; +} + +void model_destroy(struct model_t *self) +{ + if (self->filename) { + free(self->filename); + self->filename = NULL; + } + + if (self->next != self) { + self->prev->next = self->next; + self->next->prev = self->prev; + } + + free(self); +} + void model_system_init (struct parameters_t *parameters) { struct dirent *modelDirEntry = NULL; + struct model_t *new_model; DIR *modelDir = NULL; char *extensionPosition; int i; - // Allocate model storage - knownModels = calloc (1, sizeof(struct model_array_t)); - // Open model directory if ((modelDir = opendir (parameters->modelDir)) <= 0) { printerr ("Could not open %s\n", parameters->modelDir); @@ -575,38 +862,40 @@ void model_system_init (struct parameters_t *parameters) while ((modelDirEntry = readdir (modelDir)) != NULL) { if ((extensionPosition = strstr (modelDirEntry->d_name, ".xml"))) { - i = knownModels->size; - - // Creating model - knownModels->models = reallocarray (knownModels->models, - ++knownModels->size, - sizeof(struct model_t)); - - // Allocate filename with trailing '\0' - knownModels->models[i].filename = - calloc (1, strlen (parameters->modelDir) - + sizeof((char)'/') - + strlen (modelDirEntry->d_name) - + sizeof((char)'\0')); - - memcpy (knownModels->models[i].filename, - parameters->modelDir, - strlen (parameters->modelDir)); - - knownModels->models[i].filename[ - strlen (parameters->modelDir)] = '/'; - - memcpy (knownModels->models[i].filename - + strlen (parameters->modelDir) - + sizeof((char)'/'), - modelDirEntry->d_name, - strlen (modelDirEntry->d_name)); + new_model = model_new (parameters->modelDir, modelDirEntry->d_name); // Ask to parse the new model - if (model_load (&knownModels->models[i])) + if (model_load (new_model)) { // XXX Check model is valid and/or parsed - printlog ("Loaded %s\n", knownModels->models[i].filename); + printlog ("Loaded %s\n", new_model->filename); + } else { + printerr ("Failed to load %s\n", new_model->filename); + model_unload (new_model); + model_destroy (new_model); + } } } free(modelDir); } + +void model_system_shutdown (void) +{ + struct model_t *model = knownModels; + struct model_t *next = NULL; + + while ((model != model->next)) { + printlog ("Freeing %s\n", model->filename); + next = model->next; + model_unload(model); + model_destroy(model); + model = next; + } + + if (model) { + model_unload (model); + model_destroy (model); + } + + // Cleanup function for the XML library + xmlCleanupParser(); +} diff --git a/src/scheduler.c b/src/scheduler.c index d2affad..e16deb6 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -98,20 +98,14 @@ static int sched_run(struct scheduler_t *self, int sched_start (struct scheduler_t *self, struct parameters_t *parameters) { int n_threads = omp_get_max_threads(); - int n_models = 20; int returnValue; + struct model_t *model; self->id = sched_new_id(); printlog("Scheduler %d initialized with %d threads\n", self->id, n_threads); - // Allocating models - self->models = calloc(n_models, sizeof(struct model_t*)); - for (int i = 0; i < n_models; i++) { - self->models[i] = calloc(1, sizeof(struct model_t)); - //XXX populate model - } // Allocating workers self->workers = calloc(n_threads, sizeof(struct worker_t*)); @@ -119,10 +113,13 @@ int sched_start (struct scheduler_t *self, struct parameters_t *parameters) self->workers[i] = calloc(1, sizeof(struct worker_t)); } - // Run - #pragma omp for - for (int i = 0; i < n_models; i++) - returnValue = sched_run(self, parameters, self->models[i]); + // Retrieve models + model = self->models; + while(model) { + printlog ("Scheduling model %d\n", model->id); + returnValue = sched_run(self, parameters, self->models); + model = model->next; + } // Freeing workers for (int i = 0; i < n_threads; i++) { @@ -130,11 +127,7 @@ int sched_start (struct scheduler_t *self, struct parameters_t *parameters) } free(self->workers); - // Freeing models - for (int i = 0; i < n_models; i++) { - free(self->models[i]); - } - free(self->models); - return returnValue; } + + -- 2.39.5 From 19acd2672cfde43f2a1a7a622e25e8f228d7d7d5 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Fri, 22 Mar 2024 01:07:07 +0200 Subject: [PATCH 12/24] src/model.c : debugging parsing --- data/models/dimers random walk.xml | 1 + include/model.h | 5 +-- src/model.c | 71 +++++++++++++++++++----------- 3 files changed, 48 insertions(+), 29 deletions(-) diff --git a/data/models/dimers random walk.xml b/data/models/dimers random walk.xml index 277f9ef..a7c9efc 100644 --- a/data/models/dimers random walk.xml +++ b/data/models/dimers random walk.xml @@ -116,3 +116,4 @@ + diff --git a/include/model.h b/include/model.h index 1891e3a..b278694 100644 --- a/include/model.h +++ b/include/model.h @@ -52,15 +52,14 @@ struct space_unit_t { - bool lock; - struct arrow_t **sites; // Array of struct arrow_t* elements + uint id; + struct arrow_t *sites[]; // Array of struct arrow_t elements // - lenght is multiplicity }; struct space_t { // Dimensions of space. - // Note that a value 0 is not allowed, minimum is 1 int x_dim; int y_dim; int z_dim; diff --git a/src/model.c b/src/model.c index 09b96ea..b2077d3 100644 --- a/src/model.c +++ b/src/model.c @@ -421,6 +421,8 @@ bool model_get_next_conditions (struct model_t *self, char *new_cond_id) xmlAttr *attribute; xmlChar *value; + printlog("NEW CALL : cur_node = %p\n", cur_node); + if (cur_node == NULL) { // Get first state cur_node = model_get_node(self, (xmlChar *)"conditions"); @@ -433,6 +435,8 @@ bool model_get_next_conditions (struct model_t *self, char *new_cond_id) return false; } + printlog("END CALL : cur_node = %p\n", cur_node); + // Lookup in properties if (model_get_node_str_attrib(cur_node, "id", new_cond_id)) return true; @@ -453,7 +457,7 @@ bool model_get_next_condition (struct model_t *self, char temp_char[25]; uint check = 0; // bit field checker - //printf("NEW CALL : cur_node = %p\n", cur_node); + printlog("NEW CALL : cur_node = %p\n", cur_node); assert(new_condition); assert(cond_id); @@ -520,8 +524,8 @@ bool model_get_next_condition (struct model_t *self, } } - //printf("DURING CALL : cur_node = %p\n", cur_node); - //printf("DURING CALL : cur_node->name = %s\n", cur_node->name); + printlog("DURING CALL : cur_node = %p\n", cur_node); + printlog("DURING CALL : cur_node->name = %s\n", cur_node->name); // Lookup in properties if (cur_node && cur_node->properties) { @@ -653,22 +657,16 @@ bool model_load (struct model_t *self) // Dimensions self->space = calloc (1, sizeof(struct space_t)); - self->space->x_dim = 1; - self->space->y_dim = 1; - self->space->z_dim = 1; + self->space->x_dim = 0; + self->space->y_dim = 0; + self->space->z_dim = 0; switch(self->dimension) { case 3: - // even in 1D, we must be able to see a grid, so prevent to be 0 - if (self->space->z_dim) self->space->z_dim = model_get_dim_value (self, "z"); case 2: - // even in 1D, we must be able to see a grid, so prevent to be 0 - if (self->space->y_dim) self->space->y_dim = model_get_dim_value (self, "y"); case 1: - // even in 1D, we must be able to see a grid, so prevent to be 0 - if (self->space->x_dim) self->space->x_dim = model_get_dim_value (self, "x"); printlog("x_dim=%d, y_dim=%d, z_dim=%d\n", @@ -685,7 +683,8 @@ bool model_load (struct model_t *self) self->space->units = calloc (self->space->x_dim * self->space->y_dim * self->space->z_dim, - sizeof(struct space_unit_t)); + sizeof(struct space_unit_t) + + self->multiplicity * sizeof(struct arrow_t*)); // Multiplicity self->multiplicity = model_get_multiplicity(self); @@ -705,20 +704,40 @@ bool model_load (struct model_t *self) printlog("Initial state : %s\n", &self->states[0].id); // Initial state arrows - self->arrows = arrow_new (self->arrows); - - if (model_get_next_arrow(self, - self->arrows, - &self->states[0].id, - self->dimension)) { - self->n_arrows++; - - while (model_get_next_arrow(self, - arrow_new (self->arrows), - &self->states[0].id, - self->dimension)) { - self->n_arrows++; + while (arrow = arrow_new (self->arrows), + model_get_next_arrow(self, + arrow, + &self->states[0].id, + self->dimension)) { + if (arrow->x > self->space->x_dim || + arrow->y > self->space->y_dim || + arrow->z > self->space->z_dim) { + printerr("Invalid coordinates for arrow %d (%d,%d,%d)\n", + self->n_arrows, + arrow->x, + arrow->y, + arrow->z); + return false; } + + printlog("Loading arrow %d (%d,%d,%d, %d)\n", + self->n_arrows, + arrow->x, + arrow->y, + arrow->z, + arrow->site); + + self->space->units [ + arrow->x + + self->space->x_dim * arrow->y + + self->space->x_dim * self->space->y_dim * arrow->z + ].sites[arrow->site] = arrow; + + if (self->n_arrows == 0) { + self->arrows = arrow; + } + + self->n_arrows++; } printlog("Loaded %d arrows\n", self->n_arrows); -- 2.39.5 From b2cd2c2fcaf1dac04973d5612263b80db4876349 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Sun, 24 Mar 2024 22:04:07 +0200 Subject: [PATCH 13/24] src/model.c: making node research efficient and safe --- src/model.c | 106 +++++++++++++++++----------------------------------- 1 file changed, 35 insertions(+), 71 deletions(-) diff --git a/src/model.c b/src/model.c index b2077d3..16b0bad 100644 --- a/src/model.c +++ b/src/model.c @@ -2,7 +2,7 @@ // Model management module // // // // Copyright © 2021 Libre en Communs (contact@a-lec.org) // -// Copyright © 2021 Adrien Bourmault (neox@a-lec.org) // +// Copyright © 2021-2024 Adrien Bourmault (neox@a-lec.org) // // // // This file is part of gem-graph. // // // @@ -40,85 +40,49 @@ struct model_t *knownModels = NULL; /* -------------------------------------------------------------------------- */ -static inline xmlNodePtr getNextChild (xmlNodePtr node, xmlChar *last) -{ - while (node != NULL && xmlStrcmp(node->name, last)) { - // //printf(" <>--- line n°%lu <%s>\n", xmlGetLineNo(node), node->name); - node = node->next; - } - return node; -} +/* + * Finds a node by a path of tags. + * + * @param node The starting node for the search. + * @param path The path to the tag, with '/' as the delimiter. + * @return The content of the tag at the end of the path, or NULL if not found. + */ +char* model_find_node_by_path(xmlNode *node, const char *path) { + char *pathCopy = strdup(path); // Duplicate path to modify it + char *tag = strtok(pathCopy, "/"); // Get the first tag in the path + xmlNode *currentNode = node; -static inline xmlChar* splitStrAtSlash (xmlChar *toSplit) -{ - toSplit = (xmlChar *)xmlStrchr(toSplit, '/'); - toSplit = xmlStrsub (toSplit, 1, xmlStrlen(toSplit)); - return toSplit; -} + while (tag != NULL && currentNode != NULL) { + xmlNode *foundNode = NULL; -static inline xmlChar* getFirstTag (xmlChar *path) -{ - xmlChar *preop = path; - path = (xmlChar *)xmlStrchr(path, '/'); - path = xmlStrsub (path, 1, xmlStrlen(path)); - - return xmlStrsub (preop, 0, xmlStrlen(preop) - xmlStrlen(path) - 1); -} - -static inline xmlChar* getLastTag (xmlChar *path) -{ - while ((ulong)xmlStrchr (path, '/')) - path = splitStrAtSlash ((xmlChar *)path); - - // //printf("last tag in the path = <%s>\n", path); - return path; // which is no more the given path but only its last tag ! -} - -/* -------------------------------------------------------------------------- */ - -static xmlNodePtr model_get_node (struct model_t *self, xmlChar *path) -{ - xmlNodePtr node; - xmlChar *extrait; - xmlChar *reste, *last, *affich; - - // Lookup for node from path in hash table - node = xmlHashLookup(self->hashtable, path); - - // Found a node in hash table - if (node) { - return node; - - // no node found in hash table - } else { - reste = path; - affich = reste; - last = getLastTag(reste); - node = xmlDocGetRootElement(self->doc); - - while (xmlStrchr (reste, '/')) { - extrait = getFirstTag(reste); - reste = splitStrAtSlash((xmlChar *)reste); - node = node->xmlChildrenNode; - node = getNextChild(node, extrait); - } - - if(node && xmlStrcmp(node->name, last)) { - node = node->xmlChildrenNode; - - while (node && xmlStrcmp(node->name, last)) { - node = node->next; + // Search for the tag at the current level + for (xmlNode *child = currentNode->children; child; child = child->next) { + if (child->type == XML_ELEMENT_NODE && strcmp((const char *)child->name, tag) == 0) { + foundNode = child; // Tag found at the current level + break; } - xmlHashAddEntry (self->hashtable, path, node); } - return node; + tag = strtok(NULL, "/"); // Move to the next tag in the path + // If there are more tags to find, continue with the found node's children + if (tag != NULL && foundNode != NULL) { + currentNode = foundNode; + } else { + // If this was the last tag, try to get its content + if (foundNode != NULL && tag == NULL) { + char *content = (char *)xmlNodeGetContent(foundNode); + free(pathCopy); + return content; + } + + currentNode = NULL; // End loop if tag not found + } } - return NULL; + free(pathCopy); + return NULL; // Path not fully matched } - /* -------------------------------------------------------------------------- */ static inline long model_get_node_long_attrib (xmlNodePtr node, char *id) -- 2.39.5 From 1ea9a356a915d65fb9a304d4e40420e8ad518995 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Mon, 25 Mar 2024 23:07:01 +0200 Subject: [PATCH 14/24] src/model.c: making node research use hashtable --- src/model.c | 71 ++++++++++++++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 34 deletions(-) diff --git a/src/model.c b/src/model.c index 16b0bad..3528ab4 100644 --- a/src/model.c +++ b/src/model.c @@ -40,6 +40,42 @@ struct model_t *knownModels = NULL; /* -------------------------------------------------------------------------- */ +/** + * Recursively populates a hash table with XML nodes, using the node paths as keys. + * + * @param node The current node being processed. + * @param hashTable The hash table to populate. + * @param currentPath The path to the current node. + */ +void model_populate_hashtable(xmlNode *node, + xmlHashTablePtr hashTable, + char *currentPath) { + if (node == NULL) return; + + // Skip text nodes and others that are not element nodes + if (node->type != XML_ELEMENT_NODE) { + model_populate_hashtable(node->next, hashTable, currentPath); + return; + } + + // Calculate new path + char newPath[1024]; // large enough + if (currentPath && strlen(currentPath) > 0) { + snprintf(newPath, sizeof(newPath), "%s/%s", currentPath, node->name); + } else { + snprintf(newPath, sizeof(newPath), "%s", node->name); // Root element + } + + // Add current node to hash table with its path as the key + xmlHashAddEntry(hashTable, (const xmlChar *)newPath, node); + + // Recurse into child nodes with the updated path + model_populate_hashtable(node->children, hashTable, newPath); + + // Continue with the next sibling + model_populate_hashtable(node->next, hashTable, currentPath); +} + /* * Finds a node by a path of tags. * @@ -48,40 +84,7 @@ struct model_t *knownModels = NULL; * @return The content of the tag at the end of the path, or NULL if not found. */ char* model_find_node_by_path(xmlNode *node, const char *path) { - char *pathCopy = strdup(path); // Duplicate path to modify it - char *tag = strtok(pathCopy, "/"); // Get the first tag in the path - xmlNode *currentNode = node; - - while (tag != NULL && currentNode != NULL) { - xmlNode *foundNode = NULL; - - // Search for the tag at the current level - for (xmlNode *child = currentNode->children; child; child = child->next) { - if (child->type == XML_ELEMENT_NODE && strcmp((const char *)child->name, tag) == 0) { - foundNode = child; // Tag found at the current level - break; - } - } - - tag = strtok(NULL, "/"); // Move to the next tag in the path - - // If there are more tags to find, continue with the found node's children - if (tag != NULL && foundNode != NULL) { - currentNode = foundNode; - } else { - // If this was the last tag, try to get its content - if (foundNode != NULL && tag == NULL) { - char *content = (char *)xmlNodeGetContent(foundNode); - free(pathCopy); - return content; - } - - currentNode = NULL; // End loop if tag not found - } - } - - free(pathCopy); - return NULL; // Path not fully matched + return (xmlNodePtr)xmlHashLookup(hashTable, (const xmlChar *)path); } /* -------------------------------------------------------------------------- */ -- 2.39.5 From 394d9644a0a5e7cd74c01aaf74e20ec98cc18075 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 27 Mar 2024 23:00:00 +0200 Subject: [PATCH 15/24] src/model.c: now able to modify node's content --- src/model.c | 60 +++++++++++++++++------------------------------------ 1 file changed, 19 insertions(+), 41 deletions(-) diff --git a/src/model.c b/src/model.c index 3528ab4..23675d9 100644 --- a/src/model.c +++ b/src/model.c @@ -86,49 +86,27 @@ void model_populate_hashtable(xmlNode *node, char* model_find_node_by_path(xmlNode *node, const char *path) { return (xmlNodePtr)xmlHashLookup(hashTable, (const xmlChar *)path); } -/* -------------------------------------------------------------------------- */ -static inline long model_get_node_long_attrib (xmlNodePtr node, char *id) -{ - xmlAttr *attribute; - xmlChar* value; - long ret_value; - - if (node && node->properties) { - attribute = node->properties; - while(attribute && attribute->name && attribute->children) { - if (!xmlStrcmp(attribute->name, (const xmlChar *)id)) { - value = xmlNodeListGetString(node->doc, attribute->children, 1); - ret_value = strtol((char *)value, NULL, 0); - xmlFree(value); - return ret_value; - } - attribute = attribute->next; - } +/** + * Modifies the content of an XML node identified by its path in the hash table. + * + * @param hashTable The hash table containing the XML nodes. + * @param path The path to the node to modify. + * @param newContent The new content to set for the node. + * @return 1 if the node was found and modified, 0 otherwise. + */ +int model_modify_node(xmlHashTablePtr hashTable, + const char *path, + const char *newContent) { + xmlNodePtr node = model_find_node_by_path(hashTable, path); + if (node) { + // Free the current content of the node, if any + xmlNodeSetContent(node, (const xmlChar *)""); // Clear existing content + // Set new content + xmlNodeSetContent(node, (const xmlChar *)newContent); + return 1; // Success } - return 0; -} - -static inline bool model_get_node_str_attrib (xmlNodePtr node, - char *id, - char *dest) -{ - xmlAttr *attribute; - xmlChar* value; - - if (node && node->properties) { - attribute = node->properties; - while(attribute && attribute->name && attribute->children) { - if (!xmlStrcmp(attribute->name, (const xmlChar *)id)) { - value = xmlNodeListGetString(node->doc, attribute->children, 1); - strcpy(dest, value); - xmlFree(value); - return true; - } - attribute = attribute->next; - } - } - return false; + return 0; // Node not found or unable to modify } /* -------------------------------------------------------------------------- */ -- 2.39.5 From 38cbe2a44390c6d4b47b15adc329f8d52d23155f Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Thu, 28 Mar 2024 22:00:00 +0200 Subject: [PATCH 16/24] src/model.c: sanitize style --- src/model.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/model.c b/src/model.c index 23675d9..4b4b29e 100644 --- a/src/model.c +++ b/src/model.c @@ -49,7 +49,8 @@ struct model_t *knownModels = NULL; */ void model_populate_hashtable(xmlNode *node, xmlHashTablePtr hashTable, - char *currentPath) { + char *currentPath) +{ if (node == NULL) return; // Skip text nodes and others that are not element nodes @@ -83,7 +84,8 @@ void model_populate_hashtable(xmlNode *node, * @param path The path to the tag, with '/' as the delimiter. * @return The content of the tag at the end of the path, or NULL if not found. */ -char* model_find_node_by_path(xmlNode *node, const char *path) { +char* model_find_node_by_path(xmlNode *node, const char *path) +{ return (xmlNodePtr)xmlHashLookup(hashTable, (const xmlChar *)path); } @@ -97,7 +99,8 @@ char* model_find_node_by_path(xmlNode *node, const char *path) { */ int model_modify_node(xmlHashTablePtr hashTable, const char *path, - const char *newContent) { + const char *newContent) +{ xmlNodePtr node = model_find_node_by_path(hashTable, path); if (node) { // Free the current content of the node, if any -- 2.39.5 From 14e630cb35818ad8ce5547ac95b83ed28d6522a9 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Fri, 29 Mar 2024 22:00:00 +0200 Subject: [PATCH 17/24] src/model.c: manage attributes --- src/model.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/model.c b/src/model.c index 4b4b29e..c272f69 100644 --- a/src/model.c +++ b/src/model.c @@ -112,6 +112,48 @@ int model_modify_node(xmlHashTablePtr hashTable, return 0; // Node not found or unable to modify } +/** + * Retrieves the value of an attribute for a node identified by its path. + * + * @param hashTable The hash table containing nodes indexed by their paths. + * @param nodePath The path to the node. + * @param attributeName The name of the attribute. + * @return The value of the attribute, or NULL if not found. + */ +char* model_get_attribute (xmlHashTablePtr hashTable, + const char* nodePath, + const char* attributeName) +{ + xmlNodePtr node = (xmlNodePtr)xmlHashLookup(hashTable, (const xmlChar*)nodePath); + if (node) { + return getAttributeValue(node, attributeName); + } else { + return NULL; + } +} + +/** + * Sets or updates the value of an attribute for a node identified by its path. + * + * @param hashTable The hash table containing nodes indexed by their paths. + * @param nodePath The path to the node. + * @param attributeName The name of the attribute to set. + * @param attributeValue The value to set for the attribute. + * @return 1 on success, 0 on failure. + */ +int model_set_attribute (xmlHashTablePtr hashTable, + const char* nodePath, + const char* attributeName, + const char* attributeValue) +{ + xmlNodePtr node = (xmlNodePtr)xmlHashLookup(hashTable, (const xmlChar*)nodePath); + if (node) { + return setAttributeValue(node, attributeName, attributeValue); + } else { + return 0; + } +} + /* -------------------------------------------------------------------------- */ char model_get_dim (struct model_t *self) -- 2.39.5 From 31d4e575df93e663f6ea5472f728d29136d6b907 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Sat, 30 Mar 2024 22:01:00 +0200 Subject: [PATCH 18/24] src/model.c: sanitize style --- src/model.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/model.c b/src/model.c index c272f69..6e68723 100644 --- a/src/model.c +++ b/src/model.c @@ -47,7 +47,7 @@ struct model_t *knownModels = NULL; * @param hashTable The hash table to populate. * @param currentPath The path to the current node. */ -void model_populate_hashtable(xmlNode *node, +void model_populate_hashtable (xmlNode *node, xmlHashTablePtr hashTable, char *currentPath) { @@ -84,7 +84,7 @@ void model_populate_hashtable(xmlNode *node, * @param path The path to the tag, with '/' as the delimiter. * @return The content of the tag at the end of the path, or NULL if not found. */ -char* model_find_node_by_path(xmlNode *node, const char *path) +char* model_find_node_by_path (xmlNode *node, const char *path) { return (xmlNodePtr)xmlHashLookup(hashTable, (const xmlChar *)path); } @@ -97,7 +97,7 @@ char* model_find_node_by_path(xmlNode *node, const char *path) * @param newContent The new content to set for the node. * @return 1 if the node was found and modified, 0 otherwise. */ -int model_modify_node(xmlHashTablePtr hashTable, +int model_modify_node (xmlHashTablePtr hashTable, const char *path, const char *newContent) { -- 2.39.5 From e70154923fe970a67502cb763ba5af9166cd3752 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Sun, 31 Mar 2024 22:04:00 +0200 Subject: [PATCH 19/24] src/model.c: support node copying and changing attribute (e.g. ID) on the fly --- src/model.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 7 deletions(-) diff --git a/src/model.c b/src/model.c index 6e68723..df47a08 100644 --- a/src/model.c +++ b/src/model.c @@ -38,6 +38,8 @@ struct model_t *knownModels = NULL; +static xmlHashTablePtr hashTable == NULL; + /* -------------------------------------------------------------------------- */ /** @@ -48,11 +50,12 @@ struct model_t *knownModels = NULL; * @param currentPath The path to the current node. */ void model_populate_hashtable (xmlNode *node, - xmlHashTablePtr hashTable, char *currentPath) { if (node == NULL) return; + assert(hashTable == NULL); + // Skip text nodes and others that are not element nodes if (node->type != XML_ELEMENT_NODE) { model_populate_hashtable(node->next, hashTable, currentPath); @@ -86,6 +89,8 @@ void model_populate_hashtable (xmlNode *node, */ char* model_find_node_by_path (xmlNode *node, const char *path) { + assert(hashTable != NULL); + return (xmlNodePtr)xmlHashLookup(hashTable, (const xmlChar *)path); } @@ -97,10 +102,11 @@ char* model_find_node_by_path (xmlNode *node, const char *path) * @param newContent The new content to set for the node. * @return 1 if the node was found and modified, 0 otherwise. */ -int model_modify_node (xmlHashTablePtr hashTable, - const char *path, +int model_modify_node (const char *path, const char *newContent) { + assert(hashTable != NULL); + xmlNodePtr node = model_find_node_by_path(hashTable, path); if (node) { // Free the current content of the node, if any @@ -120,10 +126,11 @@ int model_modify_node (xmlHashTablePtr hashTable, * @param attributeName The name of the attribute. * @return The value of the attribute, or NULL if not found. */ -char* model_get_attribute (xmlHashTablePtr hashTable, - const char* nodePath, +char* model_get_attribute (const char* nodePath, const char* attributeName) { + assert(hashTable != NULL); + xmlNodePtr node = (xmlNodePtr)xmlHashLookup(hashTable, (const xmlChar*)nodePath); if (node) { return getAttributeValue(node, attributeName); @@ -141,11 +148,12 @@ char* model_get_attribute (xmlHashTablePtr hashTable, * @param attributeValue The value to set for the attribute. * @return 1 on success, 0 on failure. */ -int model_set_attribute (xmlHashTablePtr hashTable, - const char* nodePath, +int model_set_attribute (const char* nodePath, const char* attributeName, const char* attributeValue) { + assert(hashTable != NULL); + xmlNodePtr node = (xmlNodePtr)xmlHashLookup(hashTable, (const xmlChar*)nodePath); if (node) { return setAttributeValue(node, attributeName, attributeValue); @@ -154,6 +162,46 @@ int model_set_attribute (xmlHashTablePtr hashTable, } } +/** + * Copies a node recursively to a new sibling node with a modified attribute and updates the hashtable. + * + * @param doc The document to which the nodes belong. + * @param node The node to copy. + * @param hashTable The hashtable for tracking nodes by path. + * @param originalNodePath The path of the original node in the hashtable. + * @param attributeName The name of the attribute to modify in the copied node. + * @param newAttributeValue The new value for the specified attribute in the copied node. + * @return The copied node with the modified attribute, or NULL on failure. + */ +xmlNodePtr model_copy_node(struct model_t *self, + xmlNodePtr node, + const char* originalNodePath, + const char* attributeName, + const char* newAttributeValue) +{ + assert(hashTable != NULL); + + // Deep copy the node + xmlNodePtr copiedNode = xmlDocCopyNode(node, self->doc, 1); + if (copiedNode == NULL) { + return NULL; // Failed to copy + } + + // Modify the specified attribute in the copied node + xmlSetProp(copiedNode, (const xmlChar*)attributeName, (const xmlChar*)newAttributeValue); + + // Insert the copied node as the next sibling of the original node + xmlAddNextSibling(node, copiedNode); + + // Calculate the new node's path. + char newPath[1024]; + snprintf(newPath, sizeof(newPath), "%s_copy", originalNodePath); + + // Update the hashtable with the new node's path + xmlHashAddEntry(hashTable, (const xmlChar*)newPath, copiedNode); + + return copiedNode; +} /* -------------------------------------------------------------------------- */ char model_get_dim (struct model_t *self) -- 2.39.5 From 256fa60f13d36277babc370e2fb807f302a4857c Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Tue, 2 Apr 2024 23:04:07 +0200 Subject: [PATCH 20/24] src/model.c: iteration on siblings of a given node --- src/model.c | 407 +++++----------------------------------------------- 1 file changed, 32 insertions(+), 375 deletions(-) diff --git a/src/model.c b/src/model.c index df47a08..cdbd47a 100644 --- a/src/model.c +++ b/src/model.c @@ -202,6 +202,38 @@ xmlNodePtr model_copy_node(struct model_t *self, return copiedNode; } + +/** + * Iterates over all sibling nodes of a given node, typically used to process + * each child of a specific parent node. + * + * This function takes a starting node (expected to be a parent node like ) + * and iterates through all of its child nodes. + * It calls a user-provided callback function for each child node that is an + * element node (ignores text and other types of nodes). + * + * @param node The starting node whose siblings (children) will be iterated + * over. This node is not processed by the callback, only its siblings are. + * @param processNode A callback function that will be called for each sibling + * node. This function should have a signature of + * void functionName(xmlNodePtr node, void* userData), where + * 'node' is the current sibling element node, and + * 'userData' is a pointer to user-defined data passed + * through iterateSiblings. + * @param userData A void pointer to user-defined data. This can be used to pass + * additional information to the callback function. It is passed + * directly through to the callback function as its second argument. + */ +void model_iterate_node(xmlNodePtr node, + void (*processNode)(xmlNodePtr node, void* userData), void* userData) +{ + for (xmlNodePtr curNode = node->children; curNode; curNode = curNode->next) { + if (curNode->type == XML_ELEMENT_NODE) { + processNode (curNode, userData); + } + } +} + /* -------------------------------------------------------------------------- */ char model_get_dim (struct model_t *self) @@ -242,381 +274,6 @@ char model_get_multiplicity (struct model_t *self) return 0; } -bool model_get_next_state (struct model_t *self, char *new_state_id) -{ - static xmlNodePtr cur_node = NULL; - xmlAttr *attribute; - xmlChar *value; - - if (cur_node == NULL) { - // Get first state - cur_node = model_get_node(self, (xmlChar *)"savedstates/state"); - - } else { - // Get next state - if (cur_node->next) - cur_node = cur_node->next; - else - return false; - } - - // Lookup in properties - if (model_get_node_str_attrib(cur_node, "id", new_state_id)) - return true; - - cur_node = NULL; - return false; -} - -long model_get_state_arrows_count (struct model_t *self, const char *state_id) -{ - xmlNodePtr cur_node = NULL; - xmlAttr *attribute; - long value = 0; - bool found = false; - char temp_char[25]; - uint check = 0; // bit field checker - - //printf("NEW CALL : cur_node = %p\n", cur_node); - - assert(state_id); - - // Get first state node - cur_node = model_get_node(self, (xmlChar *)"savedstates/state"); - - // Lookup in properties - while (cur_node && cur_node->properties) { - attribute = cur_node->properties; - - // Look for the id attribute - if (model_get_node_str_attrib(cur_node, "id", &temp_char)) { - if (!xmlStrcmp(temp_char, (const xmlChar *)state_id)) { - found = true; - break; - } - } - cur_node = cur_node->next; - } - - // Check if the state has been found - if (!found) { - cur_node = NULL; - return -1; - } - - - // Count arrows - if (cur_node->children) { - cur_node = cur_node->children; - while (cur_node) { - if (!xmlStrcmp(cur_node->name, (const xmlChar *)"arrow")) - value++; - cur_node = cur_node->next; - } - } else { - return -1; - } - return value; -} - -bool model_get_next_arrow (struct model_t *self, - struct arrow_t *new_arrow, - const char *state_id, - char dimension) -{ - static xmlNodePtr cur_node = NULL; - xmlAttr *attribute; - xmlChar *value; - bool found = false; - char temp_char[25]; - uint check = 0; // bit field checker - - //printf("NEW CALL : cur_node = %p\n", cur_node); - - assert(new_arrow); - assert(state_id); - - if (cur_node == NULL) { - // Get first state node - cur_node = model_get_node(self, (xmlChar *)"savedstates/state"); - - // Lookup in properties - while (cur_node && cur_node->properties) { - attribute = cur_node->properties; - - // Look for the id attribute - if (model_get_node_str_attrib(cur_node, "id", &temp_char)) { - if (!xmlStrcmp(temp_char, (const xmlChar *)state_id)) { - found = true; - break; - } - } - cur_node = cur_node->next; - } - - // Check if the state has been found - if (!found) { - cur_node = NULL; - return false; - } - - // Get first arrow - if (cur_node->children) { - cur_node = cur_node->children; - - found = false; - while (cur_node && cur_node->name) { - if (!xmlStrcmp(cur_node->name, (const xmlChar *)"arrow")) { - found = true; - break; - } - cur_node = cur_node->next; - } - } - - // Check if the state has been found - if (!found) { - cur_node = NULL; - return false; - } - - } else { - // Get next arrow - found = false; - while (cur_node->next) { - cur_node = cur_node->next; - if (!xmlStrcmp(cur_node->name, (const xmlChar *)"arrow")) { - found = true; - break; - } - } - - // Check if the state has been found - if (!found) { - cur_node = NULL; - return false; - } - } - - //printf("DURING CALL : cur_node = %p\n", cur_node); - //printf("DURING CALL : cur_node->name = %s\n", cur_node->name); - - // Lookup in properties - if (cur_node && cur_node->properties) { - attribute = cur_node->properties; - - while(attribute && attribute->name && attribute->children) { - //printf("attr name : %s\n", attribute->name); - if (!xmlStrcmp(attribute->name, (const xmlChar *)"site")) { - value = xmlNodeListGetString(cur_node->doc, attribute->children, 1); - new_arrow->site = strtol((char *)value, NULL, 0); - xmlFree(value); - check |= READ_SITE; - } - if (!xmlStrcmp(attribute->name, (const xmlChar *)"weight")) { - value = xmlNodeListGetString(cur_node->doc, attribute->children, 1); - new_arrow->load = strtol((char *)value, NULL, 0); - xmlFree(value); - check |= READ_WEIGHT; - } - if (!xmlStrcmp(attribute->name, (const xmlChar *)"x")) { - value = xmlNodeListGetString(cur_node->doc, attribute->children, 1); - new_arrow->x = strtol((char *)value, NULL, 0); - xmlFree(value); - check |= READ_X; - } - if (!xmlStrcmp(attribute->name, (const xmlChar *)"y")) { - value = xmlNodeListGetString(cur_node->doc, attribute->children, 1); - new_arrow->y = strtol((char *)value, NULL, 0); - xmlFree(value); - check |= READ_Y; - } - if (!xmlStrcmp(attribute->name, (const xmlChar *)"z")) { - value = xmlNodeListGetString(cur_node->doc, attribute->children, 1); - new_arrow->z = strtol((char *)value, NULL, 0); - xmlFree(value); - check |= READ_Z; - } - attribute = attribute->next; - } - - switch(dimension) { - case 3: - return (bool)(check & SUCCESSFUL_READ_ARROW_XYZ); - case 2: - return (bool)(check & SUCCESSFUL_READ_ARROW_XY); - case 1: - return (bool)(check & SUCCESSFUL_READ_ARROW_X); - } - } - cur_node = NULL; - return false; -} - -bool model_get_next_conditions (struct model_t *self, char *new_cond_id) -{ - static xmlNodePtr cur_node = NULL; - xmlAttr *attribute; - xmlChar *value; - - printlog("NEW CALL : cur_node = %p\n", cur_node); - - if (cur_node == NULL) { - // Get first state - cur_node = model_get_node(self, (xmlChar *)"conditions"); - - } else { - // Get next state - if (cur_node->next) - cur_node = cur_node->next; - else - return false; - } - - printlog("END CALL : cur_node = %p\n", cur_node); - - // Lookup in properties - if (model_get_node_str_attrib(cur_node, "id", new_cond_id)) - return true; - - cur_node = NULL; - return false; -} - -bool model_get_next_condition (struct model_t *self, - struct condition_t *new_condition, - const char *cond_id, - char dimension) -{ - static xmlNodePtr cur_node = NULL; - xmlAttr *attribute; - xmlChar *value; - bool found = false; - char temp_char[25]; - uint check = 0; // bit field checker - - printlog("NEW CALL : cur_node = %p\n", cur_node); - - assert(new_condition); - assert(cond_id); - - if (cur_node == NULL) { - // Get first state node - cur_node = model_get_node(self, (xmlChar *)"conditions"); - - // Lookup in properties - while (cur_node && cur_node->properties) { - attribute = cur_node->properties; - - // Look for the id attribute - if (model_get_node_str_attrib(cur_node, "id", &temp_char)) { - if (!xmlStrcmp(temp_char, (const xmlChar *)cond_id)) { - found = true; - break; - } - } - cur_node = cur_node->next; - } - - // Check if the state has been found - if (!found) { - cur_node = NULL; - return false; - } - - // Get first arrow - if (cur_node->children) { - cur_node = cur_node->children; - - found = false; - while (cur_node && cur_node->name) { - if (!xmlStrcmp(cur_node->name, (const xmlChar *)"arrow")) { - found = true; - break; - } - cur_node = cur_node->next; - } - } - - // Check if the state has been found - if (!found) { - cur_node = NULL; - return false; - } - - } else { - // Get next condition - found = false; - while (cur_node->next) { - cur_node = cur_node->next; - if (!xmlStrcmp(cur_node->name, (const xmlChar *)"arrow")) { - found = true; - break; - } - } - - // Check if the state has been found - if (!found) { - cur_node = NULL; - return false; - } - } - - printlog("DURING CALL : cur_node = %p\n", cur_node); - printlog("DURING CALL : cur_node->name = %s\n", cur_node->name); - - // Lookup in properties - if (cur_node && cur_node->properties) { - attribute = cur_node->properties; - - while(attribute && attribute->name && attribute->children) { - //printf("attr name : %s\n", attribute->name); - if (!xmlStrcmp(attribute->name, (const xmlChar *)"site")) { - value = xmlNodeListGetString(cur_node->doc, attribute->children, 1); - new_condition->site = strtol((char *)value, NULL, 0); - xmlFree(value); - check |= READ_SITE; - } - if (!xmlStrcmp(attribute->name, (const xmlChar *)"weight")) { - value = xmlNodeListGetString(cur_node->doc, attribute->children, 1); - new_condition->load = strtol((char *)value, NULL, 0); - xmlFree(value); - check |= READ_WEIGHT; - } - if (!xmlStrcmp(attribute->name, (const xmlChar *)"x")) { - value = xmlNodeListGetString(cur_node->doc, attribute->children, 1); - new_condition->x = strtol((char *)value, NULL, 0); - xmlFree(value); - check |= READ_X; - } - if (!xmlStrcmp(attribute->name, (const xmlChar *)"y")) { - value = xmlNodeListGetString(cur_node->doc, attribute->children, 1); - new_condition->y = strtol((char *)value, NULL, 0); - xmlFree(value); - check |= READ_Y; - } - if (!xmlStrcmp(attribute->name, (const xmlChar *)"z")) { - value = xmlNodeListGetString(cur_node->doc, attribute->children, 1); - new_condition->z = strtol((char *)value, NULL, 0); - xmlFree(value); - check |= READ_Z; - } - attribute = attribute->next; - } - - switch(dimension) { - case 3: - return (bool)(check & SUCCESSFUL_READ_ARROW_XYZ); - case 2: - return (bool)(check & SUCCESSFUL_READ_ARROW_XY); - case 1: - return (bool)(check & SUCCESSFUL_READ_ARROW_X); - } - } - cur_node = NULL; - return false; -} - /* -------------------------------------------------------------------------- */ static bool model_init(struct model_t *self) -- 2.39.5 From 6da0e650731ad4c21322c43e3a5cf0fc2c0b3d78 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 3 Apr 2024 22:05:56 +0200 Subject: [PATCH 21/24] src/model.c: sanitize style --- src/model.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/model.c b/src/model.c index cdbd47a..08d5ec5 100644 --- a/src/model.c +++ b/src/model.c @@ -58,26 +58,26 @@ void model_populate_hashtable (xmlNode *node, // Skip text nodes and others that are not element nodes if (node->type != XML_ELEMENT_NODE) { - model_populate_hashtable(node->next, hashTable, currentPath); + model_populate_hashtable (node->next, hashTable, currentPath); return; } // Calculate new path char newPath[1024]; // large enough if (currentPath && strlen(currentPath) > 0) { - snprintf(newPath, sizeof(newPath), "%s/%s", currentPath, node->name); + snprintf (newPath, sizeof(newPath), "%s/%s", currentPath, node->name); } else { - snprintf(newPath, sizeof(newPath), "%s", node->name); // Root element + snprintf (newPath, sizeof(newPath), "%s", node->name); // Root element } // Add current node to hash table with its path as the key - xmlHashAddEntry(hashTable, (const xmlChar *)newPath, node); + xmlHashAddEntry (hashTable, (const xmlChar *)newPath, node); // Recurse into child nodes with the updated path - model_populate_hashtable(node->children, hashTable, newPath); + model_populate_hashtable (node->children, hashTable, newPath); // Continue with the next sibling - model_populate_hashtable(node->next, hashTable, currentPath); + model_populate_hashtable (node->next, hashTable, currentPath); } /* @@ -110,9 +110,9 @@ int model_modify_node (const char *path, xmlNodePtr node = model_find_node_by_path(hashTable, path); if (node) { // Free the current content of the node, if any - xmlNodeSetContent(node, (const xmlChar *)""); // Clear existing content + xmlNodeSetContent (node, (const xmlChar *)""); // Clear existing content // Set new content - xmlNodeSetContent(node, (const xmlChar *)newContent); + xmlNodeSetContent (node, (const xmlChar *)newContent); return 1; // Success } return 0; // Node not found or unable to modify @@ -131,9 +131,9 @@ char* model_get_attribute (const char* nodePath, { assert(hashTable != NULL); - xmlNodePtr node = (xmlNodePtr)xmlHashLookup(hashTable, (const xmlChar*)nodePath); + xmlNodePtr node = (xmlNodePtr)xmlHashLookup (hashTable, (const xmlChar*)nodePath); if (node) { - return getAttributeValue(node, attributeName); + return getAttributeValue (node, attributeName); } else { return NULL; } @@ -154,9 +154,9 @@ int model_set_attribute (const char* nodePath, { assert(hashTable != NULL); - xmlNodePtr node = (xmlNodePtr)xmlHashLookup(hashTable, (const xmlChar*)nodePath); + xmlNodePtr node = (xmlNodePtr)xmlHashLookup (hashTable, (const xmlChar*)nodePath); if (node) { - return setAttributeValue(node, attributeName, attributeValue); + return setAttributeValue (node, attributeName, attributeValue); } else { return 0; } @@ -188,17 +188,17 @@ xmlNodePtr model_copy_node(struct model_t *self, } // Modify the specified attribute in the copied node - xmlSetProp(copiedNode, (const xmlChar*)attributeName, (const xmlChar*)newAttributeValue); + xmlSetProp (copiedNode, (const xmlChar*)attributeName, (const xmlChar*)newAttributeValue); // Insert the copied node as the next sibling of the original node - xmlAddNextSibling(node, copiedNode); + xmlAddNextSibling (node, copiedNode); // Calculate the new node's path. char newPath[1024]; - snprintf(newPath, sizeof(newPath), "%s_copy", originalNodePath); + snprintf (newPath, sizeof(newPath), "%s_copy", originalNodePath); // Update the hashtable with the new node's path - xmlHashAddEntry(hashTable, (const xmlChar*)newPath, copiedNode); + xmlHashAddEntry (hashTable, (const xmlChar*)newPath, copiedNode); return copiedNode; } -- 2.39.5 From bad939f9d6f4b335aff3c52806b05f5d0eb5a6bb Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Thu, 4 Apr 2024 23:01:00 +0200 Subject: [PATCH 22/24] src/model.c: integrating within actual parsing --- src/model.c | 81 ++++++++++++++++++++++++++++------------------------- 1 file changed, 43 insertions(+), 38 deletions(-) diff --git a/src/model.c b/src/model.c index 08d5ec5..3fecbc5 100644 --- a/src/model.c +++ b/src/model.c @@ -38,27 +38,25 @@ struct model_t *knownModels = NULL; -static xmlHashTablePtr hashTable == NULL; - /* -------------------------------------------------------------------------- */ /** * Recursively populates a hash table with XML nodes, using the node paths as keys. * + * @param self The self structure containing hash table containing + * nodes indexed by their paths to populate * @param node The current node being processed. - * @param hashTable The hash table to populate. * @param currentPath The path to the current node. */ -void model_populate_hashtable (xmlNode *node, - char *currentPath) +void model_populate_hashTable (struct model_t *self, xmlNode *node, char *currentPath) { if (node == NULL) return; - assert(hashTable == NULL); + assert(self->hashTable == NULL); // Skip text nodes and others that are not element nodes if (node->type != XML_ELEMENT_NODE) { - model_populate_hashtable (node->next, hashTable, currentPath); + model_populate_hashTable (node->next, self->hashTable, currentPath); return; } @@ -71,43 +69,46 @@ void model_populate_hashtable (xmlNode *node, } // Add current node to hash table with its path as the key - xmlHashAddEntry (hashTable, (const xmlChar *)newPath, node); + xmlHashAddEntry (self->hashTable, (const xmlChar *)newPath, node); // Recurse into child nodes with the updated path - model_populate_hashtable (node->children, hashTable, newPath); + model_populate_hashTable (self, node->children, newPath); // Continue with the next sibling - model_populate_hashtable (node->next, hashTable, currentPath); + model_populate_hashTable (self, node->next, currentPath); } /* * Finds a node by a path of tags. * - * @param node The starting node for the search. + * @param self The self structure containing hash table containing + * nodes indexed by their paths. * @param path The path to the tag, with '/' as the delimiter. * @return The content of the tag at the end of the path, or NULL if not found. */ -char* model_find_node_by_path (xmlNode *node, const char *path) +xmlNodePtr model_get_node (struct model_t *self, const char *path) { - assert(hashTable != NULL); + assert(self->hashTable != NULL); - return (xmlNodePtr)xmlHashLookup(hashTable, (const xmlChar *)path); + return (xmlNodePtr)xmlHashLookup (self->hashTable, (const xmlChar *)path); } /** * Modifies the content of an XML node identified by its path in the hash table. * - * @param hashTable The hash table containing the XML nodes. + * @param self The self structure containing hash table containing + * nodes indexed by their paths. * @param path The path to the node to modify. * @param newContent The new content to set for the node. * @return 1 if the node was found and modified, 0 otherwise. */ -int model_modify_node (const char *path, - const char *newContent) +int model_modify_node (struct model_t *self, + const char *path, + const char *newContent) { - assert(hashTable != NULL); + assert(self->hashTable != NULL); - xmlNodePtr node = model_find_node_by_path(hashTable, path); + xmlNodePtr node = model_get_node (self, path); if (node) { // Free the current content of the node, if any xmlNodeSetContent (node, (const xmlChar *)""); // Clear existing content @@ -121,17 +122,19 @@ int model_modify_node (const char *path, /** * Retrieves the value of an attribute for a node identified by its path. * - * @param hashTable The hash table containing nodes indexed by their paths. + * @param self The self structure containing hash table containing + * nodes indexed by their paths. * @param nodePath The path to the node. * @param attributeName The name of the attribute. * @return The value of the attribute, or NULL if not found. */ -char* model_get_attribute (const char* nodePath, +char *model_get_attribute (struct model_t *self, + const char* nodePath, const char* attributeName) { - assert(hashTable != NULL); + assert(self->hashTable != NULL); - xmlNodePtr node = (xmlNodePtr)xmlHashLookup (hashTable, (const xmlChar*)nodePath); + xmlNodePtr node = (xmlNodePtr)xmlHashLookup (self->hashTable, (const xmlChar*)nodePath); if (node) { return getAttributeValue (node, attributeName); } else { @@ -142,19 +145,21 @@ char* model_get_attribute (const char* nodePath, /** * Sets or updates the value of an attribute for a node identified by its path. * - * @param hashTable The hash table containing nodes indexed by their paths. + * @param self The self structure containing hash table containing + * nodes indexed by their paths. * @param nodePath The path to the node. * @param attributeName The name of the attribute to set. * @param attributeValue The value to set for the attribute. * @return 1 on success, 0 on failure. */ -int model_set_attribute (const char* nodePath, +int model_set_attribute (struct model_t *self, + const char* nodePath, const char* attributeName, const char* attributeValue) { - assert(hashTable != NULL); + assert(self->hashTable != NULL); - xmlNodePtr node = (xmlNodePtr)xmlHashLookup (hashTable, (const xmlChar*)nodePath); + xmlNodePtr node = (xmlNodePtr)xmlHashLookup (self->hashTable, (const xmlChar*)nodePath); if (node) { return setAttributeValue (node, attributeName, attributeValue); } else { @@ -163,12 +168,11 @@ int model_set_attribute (const char* nodePath, } /** - * Copies a node recursively to a new sibling node with a modified attribute and updates the hashtable. + * Copies a node recursively to a new sibling node with a modified attribute and updates the self->hashTable. * - * @param doc The document to which the nodes belong. * @param node The node to copy. - * @param hashTable The hashtable for tracking nodes by path. - * @param originalNodePath The path of the original node in the hashtable. + * @param self The self structure for tracking nodes by path. + * @param originalNodePath The path of the original node in the self->hashTable. * @param attributeName The name of the attribute to modify in the copied node. * @param newAttributeValue The new value for the specified attribute in the copied node. * @return The copied node with the modified attribute, or NULL on failure. @@ -179,7 +183,7 @@ xmlNodePtr model_copy_node(struct model_t *self, const char* attributeName, const char* newAttributeValue) { - assert(hashTable != NULL); + assert(self->hashTable != NULL); // Deep copy the node xmlNodePtr copiedNode = xmlDocCopyNode(node, self->doc, 1); @@ -197,8 +201,8 @@ xmlNodePtr model_copy_node(struct model_t *self, char newPath[1024]; snprintf (newPath, sizeof(newPath), "%s_copy", originalNodePath); - // Update the hashtable with the new node's path - xmlHashAddEntry (hashTable, (const xmlChar*)newPath, copiedNode); + // Update the self->hashTable with the new node's path + xmlHashAddEntry (self->hashTable, (const xmlChar*)newPath, copiedNode); return copiedNode; } @@ -224,7 +228,8 @@ xmlNodePtr model_copy_node(struct model_t *self, * additional information to the callback function. It is passed * directly through to the callback function as its second argument. */ -void model_iterate_node(xmlNodePtr node, +void model_iterate_node(struct model_t *self, + xmlNodePtr node, void (*processNode)(xmlNodePtr node, void* userData), void* userData) { for (xmlNodePtr curNode = node->children; curNode; curNode = curNode->next) { @@ -308,9 +313,9 @@ static bool model_init(struct model_t *self) return false; } - self->hashtable = xmlHashCreate(0); + model_populate_hashTable(self, node, "/"); - if (self->hashtable == NULL) { + if (self->hashTable == NULL) { printerr("Can't create model hash table !\n"); xmlFreeDoc(self->doc); return false; @@ -322,7 +327,7 @@ static bool model_init(struct model_t *self) static bool model_shutdown(struct model_t *self) { xmlFreeDoc(self->doc); - xmlHashFree(self->hashtable, NULL); + xmlHashFree(self->hashTable, NULL); // This is to debug memory for regression tests //xmlMemoryDump(); -- 2.39.5 From dd00857495c8ec83cc2efdca407335f4512be32b Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Fri, 5 Apr 2024 23:00:00 +0200 Subject: [PATCH 23/24] src/model.c: fix bug with root path at init --- src/model.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model.c b/src/model.c index 3fecbc5..0593669 100644 --- a/src/model.c +++ b/src/model.c @@ -313,7 +313,7 @@ static bool model_init(struct model_t *self) return false; } - model_populate_hashTable(self, node, "/"); + model_populate_hashTable(self, node, ""); if (self->hashTable == NULL) { printerr("Can't create model hash table !\n"); -- 2.39.5 From 6d543ee93c3cd237d79ed221fdfd2fce7603824f Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Sat, 6 Apr 2024 23:00:03 +0200 Subject: [PATCH 24/24] src/model.c: fix hashtable variable namming --- src/model.c | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/model.c b/src/model.c index 0593669..a0ed04e 100644 --- a/src/model.c +++ b/src/model.c @@ -48,15 +48,15 @@ struct model_t *knownModels = NULL; * @param node The current node being processed. * @param currentPath The path to the current node. */ -void model_populate_hashTable (struct model_t *self, xmlNode *node, char *currentPath) +void model_populate_hashtable (struct model_t *self, xmlNode *node, char *currentPath) { if (node == NULL) return; - assert(self->hashTable == NULL); + assert(self->hashtable == NULL); // Skip text nodes and others that are not element nodes if (node->type != XML_ELEMENT_NODE) { - model_populate_hashTable (node->next, self->hashTable, currentPath); + model_populate_hashtable (node->next, self->hashtable, currentPath); return; } @@ -69,13 +69,13 @@ void model_populate_hashTable (struct model_t *self, xmlNode *node, char *curren } // Add current node to hash table with its path as the key - xmlHashAddEntry (self->hashTable, (const xmlChar *)newPath, node); + xmlHashAddEntry (self->hashtable, (const xmlChar *)newPath, node); // Recurse into child nodes with the updated path - model_populate_hashTable (self, node->children, newPath); + model_populate_hashtable (self, node->children, newPath); // Continue with the next sibling - model_populate_hashTable (self, node->next, currentPath); + model_populate_hashtable (self, node->next, currentPath); } /* @@ -88,9 +88,9 @@ void model_populate_hashTable (struct model_t *self, xmlNode *node, char *curren */ xmlNodePtr model_get_node (struct model_t *self, const char *path) { - assert(self->hashTable != NULL); + assert(self->hashtable != NULL); - return (xmlNodePtr)xmlHashLookup (self->hashTable, (const xmlChar *)path); + return (xmlNodePtr)xmlHashLookup (self->hashtable, (const xmlChar *)path); } /** @@ -106,7 +106,7 @@ int model_modify_node (struct model_t *self, const char *path, const char *newContent) { - assert(self->hashTable != NULL); + assert(self->hashtable != NULL); xmlNodePtr node = model_get_node (self, path); if (node) { @@ -132,9 +132,9 @@ char *model_get_attribute (struct model_t *self, const char* nodePath, const char* attributeName) { - assert(self->hashTable != NULL); + assert(self->hashtable != NULL); - xmlNodePtr node = (xmlNodePtr)xmlHashLookup (self->hashTable, (const xmlChar*)nodePath); + xmlNodePtr node = (xmlNodePtr)xmlHashLookup (self->hashtable, (const xmlChar*)nodePath); if (node) { return getAttributeValue (node, attributeName); } else { @@ -157,9 +157,9 @@ int model_set_attribute (struct model_t *self, const char* attributeName, const char* attributeValue) { - assert(self->hashTable != NULL); + assert(self->hashtable != NULL); - xmlNodePtr node = (xmlNodePtr)xmlHashLookup (self->hashTable, (const xmlChar*)nodePath); + xmlNodePtr node = (xmlNodePtr)xmlHashLookup (self->hashtable, (const xmlChar*)nodePath); if (node) { return setAttributeValue (node, attributeName, attributeValue); } else { @@ -168,11 +168,11 @@ int model_set_attribute (struct model_t *self, } /** - * Copies a node recursively to a new sibling node with a modified attribute and updates the self->hashTable. + * Copies a node recursively to a new sibling node with a modified attribute and updates the self->hashtable. * * @param node The node to copy. * @param self The self structure for tracking nodes by path. - * @param originalNodePath The path of the original node in the self->hashTable. + * @param originalNodePath The path of the original node in the self->hashtable. * @param attributeName The name of the attribute to modify in the copied node. * @param newAttributeValue The new value for the specified attribute in the copied node. * @return The copied node with the modified attribute, or NULL on failure. @@ -183,7 +183,7 @@ xmlNodePtr model_copy_node(struct model_t *self, const char* attributeName, const char* newAttributeValue) { - assert(self->hashTable != NULL); + assert(self->hashtable != NULL); // Deep copy the node xmlNodePtr copiedNode = xmlDocCopyNode(node, self->doc, 1); @@ -201,8 +201,8 @@ xmlNodePtr model_copy_node(struct model_t *self, char newPath[1024]; snprintf (newPath, sizeof(newPath), "%s_copy", originalNodePath); - // Update the self->hashTable with the new node's path - xmlHashAddEntry (self->hashTable, (const xmlChar*)newPath, copiedNode); + // Update the self->hashtable with the new node's path + xmlHashAddEntry (self->hashtable, (const xmlChar*)newPath, copiedNode); return copiedNode; } @@ -313,9 +313,9 @@ static bool model_init(struct model_t *self) return false; } - model_populate_hashTable(self, node, ""); + model_populate_hashtable(self, node, ""); - if (self->hashTable == NULL) { + if (self->hashtable == NULL) { printerr("Can't create model hash table !\n"); xmlFreeDoc(self->doc); return false; @@ -327,7 +327,7 @@ static bool model_init(struct model_t *self) static bool model_shutdown(struct model_t *self) { xmlFreeDoc(self->doc); - xmlHashFree(self->hashTable, NULL); + xmlHashFree(self->hashtable, NULL); // This is to debug memory for regression tests //xmlMemoryDump(); -- 2.39.5