2024-09-10 12:58:18 +02:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
|
|
* Gem-graph client *
|
|
|
|
* Finite State Machine - Journal *
|
|
|
|
* *
|
|
|
|
* Copyright © 2024 Libre en Communs <contact@a-lec.org> *
|
|
|
|
* Copyright © 2024 Jean Sirmai <jean@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 <http://www.gnu.org/licenses/>. *
|
|
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
2024-08-29 08:21:22 +02:00
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2024-09-11 09:05:10 +02:00
|
|
|
#include <time.h>
|
|
|
|
#include <sys/time.h>
|
2024-08-29 08:21:22 +02:00
|
|
|
#include "../../include/fsm.h"
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2024-09-10 22:37:31 +02:00
|
|
|
/* A journal stores chronologically the fsm events */
|
|
|
|
/* during a session run (rules exec, mainly) */
|
2024-08-29 08:21:22 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
2024-09-11 10:23:07 +02:00
|
|
|
void fsm_get_time (char *message)
|
2024-09-11 09:05:10 +02:00
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
2024-09-11 10:23:07 +02:00
|
|
|
printf(" Seconds since Jan. 1, 1970: %ld %s\n", tv.tv_sec, message);
|
|
|
|
/*
|
2024-09-11 09:05:10 +02:00
|
|
|
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
|
2024-09-11 10:23:07 +02:00
|
|
|
printf(" Current timestamp: %ld %s\n", current_time, message);
|
2024-09-11 09:05:10 +02:00
|
|
|
printf(" Current time: %s", ctime(¤t_time));
|
2024-09-11 10:23:07 +02:00
|
|
|
*/
|
2024-09-11 09:05:10 +02:00
|
|
|
}
|
2024-08-29 08:21:22 +02:00
|
|
|
|
2024-09-11 10:23:07 +02:00
|
|
|
void fsm_journal_init (journal *jj, char *message)
|
2024-08-29 08:21:22 +02:00
|
|
|
{
|
2024-09-11 09:05:10 +02:00
|
|
|
jj->first = NULL;
|
|
|
|
jj->last = NULL;
|
2024-09-11 10:23:07 +02:00
|
|
|
fsm_get_time (message);
|
2024-08-29 08:21:22 +02:00
|
|
|
}
|
|
|
|
|
2024-09-11 10:23:07 +02:00
|
|
|
void fsm_journal_clear (journal *jj, char *message)
|
2024-08-29 08:21:22 +02:00
|
|
|
{
|
2024-09-10 22:37:31 +02:00
|
|
|
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;
|
2024-08-29 08:21:22 +02:00
|
|
|
}
|
|
|
|
|
2024-09-11 10:23:07 +02:00
|
|
|
int fsm_journal_push_back (journal *jj, int event, char *message)
|
2024-08-29 08:21:22 +02:00
|
|
|
{
|
2024-09-10 22:37:31 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-09-11 10:23:07 +02:00
|
|
|
int fsm_journal_push_front (journal *jj, int event, char *message)
|
2024-09-10 22:37:31 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-09-11 10:23:07 +02:00
|
|
|
int fsm_journal_pop_back (journal *jj, char *message)
|
2024-09-10 22:37:31 +02:00
|
|
|
{
|
|
|
|
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.
|
|
|
|
}
|
|
|
|
|
2024-09-11 10:23:07 +02:00
|
|
|
int fsm_journal_pop_front (journal *jj, char *message)
|
2024-09-10 22:37:31 +02:00
|
|
|
{
|
|
|
|
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.
|
2024-08-29 08:21:22 +02:00
|
|
|
}
|
|
|
|
|
2024-09-11 10:23:07 +02:00
|
|
|
int fsm_journal_length (journal jj, char *message)
|
2024-08-29 08:21:22 +02:00
|
|
|
{
|
2024-09-10 22:37:31 +02:00
|
|
|
unit *a_unit = jj.first;
|
|
|
|
int nb = 0;
|
|
|
|
while (a_unit)
|
2024-08-29 08:21:22 +02:00
|
|
|
{
|
2024-09-10 22:37:31 +02:00
|
|
|
nb ++;
|
|
|
|
a_unit = a_unit->next;
|
2024-08-29 08:21:22 +02:00
|
|
|
}
|
2024-09-11 10:23:07 +02:00
|
|
|
printf (" fsm_journal_length (n = %d) %s\n", nb, "");
|
|
|
|
fsm_get_time (message);
|
2024-09-10 22:37:31 +02:00
|
|
|
return nb;
|
|
|
|
}
|
|
|
|
|
2024-09-11 10:23:07 +02:00
|
|
|
void fsm_journal_seek (journal jj, int event, char *message)
|
2024-09-10 22:37:31 +02:00
|
|
|
{
|
2024-09-11 08:13:00 +02:00
|
|
|
unit *a_unit = jj.first;
|
2024-09-11 09:05:10 +02:00
|
|
|
int nb = 0;
|
2024-09-11 08:13:00 +02:00
|
|
|
while (a_unit)
|
|
|
|
{
|
2024-09-11 09:05:10 +02:00
|
|
|
if (event == a_unit->event) nb++;
|
2024-09-11 08:13:00 +02:00
|
|
|
a_unit = a_unit->next;
|
|
|
|
}
|
2024-09-11 09:05:10 +02:00
|
|
|
if (nb > 0) printf ("> event %d found %d times in journal\n", event, nb);
|
|
|
|
else printf ("> event %d not found in journal\n", event);
|
2024-09-11 10:23:07 +02:00
|
|
|
fsm_get_time (message);
|
2024-08-29 08:21:22 +02:00
|
|
|
}
|
|
|
|
|
2024-09-11 10:23:07 +02:00
|
|
|
void fsm_journal_view (journal jj, char *message)
|
2024-08-29 08:21:22 +02:00
|
|
|
{
|
2024-09-11 10:23:07 +02:00
|
|
|
printf ("view journal (n = %d)\n", fsm_journal_length (jj, message));
|
2024-09-10 22:37:31 +02:00
|
|
|
unit *a_unit = jj.first;
|
|
|
|
while (a_unit)
|
2024-08-29 08:21:22 +02:00
|
|
|
{
|
2024-09-10 22:37:31 +02:00
|
|
|
printf ("> %d <\n", a_unit->event);
|
|
|
|
a_unit = a_unit->next;
|
2024-08-29 08:21:22 +02:00
|
|
|
}
|
|
|
|
puts ("------");
|
|
|
|
}
|
|
|
|
|
2024-09-11 10:23:07 +02:00
|
|
|
void fsm_journal_test(char *message) // call: widget/dispatch
|
2024-08-29 08:21:22 +02:00
|
|
|
{
|
2024-09-10 22:37:31 +02:00
|
|
|
journal exemple;
|
|
|
|
|
2024-09-11 10:23:07 +02:00
|
|
|
fsm_journal_init (&exemple, message);
|
2024-09-10 22:37:31 +02:00
|
|
|
|
2024-09-11 10:23:07 +02:00
|
|
|
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, ""));
|
2024-09-10 22:37:31 +02:00
|
|
|
|
2024-09-11 10:23:07 +02:00
|
|
|
fsm_journal_view (exemple, "");
|
|
|
|
fsm_journal_seek (exemple, 2, "");
|
2024-08-29 08:21:22 +02:00
|
|
|
|
2024-09-11 10:23:07 +02:00
|
|
|
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, ""));
|
2024-09-10 22:37:31 +02:00
|
|
|
|
2024-09-11 10:23:07 +02:00
|
|
|
fsm_journal_view (exemple, "");
|
2024-09-10 22:37:31 +02:00
|
|
|
|
2024-09-11 10:23:07 +02:00
|
|
|
printf ("push back > %d\n", fsm_journal_push_back (&exemple, 3, ""));
|
|
|
|
printf ("push front > %d\n", fsm_journal_push_front (&exemple, 0, ""));
|
2024-09-10 22:37:31 +02:00
|
|
|
|
2024-09-11 10:23:07 +02:00
|
|
|
fsm_journal_view (exemple, "");
|
|
|
|
fsm_journal_seek (exemple, 2, "");
|
2024-09-10 22:37:31 +02:00
|
|
|
|
2024-09-11 10:23:07 +02:00
|
|
|
printf ("clear list\n"); fsm_journal_clear (&exemple, "");
|
2024-09-10 22:37:31 +02:00
|
|
|
|
2024-09-11 10:23:07 +02:00
|
|
|
fsm_journal_view (exemple, message);
|
2024-08-29 08:21:22 +02:00
|
|
|
}
|
2024-09-10 22:37:31 +02:00
|
|
|
|