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 1d5cc68..6c94108 100644 --- a/Makefile +++ b/Makefile @@ -20,120 +20,123 @@ # 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 -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 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 - @gdb bin/gem-graph-server \ - -ex "set args -C debian/etc -M debian/var/models -U debian/var/users" +build_system: + @echo -e ${CL2}[$@] ${CL}building...${CL3} + @make $(BINDIR)/gem-graph-server -j $(NTHREADS) + @echo -e ${CL2}[$@] ${CL}done.${CL3} -run-valgrind: all +run: build_system + @echo -e ${CL2}[$@] ${CL}executing...${CL3} + @bin/gem-graph-server -C data/config -M data/models -U data/users + @echo -e ${CL2}[$@] ${CL}done.${CL3} + +debug: build_system + @echo -e ${CL2}[$@] ${CL}executing...${CL3} + @gdb --args $(BINDIR)/gem-graph-server -C data/config -M data/models \ + -U data/users + @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 + bin/gem-graph-server -C data/config -M data/models -U data/users + @echo -e ${CL2}[$@] ${CL}done.${CL3} -run-both: all - @bin/gem-graph-server -C debian/etc -M debian/var/models \ - -U debian/var/users & sleep 1 && bin/gem-graph-ctl -run-server: all - @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 +run-both: build_system + @echo -e ${CL2}[$@] ${CL}executing...${CL3} + @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 data/config -M data/models -U data/users + @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/data/models/dimers random walk.xml b/data/models/dimers random walk.xml new file mode 100644 index 0000000..a7c9efc --- /dev/null +++ b/data/models/dimers random walk.xml @@ -0,0 +1,119 @@ + + + + + Modèle de test + + Léontine Patinette + + 2 + + 1630000000 + + 1.0 + + Ref + + + + + 0 + 9 + + + + + + + + + + + + + + + + 2 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/data/models/dimers_random_walk_recursif.xml b/data/models/dimers_random_walk_recursif.xml new file mode 100644 index 0000000..976512c --- /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 + + + + + + + + + + + + + + + + 2 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/debian/var/models/schemas/model_0.2.xmls b/data/schemas/models_0.2.1.xmls similarity index 70% rename from debian/var/models/schemas/model_0.2.xmls rename to data/schemas/models_0.2.1.xmls index a4f255d..3a623f5 100644 --- a/debian/var/models/schemas/model_0.2.xmls +++ b/data/schemas/models_0.2.1.xmls @@ -1,9 +1,9 @@ - + @@ -11,14 +11,16 @@ - - - - - + + + + + + + @@ -27,14 +29,6 @@ - - - - - - - - @@ -48,23 +42,45 @@ + + + + + + + + + + + + - - - - + + + + + + + + + - - - - + + + + + + + + + @@ -79,16 +95,6 @@ - - - - - - - - - - @@ -103,50 +109,38 @@ - + - + - + - + - + - - + - - - - - - - - - - - - + + - @@ -159,16 +153,16 @@ - + - + - + @@ -197,3 +191,4 @@ + 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/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/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..4d70404 100644 --- a/include/arrows.h +++ b/include/arrows.h @@ -27,33 +27,38 @@ /* -------------------------------------------------------------------------- */ -static inline int ArrowsInitLock(ArrowArray_t *arrowArray) -{ - return pthread_spin_init(&arrowArray->lock, PTHREAD_PROCESS_SHARED); -} +/* + * Structure describing an arrow + */ +struct arrow_t { + uint load; + uint site; + uint x; + uint y; + uint z; -static inline int ArrowsDestroyLock(ArrowArray_t *arrowArray) -{ - return pthread_spin_destroy(&arrowArray->lock); -} + struct arrow_t *next; + struct arrow_t *prev; +}; -static inline int ArrowsAcquireLock(ArrowArray_t *arrowArray) -{ - return pthread_spin_lock(&arrowArray->lock); -} +struct condition_t { + uint id; + uint load; + uint site; + uint x; + uint y; + uint z; -static inline int ArrowsAcquireNonBlockingLock(ArrowArray_t *arrowArray) -{ - return pthread_spin_trylock(&arrowArray->lock); -} + struct condition_t *child; + struct condition_t *parent; +}; -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); +struct arrow_t *arrow_new(struct arrow_t *self); -bool ArrowRemove(Scheduler_t *scheduler, int x, int y, int z, int siteId, - int weight); +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/base.h b/include/base.h index c348dcf..abf3e9a 100644 --- a/include/base.h +++ b/include/base.h @@ -22,185 +22,44 @@ #include #include -#include +#include #include #include #include #include +#include #include #include #include +#include +#include #define BASE_H /* -------------------------------------------------------------------------- */ -#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[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])) /* -------------------------------------------------------------------------- */ -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 { +struct parameters_t +{ char *configDir; - char *modelDir; char *userDir; -} typedef Parameters_t; + char *modelDir; +}; + + + + + + diff --git a/include/centers.h b/include/centers.h deleted file mode 100644 index be2c188..0000000 --- a/include/centers.h +++ /dev/null @@ -1,38 +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 - -/* -------------------------------------------------------------------------- */ - -// -------------------------------------------------------------------------- // -// 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..8c0fe70 100644 --- a/include/cmds.h +++ b/include/cmds.h @@ -32,19 +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 *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..b278694 100644 --- a/include/model.h +++ b/include/model.h @@ -27,6 +27,12 @@ #include #include +#include +#include +#include +#include + +#include "../include/arrows.h" /* -------------------------------------------------------------------------- */ @@ -44,47 +50,144 @@ /* -------------------------------------------------------------------------- */ +struct space_unit_t +{ + uint id; + struct arrow_t *sites[]; // Array of struct arrow_t elements + // - lenght is multiplicity +}; + +struct space_t +{ + // Dimensions of space. + 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 + char id[25]; + int owner_id; + time_t date; + + struct space_t *space; + struct arrow_t *arrows; +}; + +struct model_t +{ + // Metadata + char id; + int owner_id; + time_t date; + union version + { + int major; + int minor; + }; + + // XML handlers + 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 + 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 conditions + struct condition_t *conditions; + + // 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_t *next; + struct model_t *prev; +}; + // -------------------------------------------------------------------------- // // Model init function (and model discovery) // // -------------------------------------------------------------------------- // -void ModelSystemInit(Parameters_t *parameters); +void model_system_init (struct parameters_t *parameters); // -------------------------------------------------------------------------- // // Model stopping function // // -------------------------------------------------------------------------- // -void ModelSystemDestroy(void); +void model_system_shutdown (void); // -------------------------------------------------------------------------- // // Load a model ready to execute // // -------------------------------------------------------------------------- // -int ModelLoad(int id); +bool model_load (struct model_t *self); // -------------------------------------------------------------------------- // // 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); + +// -------------------------------------------------------------------------- // +// Parsing primitives // +// -------------------------------------------------------------------------- // + +char model_get_dim(struct model_t *self); + +long model_get_dim_value(struct model_t *self, const char *axis); + +char model_get_multiplicity(struct model_t *self); + +bool model_get_next_state(struct model_t *self, char *new_state_id); + +bool model_get_next_arrow(struct model_t *self, + struct arrow_t *new_arrow, + const char *state_id, + char dimension); diff --git a/include/parsing.h b/include/parsing.h deleted file mode 100644 index 128a1f9..0000000 --- a/include/parsing.h +++ /dev/null @@ -1,73 +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 - -/* -------------------------------------------------------------------------- */ - -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..6306b9f 100644 --- a/include/scheduler.h +++ b/include/scheduler.h @@ -27,52 +27,72 @@ #include +#include "../include/worker.h" +#include "../include/model.h" + /* -------------------------------------------------------------------------- */ +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 +}; + +struct scheduler_t +{ + int id; + int n_workers; + int pleaseStop; + + struct model_t **models; + struct worker_t **workers; +}; + // -------------------------------------------------------------------------- // -// Scheduler init function // +// Scheduler main function // // -------------------------------------------------------------------------- // -void SchedInit(Scheduler_t *scheduler); +int sched_start (struct scheduler_t *scheduler, struct parameters_t *parameters); // -------------------------------------------------------------------------- // // Scheduler content destructor function // // -------------------------------------------------------------------------- // -static inline void SchedContentDestroy(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; - } - if (scheduler->arrowArray) { - if (scheduler->arrowArray->array) { - free(scheduler->arrowArray->array); - scheduler->arrowArray->array = NULL; - } - free(scheduler->arrowArray); - scheduler->arrowArray = NULL; - } - } -} +// 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->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) -{ - if (scheduler) { - free(scheduler); - } -} - -// -------------------------------------------------------------------------- // -// Scheduler wait function // -// -------------------------------------------------------------------------- // -static inline void SchedWait(Scheduler_t *scheduler) -{ - pthread_join(scheduler->id, 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 203938d..6fb2137 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..8a9630b 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) -{ -} - -// -------------------------------------------------------------------------- // -// Supervisor wait function // -// -------------------------------------------------------------------------- // -static inline void SupervisorWait(Supervisor_t *supervisor) -{ - pthread_join(supervisor->id, NULL); -} +// static inline void superv_destroy (supervisor_t *supervisor) +// { +// ; +// } 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..978912c 100644 --- a/include/worker.h +++ b/include/worker.h @@ -25,31 +25,51 @@ #include "../include/base.h" #endif +#include "../include/scheduler.h" +#include "../include/arrows.h" + /* -------------------------------------------------------------------------- */ +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 +}; + +struct scheduler_t; + +struct worker_t +{ + int id; + int thread_num; + int status; + + struct arrow_t *elected_arrow; +}; + // -------------------------------------------------------------------------- // // Worker init function // // -------------------------------------------------------------------------- // -void WorkerInit(Worker_t *worker); +void worker_start(struct worker_t *self, struct scheduler_t *scheduler); // -------------------------------------------------------------------------- // // Worker destructor function // // -------------------------------------------------------------------------- // -static inline void WorkerDestroy(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 wait function // -// -------------------------------------------------------------------------- // -static inline void WorkerWait(Worker_t *worker) -{ - pthread_join(worker->id, NULL); -} +// worker->id = 0; +// free(worker->localWorkAreaCenter); +// worker->localWorkAreaCenter = 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..ffdc2c5 --- /dev/null +++ b/manifest.scm @@ -0,0 +1,27 @@ +;; +;; 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" + "libomp" + ) +) diff --git a/src/arrows.c b/src/arrows.c index d24e219..3fa91ff 100644 --- a/src/arrows.c +++ b/src/arrows.c @@ -25,7 +25,67 @@ /* -------------------------------------------------------------------------- */ +struct arrow_t *arrow_new(struct arrow_t *self) +{ + if (self == NULL) { + return calloc (1, sizeof(struct arrow_t)); + } -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); + 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/centers.c b/src/centers.c deleted file mode 100644 index 2eba517..0000000 --- a/src/centers.c +++ /dev/null @@ -1,54 +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" - -/* -------------------------------------------------------------------------- */ - -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..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" @@ -28,195 +29,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..cef1e09 100644 --- a/src/main.c +++ b/src/main.c @@ -21,48 +21,48 @@ //=-------------------------------------------------------------------------=// #include "../include/base.h" -#include "../include/server.h" -#include "../include/model.h" +#include "../include/scheduler.h" /* -------------------------------------------------------------------------- */ -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; + 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) { 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,32 +106,38 @@ 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, 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); + srand ((unsigned)t); - server = calloc(1, sizeof(*server)); + /* server = calloc(1, sizeof(*server)); */ // Initializing model system - ModelSystemInit(¶meters); + 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; */ + + while (returnValue > SCHED_NON_FATAL_ERRORS) { + returnValue = sched_start (&scheduler, ¶meters); + } + + model_system_shutdown(); free(parameters.userDir); free(parameters.modelDir); diff --git a/src/model.c b/src/model.c index db08dbd..a0ed04e 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. // // // @@ -20,291 +20,601 @@ // along with this program. If not, see . // //=-------------------------------------------------------------------------=// +#include "../include/base.h" #include "../include/model.h" -#include "../include/parsing.h" #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; +#define ALLOWED_RADIUS 10 +#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) /* -------------------------------------------------------------------------- */ -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; -} +struct model_t *knownModels = NULL; /* -------------------------------------------------------------------------- */ -void ModelAddToKnown(Model_t **newModel) +/** + * 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 currentPath The path to the current node. + */ +void model_populate_hashtable (struct model_t *self, xmlNode *node, char *currentPath) { - // 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]; + if (node == NULL) return; - // continue. model population - knownModel[knownModelSize-1]->name = - calloc(1, sizeof(char) * MODEL_STRING_SIZE); + assert(self->hashtable == NULL); - 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); + // Skip text nodes and others that are not element nodes + if (node->type != XML_ELEMENT_NODE) { + model_populate_hashtable (node->next, self->hashtable, currentPath); return; } - while ((modelDirEntry = readdir(modelDir)) != NULL) { - if ((extensionPosition = strstr(modelDirEntry->d_name, ".xml"))) { + // 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 + } - // Creating model - ModelAddToKnown(&newModel); + // Add current node to hash table with its path as the key + xmlHashAddEntry (self->hashtable, (const xmlChar *)newPath, node); - // 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)); + // Recurse into child nodes with the updated path + model_populate_hashtable (self, node->children, newPath); - // Write model name - strncpy(newModel->name, modelDirEntry->d_name, - extensionPosition - modelDirEntry->d_name); + // Continue with the next sibling + model_populate_hashtable (self, node->next, currentPath); +} + +/* + * Finds a node by a path of tags. + * + * @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. + */ +xmlNodePtr model_get_node (struct model_t *self, const char *path) +{ + assert(self->hashtable != NULL); + + return (xmlNodePtr)xmlHashLookup (self->hashtable, (const xmlChar *)path); +} + +/** + * Modifies the content of an XML node identified by its path in the hash table. + * + * @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 (struct model_t *self, + const char *path, + const char *newContent) +{ + assert(self->hashtable != NULL); + + 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 + // Set new content + xmlNodeSetContent (node, (const xmlChar *)newContent); + return 1; // Success + } + return 0; // Node not found or unable to modify +} + +/** + * Retrieves the value of an attribute for a node identified by its path. + * + * @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 (struct model_t *self, + const char* nodePath, + const char* attributeName) +{ + assert(self->hashtable != NULL); + + xmlNodePtr node = (xmlNodePtr)xmlHashLookup (self->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 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 (struct model_t *self, + const char* nodePath, + const char* attributeName, + const char* attributeValue) +{ + assert(self->hashtable != NULL); + + xmlNodePtr node = (xmlNodePtr)xmlHashLookup (self->hashtable, (const xmlChar*)nodePath); + if (node) { + return setAttributeValue (node, attributeName, attributeValue); + } else { + return 0; + } +} + +/** + * 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 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(self->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 self->hashtable with the new node's path + xmlHashAddEntry (self->hashtable, (const xmlChar*)newPath, copiedNode); + + 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(struct model_t *self, + 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) +{ + xmlAttr *attribute; + xmlChar* value; + xmlNodePtr node = model_get_node(self, + (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 (struct model_t *self, const char *axis) +{ + xmlAttr *attribute; + xmlChar *value; + long ret_value; + xmlNodePtr node = model_get_node(self, + (xmlChar *)"parameters/space-param/dimension"); + + return model_get_node_long_attrib(node, axis); +} + +char model_get_multiplicity (struct model_t *self) +{ + xmlAttr *attribute; + xmlChar* value; + xmlNodePtr node = model_get_node(self, + (xmlChar *)"parameters/space-param/site_multiplicity"); + + if (node->children) + if (node->children->content) + return (char)strtol((char *)node->children->content, NULL, 0); + + return 0; +} + +/* -------------------------------------------------------------------------- */ + +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; + } + + model_populate_hashtable(self, node, ""); + + 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; + + 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 = 0; + self->space->y_dim = 0; + self->space->z_dim = 0; + + switch(self->dimension) { + case 3: + self->space->z_dim = model_get_dim_value (self, "z"); + case 2: + self->space->y_dim = model_get_dim_value (self, "y"); + case 1: + 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) + + self->multiplicity * sizeof(struct arrow_t*)); + + // Multiplicity + 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; + } + + 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 + 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); + + 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; + + // 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"))) { + + new_model = model_new (parameters->modelDir, 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; + if (model_load (new_model)) { + // XXX Check model is valid and/or parsed + 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); } - - // Succeeded ! - printLog("Loaded model %s\n", newModel->name); } } free(modelDir); } -void ModelSystemDestroy(void) +void model_system_shutdown (void) { - for (int i = 0; i < loadedModelSize; i++) { - ModelDelete(i); + 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; } - free(loadedModel); - loadedModel = NULL; - free(knownModel); - knownModel = NULL; + + if (model) { + model_unload (model); + model_destroy (model); + } + + // Cleanup function for the XML library + xmlCleanupParser(); } diff --git a/src/parsing.c b/src/parsing.c deleted file mode 100644 index 35839cb..0000000 --- a/src/parsing.c +++ /dev/null @@ -1,583 +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" - -// -------------------------------------------------------------------------- // -// 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..e16deb6 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -20,209 +20,114 @@ // 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 void *schedulerMain(void *scheduler); - -/* -------------------------------------------------------------------------- */ - -// -------------------------------------------------------------------------- // -// Scheduler init function // -// -------------------------------------------------------------------------- // -void SchedInit(Scheduler_t *scheduler) +// +// +// +// +static int sched_new_id (void) { - pthread_create(&scheduler->id, NULL, schedulerMain, scheduler); + static int id = 0; + return id; } -/* -------------------------------------------------------------------------- */ - -// -------------------------------------------------------------------------- // -// Scheduler area finder function // -// -------------------------------------------------------------------------- // -static Center_t *findWorkArea(Center_t *centersList, Arrow_t *electedArrow, - int ruleRadius, int xmax, int ymax, int zmax) +// +// +// +// +static int sched_run(struct scheduler_t *self, + struct parameters_t *parameters, + struct model_t *model) { - register Center_t *currentCenter, *newCenter; + int n_threads = omp_get_max_threads(); + int n_arrows; + int workers_per_cycle; + int max_cycles; + int max_workers; - currentCenter = centersList->next; - newCenter = malloc(sizeof(Center_t)); + self->pleaseStop = false; + self->n_workers = 0; + n_arrows = 2000; + max_cycles = 1; + workers_per_cycle = (int) round((double)n_arrows / (double)n_threads); + max_workers = workers_per_cycle * n_threads * max_cycles; - 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; - } + printlog("Begin simulation of model %d with %d workers per cycle" + " (%d arrows to work on)\n", + model->id, + workers_per_cycle, + n_arrows); - newCenter->x = electedArrow->x; - newCenter->y = electedArrow->y; - newCenter->z = electedArrow->z; - return newCenter; -} + #pragma omp parallel + { + int thread_num; + thread_num = omp_get_thread_num(); -/* -------------------------------------------------------------------------- */ + while (!self->pleaseStop) { + // Get an ID; + self->workers[thread_num]->id = self->n_workers; -// -------------------------------------------------------------------------- // -// 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; + // Elect an arrow (XXX TODO) + self->workers[thread_num]->elected_arrow = NULL; - int ncpu, nworker, err; + // Launch evaluation + worker_start(self->workers[thread_num], self); - // 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--; - } + // Check if this is the end + if (self->n_workers > max_workers) { + self->pleaseStop = true; } } } - // 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("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 returnValue; + struct model_t *model; + self->id = sched_new_id(); + + printlog("Scheduler %d initialized with %d threads\n", + self->id, + n_threads); + + + // Allocating workers + self->workers = calloc(n_threads, sizeof(struct worker_t*)); + for (int i = 0; i < n_threads; i++) { + self->workers[i] = calloc(1, sizeof(struct worker_t)); } - printLog("Scheduler #%lu offline\n", args->id); + // 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++) { + free(self->workers[i]); + } + free(self->workers); - ArrowsDestroyLock(args->arrowArray); - - free(workerArray); - workerArray = NULL; - free(centersList); - centersList = NULL; - - return NULL; + return returnValue; } + + diff --git a/src/server.c b/src/server.c index ff738a8..b2e0981 100644 --- a/src/server.c +++ b/src/server.c @@ -20,243 +20,229 @@ // along with this program. If not, see . // //=-------------------------------------------------------------------------=// +#include "../include/base.h" #include "../include/server.h" #include "../include/cmds.h" /* -------------------------------------------------------------------------- */ -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 *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 *serverCommunicationInstance(void *serverCom) -{ - ServerCommunication_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 = (ServerCommunication_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 *serverMain(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, - serverCommunicationInstance, - (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; */ +/* } */ diff --git a/src/supervisor.c b/src/supervisor.c index 03cf758..0ff04ec 100644 --- a/src/supervisor.c +++ b/src/supervisor.c @@ -20,26 +20,9 @@ // along with this program. If not, see . // //=-------------------------------------------------------------------------=// +#include "../include/base.h" #include "../include/supervisor.h" /* -------------------------------------------------------------------------- */ -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 79b5550..0000000 Binary files a/src/tests/getchar and /dev/null differ diff --git a/src/tests/test_realloc b/src/tests/test_realloc deleted file mode 100755 index 03248fc..0000000 Binary files a/src/tests/test_realloc and /dev/null differ diff --git a/src/tests/xml b/src/tests/xml deleted file mode 100755 index b082672..0000000 Binary files a/src/tests/xml and /dev/null differ diff --git a/src/worker.c b/src/worker.c index 19d9b5c..a54a34f 100644 --- a/src/worker.c +++ b/src/worker.c @@ -20,44 +20,24 @@ // along with this program. If not, see . // //=-------------------------------------------------------------------------=// +#include "../include/base.h" #include "../include/worker.h" /* -------------------------------------------------------------------------- */ -static void *WorkerMain(void *worker); +double truc; -/* -------------------------------------------------------------------------- */ - -// -------------------------------------------------------------------------- // -// Worker init function // -// -------------------------------------------------------------------------- // -void WorkerInit(Worker_t *worker) +// +// +// +// +void worker_start(struct worker_t *self, struct scheduler_t *scheduler) { - if (pthread_create(&worker->id, NULL, WorkerMain, worker)) { - printLog("Worker #%lu can't be initialized!\n", worker->id); - return; + // Locking ressources + #pragma omp critical + { + scheduler->n_workers++; + //XXX lock space units } -} - -// -------------------------------------------------------------------------- // -// 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; + self->status = WORKER_NORMAL_EXIT; }