/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Gem-graph client * * Finite State Machine - Journal * * * * Copyright © 2024 Libre en Communs * * Copyright © 2024 Jean Sirmai * * * * 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 #include #include #include #include "../../include/fsm.h" /******************************************************************************/ /* A journal stores chronologically the fsm events */ /* during a session run (rules exec, mainly) */ /******************************************************************************/ void fsm_get_time (char *message) { struct timeval tv; gettimeofday(&tv, NULL); printf(" Seconds since Jan. 1, 1970: %ld %s\n", tv.tv_sec, message); /* time_t current_time = time(NULL); // Check if the time retrieval was successful if (current_time == ((time_t)-1)) printf("Error getting current time.\n"); // Convert to local time format and print printf(" Current timestamp: %ld %s\n", current_time, message); printf(" Current time: %s", ctime(¤t_time)); */ } void fsm_journal_init (journal *jj, char *message) { jj->first = NULL; jj->last = NULL; fsm_get_time (message); } void fsm_journal_clear (journal *jj, char *message) { unit *tmp; unit *a_unit = jj->first; while(a_unit) { tmp = a_unit; a_unit = a_unit->next; free(tmp); } jj->first = NULL; jj->last = NULL; } int fsm_journal_push_back (journal *jj, int event, char *message) { unit *new_unit = malloc (sizeof(unit)); if (! new_unit) exit (EXIT_FAILURE); new_unit->event = event; new_unit->prev = jj->last; new_unit->next = NULL; if (jj->last) jj->last->next = new_unit; else jj->first = new_unit; jj->last = new_unit; return event; } int fsm_journal_push_front (journal *jj, int event, char *message) { unit *new_unit = malloc(sizeof(unit)); if (! new_unit) exit (EXIT_FAILURE); new_unit->event = event; new_unit->next = jj->first; new_unit->prev = NULL; if (jj->first) jj->first->prev = new_unit; else jj->last = new_unit; jj->first = new_unit; return event; } int fsm_journal_pop_back (journal *jj, char *message) { int event; unit *tmp = jj->last; if (! tmp) return -1; event = tmp->event; jj->last = tmp->prev; if (jj->last) jj->last->next = NULL; else jj->first = NULL; free (tmp); return event; // retourne l'évènement retiré du journal. } int fsm_journal_pop_front (journal *jj, char *message) { int event; unit *tmp = jj->first; if (! tmp) return -1; event = tmp->event; jj->first = tmp->next; if (jj->first) jj->first->prev = NULL; else jj->last = NULL; free (tmp); return event; // retourne l'évènement retiré du journal. } int fsm_journal_length (journal jj, char *message) { unit *a_unit = jj.first; int nb = 0; while (a_unit) { nb ++; a_unit = a_unit->next; } printf (" fsm_journal_length (n = %d) %s\n", nb, ""); fsm_get_time (message); return nb; } void fsm_journal_seek (journal jj, int event, char *message) { unit *a_unit = jj.first; int nb = 0; while (a_unit) { if (event == a_unit->event) nb++; a_unit = a_unit->next; } if (nb > 0) printf ("> event %d found %d times in journal\n", event, nb); else printf ("> event %d not found in journal\n", event); fsm_get_time (message); } void fsm_journal_view (journal jj, char *message) { printf ("view journal (n = %d)\n", fsm_journal_length (jj, message)); unit *a_unit = jj.first; while (a_unit) { printf ("> %d <\n", a_unit->event); a_unit = a_unit->next; } puts ("------"); } void fsm_journal_test(char *message) // call: widget/dispatch { journal exemple; fsm_journal_init (&exemple, message); printf ("push front > %d\n", fsm_journal_push_front (&exemple, 10, "")); printf ("push back > %d\n", fsm_journal_push_back (&exemple, 100, "")); printf ("push back > %d\n", fsm_journal_push_back (&exemple, 1000, "")); printf ("push front > %d\n", fsm_journal_push_front (&exemple, 1, "")); fsm_journal_view (exemple, ""); fsm_journal_seek (exemple, 2, ""); printf ("pop back > %d\n", fsm_journal_pop_back (&exemple, "")); printf ("pop back > %d\n", fsm_journal_pop_back (&exemple, "")); printf ("pop back > %d\n", fsm_journal_pop_back (&exemple, "")); printf ("push back > %d\n", fsm_journal_push_back (&exemple, 2, "")); fsm_journal_view (exemple, ""); printf ("push back > %d\n", fsm_journal_push_back (&exemple, 3, "")); printf ("push front > %d\n", fsm_journal_push_front (&exemple, 0, "")); fsm_journal_view (exemple, ""); fsm_journal_seek (exemple, 2, ""); printf ("clear list\n"); fsm_journal_clear (&exemple, ""); fsm_journal_view (exemple, message); }