Christos Houtouridis c9f13c07c8 Testing version
2019-10-03 15:19:33 +03:00

102 lines
2.9 KiB
C
Executable File

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <pthread.h>
#include "listener.h"
#include "client.h"
/*!
* Global data
*/
//! @{
settings_t settings_init (settings);
devList_t devList_init (devList[AEMLIST_SIZE]);
stats_t stats;
//! @}
/*!
* CLI short options
*/
const char *short_opt = "nd:i:l:p:s:w:th";
/*!
* CLI long options
*/
const struct option long_opt[] = {
{"port", required_argument, NULL, 'n'},
{"duration", required_argument, NULL, 'd'},
{"interval", required_argument, NULL, 'i'},
{"outlevel", required_argument, NULL, 'l'},
{"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 in Pointer to \ref kcli_input_t structure 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 'n': s->port = atoi(optarg); break;
case 'd': s->duration = atoi (optarg); break;
case 'i': s->msgInterval = atoi (optarg); break;
case 'l':
s->outLevel = atoi (optarg);
if (s->outLevel >= OUTLEVEL_2) s->outLevel = OUTLEVEL_2;
if (s->outLevel < OUTLEVEL_0) s->outLevel = OUTLEVEL_0;
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");
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[]) {
parse_args (&settings, argc, argv);
log_init ();
stats_init (&stats);
msgList_init (&msgList);
// Create threads
pthread_t ptL, ptC;
pthread_create (&ptL, NULL, pthListener, NULL);
pthread_create (&ptC, NULL, pthClient, NULL);
// block here
pthread_join (ptL, NULL);
pthread_join (ptC, NULL);
return 0;
}