2019-10-06 15:18:19 +03:00

113 lines
3.1 KiB
C

/*!
* \file main.c
* This is the main file of the RTES final task.
*
* \author Christos Choutouridis AEM:8997 <cchoutou@ece.auth.gr>
*/
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include<signal.h>
#include <unistd.h>
#include <pthread.h>
#include "listener.h"
#include "client.h"
/*!
* Global data
*/
//! @{
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 = "v:i:p:s:w:th";
/*!
* CLI long options
*/
const struct option long_opt[] = {
{"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'},
{"tracktime", no_argument, NULL, 't'},
{"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, 0}
};
/*!
* \brief
* Parse input argument and fill the kcli_input_t struct
* \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
* \arg 0 Success
* \arg 1 Fail
*/
int parse_args (settings_t *s, int argc, char const *argv[]) {
int c;
while ((c = getopt_long (argc, (char *const *)argv, short_opt, long_opt, NULL)) != -1) {
switch (c) {
case -1: /* no more arguments */
case 0: /* long options toggles */
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;
case 't': s->trackTime = true; break;
case 'h': printf ("This will be the help text\n"); break;
case ':':
default:
case '?':
fprintf (stderr, "%s: invalid option -- %c\n", argv[0], c);
fprintf (stderr, "Try `%s --help' for more information.\n", argv[0]);
exit(1);
}
}
return 0;
}
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, 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;
}