From 0f1f9ac088cfdb3fe089e6df44f6d3ca6801a95c Mon Sep 17 00:00:00 2001 From: Christos Houtouridis Date: Sun, 6 Oct 2019 15:18:19 +0300 Subject: [PATCH] Seeker now is seperate thread --- src/client.c | 45 ++++++++++++++++++++++++---------- src/client.h | 2 +- src/core.c | 65 +++++++++++++++++++++++++++++++++++++++++--------- src/core.h | 4 ++++ src/listener.c | 11 ++++++--- src/main.c | 26 ++++++++++---------- src/msg_impl.h | 24 ++++++++++--------- 7 files changed, 127 insertions(+), 50 deletions(-) diff --git a/src/client.c b/src/client.c index bdb623e..993a15e 100644 --- a/src/client.c +++ b/src/client.c @@ -46,7 +46,11 @@ static bool_t ping (devAEM_t dev) { */ static size_t seeker (void) { size_t cnt =0; // count devices on range + + sleep (settings.seekerInterval); + log_debug ("Debug: Pinging devices...\n"); + devList_acquire (); for (int i=0 ; i for each recipient for (size_t i=0 ; i= value) apply = value; \ -} while (0) +/*! + * Initialize the devList + * @param msgList Pointer to mesList t initialize + * @return The status of the operation + */ +status_t devList_init (devList_t* devList) { + devAEM_t l[] = AEMLIST; + if (pthread_mutex_init(&lock_devList, NULL) != 0) { + log_error ("Error: mutex init has failed\n"); + return MSG_ERROR; + } + memset ((void*)devList, 0, sizeof(devList_t)); + for (size_t i =0 ; i= value) apply = value; \ +} while (0) + +/*! Macro helper to saturate decreased values */ +#define _btm_saturate(test, apply, value) do { \ + if (test < value) apply = value; \ +while (0) + /*! * Initialize the msgList * @param msgList Pointer to mesList t initialize @@ -643,14 +672,28 @@ status_t statsPrint (stats_t* stats) { fprintf (fp, "Out direct messages: %d\n", stats->outDirectMsg); fprintf (fp, "Average message size: %g\n", stats->avMsgSize); fprintf (fp, "Average time to me: %g\n", stats->avTimeToMe); + fclose (fp); + return MSG_OK; +} +/*! + * Device online timing print functionality + * @param devList Pointer to devList to print + * @return The status of the operation + */ +status_t statsTimesPrint (devList_t *devList) { + FILE* fp = fopen ("devices.txt", "w"); + if (fp == NULL) { + fclose (fp); + return MSG_ERROR; + } + fprintf (fp, "\n Device timings\n================\n"); for (size_t i =0 ; i */ @@ -21,24 +21,23 @@ */ //! @{ -settings_t settings_init (settings); -devList_t devList_init(devList[AEMLIST_SIZE]); -stats_t stats; +settings_t settings_init (settings); //!< Application settings +devList_t devList[AEMLIST_SIZE]; //!< Device list +stats_t stats; //!< Statistical data //! @} /*! * CLI short options */ -const char *short_opt = "li:v:p:s:w:th"; +const char *short_opt = "v:i:p:s:w:th"; /*! * CLI long options */ const struct option long_opt[] = { - {"port", required_argument, NULL, 'l'}, - {"interval", required_argument, NULL, 'i'}, {"outlevel", required_argument, NULL, 'v'}, + {"interval", required_argument, NULL, 'i'}, {"pingtimeout",required_argument, NULL, 'p'}, {"sendtimeout",required_argument, NULL, 's'}, {"who", required_argument, NULL, 'w'}, @@ -50,7 +49,7 @@ const struct option long_opt[] = { /*! * \brief * Parse input argument and fill the kcli_input_t struct - * \param in Pointer to \ref kcli_input_t structure to fill + * \param s Pointer to settings_t data to fill * \param argc The argument count as passed to the main() * \param argv Argument array as passed to the main() * \return The status of the operation @@ -65,13 +64,12 @@ int parse_args (settings_t *s, int argc, char const *argv[]) { case -1: /* no more arguments */ case 0: /* long options toggles */ break; - case 'l': s->port = atoi(optarg); break; - case 'i': s->msgInterval = atoi (optarg); break; case 'v': s->outLevel = atoi (optarg); if (s->outLevel >= OUTLEVEL_2) s->outLevel = OUTLEVEL_2; if (s->outLevel < OUTLEVEL_0) s->outLevel = OUTLEVEL_0; break; + case 'i': s->seekerInterval = atoi (optarg); break; case 'p': s->pingTimeout = atoi (optarg); break; case 's': s->sendTimeout.tv_sec = atoi (optarg); break; case 'w': s->me = atoi (optarg); break; @@ -89,22 +87,26 @@ int parse_args (settings_t *s, int argc, char const *argv[]) { } -int main(int argc, char const *argv[]) { +int main (int argc, char const *argv[]) { + // get command line arguments parse_args (&settings, argc, argv); // Initialize all subsystems log_init (); stats_init (&stats); + devList_init (devList); msgList_init (&msgList); // Create threads - pthread_t ptL, ptC; + pthread_t ptL, ptS, ptC; pthread_create (&ptL, NULL, pthListener, NULL); + pthread_create (&ptS, NULL, pthSeeker, NULL); pthread_create (&ptC, NULL, pthClient, NULL); // block here pthread_join (ptL, NULL); + pthread_join (ptS, NULL); pthread_join (ptC, NULL); return 0; } diff --git a/src/msg_impl.h b/src/msg_impl.h index c782a49..2db5677 100644 --- a/src/msg_impl.h +++ b/src/msg_impl.h @@ -21,17 +21,17 @@ */ #define AEMLIST_SIZE (10) -#define devList_init(l) l = { \ - { 7700, 0, 0, 0}, \ - { 8261, 0, 0, 0}, \ - { 8765, 0, 0, 0}, \ - { 8844, 0, 0, 0}, \ - { 8880, 0, 0, 0}, \ - { 8861, 0, 0, 0}, \ - { 8877, 0, 0, 0}, \ - { 8941, 0, 0, 0}, \ - { 8934, 0, 0, 0}, \ - { 8997, 0, 0, 0} \ +#define AEMLIST { \ + 7700, \ + 8261, \ + 8765, \ + 8844, \ + 8880, \ + 8861, \ + 8877, \ + 8941, \ + 8934, \ + 8997 \ } /*! @@ -217,6 +217,7 @@ typedef enum { typedef struct { devAEM_t me; uint16_t port; + time_t seekerInterval; time_t msgInterval; time_t msgRand; outLevel_en outLevel; @@ -230,6 +231,7 @@ extern settings_t settings; #define settings_init(s) s = { \ .me = 8997, \ .port = 2288, \ + .seekerInterval = 30, \ .msgInterval = 60, \ .msgRand = 240, \ .outLevel = OUTLEVEL_1, \