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>
|
2024-09-14 23:03:19 +02:00
|
|
|
|
#include <string.h>
|
2024-09-11 09:05:10 +02:00
|
|
|
|
#include <sys/time.h>
|
2024-08-29 08:21:22 +02:00
|
|
|
|
#include "../../include/fsm.h"
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2024-09-14 23:03:19 +02:00
|
|
|
|
/* A journal stores chronologically the fsm events */
|
2024-09-10 22:37:31 +02:00
|
|
|
|
/* during a session run (rules exec, mainly) */
|
2024-08-29 08:21:22 +02:00
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
2024-09-11 10:23:07 +02:00
|
|
|
|
/*
|
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 23:57:13 +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-08-29 08:21:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:57:13 +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 23:57:13 +02:00
|
|
|
|
void fsm_journal_push_front (journal *jj, char *message)
|
2024-09-10 22:37:31 +02:00
|
|
|
|
{
|
2024-09-11 23:57:13 +02:00
|
|
|
|
struct timeval tv;
|
|
|
|
|
gettimeofday(&tv, NULL);
|
2024-09-10 22:37:31 +02:00
|
|
|
|
unit *new_unit = malloc(sizeof(unit));
|
|
|
|
|
if (! new_unit) exit (EXIT_FAILURE);
|
2024-09-11 23:57:13 +02:00
|
|
|
|
new_unit->yy_dd_mm = tv.tv_sec;
|
|
|
|
|
new_unit->usec = tv.tv_usec;
|
|
|
|
|
new_unit->message = message;
|
2024-09-10 22:37:31 +02:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:57:13 +02:00
|
|
|
|
long fsm_journal_pop_back (journal *jj, char *message)
|
2024-09-10 22:37:31 +02:00
|
|
|
|
{
|
2024-09-11 23:57:13 +02:00
|
|
|
|
long usec;
|
2024-09-10 22:37:31 +02:00
|
|
|
|
unit *tmp = jj->last;
|
|
|
|
|
if (! tmp) return -1;
|
2024-09-11 23:57:13 +02:00
|
|
|
|
usec = tmp->usec;
|
2024-09-10 22:37:31 +02:00
|
|
|
|
jj->last = tmp->prev;
|
|
|
|
|
if (jj->last) jj->last->next = NULL;
|
|
|
|
|
else jj->first = NULL;
|
|
|
|
|
free (tmp);
|
2024-09-11 23:57:13 +02:00
|
|
|
|
return usec; // retourne l'évènement retiré du journal.
|
2024-08-29 08:21:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:57:13 +02:00
|
|
|
|
int fsm_journal_length (journal jj)
|
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 23:57:13 +02:00
|
|
|
|
// printf ("fsm journal length = %d\n", nb);
|
2024-09-10 22:37:31 +02:00
|
|
|
|
return nb;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 23:57:13 +02:00
|
|
|
|
void fsm_journal_seek (journal jj, long usec, 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 23:57:13 +02:00
|
|
|
|
if (usec == a_unit->usec) nb++;
|
2024-09-11 08:13:00 +02:00
|
|
|
|
a_unit = a_unit->next;
|
|
|
|
|
}
|
2024-09-11 23:57:13 +02:00
|
|
|
|
if (nb > 0) printf ("> date (usec) %ld found %d times in journal\n", usec, nb);
|
|
|
|
|
else printf ("> date (usec) %ld not found in journal\n", usec);
|
2024-08-29 08:21:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-14 23:03:19 +02:00
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------------
|
|
|
|
|
|
2024-09-15 23:43:56 +02:00
|
|
|
|
#define __STDC_WANT_LIB_EXT1__ 1
|
2024-09-14 23:03:19 +02:00
|
|
|
|
|
2024-09-13 09:38:07 +02:00
|
|
|
|
void fsm_journal_publish (journal jj, char *message)
|
2024-08-29 08:21:22 +02:00
|
|
|
|
{
|
2024-09-11 23:57:13 +02:00
|
|
|
|
unit *a_unit = jj.last;
|
2024-09-13 09:38:07 +02:00
|
|
|
|
char buf [JOURNAL_DATE_MAX_LENGTH];
|
2024-09-11 23:57:13 +02:00
|
|
|
|
int nb = 0;
|
2024-09-10 22:37:31 +02:00
|
|
|
|
while (a_unit)
|
2024-08-29 08:21:22 +02:00
|
|
|
|
{
|
2024-09-11 23:57:13 +02:00
|
|
|
|
// Format time, "ddd yyyy-mm-dd hh:mm:ss zzz" "%Y-%m-%d %H:%M:%S"
|
|
|
|
|
// https://www.man7.org/linux/man-pages/man3/strftime.3.html
|
2024-09-15 23:43:56 +02:00
|
|
|
|
// https://en.cppreference.com/w/c/io/fprintf
|
|
|
|
|
// https://nicolasj.developpez.com/articles/libc/string/
|
|
|
|
|
// https://thelinuxcode.com/pass-string-function-c-language/
|
2024-09-11 23:57:13 +02:00
|
|
|
|
strftime(buf, sizeof(buf), "%D %T", localtime(&a_unit->yy_dd_mm));
|
2024-09-14 23:03:19 +02:00
|
|
|
|
printf ("%s + %6ld %6d %s\n", buf, a_unit->usec, nb, a_unit->message);
|
2024-09-11 23:57:13 +02:00
|
|
|
|
a_unit = a_unit->prev;
|
|
|
|
|
nb ++;
|
2024-08-29 08:21:22 +02:00
|
|
|
|
}
|
2024-09-14 23:03:19 +02:00
|
|
|
|
|
2024-09-15 23:43:56 +02:00
|
|
|
|
struct timeval tv;
|
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
|
long yy_dd_mm = tv.tv_sec;
|
|
|
|
|
// long usec = tv.tv_usec; << how to use if (format)
|
2024-09-14 23:03:19 +02:00
|
|
|
|
|
2024-09-15 23:43:56 +02:00
|
|
|
|
strftime(buf, sizeof(buf), "%D %T", localtime(&yy_dd_mm));
|
|
|
|
|
|
|
|
|
|
printf ("%s + %6d %s\n", buf, nb, message);
|
2024-08-29 08:21:22 +02:00
|
|
|
|
}
|
2024-09-10 22:37:31 +02:00
|
|
|
|
|
2024-09-14 23:03:19 +02:00
|
|
|
|
//
|
|
|
|
|
/*
|
|
|
|
|
void modifyString(char *str) {
|
|
|
|
|
*str = ‘J‘; // Dereference to change first char
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 23:43:56 +02:00
|
|
|
|
int fonction_appelante() {
|
2024-09-14 23:03:19 +02:00
|
|
|
|
char myString[] = "Hello World";
|
|
|
|
|
modifyString(myString);
|
|
|
|
|
printf("%s", myString); // Prints Jello World
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Because modifyString receives a pointer to the string,
|
2024-09-15 23:43:56 +02:00
|
|
|
|
it can modify the original by dereferencing.
|
2024-09-14 23:03:19 +02:00
|
|
|
|
*/
|