FACT++  1.0
void initialize ( )

Definition at line 385 of file feeserver.c.

References createLogMessage(), dim_dummy_exit_handler(), dim_error_msg_handler(), dis_add_error_handler(), dis_add_exit_handler, dtq_sleep(), fee_exit_handler(), initMessageStruct(), Memory::malloc(), sprintf(), start(), threadInitializeCE(), time, triggerRestart(), unpublishCharItemList(), unpublishIntItemList(), and unpublishItemList().

Referenced by FloatInfo::FloatInfo(), and main().

385  {
386  //-- Declaring variables --
387  struct timeval now;
388  struct timespec timeout;
389  pthread_attr_t attr;
390  int nRet;
391  int status;
392  int initState = FEE_CE_NOTINIT;
393  char* name = 0;
394  char* dns = 0;
395  bool initOk = true;
396  unsigned int envVal = 0;
397  char msg[250];
398  int restartCount = 0;
399 
400  //-- register interrupt handler (CTRL-C)
401  // not used yet, causes problems
402 // if (signal(SIGINT, interrupt_handler) == SIG_ERR) {
403 //# ifdef __DEBUG
404 // printf("Unable to register interrupt handler.\n");
405 // printf("This is not fatal -> continuing.\n");
406 //# endif
407 // }
408 
409  //-- get name of the server --
410  name = getenv("FEE_SERVER_NAME");
411  if (name == 0) {
412 # ifdef __DEBUG
413  printf("No FEE_SERVER_NAME \n");
414 # endif
415  exit(202);
416  }
417 
418  serverName = (char*) malloc(strlen(name) + 1);
419  if (serverName == 0) {
420  //no memory available!
421 # ifdef __DEBUG
422  printf("no memory available while trying to create server name!\n");
423 # endif
424  exit(201);
425  }
426  strcpy(serverName, name);
427  serverNameLength = strlen(serverName);
428 
429  //-- test, if DIM_DNS_NODE is specified
430  dns = getenv("DIM_DNS_NODE");
431  if (dns == 0) {
432 # ifdef __DEBUG
433  printf("No DIM_DNS_NODE specified. \n");
434 # endif
435  exit(203);
436  }
437 
438  // set the desired log level, if provided
439  if (getenv("FEE_LOG_LEVEL")) {
440  sscanf(getenv("FEE_LOG_LEVEL"), "%d", &envVal);
441  if ((envVal < 0) || (envVal > MSG_MAX_VAL)) {
442 # ifdef __DEBUG
443  printf("Environmental variable has invalid Log Level, using default instead.\n");
444  fflush(stdout);
445 # endif
446  } else {
447  logLevel = envVal | MSG_ALARM;
448  }
449  }
450 
451  // set logWatchDogTimeout, if env variable "FEE_LOGWATCHDOG_TIMEOUT" is set
452  if (getenv("FEE_LOGWATCHDOG_TIMEOUT")) {
453  sscanf(getenv("FEE_LOGWATCHDOG_TIMEOUT"), "%d", &envVal);
454  if ((envVal <= 0) || (envVal > MAX_TIMEOUT)) {
455 # ifdef __DEBUG
456  printf("Environmental variable has invalid LogWatchDog Timeout, using default instead.\n");
457  fflush(stdout);
458 # endif
459  } else {
460  logWatchDogTimeout = envVal;
461  }
462  }
463 
464  // get restart counter
465  if (getenv("FEESERVER_RESTART_COUNT")) {
466  restartCount = atoi(getenv("FEESERVER_RESTART_COUNT"));
467  }
468 
469  // Initial printout
470 # ifdef __DEBUG
471  printf("\n ** FeeServer version %s ** \n\n", FEESERVER_VERSION);
472  printf("FeeServer name: %s\n", serverName);
473  printf("Using DIM_DNS_NODE: %s\n", dns);
474 # ifdef __BENCHMARK
475  printf(" -> Benchmark version of FeeServer <- \n");
476 # endif
477  printf("Current log level is: %d (MSG_ALARM (%d) is always on)\n", logLevel, MSG_ALARM);
478  printf("Restart Count is: %d; Restart-Env is: %s\n", restartCount,
479  getenv("FEESERVER_RESTART_COUNT"));
480 # endif
481 
482  //set dummy exit_handler to disable framework exit command, returns void
484 
485  //set error handler to catch DIM framework messages
487 
488  // to ensure that signal is in correct state before init procedure
489  ceReadySignaled = false;
490 
491  // lock mutex
492  status = pthread_mutex_lock(&wait_init_mut);
493  if (status != 0) {
494 # ifdef __DEBUG
495  printf("Lock init mutex error: %d\n", status);
496  fflush(stdout);
497 # endif
498  initOk = false;
499  } else {
500  // initiailisation of thread attribute only if mutex has been locked
501  status = pthread_attr_init(&attr);
502  if (status != 0) {
503 # ifdef __DEBUG
504  printf("Init attribute error: %d\n", status);
505  fflush(stdout);
506 # endif
507  initOk = false;
508  } else {
509  status = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
510  if (status != 0) {
511 # ifdef __DEBUG
512  printf("Set attribute error: %d\n", status);
513  fflush(stdout);
514 # endif
515  initOk = false;
516  }
517  }
518  }
519 
520  if (initOk == true) {
521  // call only if initOk == true,
522  status = pthread_create(&thread_init, &attr, (void*) &threadInitializeCE, 0);
523  if (status != 0) {
524 # ifdef __DEBUG
525  printf("Create thread error: %d\n", status);
526  fflush(stdout);
527 # endif
528  initState = FEE_CE_NOTINIT;
529  } else {
530 # ifdef __DEBUG // for debugging the time amount the watchdog really waits (START)
531  time_t initStartTime = time(NULL);
532 # endif //__DEBUG
533 
534  // timeout set in ms, should be enough for initialisation; see fee_defines.h for current value
535  status = gettimeofday(&now, 0);
536  if ((status != 0) || (restartCount <= 0)) {
537  // backup solution for detetcting end of init process
538 # ifdef __DEBUG
539  printf("Get time of day error: %d or restartCount <= 0 (%d), using backup solution\n",
540  status, restartCount);
541  fflush(stdout);
542 # endif
543  // unlock mutex to enable functionality of signalCEReady
544  status = pthread_mutex_unlock(&wait_init_mut);
545 # ifdef __DEBUG
546  if (status != 0) {
547  printf("Unlock mutex error: %d\n", status);
548  fflush(stdout);
549  }
550 # endif
551  // sleep init-timeout length
552  usleep((TIMEOUT_INIT_CE_MSEC * 1000)); // sleep the microsec fraction
553 
554  const int sleepFraction = 1; // to check ready signal each second
555  int sleepLoops = TIMEOUT_INIT_CE_SEC / sleepFraction;
556  int cycles = 0;
557  do {
558  dtq_sleep(sleepFraction);
559  if (ceReadySignaled) {
560  break;
561  }
562  } while ( cycles++ < sleepLoops);
563 // dtq_sleep(TIMEOUT_INIT_CE_SEC); // old style without check each second
564 
565  if (ceReadySignaled == false) {
566  status = pthread_cancel(thread_init);
567 # ifdef __DEBUG
568  if (status != 0) {
569  printf("No thread to cancel: %d\n", status);
570  fflush(stdout);
571  }
572 # endif
573  // start with "the CE is not initialized!"
574  initState = FEE_CE_NOTINIT;
575 # ifdef __DEBUG
576  printf("Timeout in init [sleep]: %d\n", initState);
577  fflush(stdout);
578 # endif
579  } else {
580  if (ceInitState != CE_OK) {
581  // init failed, but no timeout occured
582  // (insufficient memory, etc. ... or something else)
583 # ifdef __DEBUG
584  printf("Init of CE failed, error: %d\n", ceInitState);
585  fflush(stdout);
586 # endif
587  initState = FEE_CE_NOTINIT;
588  } else {
589  // start with "everything is fine"
590  initState = FEE_OK;
591 # ifdef __DEBUG
592  printf("Init OK\n");
593  fflush(stdout);
594 # endif
595  }
596  }
597  } else {
598  timeout.tv_sec = now.tv_sec + TIMEOUT_INIT_CE_SEC;
599  timeout.tv_nsec = (now.tv_usec * 1000) +
600  (TIMEOUT_INIT_CE_MSEC * 1000000);
601 
602  // wait for finishing "issue" or timeout after the mutex is unlocked
603  // a retcode of 0 means, that pthread_cond_timedwait has returned
604  // with the cond_init signaled
605  status = pthread_cond_timedwait(&init_cond, &wait_init_mut, &timeout);
606  // -- start FeeServer depending on the state of the CE --
607  if (status != 0) {
608  status = pthread_cancel(thread_init);
609 # ifdef __DEBUG
610  if (status != 0) {
611  printf("No thread to cancel: %d\n", status);
612  fflush(stdout);
613  }
614 # endif
615  // start with "the CE is not initialized!"
616  initState = FEE_CE_NOTINIT;
617 # ifdef __DEBUG
618  printf("Timeout in init [timed_wait]: %d\n", initState);
619  fflush(stdout);
620 # endif
621  } else {
622  if (ceInitState != CE_OK) {
623  // init failed, but no timeout occured
624  // (insufficient memory, etc. ... or something else)
625 # ifdef __DEBUG
626  printf("Init of CE failed, error: %d\n", ceInitState);
627  fflush(stdout);
628 # endif
629  initState = FEE_CE_NOTINIT;
630  } else {
631  // start with "everything is fine"
632  initState = FEE_OK;
633 # ifdef __DEBUG
634  printf("Init OK\n");
635  fflush(stdout);
636 # endif
637  }
638  }
639  }
640 # ifdef __DEBUG // for debugging the time amout the watchdog waits (STOP)
641  time_t initStopTime = time(NULL);
642  if (initState != FEE_OK) {
643  printf("Watchdog: CE init tread\n started %s",
644  ctime(&initStartTime));
645  printf(" killed %s\n", ctime(&initStopTime));
646  // don't put this into one printf line -
647  // ctime or printf doe not work correct then, why?
648  fflush(stdout);
649  } else {
650  printf("Watchdog: CE init tread\n started %s",
651  ctime(&initStartTime));
652  printf(" finished %s\n", ctime(&initStopTime));
653  // don't put this into one printf line -
654  // ctime or printf doe not work correct then, why?
655  fflush(stdout);
656  }
657 # endif //__DEBUG
658  }
659  // destroy thread attribute
660  status = pthread_attr_destroy(&attr);
661 # ifdef __DEBUG
662  if (status != 0) {
663  printf("Destroy attribute error: %d\n", status);
664  fflush(stdout);
665  }
666 # endif
667  }
668 
669  // init message struct -> FeeServer name, version and DNS are also provided
671 
672  if (initState != FEE_OK) {
673  // remove all services of Items of ItemList
674 # ifdef __DEBUG
675  printf("Init failed, unpublishing item list\n");
676  fflush(stdout);
677 # endif
679  // new since version 0.8.1 -> int channels
681  // new since version 0.8.2b -> char channels
683  }
684 
685  // add div. services and the command channel and then start DIM server
686  nRet = start(initState);
687 
688  // unlock mutex
689  status = pthread_mutex_unlock(&wait_init_mut);
690  if (status != 0) {
691 # ifdef __DEBUG
692  printf("Unlock mutex error: %d\n", status);
693  fflush(stdout);
694 # endif
695  if (nRet == FEE_OK) {
696  createLogMessage(MSG_WARNING, "Unable to unlock init mutex.", 0);
697  }
698  }
699 
700  if (nRet != FEE_OK) {
701 # ifdef __DEBUG
702  printf("unable to start DIM server, exiting.\n");
703  fflush(stdout);
704 # endif
705  fee_exit_handler(205);
706  } else {
707 # ifdef __DEBUG
708  printf("DIM Server successfully started, ready to accept commands.\n");
709  fflush(stdout);
710 # endif
711  }
712 
713 # ifdef __DEBUG
714  printf("DEBUG - Init-State: %d, CE-State: %d, Restart-Env: %s, RestartCount: %d.\n",
715  initState, ceInitState, getenv("FEESERVER_RESTART_COUNT"), restartCount);
716  fflush(stdout);
717 # endif
718 
719  // test for failed init of CE and init restart counter,
720  // counter counts backwards: only if counter > 0 restart is triggerd
721  if ((initState != FEE_OK) && (getenv("FEESERVER_RESTART_COUNT")) &&
722  (restartCount > 0)) {
723  msg[sprintf(msg,
724  "Triggering a FeeServer restart to give CE init another try. Restart count (backward counter): %d ",
725  restartCount)] = 0;
726  createLogMessage(MSG_WARNING, msg, 0);
727 # ifdef __DEBUG
728  printf("Triggering a FeeServer restart for another CE init try (backward count: %d).\n",
729  restartCount);
730  fflush(stdout);
731 # endif
732  // small sleep, that DIM is able to send log messages before restart
733  dtq_sleep(1);
734  // trigger restart to give it another try for the CE to init
735  triggerRestart(FEE_EXITVAL_TRY_INIT_RESTART);
736  // NOTE this function won't return ...
737  }
738  // look through watchdog and backup solution about ceInitState and check it again !!!
739  // afterwards the following line won't be necessary !!!
740  // needed later in information about properties !!!
741 // ceInitState = initState;
742 
743  return;
744 }
int start(int initState)
Definition: feeserver.c:1740
static bool ceReadySignaled
Definition: feeserver.c:77
void dim_dummy_exit_handler(int *bufp)
Definition: feeserver.c:3123
void fee_exit_handler(unsigned int state)
Definition: feeserver.c:3109
void * malloc()
Definition: EventBuilder.cc:99
void threadInitializeCE()
Definition: feeserver.c:747
static int serverNameLength
Definition: feeserver.c:189
void dis_add_error_handler(void(*user_routine)())
Definition: dis.c:540
unsigned int dtq_sleep(int secs)
Definition: dtq.c:837
void createLogMessage(unsigned int type, char *description, char *origin)
Definition: feeserver.c:1530
void unpublishCharItemList()
Definition: feeserver.c:4166
void dim_error_msg_handler(int severity, int error_code, char *msg)
Definition: feeserver.c:1662
static unsigned int logWatchDogTimeout
Definition: feeserver.c:133
static unsigned int logLevel
Definition: feeserver.c:211
void unpublishItemList()
Definition: feeserver.c:3076
Warning because the service this data corrsponds to might have been last updated longer ago than Local time
Definition: smartfact.txt:92
static int ceInitState
Definition: feeserver.c:83
static char * serverName
Definition: feeserver.c:183
static pthread_cond_t init_cond
Definition: feeserver.c:248
static pthread_mutex_t wait_init_mut
Definition: feeserver.c:260
void initMessageStruct()
Definition: feeserver.c:3000
void unpublishIntItemList()
Definition: feeserver.c:3439
static pthread_t thread_init
Definition: feeserver.c:217
void triggerRestart(int exitVal)
Definition: feeserver.c:2467
#define dis_add_exit_handler
Definition: dis.h:15
sprintf(name1,"NewService%d", i)

+ Here is the call graph for this function:

+ Here is the caller graph for this function: