WIP: rebooting server design

Signed-off-by: Adrien Bourmault (neox) <neox@a-lec.org>
This commit is contained in:
Adrien Bourmault 2024-03-10 22:19:12 +02:00 committed by Adrien Bourmault
parent a1b1bf772e
commit f01e882c28
Signed by: neox
GPG Key ID: 95F65F55F682A17A
39 changed files with 367 additions and 2093 deletions

176
Makefile
View File

@ -20,120 +20,124 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>. # # along with this program. If not, see <https://www.gnu.org/licenses/>. #
#=----------------------------------------------------------------------------=# #=----------------------------------------------------------------------------=#
CCOPTS=-pthread -Wall -g -Os -I/usr/include/libxml2 .PHONY: all clean install run build_system
LDFLAGS= -lc -lpthread -lxml2 .DELETE_ON_ERROR: $(BINDIR)/gem-graph-server
.DEFAULT_GOAL := all
#
# Color codes
#
CL='\033[0;32m'
CL2='\033[1;36m'
CL3='\033[0m'
NC='\033[1;37m'
#
# Variables & constants
#
NTHREADS= $(shell nproc)
CC=gcc
WARNINGS= -Wall
DEBUG= -ggdb -fno-omit-frame-pointer -fdiagnostics-color=always -fsanitize=bounds -fstack-check
#-fsanitize=address \
#-fsanitize=pointer-compare -fsanitize=pointer-subtract \
#-fsanitize=leak -fsanitize=undefined -fsanitize=null -fsanitize=bounds
OPTIMIZE= -O3
INCLUDE= $(shell pkg-config --cflags glib-2.0 libxml-2.0 gtk4)
LIBS= $(shell pkg-config --libs glib-2.0 libxml-2.0 gtk4) -lGL -lGLU -lm -lepoxy -lX11 -lGLEW
BINDIR=bin BINDIR=bin
INCDIR=include BUILDDIR=build
SRCDIR=src SRCDIR=src
DEBDIR=debian #vpath %.c $(SRCDIR)
SERVEROBJ= $(BINDIR)/scheduler.o $(BINDIR)/server.o $(BINDIR)/worker.o \ SOURCES= $(shell find $(SRCDIR) -maxdepth 1 -type f -name "*.c")
$(BINDIR)/centers.o $(BINDIR)/cmds.o $(BINDIR)/model.o \ BUILDBINS=$(patsubst %.c,$(BUILDDIR)/%.o,$(SOURCES))
$(BINDIR)/main.o $(BINDIR)/arrows.o $(BINDIR)/parsing.o BUILDDEPS=$(patsubst %.c,$(BUILDDIR)/%.d,$(SOURCES))
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))
.DEFAULT_GOAL:= all -include /etc/os-release
.PHONY: all clean deb tests install run run-cli run-tests dependencies
# ---- 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 ----------------------------------------------------- # $(BUILDDIR)/%.d: %.c | $(BUILDDIR)
TEST_SCHEDULER=$(BINDIR)/tests/centers @mkdir -p $(shell dirname $@)
TESTS=$(TEST_SCHEDULER) @$(CC) -MM -MT $(@:%.d=%.o) -MF $@ $<
@echo -e ${CL2}[$@] ${CL}dependencies generated.${CL3}
$(BINDIR)/tests/centers: $(SRCDIR)/tests/centers.c #
@echo "Compiling $<" # Compilation
@$(CC) $(CCINCLUDES) $(CCOPTS) $(CCFLAGS) $(LDFLAGS) -o $@ $< #
$(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 $(BUILDDIR)/%.o: %.c | $(BUILDDIR)
@echo "Compiling $<" @mkdir -p $(shell dirname $@)
@$(CC) $(CCINCLUDES) $(CCOPTS) $(CCFLAGS) -c -o $@ $< @$(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 $@" # Virtual recipes
@$(CC) -o $@ $(SERVEROBJ) $(LDFLAGS) #
@echo "Success!"
$(BINDIR)/gem-graph-ctl: $(CLIOBJ)
@echo "Building program to $@"
@$(CC) -o $@ $(CLIOBJ) $(LDFLAGS)
@echo "Success!"
# ---- Misc recipes ---------------------------------------------------------- #
clean: 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: install:
echo "Installing is not supported" echo "Installing is not supported"
# ---- Builder uses this target to run the application ----------------------- # build_system:
run: all @echo -e ${CL2}[$@] ${CL}building...${CL3}
@bin/gem-graph-server -C debian/etc -M debian/var/models \ @make $(BINDIR)/gem-graph-server -j $(NTHREADS)
-U debian/var/users @echo -e ${CL2}[$@] ${CL}done.${CL3}
run-gdb: all
run: build_system
@echo -e ${CL2}[$@] ${CL}executing...${CL3}
@gdb bin/gem-graph-server \ @gdb bin/gem-graph-server \
-ex "set args -C debian/etc -M debian/var/models -U debian/var/users" -ex "set args -C debian/etc -M debian/var/models -U debian/var/users"
@echo -e ${CL2}[$@] ${CL}done.${CL3}
run-valgrind: all debug: build_system
@echo -e ${CL2}[$@] ${CL}executing...${CL3}
@gdb $(BINDIR)/gem-graph-server
@echo -e ${CL2}[$@] ${CL}done.${CL3}
valgrind: build_system
@echo -e ${CL2}[$@] ${CL}executing...${CL3}
@valgrind --leak-check=full --show-leak-kinds=all -s \ @valgrind --leak-check=full --show-leak-kinds=all -s \
bin/gem-graph-server -C debian/etc -M debian/var/models \ bin/gem-graph-server -C debian/etc -M debian/var/models \
-U debian/var/users -U debian/var/users
@echo -e ${CL2}[$@] ${CL}done.${CL3}
run-both: all run-both: build_system
@echo -e ${CL2}[$@] ${CL}executing...${CL3}
@bin/gem-graph-server -C debian/etc -M debian/var/models \ @bin/gem-graph-server -C debian/etc -M debian/var/models \
-U debian/var/users & sleep 1 && bin/gem-graph-ctl -U debian/var/users & sleep 1 && bin/gem-graph-ctl
run-server: all @echo -e ${CL2}[$@] ${CL}done.${CL3}
run-server: build_system
@echo -e ${CL2}[$@] ${CL}executing...${CL3}
@bin/gem-graph-server -C debian/etc -M debian/var/models \ @bin/gem-graph-server -C debian/etc -M debian/var/models \
-U debian/var/users -U debian/var/users
run-cli: all @echo -e ${CL2}[$@] ${CL}done.${CL3}
@bin/gem-graph-ctl
run-tests: tests all: build_system
@bin/tests/scheduler @echo -e ${CL2}[$@] ${CL}done.${CL3}

View File

View File

1
debian/conffiles vendored
View File

@ -1 +0,0 @@
/etc/gem-graph-server/server.conf

10
debian/control vendored
View File

@ -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

13
debian/copyright vendored
View File

@ -1,13 +0,0 @@
This is a packacked upstream version of gem-graph-server
The sources may be found at <https://gitlab.os-k.eu/gem-graph-team/gem-graph-server>.
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 <https://www.gnu.org/licenses/#AGPL>.

View File

@ -1,2 +0,0 @@
# CECI EST UN FICHIER DE CONFIGURATION
server_enabled=true

View File

View File

@ -1,111 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<gem-graph-model version="0.2">
<!-- Model identity informations -->
<identity>
<name>Modèle de test</name>
<owner>Gaston Lagaffe</owner>
<owner_id>2</owner_id>
<date>1629830000</date>
<version>1.0</version>
<g_ref id="texte" date="1629830000" author="Jean" lang="en">Ref</g_ref>
</identity>
<!-- Model parameters -->
<parameters id="texte" date="0" author="Gaston Lagaffe">
<simulation>
<max_thread>0</max_thread>
<max_cycles>9</max_cycles>
</simulation>
<space-param>
<dimension>1</dimension>
<size x="29" y="0" z="0"/>
<site_multiplicity>3</site_multiplicity>
</space-param>
</parameters>
<!-- Model objects definition -->
<objects id="texte" date="1629830000" author="Jean Sirmai">
<object id="dimer 1-1" date="1629830000" author="Jean Sirmai">
<arrow x="0" y="0" z="0" site="1" weight="1"/>
<arrow x="1" y="0" z="0" site="2" weight="1"/>
</object>
<object id="dimer 2-2" date="1629830000" author="Jean Sirmai">
<arrow x="0" y="0" z="0" site="1" weight="2"/>
<arrow x="1" y="0" z="0" site="2" weight="2"/>
</object>
<object id="dimer 3-3" date="1629830000" author="Jean Sirmai">
<arrow x="0" y="0" z="0" site="1" weight="3"/>
<arrow x="1" y="0" z="0" site="2" weight="3"/>
</object>
</objects>
<!-- Saved space description (initial space is sequence 0) -->
<savestates id="texte" date="1629830000" author="THE MATRIX">
<space id="initial" date="1629830000" author="Jean Sirmai" sequence="0">
<arrow x="0" y="0" z="0" site="1" weight="1"/>
<arrow x="1" y="0" z="0" site="2" weight="1"/>
<arrow x="10" y="0" z="0" site="1" weight="1"/>
<arrow x="11" y="0" z="0" site="2" weight="1"/>
<arrow x="20" y="0" z="0" site="1" weight="1"/>
<arrow x="21" y="0" z="0" site="2" weight="1"/>
</space>
</savestates>
<!-- Model transitions definition (rules) -->
<!-- In this version : <xs:sequence minOccurs="0" maxOccurs="unbounded">-->
<!-- As long as there is no client, transitions have to be edited by hand
and should be written using a 'human-readable' format -->
<conditions id="random walk of dimers" date="1629822222" author="Jean">
<condition x= "0" y="0" z="0" site="1" weight="1" node_id="1" parent="0"/>
<condition x= "1" y="0" z="0" site="2" weight="1" node_id="2" parent="1"/>
<condition x= "2" y="0" z="0" site="1" weight="0" node_id="3" parent="2"/>
<condition x="-1" y="0" z="0" site="2" weight="0" node_id="4" parent="2"/>
</conditions>
<transitions>
<transition id="move_a_dimer_to_east" date="1629811111" author="Jean"
parent="3" probability="1">
<arrow x="0" y="0" z="0" site="1" weight="0"/>
<arrow x="1" y="0" z="0" site="2" weight="0"/>
<arrow x="1" y="0" z="0" site="1" weight="1"/>
<arrow x="2" y="0" z="0" site="2" weight="1"/>
</transition>
<transition id="move_a_dimer_to_west" date="1629811112" author="Jean"
parent="4" probability="1">
<arrow x= "0" y="0" z="0" site="1" weight="0"/>
<arrow x= "1" y="0" z="0" site="2" weight="0"/>
<arrow x= "0" y="0" z="0" site="2" weight="1"/>
<arrow x="-1" y="0" z="0" site="1" weight="1"/>
</transition>
</transitions>
</gem-graph-model>

View File

@ -1,199 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- https://www.freeformatter.com/xml-validator-xsd.html -->
<!-- https://www.w3schools.com/xml/el_attribute.asp -->
<xs:attribute name="id" type="xs:string"/>
<xs:attribute name="node_id" type="xs:integer"/>
<xs:attribute name="date" type="xs:integer"/>
<xs:attribute name="name" type="xs:string"/>
<xs:attribute name="lang" type="xs:string"/>
<xs:attribute name="owner" type="xs:string"/>
<xs:attribute name="owner_id" type="xs:integer"/>
<xs:attribute name="author" type="xs:string"/>
<xs:attribute name="version" type="xs:float"/>
<xs:attribute name="sequence" type="xs:integer"/>
<xs:attribute name="probability" type="xs:integer"/>
<xs:attribute name="parent" type="xs:integer"/>
<xs:attribute name="x" type="xs:integer" default="0"/>
<xs:attribute name="y" type="xs:integer" default="0"/>
<xs:attribute name="z" type="xs:integer" default="0"/>
<xs:attribute name="site" type="xs:integer" default="0"/>
<xs:attribute name="weight" type="xs:integer" default="0"/>
<xs:attributeGroup name="id_date_author">
<xs:attribute ref="id"/>
<xs:attribute ref="date"/>
<xs:attribute ref="author"/>
</xs:attributeGroup>
<xs:attributeGroup name="xyz_site_nb">
<xs:attribute ref="x"/>
<xs:attribute ref="y"/>
<xs:attribute ref="z"/>
<xs:attribute ref="site"/>
<xs:attribute ref="weight"/>
</xs:attributeGroup>
<xs:element name="g_ref">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attributeGroup ref="id_date_author"/>
<xs:attribute ref="lang" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="quote" substitutionGroup="g_ref"/>
<xs:element name="arrow">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" ref="quote"/>
</xs:sequence>
<xs:attributeGroup ref="xyz_site_nb"/>
</xs:complexType>
</xs:element>
<xs:element name="condition">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" ref="quote"/>
</xs:sequence>
<xs:attributeGroup ref="xyz_site_nb"/>
<xs:attribute ref="node_id" use="required"/>
<xs:attribute ref="parent" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="state">
<xs:complexType>
<xs:sequence minOccurs="1" maxOccurs="unbounded">
<xs:element ref="arrow"/>
</xs:sequence>
<xs:attributeGroup ref="id_date_author"/>
</xs:complexType>
</xs:element>
<xs:element name="object" substitutionGroup="state"/>
<xs:element name="space">
<xs:complexType>
<xs:sequence minOccurs="1" maxOccurs="unbounded">
<xs:element ref="arrow"/>
</xs:sequence>
<xs:attributeGroup ref="id_date_author"/>
<xs:attribute ref="sequence" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="transition">
<xs:complexType>
<xs:sequence minOccurs="1" maxOccurs="unbounded">
<xs:element minOccurs="0" ref="quote"/>
<xs:element ref="arrow"/>
</xs:sequence>
<xs:attributeGroup ref="id_date_author"/>
<xs:attribute ref="parent" use="required"/>
<xs:attribute ref="probability" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="gem-graph-model">
<xs:complexType>
<xs:sequence>
<xs:element name="identity">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="owner" type="xs:string"/>
<xs:element name="owner_id" type="xs:integer"/>
<xs:element name="date" type="xs:integer"/>
<xs:element name="version" type="xs:float"/>
<xs:element minOccurs="0" ref="g_ref"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="parameters">
<xs:complexType>
<xs:sequence>
<xs:element name="simulation">
<xs:complexType>
<xs:sequence>
<xs:element name="max_thread" type="xs:integer"/>
<xs:element name="max_cycles" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="space-param">
<xs:complexType>
<xs:sequence>
<xs:element name="dimension" type="xs:integer"/>
<xs:element name="size">
<xs:complexType>
<xs:attributeGroup ref="xyz_site_nb"/>
</xs:complexType>
</xs:element>
<xs:element name="site_multiplicity" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attributeGroup ref="id_date_author"/>
</xs:complexType>
</xs:element>
<xs:element name="objects">
<xs:complexType>
<xs:sequence minOccurs="1" maxOccurs="unbounded">
<xs:element ref="state"/>
</xs:sequence>
<xs:attributeGroup ref="id_date_author"/>
</xs:complexType>
</xs:element>
<xs:element name="savestates">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element ref="space"/>
</xs:sequence>
<xs:attributeGroup ref="id_date_author"/>
</xs:complexType>
</xs:element>
<xs:element name="conditions">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element minOccurs="0" ref="quote"/>
<xs:element ref="condition"/>
</xs:sequence>
<xs:attributeGroup ref="id_date_author"/>
</xs:complexType>
</xs:element>
<xs:element name="transitions">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element ref="transition"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute ref="version" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>

View File

View File

@ -27,33 +27,4 @@
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
static inline int ArrowsInitLock(ArrowArray_t *arrowArray)
{
return pthread_spin_init(&arrowArray->lock, PTHREAD_PROCESS_SHARED);
}
static inline int ArrowsDestroyLock(ArrowArray_t *arrowArray)
{
return pthread_spin_destroy(&arrowArray->lock);
}
static inline int ArrowsAcquireLock(ArrowArray_t *arrowArray)
{
return pthread_spin_lock(&arrowArray->lock);
}
static inline int ArrowsAcquireNonBlockingLock(ArrowArray_t *arrowArray)
{
return pthread_spin_trylock(&arrowArray->lock);
}
static inline int ArrowsReleaseLock(ArrowArray_t *arrowArray)
{
return pthread_spin_unlock(&arrowArray->lock);
}
Arrow_t *ArrowAdd(Scheduler_t *scheduler, int x, int y, int z, int siteId,
int weight);
bool ArrowRemove(Scheduler_t *scheduler, int x, int y, int z, int siteId,
int weight);

View File

@ -44,163 +44,4 @@
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
struct IntArray_t {
size_t size;
int *space;
} typedef IntArray_t;
struct Arrow_t {
int x;
int y;
int z;
int siteId;
int weight;
} typedef Arrow_t;
struct ArrowArray_t {
size_t size;
unsigned int freeSlotCount;
Arrow_t **freeSlots;
pthread_spinlock_t lock;
Arrow_t *array;
} typedef ArrowArray_t;
struct Site_t {
char *label;
int nArrow;
Arrow_t *arrowsPtr;
} typedef Site_t;
struct SpaceUnit_t {
int nSite;
char *label;
Site_t *sites;
} typedef SpaceUnit_t;
struct Space_t {
int xMax;
int yMax;
int zMax;
size_t size;
SpaceUnit_t *space;
} typedef Space_t;
/* -------------------------------------------------------------------------- */
//
// Scheduler
//
struct Scheduler_t {
pthread_t id;
bool pleaseStop;
Space_t *globalDrawingSpace;
IntArray_t *conditionTree;
ArrowArray_t *arrowArray;
int nMaxThread;
int nMaxCycles;
int ruleRadius;
} typedef Scheduler_t;
/* -------------------------------------------------------------------------- */
//
// Local threads
//
struct Center_t {
int x;
int y;
int z;
struct Center_t *next;
struct Center_t *prev;
} typedef Center_t;
struct Worker_t {
pthread_t id;
Center_t *localWorkAreaCenter;
Space_t *globalDrawingSpace;
IntArray_t *conditionTree; // XXX
ArrowArray_t *arrowArray;
bool pleaseStop;
bool terminated;
int returnValue;
} typedef Worker_t;
/* -------------------------------------------------------------------------- */
//
// Server
//
struct Server_t {
pthread_t id;
bool pleaseStop;
int returnValue;
int sockfd; // Socket file descriptor
} typedef Server_t;
struct ServerCommunication_t {
pthread_t id;
bool pleaseStop;
Server_t *associatedServer;
int sockfd; // Socket file descriptor
struct sockaddr_in clientAddr;
socklen_t sockLen;
} typedef ServerCommunication_t;
/* -------------------------------------------------------------------------- */
//
// Supervisor
//
struct Supervisor_t {
pthread_t id;
bool pleaseStop;
int *workerCreationOccurency; // XXX ???
} typedef Supervisor_t;
/* -------------------------------------------------------------------------- */
//
// Model
//
struct Model_t {
// Identity
int id;
bool validated; // true if model has been correctly parsed
char *name;
char *owner;
char *owner_id;
time_t date;
char *version;
char *filename;
// Parameters
int space_xMax; // TODO more than 3 dimension ?
int space_yMax;
int space_zMax;
int nmaxThread;
int nmaxCycles;
int siteNumber;
// Model instance
bool isRunning;
Scheduler_t *scheduler;
Supervisor_t *supervisor;
} typedef Model_t;
struct ModelField_t {
bool mandatory; // true if field must exist in file
char tag[25]; // XML tag attached to that field
char *destination; // data destination of that field
size_t size; // max size of data allowed
struct ModelField_t *son; // son in XML tree
struct ModelField_t *next; // brother in XML tree
} typedef ModelField_t;
/* -------------------------------------------------------------------------- */
//
// Parameters
//
struct Parameters_t {
char *configDir;
char *modelDir;
char *userDir;
} typedef Parameters_t;

View File

@ -27,12 +27,4 @@
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
// -------------------------------------------------------------------------- //
// Add a center to the center list //
// -------------------------------------------------------------------------- //
Center_t *CenterAdd(Center_t *anyCenter, Center_t *newCenter);
// -------------------------------------------------------------------------- //
// Remove a center from the center list //
// -------------------------------------------------------------------------- //
void CenterRemove(Center_t *oldCenter);

View File

@ -32,19 +32,26 @@
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
struct Command_t { struct command_t {
const char *name; const char *name;
char* (*execute)(char*, char**, Server_t*); char* (*execute)(char*, char**, Server_t*);
const char *help; const char *help;
} typedef Command_t; } typedef command_t;
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
// //
// Existing commands // Existing commands
// //
char *CmdModel(char*, char**, Server_t*); char *Cmd_model(char*, char**, Server_t*);
char *CmdShutdown(char*, char**, Server_t*); char *cmd_shutdown(char*, char**, Server_t*);
char *CmdHelp(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"},
};
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */

View File

@ -27,6 +27,10 @@
#include <dirent.h> #include <dirent.h>
#include <signal.h> #include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <libxml2/libxml/xmlmemory.h>
#include <libxml2/libxml/parser.h>
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
@ -47,44 +51,44 @@
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Model init function (and model discovery) // // Model init function (and model discovery) //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
void ModelSystemInit(Parameters_t *parameters); void model_system_init (parameters_t *parameters);
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Model stopping function // // Model stopping function //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
void ModelSystemDestroy(void); void model_system_shutdown (void);
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Load a model ready to execute // // Load a model ready to execute //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
int ModelLoad(int id); int model_load (int id);
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Unload a model // // Unload a model //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
int ModelUnload(int id); int model_unload (int id);
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Add a model to the known model list // // 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 // // Print informations about all models (known and loaded) to the client //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
void printModels(char *buf); void print_models (char *buf);
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Launch a model execution // // Launch a model execution //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
int ModelRun(int id); int model_run (int id);
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Stop a model execution // // Stop a model execution //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
int ModelStop(int id); int model_stop (int id);
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Stop and unload all loaded or running model // // Stop and unload all loaded or running model //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
void ModelShutdown(void); void model_shutdown (void);

View File

@ -36,38 +36,4 @@
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
struct ModelParserTableXML_t;
struct ParserTableXML_t
{
const xmlChar *tag;
int (*parse) (xmlDoc*,
struct ModelParserTableXML_t*,
int currentParser,
xmlNode*);
void *destination;
} typedef ParserTableXML_t;
struct ModelParserTableXML_t
{
size_t len;
ParserTableXML_t *table;
} typedef ModelParserTableXML_t;
/* -------------------------------------------------------------------------- */
int parseParentFieldXML(xmlDocPtr,
ModelParserTableXML_t*,
int,
xmlNodePtr);
int parseTextField(xmlDocPtr,
ModelParserTableXML_t*,
int,
xmlNodePtr);
int ParseModelXML(Model_t*);
int ParseModelIdentityXML(Model_t*, Parameters_t*);
/* -------------------------------------------------------------------------- */

View File

@ -32,14 +32,15 @@
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Scheduler init function // // Scheduler init function //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
void SchedInit(Scheduler_t *scheduler); void sched_init(scheduler_t *scheduler);
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Scheduler content destructor function // // Scheduler content destructor function //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
static inline void SchedContentDestroy(Scheduler_t *scheduler) static inline void sched_clean (scheduler_t *scheduler)
{ {
if (scheduler) { assert(scheduler);
if (scheduler->globalDrawingSpace) { if (scheduler->globalDrawingSpace) {
if (scheduler->globalDrawingSpace->space) { if (scheduler->globalDrawingSpace->space) {
free(scheduler->globalDrawingSpace->space); free(scheduler->globalDrawingSpace->space);
@ -48,6 +49,7 @@ static inline void SchedContentDestroy(Scheduler_t *scheduler)
free(scheduler->globalDrawingSpace); free(scheduler->globalDrawingSpace);
scheduler->globalDrawingSpace = NULL; scheduler->globalDrawingSpace = NULL;
} }
if (scheduler->arrowArray) { if (scheduler->arrowArray) {
if (scheduler->arrowArray->array) { if (scheduler->arrowArray->array) {
free(scheduler->arrowArray->array); free(scheduler->arrowArray->array);
@ -56,23 +58,14 @@ static inline void SchedContentDestroy(Scheduler_t *scheduler)
free(scheduler->arrowArray); free(scheduler->arrowArray);
scheduler->arrowArray = NULL; scheduler->arrowArray = NULL;
} }
}
} }
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Scheduler destructor function // // Scheduler destructor function //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
static inline void SchedDestroy(Scheduler_t *scheduler) static inline void sched_shutdown (scheduler_t *scheduler)
{ {
if (scheduler) { assert(scheduler);
free(scheduler); free(scheduler);
} scheduler = NULL;
}
// -------------------------------------------------------------------------- //
// Scheduler wait function //
// -------------------------------------------------------------------------- //
static inline void SchedWait(Scheduler_t *scheduler)
{
pthread_join(scheduler->id, NULL);
} }

View File

@ -37,12 +37,4 @@
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Server init function // // Server init function //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
void ServerInit(Server_t *server); void server_init (server_t *server);
// -------------------------------------------------------------------------- //
// Server wait function //
// -------------------------------------------------------------------------- //
static inline void ServerWait(Server_t *server)
{
pthread_join(server->id, NULL);
}

View File

@ -30,19 +30,12 @@
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Supervisor init function // // Supervisor init function //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
void SupervisorInit(Supervisor_t *supervisor); void superv_init (supervisor_t *supervisor);
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Supervisor destructor function // // Supervisor destructor function //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
static inline void SupervisorDestroy(Supervisor_t *supervisor) static inline void superv_destroy (supervisor_t *supervisor)
{ {
} ;
// -------------------------------------------------------------------------- //
// Supervisor wait function //
// -------------------------------------------------------------------------- //
static inline void SupervisorWait(Supervisor_t *supervisor)
{
pthread_join(supervisor->id, NULL);
} }

View File

@ -54,14 +54,14 @@
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
volatile struct winsize TermWindowSize; volatile struct winsize termWinSize;
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Get a character code from the keyboard // // Get a character code from the keyboard //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
static inline int TermGetch(bool nonBlocking) static inline int term_getch(bool nonBlocking)
{ {
int buf = 0; int buf = 0;
// old terminal // old terminal
@ -123,16 +123,16 @@ static inline int TermGetch(bool nonBlocking)
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Get the screen size // // Get the screen size //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
static inline void TermGetScreenSize(int signum) static inline void term_get_screensize(int signum)
{ {
// Get current terminal size // Get current terminal size
ioctl(STDOUT_FILENO, TIOCGWINSZ, &TermWindowSize); ioctl(STDOUT_FILENO, TIOCGWINSZ, &termWinSize);
} }
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Set cursor location // // 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); printf("\x1b[%d;%dH", y, x);
} }
@ -140,7 +140,7 @@ static inline void TermSetCursorLocation(char x, char y)
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Save cursor location // // Save cursor location //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
static inline void TermSaveCursorLocation(void) static inline void term_save_cursor_location(void)
{ {
printf(C_SAVE_CURSORPOS); printf(C_SAVE_CURSORPOS);
} }
@ -148,7 +148,7 @@ static inline void TermSaveCursorLocation(void)
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Restore cursor location // // Restore cursor location //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
static inline void TermRestoreCursorLocation(void) static inline void term_restore_cursor_location(void)
{ {
printf(C_RESTORE_CURSORPOS); printf(C_RESTORE_CURSORPOS);
} }
@ -156,7 +156,7 @@ static inline void TermRestoreCursorLocation(void)
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Move cursor location to the right // // Move cursor location to the right //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
static inline void TermMoveCursorForward(void) static inline void term_move_cursor_forward(void)
{ {
printf(C_CURSORRIGHT); printf(C_CURSORRIGHT);
} }
@ -164,7 +164,7 @@ static inline void TermMoveCursorForward(void)
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Move cursor location to the left // // Move cursor location to the left //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
static inline void TermMoveCursorBackward(void) static inline void term_move_cursor_backward(void)
{ {
printf(C_CURSORLEFT); printf(C_CURSORLEFT);
} }
@ -172,10 +172,10 @@ static inline void TermMoveCursorBackward(void)
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Clear screen // // Clear screen //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
static inline void TermClearScreen(void) static inline void term_clear_screen(void)
{ {
printf(C_CLEARSCREEN); printf(C_CLEARSCREEN);
TermSetCursorLocation(1,1); term_set_cursor_location(1,1);
fflush(stdout); fflush(stdout);
} }

View File

@ -30,12 +30,12 @@
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Worker init function // // Worker init function //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
void WorkerInit(Worker_t *worker); void worker_init(worker_t *worker);
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Worker destructor function // // Worker destructor function //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
static inline void WorkerDestroy(Worker_t *worker) static inline void worker_destroy(worker_t *worker)
{ {
worker->pleaseStop = false; worker->pleaseStop = false;
worker->terminated = false; worker->terminated = false;
@ -45,11 +45,3 @@ static inline void WorkerDestroy(Worker_t *worker)
free(worker->localWorkAreaCenter); free(worker->localWorkAreaCenter);
worker->localWorkAreaCenter = NULL; worker->localWorkAreaCenter = NULL;
} }
// -------------------------------------------------------------------------- //
// Worker wait function //
// -------------------------------------------------------------------------- //
static inline void WorkerWait(Worker_t *worker)
{
pthread_join(worker->id, NULL);
}

View File

@ -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 <https://www.gnu.org/licenses/>. //
//=-------------------------------------------------------------------------=//
#pragma once
#ifndef BASE_H
#include "../include/base.h"
#endif
/* -------------------------------------------------------------------------- */
#include <string.h>
#include <stdlib.h>
#include <libxml2/libxml/xmlmemory.h>
#include <libxml2/libxml/parser.h>

26
manifest.scm Normal file
View File

@ -0,0 +1,26 @@
;;
;; Dépendances sous GNU Guix
;;
(specifications->manifest
(list
"bash"
"coreutils"
"gcc-toolchain"
"pkg-config"
"valgrind"
"findutils"
"gdb"
"make"
"libxml2"
"glu"
"glew"
"glfw"
"cglm"
"libepoxy"
"glib"
"mesa-headers"
"mesa"
)
)

View File

@ -25,7 +25,3 @@
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
static inline int location(Space_t *space, int x, int y, int z) {
return x + y * (space->xMax+1) + z * (space->xMax+1) * (space->zMax+1);
}

View File

@ -24,31 +24,4 @@
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
Center_t *CenterAdd(Center_t *anyCenter, Center_t *newCenter)
{
if (!newCenter) return NULL;
if (anyCenter->next) {
anyCenter->next->prev = newCenter;
}
newCenter->next = anyCenter->next;
anyCenter->next = newCenter;
newCenter->prev = anyCenter;
return newCenter;
}
void CenterRemove(Center_t *oldCenter)
{
register Center_t *prev;
register Center_t *next;
//printLog("Removing center %p\n", oldCenter);
if (!oldCenter) return;
prev = oldCenter->prev;
next = oldCenter->next;
if (prev) prev->next = oldCenter->next;
if (next) next->prev = oldCenter->prev;
}

View File

@ -28,195 +28,195 @@
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
char *CmdModel(char *buf, char **argv, Server_t *args) /* char *cmd_model(char *buf, char **argv, server_t *args) */
{ /* { */
int id, eid; /* int id, eid; */
Model_t *newModel; /* model_t *newModel; */
// invalid use /* // invalid use */
if (!argv[1]) { /* if (!argv[1]) { */
strcat(buf, "{create | delete | load | unload | run | stop | list |" /* strcat(buf, "{create | delete | load | unload | run | stop | list |" */
"info}\n"); /* "info}\n"); */
goto CmdModelEnd; /* goto cmd_ModelEnd; */
} /* } */
if (strcmp(argv[1], "create") == 0) { /* if (strcmp(argv[1], "create") == 0) { */
if (!argv[2] || !argv[3]) { /* if (!argv[2] || !argv[3]) { */
goto createEnd; /* goto createEnd; */
} /* } */
if (strncmp(argv[2], "name=", 5) == 0) { /* if (strncmp(argv[2], "name=", 5) == 0) { */
if (strncmp(argv[3], "file=", 5) == 0) { /* if (strncmp(argv[3], "file=", 5) == 0) { */
// TODO get the file content (sent by the client) from args /* // TODO get the file content (sent by the client) from args */
// Creating model /* // Creating model */
ModelAddToKnown(&newModel); /* //XXX ModelAddToKnown(&newModel); */
// TODO modify model according to things in file /* // TODO modify model according to things in file */
// Write name /* // Write name */
strcpy(newModel->name, argv[2] + 5); /* strcpy(newModel->name, argv[2] + 5); */
// Write filename /* // Write filename */
strcpy(newModel->filename, argv[3] + 5); /* strcpy(newModel->filename, argv[3] + 5); */
sprintf(buf + strlen(buf), "Model %s created with id %d\n", /* sprintf(buf + strlen(buf), "Model %s created with id %d\n", */
newModel->name, newModel->id); /* newModel->name, newModel->id); */
goto CmdModelEnd; /* goto cmd_ModelEnd; */
} else { /* } else { */
goto createEnd; /* goto createEnd; */
} /* } */
} else { /* } else { */
goto createEnd; /* goto createEnd; */
} /* } */
createEnd: /* createEnd: */
// invalid use /* // invalid use */
strcat(buf, "Creates a model structure\n"); /* strcat(buf, "Creates a model structure\n"); */
strcat(buf, "Usage: model create name=NAME file=FILENAME\n"); /* strcat(buf, "Usage: model create name=NAME file=FILENAME\n"); */
goto CmdModelEnd; /* goto cmd_ModelEnd; */
} /* } */
else if (strcmp(argv[1], "load") == 0) { /* else if (strcmp(argv[1], "load") == 0) { */
if (!argv[2]) { /* if (!argv[2]) { */
goto loadEnd; /* 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) { /* if (id == 0 || (eid = ModelLoad(id)) <= 0) { */
sprintf(buf + strlen(buf), "Failed to load model id %d\n", id); /* sprintf(buf + strlen(buf), "Failed to load model id %d\n", id); */
goto CmdModelEnd; /* goto cmd_ModelEnd; */
} /* } */
sprintf(buf + strlen(buf), "Model id %d loaded with effective " /* sprintf(buf + strlen(buf), "Model id %d loaded with effective " */
"id %d\n", id, eid); /* "id %d\n", id, eid); */
goto CmdModelEnd; /* goto cmd_ModelEnd; */
loadEnd: /* loadEnd: */
// invalid use /* // invalid use */
strcat(buf, "Loads a model structure\n"); /* strcat(buf, "Loads a model structure\n"); */
strcat(buf, "Usage: model load id=ID\n"); /* strcat(buf, "Usage: model load id=ID\n"); */
goto CmdModelEnd; /* goto cmd_ModelEnd; */
} /* } */
else if (strcmp(argv[1], "unload") == 0) { /* else if (strcmp(argv[1], "unload") == 0) { */
if (!argv[2]) { /* if (!argv[2]) { */
goto unloadEnd; /* goto unloadEnd; */
} /* } */
id = (int) strtol(argv[2] + 3, NULL, 10); /* id = (int) strtol(argv[2] + 3, NULL, 10); */
if (id == 0 || ModelUnload(id) < 0) { /* if (id == 0 || ModelUnload(id) < 0) { */
printErr("Failed to unload model id %d\n", id); /* printErr("Failed to unload model id %d\n", id); */
sprintf(buf + strlen(buf), "Failed to unload model id %d\n", id); /* sprintf(buf + strlen(buf), "Failed to unload model id %d\n", id); */
goto CmdModelEnd; /* goto cmd_ModelEnd; */
} /* } */
sprintf(buf + strlen(buf), "Model id %d unloaded\n", id); /* sprintf(buf + strlen(buf), "Model id %d unloaded\n", id); */
goto CmdModelEnd; /* goto cmd_ModelEnd; */
unloadEnd: /* unloadEnd: */
// invalid use /* // invalid use */
strcat(buf, "Unloads a model structure\n"); /* strcat(buf, "Unloads a model structure\n"); */
strcat(buf, "Usage: model unload id=ID\n"); /* strcat(buf, "Usage: model unload id=ID\n"); */
goto CmdModelEnd; /* goto cmd_ModelEnd; */
} /* } */
else if (strcmp(argv[1], "delete") == 0) { /* else if (strcmp(argv[1], "delete") == 0) { */
if (!argv[2]) { /* if (!argv[2]) { */
goto deleteEnd; /* goto deleteEnd; */
} /* } */
// TODO delete model /* // TODO delete model */
deleteEnd: /* deleteEnd: */
// invalid use /* // invalid use */
strcat(buf, "Deletes a model structure\n"); /* strcat(buf, "Deletes a model structure\n"); */
strcat(buf, "Usage: model delete id=ID\n"); /* strcat(buf, "Usage: model delete id=ID\n"); */
goto CmdModelEnd; /* goto cmd_ModelEnd; */
} /* } */
else if (strcmp(argv[1], "run") == 0) { /* else if (strcmp(argv[1], "run") == 0) { */
if (!argv[2]) { /* if (!argv[2]) { */
goto runEnd; /* goto runEnd; */
} /* } */
id = (int) strtol(argv[2] + 3, NULL, 10); /* id = (int) strtol(argv[2] + 3, NULL, 10); */
if (id == 0 || (!ModelRun(id))) { /* if (id == 0 || (!ModelRun(id))) { */
sprintf(buf + strlen(buf), "Failed to run model id %d\n", id); /* sprintf(buf + strlen(buf), "Failed to run model id %d\n", id); */
goto CmdModelEnd; /* goto cmd_ModelEnd; */
} /* } */
sprintf(buf + strlen(buf), "Model id %d is running\n", id); /* sprintf(buf + strlen(buf), "Model id %d is running\n", id); */
goto CmdModelEnd; /* goto cmd_ModelEnd; */
runEnd: /* runEnd: */
// invalid use /* // invalid use */
strcat(buf, "Run a model simulation\n"); /* strcat(buf, "Run a model simulation\n"); */
strcat(buf, "Usage: model run id=ID\n"); /* strcat(buf, "Usage: model run id=ID\n"); */
goto CmdModelEnd; /* goto cmd_ModelEnd; */
} /* } */
else if (strcmp(argv[1], "stop") == 0) { /* else if (strcmp(argv[1], "stop") == 0) { */
if (!argv[2]) { /* if (!argv[2]) { */
goto stopEnd; /* goto stopEnd; */
} /* } */
id = (int) strtol(argv[2] + 3, NULL, 10); /* id = (int) strtol(argv[2] + 3, NULL, 10); */
if (id == 0 || (!ModelStop(id))) { /* if (id == 0 || (!ModelStop(id))) { */
sprintf(buf + strlen(buf), "Failed to stop model id %d\n", id); /* sprintf(buf + strlen(buf), "Failed to stop model id %d\n", id); */
goto CmdModelEnd; /* goto cmd_ModelEnd; */
} /* } */
sprintf(buf + strlen(buf), "Model id %d is running\n", id); /* sprintf(buf + strlen(buf), "Model id %d is running\n", id); */
goto CmdModelEnd; /* goto cmd_ModelEnd; */
stopEnd: /* stopEnd: */
// invalid use /* // invalid use */
strcat(buf, "Stop a model simulation\n"); /* strcat(buf, "Stop a model simulation\n"); */
strcat(buf, "Usage: model stop id=ID\n"); /* strcat(buf, "Usage: model stop id=ID\n"); */
goto CmdModelEnd; /* goto cmd_ModelEnd; */
} /* } */
else if (strcmp(argv[1], "list") == 0) { /* else if (strcmp(argv[1], "list") == 0) { */
strcat(buf, "You asked for us to list models\n"); /* strcat(buf, "You asked for us to list models\n"); */
printModels(buf); /* printModels(buf); */
} /* } */
else if (strcmp(argv[1], "info") == 0) { /* else if (strcmp(argv[1], "info") == 0) { */
if (!argv[2]) { /* if (!argv[2]) { */
goto infoEnd; /* goto infoEnd; */
} /* } */
// TODO info model /* // TODO info model */
infoEnd: /* infoEnd: */
// invalid use /* // invalid use */
strcat(buf, "Print info about a model\n"); /* strcat(buf, "Print info about a model\n"); */
strcat(buf, "Usage: model info id=ID\n"); /* strcat(buf, "Usage: model info id=ID\n"); */
goto CmdModelEnd; /* goto cmd_ModelEnd; */
} /* } */
// invalid use /* // invalid use */
else strcat(buf, "{create | delete | load | unload | run | stop | list |" /* else strcat(buf, "{create | delete | load | unload | run | stop | list |" */
"info}\n"); /* "info}\n"); */
CmdModelEnd: /* cmd_ModelEnd: */
return buf; /* return buf; */
} /* } */
char *CmdShutdown(char *buf, char **argv, Server_t *args) /* char *cmd_shutdown(char *buf, char **argv, server_t *args) */
{ /* { */
args->pleaseStop = true; /* args->pleaseStop = true; */
strcat(buf, "Server stopping\n"); /* strcat(buf, "Server stopping\n"); */
ModelShutdown(); /* //XXX ModelShutdown(); */
strcat(buf, "All model shutted down\n"); /* strcat(buf, "All model shutted down\n"); */
return buf; /* return buf; */
} /* } */
char *CmdHelp(char *buf, char **argv, Server_t *args) /* char *cmd_help(char *buf, char **argv, server_t *args) */
{ /* { */
strcat(buf, "List of known commands:\n"); /* strcat(buf, "List of known commands:\n"); */
return buf; /* return buf; */
} /* } */

View File

@ -26,23 +26,23 @@
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
static Server_t *server; static server_t *server;
static void sigtermHandler(int signum) /* static void sig_term_handler(int signum) */
{ /* { */
server->pleaseStop = true; /* server->pleaseStop = true; */
printLog("Server stopping\n"); /* printLog("Server stopping\n"); */
ModelShutdown(); /* //XXX ModelShutdown(); */
printLog("All model shutted down\n"); /* printLog("All model shutted down\n"); */
} /* } */
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int options; int options;
time_t t; time_t t;
int returnValue = 0; int returnValue = 0;
Parameters_t parameters; parameters_t parameters;
while ((options = getopt(argc, argv, ":C:M:U:")) != -1) { while ((options = getopt(argc, argv, ":C:M:U:")) != -1) {
switch (options) { switch (options) {
@ -108,30 +108,30 @@ int main(int argc, char **argv)
// Go! // Go!
printLog("Starting gem-graph-server...\n"); printLog("Starting gem-graph-server...\n");
// Register new interrupt handler /* // Register new interrupt handler */
signal(SIGINT, sigtermHandler); /* signal(SIGINT, sig_term_handler); */
signal(SIGTERM, sigtermHandler); /* signal(SIGTERM, sig_term_handler); */
// Initializing random generator /* // Initializing random generator */
t = time(&t); /* t = time(&t); */
srand((unsigned) t); /* srand((unsigned) t); */
server = calloc(1, sizeof(*server)); /* server = calloc(1, sizeof(*server)); */
// Initializing model system /* // Initializing model system */
ModelSystemInit(&parameters); /* model_system_init(&parameters); */
// Launching server /* // Launching server */
ServerInit(server); /* server_init(server); */
// Waiting for termination /* // Waiting for termination */
ServerWait(server); /* server_wait(server); */
// Exiting /* // Exiting */
returnValue |= server->returnValue; /* returnValue |= server->returnValue; */
ModelSystemDestroy(); /* ModelSystemDestroy(); */
free(server); /* free(server); */
server = NULL; /* server = NULL; */
free(parameters.userDir); free(parameters.userDir);
free(parameters.modelDir); free(parameters.modelDir);

View File

@ -25,286 +25,4 @@
#include "../include/arrows.h" #include "../include/arrows.h"
#include "../include/scheduler.h" #include "../include/scheduler.h"
static Model_t **loadedModel;
static int loadedModelSize; // begins to 1
static Model_t **knownModel;
static int knownModelSize; // begins to 1
Model_t *lastModel;
Model_t **lastModelAddr;
/* -------------------------------------------------------------------------- */
void printModels(char *buf)
{
sprintf(buf + strlen(buf),"Known models\n");
for (int i = 0; i <= knownModelSize-1; i++) {
sprintf(buf + strlen(buf), "id: %d, addr: %p, name: %s, date: %lu, "
"owner: %s\n",
knownModel[i]->id,
knownModel[i],
knownModel[i]->name,
knownModel[i]->date,
knownModel[i]->owner
);
}
sprintf(buf + strlen(buf), "\nLoaded models\n");
for (int i = 0; i <= loadedModelSize-1; i++) {
sprintf(buf + strlen(buf), "id: %d, addr: %p, name: %s\n",
loadedModel[i]->id, loadedModel[i], loadedModel[i]->name);
}
}
int ModelLoad(int id)
{
if (id <= 0 || id > knownModelSize) {
return -1;
}
printLog("Loading model id %d (/%d models)...\n", id, knownModelSize);
// Creating structure for the Scheduler
knownModel[id-1]->scheduler =
calloc(1, sizeof(*knownModel[id-1]->scheduler));
loadedModelSize++;
loadedModel =
(Model_t**) realloc(loadedModel, loadedModelSize * sizeof(Model_t*));
loadedModel[loadedModelSize-1] = knownModel[id-1];
// Parse model
ParseModelXML(loadedModel[loadedModelSize-1]);
return loadedModelSize;
}
int ModelUnload(int id)
{
// Destroy scheduler
if (id > loadedModelSize)
return -1;
SchedContentDestroy(loadedModel[id-1]->scheduler);
SchedDestroy(loadedModel[id-1]->scheduler);
loadedModel[id-1]->scheduler = NULL;
// Prevent fragmentation by moving data in the newly freed slot
if (id-1 < loadedModelSize) {
memmove(&loadedModel[id-1],
&loadedModel[id-1] + sizeof(Model_t*),
loadedModelSize - id);
}
// Decrement loaded model index
loadedModelSize--;
// Resize loaded model list
loadedModel =
(Model_t**) realloc(loadedModel, loadedModelSize * sizeof(Model_t*));
return 0;
}
/* -------------------------------------------------------------------------- */
int ModelRun(int id)
{
if (id <= 0 || id > loadedModelSize)
return -1;
if (!loadedModel[id-1]->scheduler)
return -2;
if (loadedModel[id-1]->isRunning)
return -3;
loadedModel[id-1]->scheduler->nMaxThread = knownModel[id-1]->nmaxThread;
loadedModel[id-1]->scheduler->nMaxCycles = knownModel[id-1]->nmaxCycles;
loadedModel[id-1]->scheduler->pleaseStop = false;
loadedModel[id-1]->isRunning = true;
SchedInit(loadedModel[id-1]->scheduler);
printLog("Model %d launched\n", id);
return 0;
}
int ModelStop(int id)
{
if (id <= 0 || id > loadedModelSize) {
return -1;
}
if (!loadedModel[id-1]->scheduler) {
return -2;
}
if (!loadedModel[id-1]->isRunning) {
return -3;
}
// Stop model scheduler
loadedModel[id-1]->scheduler->pleaseStop = true;
printLog("Model %d stop bit set\n", id);
// Wait for Shceduler to stop
SchedWait(loadedModel[id-1]->scheduler);
// Disable running bit
loadedModel[id-1]->isRunning = false;
return 0;
}
/* -------------------------------------------------------------------------- */
void ModelAddToKnown(Model_t **newModel)
{
// increment index
knownModelSize++;
// create socket
knownModel =
(Model_t**) realloc(knownModel, knownModelSize * sizeof(*knownModel));
// populate socket
knownModel[knownModelSize-1] = calloc(1, sizeof(**knownModel));
// populate model
knownModel[knownModelSize-1]->id = knownModelSize;
// return value
*newModel = knownModel[knownModelSize-1];
lastModel = knownModel[knownModelSize-1];
lastModelAddr = &knownModel[knownModelSize-1];
// continue. model population
knownModel[knownModelSize-1]->name =
calloc(1, sizeof(char) * MODEL_STRING_SIZE);
knownModel[knownModelSize-1]->filename =
calloc(1, sizeof(char) * MODEL_STRING_SIZE);
knownModel[knownModelSize-1]->owner =
calloc(1, sizeof(char) * MODEL_STRING_SIZE);
knownModel[knownModelSize-1]->version =
calloc(1, sizeof(char) * MODEL_STRING_SIZE);
knownModel[knownModelSize-1]->space_xMax = XMAX;
knownModel[knownModelSize-1]->space_yMax = YMAX;
knownModel[knownModelSize-1]->space_zMax = ZMAX;
knownModel[knownModelSize-1]->nmaxThread = MAX_THREAD;
knownModel[knownModelSize-1]->nmaxCycles = MAX_CYCLES;
knownModel[knownModelSize-1]->siteNumber = 0;
}
void ModelDelete(int id) //XXX fragmentation
{
// Free a model structure
free(knownModel[id-1]->name);
knownModel[id-1]->name = NULL;
free(knownModel[id-1]->filename);
knownModel[id-1]->filename = NULL;
free(knownModel[id-1]->owner);
knownModel[id-1]->owner = NULL;
free(knownModel[id-1]->version);
knownModel[id-1]->version = NULL;
free(knownModel[id-1]);
knownModel[id-1] = NULL;
}
void ModelShutdown(void)
{
// Stop each model from running
for (int i = 0; i < loadedModelSize; i++) {
ModelStop(i);
ModelUnload(i);
}
}
/* -------------------------------------------------------------------------- */
void ModelSystemInit(Parameters_t *parameters)
{
struct dirent *modelDirEntry = NULL;
DIR *modelDir = NULL;
Model_t *newModel;
char *extensionPosition;
loadedModel = calloc(1, sizeof(*loadedModel));
knownModel = calloc(1, sizeof(*knownModel));
knownModelSize = 0;
loadedModelSize = 0;
printLog("Model system initiated with folder : %s\n", parameters->modelDir);
// Open model directory
if ((modelDir = opendir(parameters->modelDir)) <= 0) {
printErr("Could not open %s\n", parameters->modelDir);
ModelSystemDestroy();
kill(getpid(), SIGTERM);
return;
}
while ((modelDirEntry = readdir(modelDir)) != NULL) {
if ((extensionPosition = strstr(modelDirEntry->d_name, ".xml"))) {
// Creating model
ModelAddToKnown(&newModel);
// Write file path in filename
strncpy(newModel->filename, parameters->modelDir,
strlen(parameters->modelDir));
// Add a / separator
strcat(newModel->filename
+ strlen(parameters->modelDir),
"/");
// Add the file relative name
strncpy(newModel->filename
+ strlen(parameters->modelDir)
+ 1,
modelDirEntry->d_name,
strlen(modelDirEntry->d_name));
// Write model name
strncpy(newModel->name, modelDirEntry->d_name,
extensionPosition - modelDirEntry->d_name);
// Ask to parse the new model
if (ParseModelIdentityXML(newModel, parameters) != 0) {
printErr("Deleting invalid model %s from known list\n",
newModel->name);
ModelDelete(newModel->id);
continue;
};
// Check model is valid and/or parsed
if (newModel->validated == false) {
printErr("Deleting invalid model %s from known list\n",
newModel->name);
ModelDelete(newModel->id);
continue;
}
// Succeeded !
printLog("Loaded model %s\n", newModel->name);
}
}
free(modelDir);
}
void ModelSystemDestroy(void)
{
for (int i = 0; i < loadedModelSize; i++) {
ModelDelete(i);
}
free(loadedModel);
loadedModel = NULL;
free(knownModel);
knownModel = NULL;
}

View File

@ -24,560 +24,4 @@
#include "../include/arrows.h" #include "../include/arrows.h"
#include "../include/model.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 <gem-graph-model> !\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 <gem-graph-model> !\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;
}

View File

@ -27,202 +27,4 @@
#include <sys/sysinfo.h> #include <sys/sysinfo.h>
/* -------------------------------------------------------------------------- */
static void *schedulerMain(void *scheduler);
/* -------------------------------------------------------------------------- */
// -------------------------------------------------------------------------- //
// Scheduler init function //
// -------------------------------------------------------------------------- //
void SchedInit(Scheduler_t *scheduler)
{
pthread_create(&scheduler->id, NULL, schedulerMain, scheduler);
}
/* -------------------------------------------------------------------------- */
// -------------------------------------------------------------------------- //
// Scheduler area finder function //
// -------------------------------------------------------------------------- //
static Center_t *findWorkArea(Center_t *centersList, Arrow_t *electedArrow,
int ruleRadius, int xmax, int ymax, int zmax)
{
register Center_t *currentCenter, *newCenter;
currentCenter = centersList->next;
newCenter = malloc(sizeof(Center_t));
while (currentCenter){
if ( (xmax && (abs(electedArrow->x - currentCenter->x) <= ruleRadius))
|| (ymax && (abs(electedArrow->y - currentCenter->y) <= ruleRadius))
|| (zmax && (abs(electedArrow->z - currentCenter->z) <= ruleRadius))
){
free(newCenter);
newCenter = NULL;
//printLog("Can't find a free area\n");
return NULL;
}
currentCenter = currentCenter->next;
}
newCenter->x = electedArrow->x;
newCenter->y = electedArrow->y;
newCenter->z = electedArrow->z;
return newCenter;
}
/* -------------------------------------------------------------------------- */
// -------------------------------------------------------------------------- //
// Scheduler thread main function //
// -------------------------------------------------------------------------- //
static void *schedulerMain(void *scheduler)
{
Scheduler_t *args;
Worker_t *workerArray;
// A center is a structure that defines, with a radius, a cubic preempted
// area for a worker to operate on (as an approximation of a spheric one)
Center_t *centersList, *workArea;
Arrow_t *electedArrow;
int ncpu, nworker, err;
// Getting scheduler argument structure
args = (Scheduler_t*) scheduler;
printLog("Scheduler #%lu online\n", args->id);
if (!args->nMaxThread) { // nmaxthread = 0 => no minimum
ncpu = get_nprocs(); // allocating all the cpus available
} else { // n thread = min(cpu, nmaxthread)
ncpu = MIN(get_nprocs(), args->nMaxThread);
}
printLog("%d threads available.\n", ncpu);
// Data structures
workerArray = calloc(ncpu, sizeof(*workerArray));
nworker = 0;
centersList = calloc(1, sizeof(*centersList));
// Initiate the arrowArray lock
if (err = ArrowsInitLock(args->arrowArray), err != 0) {
printErr("Impossible to create the arrow array lock (error %d)\n", err);
return NULL;
}
//
// MAIN LOOP
//
while (!args->pleaseStop && 0 <= args->nMaxCycles) { //XXX count cycles
//printLog("Scheduler #%lu online: cycle %d\n", *args->id, ncycles);
//
// TODO statistics here
// Create a new thread
if (nworker < ncpu) {
workArea = NULL;
// Acquiring lock to consistently read the arrowArray
if (ArrowsAcquireNonBlockingLock(args->arrowArray)) {
// Random choice of an arrow
electedArrow =
&args->arrowArray->array[rand() % args->arrowArray->size]; // XXX
// Find a local area
workArea = findWorkArea(centersList,
electedArrow,
args->ruleRadius,
args->globalDrawingSpace->xMax,
args->globalDrawingSpace->yMax,
args->globalDrawingSpace->zMax
);
ArrowsReleaseLock(args->arrowArray);
}
// If a free area exists,
if (workArea) {
// preempt it,
CenterAdd(centersList, workArea);
// find a worker socket,
for (int i = 0; i < ncpu; i++) {
// if a location is empty
if (workerArray[i].id == 0) {
// prepare the worker for the area,
workerArray[i].localWorkAreaCenter = workArea;
workerArray[i].globalDrawingSpace =
args->globalDrawingSpace;
workerArray[i].conditionTree =
args->conditionTree;
workerArray[i].arrowArray =
args->arrowArray;
// create the worker,
WorkerInit(&workerArray[i]);
// and increment worker count.
nworker++;
printLog("Added worker at rank %d with center %p, now %d worker(s)\n",
i,
workArea,
nworker
);
// Increment partial cycle TODO
break;
}
}
}
}
// Delete finished workers
for (int i = 0; i < ncpu; i++) {
if (workerArray[i].id) {
//printLog("Checking termination of worker #%lu\n", *workerArray[i].id);
// Check worker termination
if (workerArray[i].terminated) {
// Join the thread to act his termination
WorkerWait(&workerArray[i]);
// Remove preemption on space
CenterRemove(workerArray[i].localWorkAreaCenter);
printLog("Worker #%lu terminated with return %d. Cleaning...\n",
workerArray[i].id,
workerArray[i].returnValue
);
// Remove Worker
WorkerDestroy(&workerArray[i]);
nworker--;
}
}
}
}
// Exiting scheduler properly
printLog("Stopping scheduler... (waiting for workers)\n");
// Waiting for remaining workers
for (int i = 0; i < ncpu; i++) {
if (workerArray[i].id) {
printLog("Waiting for termination of worker #%lu\n", workerArray[i].id);
// Join the thread to wait for his termination
WorkerWait(&workerArray[i]);
// Remove preemption on space
CenterRemove(workerArray[i].localWorkAreaCenter);
printLog("Worker #%lu terminated with return %d. Cleaning...\n",
workerArray[i].id,
workerArray[i].returnValue
);
// Remove Worker
WorkerDestroy(&workerArray[i]);
}
}
printLog("Scheduler #%lu offline\n", args->id);
ArrowsDestroyLock(args->arrowArray);
free(workerArray);
workerArray = NULL;
free(centersList);
centersList = NULL;
return NULL;
}

View File

@ -25,37 +25,22 @@
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
static Command_t cmdList[] = static void *server_main(void *server);
{
{"help", CmdHelp, "Help command"},
{"model", CmdModel, "Model command"},
{"shutdown", CmdShutdown, "Shutdown command"},
};
static void *serverMain(void *server);
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
// -------------------------------------------------------------------------- //
// Server init function //
// -------------------------------------------------------------------------- //
void ServerInit(Server_t *server)
{
pthread_create(&server->id, NULL, serverMain, server);
}
#define SEND_BUFFER_SIZE 80 * 24 #define SEND_BUFFER_SIZE 80 * 24
#define RECEIVE_BUFFER_SIZE 80 #define RECEIVE_BUFFER_SIZE 80
void *serverCommunicationInstance(void *serverCom) void *server_communication_instance(void *serverCom)
{ {
ServerCommunication_t *args; servercom_t *args;
char **argv = NULL; char **argv = NULL;
char receiveBuff[RECEIVE_BUFFER_SIZE]; char receiveBuff[RECEIVE_BUFFER_SIZE];
char sendBuff[SEND_BUFFER_SIZE]; char sendBuff[SEND_BUFFER_SIZE];
int tokenIndex, bytesReceived, clientPort; int tokenIndex, bytesReceived, clientPort;
char clientIP[16]; char clientIP[16];
args = (ServerCommunication_t*) serverCom; args = (servercom_t*) serverCom;
// Get ip address from client // Get ip address from client
inet_ntop(AF_INET, inet_ntop(AF_INET,
@ -132,7 +117,7 @@ void *serverCommunicationInstance(void *serverCom)
#define PORT 9000 #define PORT 9000
#define MAX_CONNECTION 100 #define MAX_CONNECTION 100
static void *serverMain(void *server) static void *server_main(void *server)
{ {
Server_t *args; Server_t *args;
ServerCommunication_t serverSlots[MAX_CONNECTION] = {0}; ServerCommunication_t serverSlots[MAX_CONNECTION] = {0};
@ -235,7 +220,7 @@ static void *serverMain(void *server)
// Create thread // Create thread
threadStatus = pthread_create(&serverSlots[serverSlotIndex].id, threadStatus = pthread_create(&serverSlots[serverSlotIndex].id,
NULL, NULL,
serverCommunicationInstance, server_communication_instance,
(void*)&serverSlots[serverSlotIndex]); (void*)&serverSlots[serverSlotIndex]);
if(threadStatus != 0) { if(threadStatus != 0) {
printErr("Error from pthread: %d (%s)\n", printErr("Error from pthread: %d (%s)\n",

View File

@ -24,22 +24,4 @@
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
static void *supervisorMain(void *supervisor);
/* -------------------------------------------------------------------------- */
// -------------------------------------------------------------------------- //
// Supervisor init function //
// -------------------------------------------------------------------------- //
void SupervisorInit(Supervisor_t *supervisor)
{
pthread_create(&supervisor->id, NULL, supervisorMain, supervisor);
}
// -------------------------------------------------------------------------- //
// Supervisor thread main function //
// -------------------------------------------------------------------------- //
static void *supervisorMain(void *supervisor)
{
return NULL;
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -24,40 +24,4 @@
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
static void *WorkerMain(void *worker);
/* -------------------------------------------------------------------------- */
// -------------------------------------------------------------------------- //
// Worker init function //
// -------------------------------------------------------------------------- //
void WorkerInit(Worker_t *worker)
{
if (pthread_create(&worker->id, NULL, WorkerMain, worker)) {
printLog("Worker #%lu can't be initialized!\n", worker->id);
return;
}
}
// -------------------------------------------------------------------------- //
// Worker thread main function //
// -------------------------------------------------------------------------- //
static void *WorkerMain(void *worker)
{
Worker_t *args;
int a = rand()%__INT_MAX__;
args = (Worker_t*) worker;
printLog("Worker #%lu online\n", args->id);
int size = args->globalDrawingSpace->size;
// TODO execute rule comparison
args->returnValue = a;
args->terminated = true;
printLog("Worker #%lu offline\n", args->id);
return NULL;
}