1 Return to feeserver.c CVS log Up to [MAIN] / dcscvs / FeeServer / feeserver / src
3 --------------------------------------------------------------------------------
4 File: [MAIN] / dcscvs / FeeServer / feeserver / src / feeserver.c (download)
5 Revision: 1.26, Wed
May 7 14:08:13 2008 UTC (22 months ago) by dominik
7 CVS Tags: d,
HEAD, FeeServer_v0-9-4_RCU-
v0-9-9-
dev, FeeServer_v0-9-4_RCU-
v0-9-8-
dev, FeeServer_v0-9-4_RCU-
v0-9-7-
dev, FeeServer_v0-9-4_RCU-
v0-9-6-
dev, FeeServer_v0-9-4_RCU-
v0-9-5-
dev, FeeServer_v0-9-4_RCU-
v0-9-4, FeeServer_v0-9-4_RCU-v0-9-14-
dev, FeeServer_v0-9-4_RCU-v0-9-13-
dev, FeeServer_v0-9-4_RCU-v0-9-12-
dev, FeeServer_v0-9-4_RCU-v0-9-11-
dev, FeeServer_v0-9-4_RCU-v0-9-10-dev
8 Changes
since 1.25: +32 -16 lines
13 --------------------------------------------------------------------------------
50 #include "fee_types.h" 51 #include "fee_functions.h" 52 #include "fee_defines.h" 53 #include "feepacket_flags.h" 54 #include "fee_errors.h" 55 #include "ce_command.h" 58 #include "fee_utest.h" 71 static int state = COLLECTING;
242 static pthread_cond_t
cond = PTHREAD_COND_INITIALIZER;
248 static pthread_cond_t
init_cond = PTHREAD_COND_INITIALIZER;
254 static pthread_mutex_t
wait_mut = PTHREAD_MUTEX_INITIALIZER;
272 static pthread_mutex_t
log_mut = PTHREAD_MUTEX_INITIALIZER;
363 int main(
int argc,
char** arg) {
388 struct timespec timeout;
392 int initState = FEE_CE_NOTINIT;
396 unsigned int envVal = 0;
398 int restartCount = 0;
410 name = getenv(
"FEE_SERVER_NAME");
413 printf(
"No FEE_SERVER_NAME \n");
418 serverName = (
char*)
malloc(strlen(name) + 1);
419 if (serverName == 0) {
422 printf(
"no memory available while trying to create server name!\n");
426 strcpy(serverName, name);
427 serverNameLength = strlen(serverName);
430 dns = getenv(
"DIM_DNS_NODE");
433 printf(
"No DIM_DNS_NODE specified. \n");
439 if (getenv(
"FEE_LOG_LEVEL")) {
440 sscanf(getenv(
"FEE_LOG_LEVEL"),
"%d", &envVal);
441 if ((envVal < 0) || (envVal > MSG_MAX_VAL)) {
443 printf(
"Environmental variable has invalid Log Level, using default instead.\n");
447 logLevel = envVal | MSG_ALARM;
452 if (getenv(
"FEE_LOGWATCHDOG_TIMEOUT")) {
453 sscanf(getenv(
"FEE_LOGWATCHDOG_TIMEOUT"),
"%d", &envVal);
454 if ((envVal <= 0) || (envVal > MAX_TIMEOUT)) {
456 printf(
"Environmental variable has invalid LogWatchDog Timeout, using default instead.\n");
460 logWatchDogTimeout = envVal;
465 if (getenv(
"FEESERVER_RESTART_COUNT")) {
466 restartCount = atoi(getenv(
"FEESERVER_RESTART_COUNT"));
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);
475 printf(
" -> Benchmark version of FeeServer <- \n");
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"));
489 ceReadySignaled =
false;
492 status = pthread_mutex_lock(&wait_init_mut);
495 printf(
"Lock init mutex error: %d\n", status);
501 status = pthread_attr_init(&attr);
504 printf(
"Init attribute error: %d\n", status);
509 status = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
512 printf(
"Set attribute error: %d\n", status);
520 if (initOk ==
true) {
525 printf(
"Create thread error: %d\n", status);
528 initState = FEE_CE_NOTINIT;
530 # ifdef __DEBUG // for debugging the time amount the watchdog really waits (START) 531 time_t initStartTime =
time(NULL);
535 status = gettimeofday(&now, 0);
536 if ((status != 0) || (restartCount <= 0)) {
539 printf(
"Get time of day error: %d or restartCount <= 0 (%d), using backup solution\n",
540 status, restartCount);
544 status = pthread_mutex_unlock(&wait_init_mut);
547 printf(
"Unlock mutex error: %d\n", status);
552 usleep((TIMEOUT_INIT_CE_MSEC * 1000));
554 const int sleepFraction = 1;
555 int sleepLoops = TIMEOUT_INIT_CE_SEC / sleepFraction;
559 if (ceReadySignaled) {
562 }
while ( cycles++ < sleepLoops);
565 if (ceReadySignaled ==
false) {
566 status = pthread_cancel(thread_init);
569 printf(
"No thread to cancel: %d\n", status);
574 initState = FEE_CE_NOTINIT;
576 printf(
"Timeout in init [sleep]: %d\n", initState);
580 if (ceInitState != CE_OK) {
584 printf(
"Init of CE failed, error: %d\n", ceInitState);
587 initState = FEE_CE_NOTINIT;
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);
605 status = pthread_cond_timedwait(&init_cond, &wait_init_mut, &timeout);
608 status = pthread_cancel(thread_init);
611 printf(
"No thread to cancel: %d\n", status);
616 initState = FEE_CE_NOTINIT;
618 printf(
"Timeout in init [timed_wait]: %d\n", initState);
622 if (ceInitState != CE_OK) {
626 printf(
"Init of CE failed, error: %d\n", ceInitState);
629 initState = FEE_CE_NOTINIT;
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));
650 printf(
"Watchdog: CE init tread\n started %s",
651 ctime(&initStartTime));
652 printf(
" finished %s\n", ctime(&initStopTime));
660 status = pthread_attr_destroy(&attr);
663 printf(
"Destroy attribute error: %d\n", status);
672 if (initState != FEE_OK) {
675 printf(
"Init failed, unpublishing item list\n");
686 nRet =
start(initState);
689 status = pthread_mutex_unlock(&wait_init_mut);
692 printf(
"Unlock mutex error: %d\n", status);
695 if (nRet == FEE_OK) {
700 if (nRet != FEE_OK) {
702 printf(
"unable to start DIM server, exiting.\n");
708 printf(
"DIM Server successfully started, ready to accept commands.\n");
714 printf(
"DEBUG - Init-State: %d, CE-State: %d, Restart-Env: %s, RestartCount: %d.\n",
715 initState, ceInitState, getenv(
"FEESERVER_RESTART_COUNT"), restartCount);
721 if ((initState != FEE_OK) && (getenv(
"FEESERVER_RESTART_COUNT")) &&
722 (restartCount > 0)) {
724 "Triggering a FeeServer restart to give CE init another try. Restart count (backward counter): %d ",
728 printf(
"Triggering a FeeServer restart for another CE init try (backward count: %d).\n",
749 status = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
753 printf(
"Set cancel state (init) error: %d\n", status);
758 status = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
762 printf(
"Set cancel type (init) error: %d\n", status);
781 status = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, 0);
784 printf(
"Set cancel type error: %d\n", status);
790 status = pthread_mutex_lock(&wait_init_mut);
793 printf(
"Lock mutex error: %d\n", status);
799 ceInitState = ceState;
803 pthread_cond_broadcast(&init_cond);
806 ceReadySignaled =
true;
809 status = pthread_mutex_unlock(&wait_init_mut);
812 printf(
"Unlock mutex error: %d\n", status);
818 status = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
821 printf(
"Set cancel type error: %d\n", status);
831 struct timespec timeout;
834 pthread_t thread_handle;
836 IssueStruct issueParam;
837 CommandHeader header;
838 char* pHeaderStream = 0;
839 MemoryNode* memNode = 0;
845 if ((size != 0 ) && (*size >= 4)) {
847 "FeeServer CommandHandler (Received command) - Packet-ID: %d",
858 issueParam.nRet = FEE_UNKNOWN_RETVAL;
861 if ((state != RUNNING) && (state != ERROR_STATE)) {
867 status = pthread_mutex_lock(&command_mut);
870 printf(
"Lock command mutex error: %d\n", status);
876 if ((tag == 0) || (address == 0) || (size == 0)) {
878 "Received null pointer of DIM framework in command handler.");
882 if (*size < HEADER_SIZE) {
884 "FeeServer received corrupted command.");
889 printf(
" Cmnd - Size: %d\n", *size);
894 memcpy(&header.id, address, HEADER_SIZE_ID);
895 memcpy(&header.errorCode, address + HEADER_OFFSET_ID, HEADER_SIZE_ERROR_CODE);
896 memcpy(&header.flags, address + HEADER_OFFSET_ERROR_CODE, HEADER_SIZE_FLAGS);
897 memcpy(&header.checksum, address + HEADER_OFFSET_FLAGS, HEADER_SIZE_CHECKSUM);
900 if ((header.flags & HUFFMAN_FLAG) != 0) {
905 issueParam.size = *size - HEADER_SIZE;
906 issueParam.command = (address + HEADER_SIZE);
909 if ((header.flags & CHECKSUM_FLAG) != 0) {
911 if (!
checkCommand(issueParam.command, issueParam.size, header.checksum)) {
914 "FeeServer received corrupted command data (checksum failed).");
920 if ((header.flags & FEESERVER_UPDATE_FLAG) != 0) {
921 #ifdef ENABLE_MASTERMODE 924 createLogMessage(MSG_WARNING,
"FeeServer is not authorized to execute shell programs, skip ...", 0);
925 #endif //ENABLE_MASTERMODE 927 issueParam.nRet = FEE_FAILED;
929 }
else if ((header.flags & FEESERVER_RESTART_FLAG) != 0) {
931 }
else if ((header.flags & FEESERVER_REBOOT_FLAG) != 0) {
935 }
else if ((header.flags & FEESERVER_SHUTDOWN_FLAG) != 0) {
939 }
else if ((header.flags & FEESERVER_EXIT_FLAG) != 0) {
941 }
else if ((header.flags & FEESERVER_SET_DEADBAND_FLAG) != 0) {
943 }
else if ((header.flags & FEESERVER_GET_DEADBAND_FLAG) != 0) {
945 }
else if ((header.flags & FEESERVER_SET_ISSUE_TIMEOUT_FLAG) != 0) {
947 }
else if ((header.flags & FEESERVER_GET_ISSUE_TIMEOUT_FLAG) != 0) {
949 }
else if ((header.flags & FEESERVER_SET_UPDATERATE_FLAG) != 0) {
951 }
else if ((header.flags & FEESERVER_GET_UPDATERATE_FLAG) != 0) {
953 }
else if ((header.flags & FEESERVER_SET_LOGLEVEL_FLAG) != 0) {
955 }
else if ((header.flags & FEESERVER_GET_LOGLEVEL_FLAG) != 0) {
959 if (state == ERROR_STATE) {
961 "FeeServer is in ERROR_STATE, ignoring command for CE!");
966 if (issueParam.size == 0) {
968 "FeeServer received empty command.");
973 status = pthread_mutex_lock(&wait_mut);
976 "Unable to lock condition mutex for watchdog.");
980 status = pthread_attr_init(&attr);
984 "Unable to initialize issue thread.");
988 status = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
992 "Unable to initialize issue thread.");
996 status = pthread_create(&thread_handle, &attr, &
threadIssue, (
void*) &issueParam);
1000 "Unable to create issue thread.");
1004 status = pthread_attr_destroy(&attr);
1007 printf(
"Destroy attribute error: %d\n", status);
1011 "Unable to destroy thread attribute.", 0);
1015 status = gettimeofday(&now, 0);
1019 timeout.tv_sec = now.tv_sec + (int) (issueTimeout / 1000);
1022 timeout.tv_nsec = (now.tv_usec * 1000) +
1023 ((issueTimeout % 1000) * 1000000);
1028 retcode = pthread_cond_timedwait(&cond, &wait_mut, &timeout);
1030 printf(
"Retcode of CMND timedwait: %d\n", retcode);
1035 if (retcode == ETIMEDOUT) {
1037 printf(
"ControlEngine watchdog detected TimeOut.\n");
1041 "ControlEngine watch dog noticed a time out for last command.", 0);
1044 pthread_cancel(thread_handle);
1046 issueParam.nRet = FEE_TIMEOUT;
1047 issueParam.size = 0;
1048 }
else if (retcode != 0) {
1051 printf(
"ControlEngine watchdog detected unknown error.\n");
1055 "ControlEngine watch dog received an unknown for last command.", 0);
1058 pthread_cancel(thread_handle);
1060 issueParam.nRet = FEE_THREAD_ERROR;
1061 issueParam.size = 0;
1066 printf(
"Get time of day error: %d\n", status);
1070 "Watchdog timer could not be initialized. Using non-reliable sleep instead.",
1076 usleep(issueTimeout * 1000);
1077 status = pthread_cancel(thread_handle);
1081 printf(
"TimeOut occured.\n");
1084 "ControlEngine issue did not return in time.", 0);
1085 issueParam.nRet = FEE_TIMEOUT;
1086 issueParam.size = 0;
1096 printf(
"Issue-nRet: %d\n", issueParam.nRet);
1100 if ((issueParam.nRet < FEE_UNKNOWN_RETVAL) ||
1101 (issueParam.nRet > FEE_MAX_RETVAL)) {
1102 issueParam.nRet = FEE_UNKNOWN_RETVAL;
1104 "ControlEngine [command] returned unkown RetVal.", 0);
1109 if ((cmndACKSize > HEADER_SIZE) && (
findMemoryNode(cmndACK + HEADER_SIZE) != 0)) {
1122 cmndACK = memNode->ptr;
1128 cmndACK = (
char*)
malloc(issueParam.size + HEADER_SIZE);
1134 printf(
"no memory available!\n");
1141 status = pthread_mutex_unlock(&command_mut);
1144 printf(
"Lock command mutex error: %d\n", status);
1148 "Error while trying to unlock command mutex.", 0);
1154 if (issueParam.size > 0) {
1162 if ((header.flags & CHECKSUM_FLAG) != 0) {
1167 header.checksum = CHECKSUM_ZERO;
1171 header.errorCode = (short) issueParam.nRet;
1173 printf(
"ErrorCode in Header: %d\n", header.errorCode);
1178 memcpy((
void*) cmndACK, (
void*) pHeaderStream, HEADER_SIZE);
1179 if (pHeaderStream != 0) {
1180 free(pHeaderStream);
1193 memcpy(((
void*) cmndACK + HEADER_SIZE), (
void*) issueParam.result,
1198 cmndACKSize = issueParam.size + HEADER_SIZE;
1210 if ((!useMM) && (issueParam.result != 0)) {
1211 free(issueParam.result);
1299 status = pthread_mutex_unlock(&command_mut);
1302 printf(
"Lock command mutex error: %d\n", status);
1306 "Error while trying to unlock command mutex.", 0);
1317 if ((tag == 0) || (*tag != ACK_SERVICE_TAG)) {
1319 printf(
"invalid ACK Service\n");
1328 if ((cmndACKSize > 0) && (cmndACK != 0)) {
1337 "FeeServer AckHandler (sending ACK) - Packet-ID: %d", *cmndACK)] = 0;
1345 unsigned int msgType,
char* message) {
1349 printf(
"%s\n", message);
1361 cmndACK =
createHeader(
id, errorCode,
false,
false, 0);
1362 cmndACKSize = HEADER_SIZE;
1366 status = pthread_mutex_unlock(&command_mut);
1369 printf(
"Lock command mutex error: %d\n", status);
1373 "Error while trying to unlock command mutex.", 0);
1381 status = pthread_mutex_unlock(&wait_mut);
1384 printf(
"Unlock condition mutex error: %d. Going in ERROR state!\n", status);
1388 "Unable to unlock watchdog mutex. No more commands will be possible for CE. Going in ERROR state!",
1390 state = ERROR_STATE;
1398 char* serviceName = 0;
1401 if (state != COLLECTING) {
1402 return FEE_WRONG_STATE;
1409 printf(
"Bad item, not published\n");
1412 return FEE_NULLPOINTER;
1414 if (item->name == 0 || item->location == 0) {
1416 printf(
"Bad item, not published\n");
1419 return FEE_NULLPOINTER;
1425 printf(
"Item name already published (float), new float item discarded.\n");
1428 return FEE_ITEM_NAME_EXISTS;
1433 printf(
"Item name already published (int), float item discarded.\n");
1436 return FEE_ITEM_NAME_EXISTS;
1441 printf(
"Item name already published in char list, float item discarded.\n");
1444 return FEE_ITEM_NAME_EXISTS;
1448 serviceName = (
char*)
malloc(serverNameLength + strlen(item->name) + 2);
1449 if (serviceName == 0) {
1450 return FEE_INSUFFICIENT_MEMORY;
1453 serviceName[
sprintf(serviceName,
"%s_%s", serverName, item->name)] = 0;
1455 sizeof(
float), 0, 0);
1466 ItemNode* newNode = 0;
1468 newNode = (ItemNode*)
malloc(
sizeof(ItemNode));
1472 printf(
"no memory available while adding itemNode!\n");
1482 newNode->item = _item;
1483 newNode->lastTransmittedValue = *(_item->location);
1485 newNode->threshold = (_item->defaultDeadband < 0) ? 0.0 : (_item->defaultDeadband / 2);
1493 newNode->locBackup = _item->location;
1495 sizeof(
volatile float*));
1496 newNode->checksumBackup = newNode->checksum;
1511 printf(
"init of %s with ID %d: %f\n", newNode->item->name, newNode->id,
1512 newNode->lastTransmittedValue);
1518 if (firstNode != 0) {
1519 lastNode->next = newNode;
1523 firstNode = newNode;
1533 int originLength = 0;
1538 if (state == COLLECTING) {
1543 status = pthread_mutex_lock(&log_mut);
1548 printf(
"Lock log mutex error: %d\n", status);
1555 status = pthread_mutex_unlock(&log_mut);
1560 printf(
"Unlock log mutex error: %d\n", status);
1568 if ((logWatchDogRunning) && (strncmp(description, lastMessage.description,
1569 (MSG_DESCRIPTION_SIZE - 1)) == 0)) {
1570 replicatedMsgCount++;
1573 status = pthread_mutex_unlock(&log_mut);
1578 printf(
"Unlock log mutex error: %d\n", status);
1594 if (description != 0) {
1596 descLength = ((strlen(description) >= MSG_DESCRIPTION_SIZE)
1597 ? (MSG_DESCRIPTION_SIZE - 1) : strlen(description));
1602 originLength = ((strlen(origin) >= MSG_SOURCE_SIZE - serverNameLength - 1)
1603 ? (MSG_SOURCE_SIZE - serverNameLength - 2) : strlen(origin));
1607 message.eventType =
type;
1609 memcpy(message.detector, LOCAL_DETECTOR, MSG_DETECTOR_SIZE);
1611 strcpy(message.source, serverName);
1614 strcpy(message.source + serverNameLength,
"/");
1616 strncpy(message.source + serverNameLength + 1, origin, originLength);
1618 message.source[serverNameLength + 1 + originLength] = 0;
1621 if (description != 0) {
1623 strncpy(message.description, description, descLength);
1625 message.description[descLength] = 0;
1627 strcpy(message.description,
"No description specified.");
1631 now = localtime(&timeVal);
1632 message.date[strftime(message.date, MSG_DATE_SIZE,
"%Y-%m-%d %H:%M:%S",
1642 status = pthread_mutex_unlock(&log_mut);
1647 printf(
"Unlock log mutex error: %d\n", status);
1656 if ((logLevel & event) != 0) {
1665 char message[MSG_DESCRIPTION_SIZE];
1671 type[
sprintf(type,
"INFO")] = 0;
1672 eventType = MSG_INFO;
1675 type[
sprintf(type,
"WARNING")] = 0;
1676 eventType = MSG_WARNING;
1679 type[
sprintf(type,
"ERROR")] = 0;
1680 eventType = MSG_ERROR;
1683 type[
sprintf(type,
"FATAL")] = 0;
1684 eventType = MSG_ERROR;
1687 type[
sprintf(type,
"UNKNOWN")] = 0;
1688 eventType = MSG_WARNING;
1694 printf(
"DIM: [%s - %d] - %d: %s.\n", type, severity, error_code, msg);
1699 if ((state == RUNNING) || (state == ERROR_STATE)) {
1702 length = strlen(message);
1703 strncpy((message + length), msg, (MSG_DESCRIPTION_SIZE - length - 1));
1704 message[MSG_DESCRIPTION_SIZE - 1] = 0;
1711 switch (errorCode) {
1712 case (0x1):
return "DIMDNSUNDEF";
1713 case (0x2):
return "DIMDNSREFUS";
1714 case (0x3):
return "DIMDNSDUPLC";
1715 case (0x4):
return "DIMDNSEXIT ";
1716 case (0x5):
return "DIMDNSTMOUT";
1718 case (0x10):
return "DIMSVCDUPLC";
1719 case (0x11):
return "DIMSVCFORMT";
1720 case (0x12):
return "DIMSVCINVAL";
1721 case (0x13):
return "DIMSVCTOOLG";
1723 case (0x20):
return "DIMTCPRDERR";
1724 case (0x21):
return "DIMTCPWRRTY";
1725 case (0x22):
return "DIMTCPWRTMO";
1726 case (0x23):
return "DIMTCPLNERR";
1727 case (0x24):
return "DIMTCPOPERR";
1728 case (0x25):
return "DIMTCPCNERR";
1729 case (0x26):
return "DIMTCPCNEST";
1731 case (0x30):
return "DIMDNSCNERR";
1732 case (0x31):
return "DIMDNSCNEST";
1734 default:
return "DIMUNKWNERR";
1741 int nRet = FEE_UNKNOWN_RETVAL;
1742 char* serviceName = 0;
1743 char* messageName = 0;
1744 char* commandName = 0;
1745 char msgStructure[50];
1747 if (state == COLLECTING) {
1749 serviceName = (
char*)
malloc(serverNameLength + 13);
1750 if (serviceName == 0) {
1753 printf(
"no memory available while trying to create ACK channel!\n");
1761 serviceName[
sprintf(serviceName,
"%s_Acknowledge", serverName)] = 0;
1768 cmndACKSize = HEADER_SIZE;
1775 messageName = (
char*)
malloc(serverNameLength + 9);
1776 if (messageName == 0) {
1779 printf(
"no memory available while trying to create message channel!\n");
1787 messageName[
sprintf(messageName,
"%s_Message", serverName)] = 0;
1789 msgStructure[
sprintf(msgStructure,
"I:1;C:%d;C:%d;C:%d;C:%d",
1790 MSG_DETECTOR_SIZE, MSG_SOURCE_SIZE, MSG_DESCRIPTION_SIZE,
1791 MSG_DATE_SIZE)] = 0;
1793 messageServiceID =
dis_add_service(messageName, msgStructure, (
int*) &message,
1794 sizeof(
unsigned int) + MSG_DETECTOR_SIZE + MSG_SOURCE_SIZE +
1795 MSG_DESCRIPTION_SIZE + MSG_DATE_SIZE, 0, 0);
1799 commandName = (
char*)
malloc(serverNameLength + 9);
1800 if (commandName == 0) {
1803 printf(
"no memory available while trying to create CMD channel!\n");
1811 commandName[
sprintf(commandName,
"%s_Command", serverName)] = 0;
1820 if (initState == FEE_OK) {
1824 if (nRet != FEE_OK) {
1826 printf(
"Could NOT start monitor thread, error: %d\n", nRet);
1830 "Unable to start monitor thread on FeeServer.", 0);
1836 "FeeServer started correctly, including monitor thread.", 0);
1839 state = ERROR_STATE;
1841 "Initialisation of ControlEngine failed. FeeServer is running in ERROR state (without CE).",
1848 if (nRet != FEE_OK) {
1850 printf(
"Could NOT start log watch dog thread, error: %d; FeeServer will run without it.\n",
1855 "Can not start LogWatchDog thread (filters replicated MSGs). Uncritical error - running without it.",
1880 pthread_attr_t attr;
1883 if ((nodesAmount == 0) && (intNodesAmount == 0)) {
1885 "No Items (float and int) for monitoring are available.", 0);
1890 status = pthread_attr_init(&attr);
1893 printf(
"Init attribute error [mon]: %d\n", status);
1896 return FEE_MONITORING_FAILED;
1898 status = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1901 printf(
"Set attribute error [mon]: %d\n", status);
1904 return FEE_MONITORING_FAILED;
1908 if (nodesAmount > 0) {
1909 status = pthread_create(&thread_mon, &attr, (
void*)&
monitorValues, 0);
1912 printf(
"Create thread error [mon - float]: %d\n", status);
1915 return FEE_MONITORING_FAILED;
1920 if (intNodesAmount > 0) {
1921 status = pthread_create(&thread_mon_int, &attr, (
void*)&
monitorIntValues, 0);
1924 printf(
"Create thread error [mon - int]: %d\n", status);
1927 return FEE_MONITORING_FAILED;
1932 status = pthread_attr_destroy(&attr);
1935 printf(
"Destroy attribute error [mon]: %d\n", status);
1947 unsigned long sleepTime = 0;
1948 ItemNode* current = 0;
1950 unsigned long innerCounter = 0;
1951 unsigned long outerCounter = 0;
1954 monitorThreadStarted =
true;
1957 status = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
1960 printf(
"Set cancel state error [mon - float]: %d\n", status);
1964 "Unable to configure monitor thread (float) properly. Monitoring is not affected.", 0);
1966 status = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
1969 printf(
"Set cancel type error [mon - float]: %d\n", status);
1973 "Unable to configure monitor thread (float) properly. Monitoring is not affected.", 0);
1976 createLogMessage(MSG_DEBUG,
"Started monitor thread for FLOAT values successfully.", 0);
1980 sleepTime = (
unsigned long) (updateRate / nodesAmount);
1981 while (current != 0) {
1983 msg[
sprintf(msg,
"Value of item %s (float) is corrupt, reconstruction failed. Ignoring!",
1984 current->item->name)] = 0;
1989 if ((fabsf((*(current->item->location)) - current->lastTransmittedValue)
1990 >= current->threshold) || (outerCounter ==
1991 (innerCounter * TIME_INTERVAL_MULTIPLIER))) {
1993 current->lastTransmittedValue = *(current->item->location);
2003 current = current->next;
2004 usleep(sleepTime * 1000);
2014 if (outerCounter >= (nodesAmount * TIME_INTERVAL_MULTIPLIER)) {
2024 if (node->item->location == node->locBackup) {
2031 &(node->item->location),
sizeof(
volatile float*))) {
2033 node->locBackup = node->item->location;
2039 &(node->locBackup),
sizeof(
volatile float*))) {
2041 node->item->location = node->locBackup;
2046 if (node->checksum == node->checksumBackup) {
2055 &(node->item->location),
sizeof(
volatile float*))) {
2057 node->locBackup = node->item->location;
2059 node->checksum = node->checksumBackup;
2065 &(node->locBackup),
sizeof(
volatile float*))) {
2067 node->item->location = node->locBackup;
2069 node->checksum = node->checksumBackup;
2081 pthread_attr_t attr;
2084 status = pthread_attr_init(&attr);
2087 printf(
"Init attribute error [LogWatchDog]: %d\n", status);
2090 return FEE_THREAD_ERROR;
2092 status = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
2095 printf(
"Set attribute error [LogWatchDog]: %d\n", status);
2098 return FEE_THREAD_ERROR;
2102 status = pthread_create(&thread_logWatchdog, &attr, (
void*) &
runLogWatchDog,
2106 printf(
"Create thread error [LogWatchDog]: %d\n", status);
2109 return FEE_THREAD_ERROR;
2113 status = pthread_attr_destroy(&attr);
2116 printf(
"Destroy attribute error [LogWatchDog]: %d\n", status);
2127 unsigned int sleepSec = 0;
2128 unsigned int sleepMilliSec = 0;
2131 status = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
2134 printf(
"Set cancel state error [LogWatchDog]: %d\n", status);
2138 "Can not set cancel state for LogWatchDog thread. WatchDog should not not be affected.",
2141 status = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
2144 printf(
"Set cancel type error [LogWatchDog]: %d\n", status);
2148 "Can not set cancel type for LogWatchDog thread. WatchDog should not not be affected.",
2153 logWatchDogRunning =
true;
2155 "LogWatchDog thread for filtering replicated log messages successfully started.",
2158 printf(
"LogWatchDog thread for filtering replicated log messages successfully started.\n");
2165 status = pthread_mutex_lock(&log_mut);
2169 printf(
"Lock log mutex error: %d\n", status);
2178 status = pthread_mutex_unlock(&log_mut);
2182 printf(
"Unlock log mutex error: %d\n", status);
2188 pthread_testcancel();
2190 sleepSec = logWatchDogTimeout / 1000;
2191 sleepMilliSec = logWatchDogTimeout % 1000;
2192 usleep(sleepMilliSec * 1000);
2195 pthread_testcancel();
2208 if (replicatedMsgCount > 0) {
2210 message.description[
sprintf(message.description,
2211 "Log message repeated %d times: ", replicatedMsgCount)] = 0;
2213 tempLength = strlen(message.description);
2214 if ((strlen(lastMessage.description) + tempLength) >=
2215 MSG_DESCRIPTION_SIZE) {
2218 strncpy((message.description + tempLength), lastMessage.description,
2219 (MSG_DESCRIPTION_SIZE - tempLength));
2220 message.description[MSG_DESCRIPTION_SIZE - 1] = 0;
2223 strcpy((message.description + tempLength),
2224 lastMessage.description);
2225 message.description[strlen(lastMessage.description) +
2226 tempLength - 1] = 0;
2230 now = localtime(&timeVal);
2231 message.date[strftime(message.date, MSG_DATE_SIZE,
"%Y-%m-%d %H:%M:%S",
2237 replicatedMsgCount = 0;
2244 IssueStruct* issueParam = (IssueStruct*) threadParam;
2247 status = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
2250 printf(
"Set cancel state error: %d\n", status);
2254 "Unable to configure issue thread properly. Execution might eventually be affected.", 0);
2257 status = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
2260 printf(
"Set cancel type error error: %d\n", status);
2264 "Unable to configure issue thread properly. Execution might eventually be affected.", 0);
2268 issueParam->nRet = issue(issueParam->command, &(issueParam->result), &(issueParam->size));
2271 status = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, 0);
2274 printf(
"Set cancel type error: %d\n", status);
2278 "Unable to configure issue thread properly. Execution might eventually be affected.", 0);
2282 status = pthread_mutex_lock(&wait_mut);
2285 printf(
"Lock cond mutex error: %d\n", status);
2289 "Unable to lock condition mutex for watchdog in issue thread. Execution might eventually be affected.",
2295 pthread_cond_broadcast(&cond);
2298 status = pthread_mutex_unlock(&wait_mut);
2301 printf(
"Unlock cond mutex error: %d\n", status);
2305 "Unable to unlock condition mutex for watchdog in issue thread. Execution might eventually be affected.",
2316 bool checksumFlag,
int checksum) {
2318 FlagBits flags = NO_FLAGS;
2322 flags |= HUFFMAN_FLAG;
2326 flags |= CHECKSUM_FLAG;
2329 pHeader = (
char*)
malloc(HEADER_SIZE);
2333 printf(
"no memory available while trying to create header!\n");
2337 "No more memory available, unable to continue serving - exiting.",
2343 memcpy(pHeader, &
id, HEADER_SIZE_ID);
2344 memcpy(pHeader + HEADER_OFFSET_ID, &errorCode, HEADER_SIZE_ERROR_CODE);
2345 memcpy(pHeader + HEADER_OFFSET_ERROR_CODE, &flags, HEADER_SIZE_FLAGS);
2346 memcpy(pHeader + HEADER_OFFSET_FLAGS, &checksum, HEADER_SIZE_CHECKSUM);
2353 unsigned int payloadChecksum = 0;
2357 if (((payload == 0) && (size > 0)) || (size < 0)) {
2363 printf(
"\nReceived Checksum: \t%x , \nCalculated Checksum: \t%x .\n\n",
2364 checksum, payloadChecksum);
2367 return (payloadChecksum == checksum) ?
true :
false;
2374 unsigned int checks = 0;
2375 unsigned long adler = 1L;
2376 unsigned long part1 = adler & 0xffff;
2377 unsigned long part2 = (adler >> 16) & 0xffff;
2380 for (n = 0; n <
size; n++) {
2381 part1 = (part1 + buffer[n]) % ADLER_BASE;
2382 part2 = (part2 + part1) % ADLER_BASE;
2384 checks = (
unsigned int) ((part2 << 16) + part1);
2390 char* tempHeader = 0;
2392 tempHeader = (
char*)
malloc(HEADER_SIZE);
2393 if (tempHeader == 0) {
2396 printf(
"no memory available!\n");
2400 "No more memory available, unable to continue serving - exiting.",
2406 memcpy(tempHeader, &(pHeader->id), HEADER_SIZE_ID);
2407 memcpy(tempHeader + HEADER_OFFSET_ID, &(pHeader->errorCode), HEADER_SIZE_ERROR_CODE);
2408 memcpy(tempHeader + HEADER_OFFSET_ERROR_CODE, &(pHeader->flags), HEADER_SIZE_FLAGS);
2409 memcpy(tempHeader + HEADER_OFFSET_FLAGS, &(pHeader->checksum), HEADER_SIZE_CHECKSUM);
2418 #ifdef ENABLE_MASTERMODE 2423 if ((*issueParam).size == 0) {
2428 printf(
"Received update command, updating FeeServer now!\n");
2432 fp = fopen(
"newFeeserver",
"w+b");
2438 for ( i = 0; i < (*issueParam).size; ++
i) {
2439 fputc((*issueParam).command[i], fp);
2449 status = pthread_mutex_unlock(&command_mut);
2452 printf(
"Unlock FeeCommand mutex error: %d\n", status);
2460 #endif //ENABLE_MASTERMODE 2472 printf(
"Triggering restart with exit value: %d\n", exitVal);
2475 msg[
sprintf(msg,
"Restarting FeeServer - exit value: %d ", exitVal)] = 0;
2483 status = pthread_mutex_unlock(&command_mut);
2486 printf(
"Unlock FeeCommand mutex error: %d\n", status);
2496 float newDeadband = 0;
2499 IntItemNode* intNode = 0;
2501 unsigned int count = 0;
2503 if ((*issueParam).size <=
sizeof(newDeadband)) {
2504 (*issueParam).size = 0;
2506 "FeeServer command for setting dead band contained invalid parameter.",
2508 return FEE_INVALID_PARAM;
2511 nameLength = (*issueParam).size -
sizeof(newDeadband);
2513 if (nameLength <= 0) {
2514 (*issueParam).size = 0;
2516 "FeeServer command for setting dead band contained no service name.",
2518 return FEE_INVALID_PARAM;
2521 itemName = (
char*)
malloc(nameLength + 1);
2522 if (itemName == 0) {
2523 (*issueParam).size = 0;
2524 return FEE_INSUFFICIENT_MEMORY;
2527 memcpy(&newDeadband, (*issueParam).command,
sizeof(newDeadband));
2528 memcpy(itemName, (*issueParam).command +
sizeof(newDeadband), nameLength);
2529 itemName[nameLength] = 0;
2531 if (itemName[0] !=
'*') {
2538 if ((node == 0) && (intNode == 0)) {
2540 msg[
sprintf(msg,
"Item %s not found in list.", itemName)] = 0;
2543 printf(
"Item %s not found in list.\n", itemName);
2548 (*issueParam).size = 0;
2550 "FeeServer command for setting dead band contained invalid parameter.",
2552 return FEE_INVALID_PARAM;
2556 node->threshold = newDeadband / 2;
2558 intNode->threshold = newDeadband / 2;
2561 printf(
"Set deadband on item %s to %f.\n", itemName, newDeadband);
2564 msg[
sprintf(msg,
"New dead band (%f) is set for item %s.",
2565 newDeadband, itemName)] = 0;
2573 printf(
"Set deadband for %d items (%s) to %f.\n", count, itemName, newDeadband);
2576 msg[
sprintf(msg,
"New dead band (%f) is set for %d items (%s).",
2577 newDeadband, count, itemName)] = 0;
2582 (*issueParam).size = 0;
2590 IntItemNode* intNode = 0;
2591 float currentDeadband = 0;
2594 if ((*issueParam).size <= 0) {
2595 (*issueParam).size = 0;
2597 "FeeServer command for getting dead band contained invalid parameter.",
2599 return FEE_INVALID_PARAM;
2602 nameLength = (*issueParam).size;
2603 itemName = (
char*)
malloc(nameLength + 1);
2604 if (itemName == 0) {
2605 (*issueParam).size = 0;
2606 return FEE_INSUFFICIENT_MEMORY;
2609 memcpy(itemName, (*issueParam).command, nameLength);
2610 itemName[nameLength] = 0;
2618 if ((node == 0) && (intNode == 0)) {
2620 msg[
sprintf(msg,
"Item %s not found in list.", itemName)] = 0;
2623 printf(
"Item %s not found in list.\n", itemName);
2628 (*issueParam).size = 0;
2630 "FeeServer command for getting dead band contained invalid parameter.",
2632 return FEE_INVALID_PARAM;
2634 (*issueParam).result = (
char*)
malloc(
sizeof(
float) + nameLength);
2635 if ((*issueParam).result == 0) {
2636 (*issueParam).size = 0;
2637 return FEE_INSUFFICIENT_MEMORY;
2642 currentDeadband = node->threshold * 2.0;
2644 currentDeadband = intNode->threshold * 2.0;
2647 memcpy((*issueParam).result, ¤tDeadband,
sizeof(
float));
2648 memcpy((*issueParam).result +
sizeof(
float), itemName, nameLength);
2649 (*issueParam).size =
sizeof(float) + nameLength;
2651 printf(
"Current deadband on item %s is %f.\n", itemName, currentDeadband);
2654 msg[
sprintf(msg,
"Current deadband for item %s is %f.", itemName,
2655 currentDeadband)] = 0;
2664 unsigned long newTimeout;
2666 if ((*issueParam).size <
sizeof(
unsigned long)) {
2667 (*issueParam).size = 0;
2669 "FeeServer command for setting issue timeout contained invalid parameter.",
2671 return FEE_INVALID_PARAM;
2673 memcpy(&newTimeout, (*issueParam).command,
sizeof(
unsigned long));
2677 if (newTimeout > MAX_ISSUE_TIMEOUT) {
2678 (*issueParam).size = 0;
2680 "New timeout for issue watchdog exceeded value limit (4294967).",
2682 return FEE_INVALID_PARAM;
2685 issueTimeout = newTimeout;
2687 printf(
"set new Issue timeout to %lu\n", issueTimeout);
2690 msg[
sprintf(msg,
"Watch dog time out is set to %lu.", issueTimeout)] = 0;
2693 (*issueParam).size = 0;
2700 (*issueParam).result = (
char*)
malloc(
sizeof(
unsigned long));
2701 if ((*issueParam).result == 0) {
2702 (*issueParam).size = 0;
2703 return FEE_INSUFFICIENT_MEMORY;
2707 memcpy((*issueParam).result, &issueTimeout,
sizeof(
unsigned long));
2708 (*issueParam).size =
sizeof(
unsigned long);
2710 printf(
"issue timeout is %lu\n", issueTimeout);
2713 msg[
sprintf(msg,
"Issue timeout is %lu.", issueTimeout)] = 0;
2721 unsigned short newRate;
2723 if ((*issueParam).size <
sizeof(
unsigned short)) {
2724 (*issueParam).size = 0;
2726 "FeeServer command for setting update rate contained invalid parameter.",
2728 return FEE_INVALID_PARAM;
2730 memcpy(&newRate, (*issueParam).command,
sizeof(
unsigned short));
2731 updateRate = newRate;
2735 printf(
"set new update rate to %d\n", updateRate);
2738 msg[
sprintf(msg,
"New update rate for monitoring items: %d.", updateRate)] = 0;
2741 (*issueParam).size = 0;
2748 (*issueParam).result = (
char*)
malloc(
sizeof(
unsigned short));
2749 if ((*issueParam).result == 0) {
2750 (*issueParam).size = 0;
2751 return FEE_INSUFFICIENT_MEMORY;
2755 memcpy((*issueParam).result, &updateRate,
sizeof(
unsigned short));
2756 (*issueParam).size =
sizeof(
unsigned short);
2758 printf(
"update rate is %d\n", updateRate);
2761 msg[
sprintf(msg,
"Monitoring update rate is %d.", updateRate)] = 0;
2771 unsigned int testLogLevel = 0;
2773 if ((*issueParam).size <
sizeof(
unsigned int)) {
2774 (*issueParam).size = 0;
2776 "FeeServer command for setting log level contained invalid parameter.",
2778 return FEE_INVALID_PARAM;
2780 memcpy(&testLogLevel, (*issueParam).command,
sizeof(
unsigned int));
2782 if (testLogLevel > MSG_MAX_VAL) {
2784 printf(
"received invalid log level %d\n", testLogLevel);
2788 "FeeServer command for setting log level contained invalid parameter.",
2790 return FEE_INVALID_PARAM;
2793 status = pthread_mutex_lock(&log_mut);
2797 printf(
"Lock log mutex error: %d\n", status);
2800 (*issueParam).size = 0;
2803 logLevel = testLogLevel | MSG_ALARM;
2805 status = pthread_mutex_unlock(&log_mut);
2808 printf(
"Unlock log mutex error: %d\n", status);
2815 printf(
"set new logLevel to %d\n", logLevel);
2818 msg[
sprintf(msg,
"New log level on FeeServer: %d.", logLevel)] = 0;
2821 (*issueParam).size = 0;
2829 (*issueParam).result = (
char*)
malloc(
sizeof(
unsigned int));
2830 if ((*issueParam).result == 0) {
2831 (*issueParam).size = 0;
2832 return FEE_INSUFFICIENT_MEMORY;
2836 memcpy((*issueParam).result, &logLevel,
sizeof(
unsigned int));
2837 (*issueParam).size =
sizeof(
unsigned int);
2839 printf(
"Requested loglevel is %d\n", logLevel);
2842 msg[
sprintf(msg,
"Requested LogLevel is %d.", logLevel)] = 0;
2849 FeeProperty feeProp;
2850 if ((ceInitState == CE_OK) && ((nodesAmount > 0) || (intNodesAmount > 0))) {
2851 feeProp.flag = PROPERTY_UPDATE_RATE;
2853 signalFeePropertyChanged(&feeProp);
2859 unsigned int count = 0;
2860 ItemNode* current = 0;
2861 IntItemNode* intCurrent = 0;
2863 char* itemNamePart = 0;
2870 namePart = strpbrk(name,
"_");
2871 if (namePart == 0) {
2877 while (current != 0) {
2879 itemNamePart = strpbrk(current->item->name,
"_");
2881 if ((itemNamePart == 0) || (itemNamePart == current->item->name)) {
2882 current = current->next;
2885 if (strcmp(namePart, itemNamePart) == 0) {
2887 current->threshold = newDeadbandBC / 2;
2890 current = current->next;
2895 while (intCurrent != 0) {
2897 itemNamePart = strpbrk(intCurrent->intItem->name,
"_");
2899 if ((itemNamePart == 0) || (itemNamePart == intCurrent->intItem->name)) {
2900 intCurrent = intCurrent->next;
2903 if (strcmp(namePart, itemNamePart) == 0) {
2905 intCurrent->threshold = newDeadbandBC / 2;
2908 intCurrent = intCurrent->next;
2917 IntItemNode* intNode = 0;
2918 CharItemNode* charNode = 0;
2926 if (state != RUNNING) {
2927 return FEE_WRONG_STATE;
2929 if (serviceName == 0) {
2930 return FEE_NULLPOINTER;
2939 if ((node == 0) || (intNode == 0)) {
2945 if ((node == 0) && (intNode == 0) && (charNode == 0)) {
2947 msg[
sprintf(msg,
"Item %s not found in list.", serviceName)] = 0;
2950 printf(
"Item %s not found in list.\n", serviceName);
2953 return FEE_INVALID_PARAM;
2962 }
else if (intNode != 0) {
2972 (charNode->charItem->user_routine)(&(charNode->charItem->tag),
2973 (
int**) &addr, &size);
2974 data = (
char*)
malloc(size +1);
2975 strncpy(data, addr, size);
2995 issueStr->command = 0;
2996 issueStr->result = 0;
3005 message.eventType = MSG_INFO;
3006 memcpy(message.detector, LOCAL_DETECTOR, MSG_DETECTOR_SIZE);
3007 memcpy(message.source,
"FeeServer\0", 10);
3008 message.description[
sprintf(message.description,
3009 "FeeServer %s (Version: %s) has been initialized (DNS: %s) ...",
3010 serverName, FEESERVER_VERSION, getenv(
"DIM_DNS_NODE"))] = 0;
3013 now = localtime(&timeVal);
3014 message.date[strftime(message.date, MSG_DATE_SIZE,
"%Y-%m-%d %H:%M:%S",
3019 lastMessage.eventType = MSG_DEBUG;
3020 memcpy(lastMessage.detector, LOCAL_DETECTOR, MSG_DETECTOR_SIZE);
3021 memcpy(lastMessage.source,
"FeeServer\0", 10);
3023 lastMessage.description[
sprintf(lastMessage.description,
3024 "Init of Backup Message Struct!")] = 0;
3027 now = localtime(&timeVal);
3028 lastMessage.date[strftime(lastMessage.date, MSG_DATE_SIZE,
3029 "%Y-%m-%d %H:%M:%S", now)] = 0;
3038 iNode->lastTransmittedValue = 0.0;
3039 iNode->threshold = 0.0;
3040 iNode->locBackup = 0;
3041 iNode->checksum = 0;
3042 iNode->checksumBackup = 0;
3047 ItemNode* current = 0;
3053 while (current != 0) {
3054 if (strcmp(name, current->item->name) == 0) {
3058 current = current->next;
3077 ItemNode* current = 0;
3080 while (current != 0) {
3082 current = current->next;
3097 printf(
"Received interrupt: %d, exiting now.\n", sig);
3101 if ((state == RUNNING) || (state == ERROR_STATE)) {
3113 printf(
"Exit state: %d\n\n", state);
3116 msg[
sprintf(msg,
"Exiting FeeServer (exit state: %d).", state)] = 0;
3125 char clientName[50];
3138 if (clientName[0] == 0) {
3140 printf(
"Framework tries to exit FeeServer (%d)\n", *bufp);
3141 printf(
"Most likely FeeServer name already exists.\n");
3149 msg[
sprintf(msg,
"Ambitious user (%s) tried to kill FeeServer, ignoring command!",
3153 printf(
"Ambitious user (%s) tried to kill FeeServer (%d)\n", clientName, *bufp);
3162 printf(
"Cleaning up FeeServer before finishing:\n");
3165 if (ceInitState == CE_OK) {
3168 printf(
" - Clean up of CE finished\n");
3173 if (monitorThreadStarted) {
3174 pthread_cancel(thread_mon);
3176 if (intMonitorThreadStarted) {
3177 pthread_cancel(thread_mon_int);
3179 if (state == RUNNING) {
3180 pthread_cancel(thread_init);
3182 if (logWatchDogRunning) {
3183 pthread_cancel(thread_logWatchdog);
3186 printf(
" - All threads except for main thread killed\n");
3192 printf(
" - DIM server stopped\n");
3205 if (serverName != 0) {
3212 printf(
" - Memory freed (lists and globaly allocated)\n");
3220 while (firstNode != 0) {
3221 if (firstNode->item != 0) {
3222 if (firstNode->item->name != 0) {
3223 free(firstNode->item->name);
3225 free(firstNode->item);
3228 tmp = firstNode->next;
3251 char* serviceName = 0;
3254 if (state != COLLECTING) {
3255 return FEE_WRONG_STATE;
3262 printf(
"Bad intItem, not published\n");
3265 return FEE_NULLPOINTER;
3267 if (intItem->name == 0 || intItem->location == 0) {
3269 printf(
"Bad intItem, not published\n");
3272 return FEE_NULLPOINTER;
3277 if (
findItem(intItem->name) != 0) {
3279 printf(
"Item name already published in float list, int item discarded.\n");
3282 return FEE_ITEM_NAME_EXISTS;
3287 printf(
"Item name already published in int list, new int item discarded.\n");
3290 return FEE_ITEM_NAME_EXISTS;
3295 printf(
"Item name already published in char list, int item discarded.\n");
3298 return FEE_ITEM_NAME_EXISTS;
3303 serviceName = (
char*)
malloc(serverNameLength + strlen(intItem->name) + 2);
3304 if (serviceName == 0) {
3305 return FEE_INSUFFICIENT_MEMORY;
3308 serviceName[
sprintf(serviceName,
"%s_%s", serverName, intItem->name)] = 0;
3319 IntItemNode* newNode = 0;
3321 newNode = (IntItemNode*)
malloc(
sizeof(IntItemNode));
3325 printf(
"no memory available while adding IntItemNode!\n");
3335 newNode->intItem = _int_item;
3336 newNode->lastTransmittedIntValue = *(_int_item->location);
3338 newNode->threshold = (_int_item->defaultDeadband < 0) ? 0.0 : (_int_item->defaultDeadband / 2);
3340 newNode->locBackup = _int_item->location;
3360 printf(
"init of %s (int) with ID %d: %d\n", newNode->intItem->name,
3361 newNode->id, newNode->lastTransmittedIntValue);
3367 if (firstIntNode != 0) {
3368 lastIntNode->next = newNode;
3370 lastIntNode = newNode;
3372 firstIntNode = newNode;
3373 lastIntNode = newNode;
3380 IntItemNode* current = 0;
3386 while (current != 0) {
3387 if (strcmp(name, current->intItem->name) == 0) {
3391 current = current->next;
3410 IntItemNode* tmp = 0;
3412 while (firstIntNode != 0) {
3413 if (firstIntNode->intItem != 0) {
3414 if (firstIntNode->intItem->name != 0) {
3415 free(firstIntNode->intItem->name);
3417 free(firstIntNode->intItem);
3420 tmp = firstIntNode->next;
3428 intItemNode->prev = 0;
3429 intItemNode->next = 0;
3430 intItemNode->id = 0;
3431 intItemNode->intItem = 0;
3432 intItemNode->lastTransmittedIntValue = 0;
3433 intItemNode->threshold = 0.0;
3434 intItemNode->locBackup = 0;
3435 intItemNode->checksum = 0;
3436 intItemNode->checksumBackup = 0;
3440 IntItemNode* current = 0;
3443 while (current != 0) {
3445 current = current->next;
3457 item = (Item*)
malloc(
sizeof(Item));
3461 printf(
"no memory available!\n");
3467 item->defaultDeadband = 0;
3472 IntItem* intItem = 0;
3474 intItem = (IntItem*)
malloc(
sizeof(IntItem));
3478 printf(
"no memory available!\n");
3482 intItem->location = 0;
3484 intItem->defaultDeadband = 0;
3488 Item*
fillItem(
float* floatLocation,
char* itemName,
float defDeadband) {
3495 printf(
"no memory available!\n");
3499 item->location = floatLocation;
3500 item->name = (
char*)
malloc(strlen(itemName) + 1);
3501 if (item->name == 0) {
3504 printf(
"no memory available!\n");
3509 strcpy(item->name, itemName);
3510 item->defaultDeadband = defDeadband;
3514 IntItem*
fillIntItem(
int* intLocation,
char* itemName,
int defDeadband) {
3515 IntItem* intItem = 0;
3521 printf(
"no memory available!\n");
3525 intItem->location = intLocation;
3526 intItem->name = (
char*)
malloc(strlen(itemName) + 1);
3527 if (intItem->name == 0) {
3530 printf(
"no memory available!\n");
3535 strcpy(intItem->name, itemName);
3536 intItem->defaultDeadband = defDeadband;
3543 unsigned long sleepTime = 0;
3544 IntItemNode* current = 0;
3546 unsigned long innerCounter = 0;
3547 unsigned long outerCounter = 0;
3550 intMonitorThreadStarted =
true;
3553 status = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
3556 printf(
"Set cancel state error [mon - int]: %d\n", status);
3560 "Unable to configure monitor thread (int) properly. Monitoring is not affected.", 0);
3562 status = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
3565 printf(
"Set cancel type error [mon - int]: %d\n", status);
3569 "Unable to configure monitor thread (int) properly. Monitoring is not affected.", 0);
3572 createLogMessage(MSG_DEBUG,
"Started monitor thread for INT values successfully.", 0);
3576 sleepTime = (
unsigned long) (updateRate / intNodesAmount);
3577 while (current != 0) {
3579 msg[
sprintf(msg,
"Value of item %s (int) is corrupt, reconstruction failed. Ignoring!",
3580 current->intItem->name)] = 0;
3585 if ((abs((*(current->intItem->location)) - current->lastTransmittedIntValue)
3586 >= current->threshold) || (outerCounter ==
3587 (innerCounter * TIME_INTERVAL_MULTIPLIER))) {
3589 current->lastTransmittedIntValue = *(current->intItem->location);
3591 printf(
"CE triggered an updated on %d clients for service %s [int]: %d\n",
3592 nRet, current->intItem->name, *(current->intItem->location));
3599 current = current->next;
3600 usleep(sleepTime * 1000);
3610 if (outerCounter >= (intNodesAmount * TIME_INTERVAL_MULTIPLIER)) {
3620 if (node->intItem->location == node->locBackup) {
3627 &(node->intItem->location),
sizeof(
volatile int*))) {
3629 node->locBackup = node->intItem->location;
3635 &(node->locBackup),
sizeof(
volatile int*))) {
3637 node->intItem->location = node->locBackup;
3642 if (node->checksum == node->checksumBackup) {
3651 &(node->intItem->location),
sizeof(
volatile int*))) {
3653 node->locBackup = node->intItem->location;
3655 node->checksum = node->checksumBackup;
3661 &(node->locBackup),
sizeof(
volatile int*))) {
3663 node->intItem->location = node->locBackup;
3665 node->checksum = node->checksumBackup;
3684 FloatItem*
fillFloatItem(
float* floatLocation,
char* itemName,
float defDeadband) {
3685 return fillItem(floatLocation, itemName, defDeadband);
3692 MemoryNode* current = 0;
3707 while (current != 0) {
3708 if (current->identityAddr == addr) {
3712 current = current->next;
3733 unsigned int preSize) {
3736 if (size <= preSize) {
3738 "Error: asking for memory smaller or equal than its prefix size.",
3741 printf(
"Error: asking for memory smaller or equal than its prefix size.\n");
3747 MemoryNode* memNode = (MemoryNode*)
malloc(
sizeof(MemoryNode));
3751 "Insufficient memory! Unable to allocate memory for MemoryNode.",
3754 printf(
"Insufficient memory! Unable to allocate memory for MemoryNode.\n");
3760 void* ptr = (
void*)
malloc(size);
3765 "Insufficient memory! Unable to allocate memory block of %d .",
3769 printf(
"Insufficient memory! Unable to allocate memory block of %d .\n",
3779 memNode->identityAddr = ptr + preSize;
3780 memNode->mmData.memSize =
size;
3781 memNode->mmData.memType =
type;
3783 strncpy(memNode->mmData.memDest, module, 30);
3784 memNode->mmData.memDest[29] = 0;
3786 memNode->mmData.memDest[0] = 0;
3789 memNode->mmData.prefixed =
true;
3791 memNode->mmData.prefixed =
false;
3793 memNode->mmData.prefixSize = preSize;
3799 if (lastMemoryNode != 0) {
3800 lastMemoryNode->next = memNode;
3802 lastMemoryNode = memNode;
3804 if (firstMemoryNode == 0) {
3805 firstMemoryNode = memNode;
3818 if (node->ptr != 0) {
3823 if (node->next != 0) {
3824 node->next->prev = node->prev;
3826 lastMemoryNode = node->prev;
3829 if (node->prev != 0) {
3830 node->prev->next = node->next;
3832 firstMemoryNode = node->next;
3842 while (current != 0) {
3843 MemoryNode* nextMemNode = current->next;
3845 current = nextMemNode;
3853 char prefixPurpose,
void** ptr) {
3854 MemoryNode* memNode = 0;
3855 unsigned int realSize = 0;
3856 unsigned int addSize = 0;
3862 "FeeServer shall allocate memory of size 0; discarding call!", 0);
3864 printf(
"FeeServer shall allocate memory of size 0; discarding call!\n");
3867 return FEE_INVALID_PARAM;
3870 switch (prefixPurpose) {
3876 realSize = size + HEADER_SIZE;
3877 addSize = HEADER_SIZE;
3882 "Received allocateMemory call with unknown prefix purpose ('%c'), discarding call.",
3883 prefixPurpose)] = 0;
3886 printf(
"%s\n", msg);
3889 return FEE_INVALID_PARAM;
3897 *ptr = memNode->identityAddr;
3913 MemoryNode* memNode = 0;
3917 "Received an NULL pointer for freeing memory, discarding call!", 0);
3919 printf(
"Received an NULL pointer for freeing memory, discarding call! \n");
3922 return FEE_NULLPOINTER;
3928 return FEE_INVALID_PARAM;
3940 char* serviceName = 0;
3943 if (state != COLLECTING) {
3944 return FEE_WRONG_STATE;
3949 if (charItem == 0) {
3951 printf(
"Bad charItem, not published\n");
3954 return FEE_NULLPOINTER;
3956 if (charItem->name == 0 || charItem->user_routine == 0) {
3958 printf(
"Bad charItem, not published\n");
3961 return FEE_NULLPOINTER;
3966 if (
findItem(charItem->name) != 0) {
3968 printf(
"Item name already published in float list, char item discarded.\n");
3971 return FEE_ITEM_NAME_EXISTS;
3976 printf(
"Item name already published in int list, char item discarded.\n");
3979 return FEE_ITEM_NAME_EXISTS;
3984 printf(
"Item name already published in char list, new char item discarded.\n");
3987 return FEE_ITEM_NAME_EXISTS;
3991 serviceName = (
char*)
malloc(serverNameLength + strlen(charItem->name) + 2);
3992 if (serviceName == 0) {
3993 return FEE_INSUFFICIENT_MEMORY;
3996 serviceName[
sprintf(serviceName,
"%s_%s", serverName, charItem->name)] = 0;
4010 CharItem* charItem = 0;
4012 charItem = (CharItem*)
malloc(
sizeof(CharItem));
4013 if (charItem == 0) {
4016 printf(
"no memory available for CharItem!\n");
4020 charItem->user_routine = 0;
4028 CharItem*
fillCharItem(
void (*funcPointer)(
long*,
int**,
int*),
char* itemName,
4030 CharItem* charItem = 0;
4033 if (charItem == 0) {
4036 printf(
"no memory available for CharItem!\n");
4040 charItem->user_routine = funcPointer;
4041 charItem->name = (
char*)
malloc(strlen(itemName) + 1);
4042 if (charItem->name == 0) {
4045 printf(
"no memory available!\n");
4050 strcpy(charItem->name, itemName);
4051 charItem->tag = tag;
4058 CharItemNode* newNode = 0;
4060 newNode = (CharItemNode*)
malloc(
sizeof(CharItemNode));
4064 printf(
"no memory available while adding CharItemNode!\n");
4074 newNode->charItem = _char_item;
4088 printf(
"init of %s (char) with ID %d; user_routine at: %p\n",
4089 newNode->charItem->name, newNode->id,
4090 newNode->charItem->user_routine);
4096 if (firstCharNode != 0) {
4097 lastCharNode->next = newNode;
4099 lastCharNode = newNode;
4101 firstCharNode = newNode;
4102 lastCharNode = newNode;
4109 CharItemNode* current = 0;
4115 while (current != 0) {
4116 if (strcmp(name, current->charItem->name) == 0) {
4120 current = current->next;
4140 CharItemNode* tmp = 0;
4142 while (firstCharNode != 0) {
4143 if (firstCharNode->charItem != 0) {
4144 if (firstCharNode->charItem->name != 0) {
4145 free(firstCharNode->charItem->name);
4147 free(firstCharNode->charItem);
4150 tmp = firstCharNode->next;
4151 free(firstCharNode);
4152 firstCharNode = tmp;
4159 charItemNode->prev = 0;
4160 charItemNode->next = 0;
4161 charItemNode->id = 0;
4162 charItemNode->charItem = 0;
4167 CharItemNode* current = 0;
4170 while (current != 0) {
4172 current = current->next;
4177 charNodesAmount = 0;
4186 bool retVal =
false;
4187 if (state != INITIALIZING) {
4189 "Trying to change a FeeProperty during wrong FeeServer state, ignoring ...",
4192 printf(
"Trying to change a FeeProperty during wrong FeeServer state, ignoring ...\n");
4200 printf(
"Received NULL pointer in setting FeeProperty, ignoring...\n");
4207 switch (prop->flag) {
4208 case (PROPERTY_UPDATE_RATE):
4209 if (prop->uShortVal > 0) {
4210 updateRate = prop->uShortVal;
4213 printf(
"FeeProperty changed: new update rate (%d).\n",
4219 printf(
"Received invalid value for setting update rate (%d), ignoring...\n",
4226 case (PROPERTY_LOGWATCHDOG_TIMEOUT):
4227 if (prop->uIntVal > 0) {
4228 logWatchDogTimeout = prop->uIntVal;
4231 printf(
"FeeProperty changed: new log watchdog timeout (%d).\n",
4232 logWatchDogTimeout);
4237 printf(
"Received invalid value for setting log watchdog timeout (%d), ignoring...\n",
4244 case (PROPERTY_ISSUE_TIMEOUT):
4245 if (prop->uLongVal > 0) {
4246 issueTimeout = prop->uLongVal;
4249 printf(
"FeeProperty changed: new issue timeout (%ld).\n",
4255 printf(
"Received invalid value for setting issue timeout (%ld), ignoring...\n",
4262 case (PROPERTY_LOGLEVEL):
4263 if ((prop->uIntVal > 0) && (prop->uIntVal <= MSG_MAX_VAL)) {
4264 logLevel = prop->uIntVal | MSG_ALARM;
4267 printf(
"FeeProperty changed: new log level (%d).\n", logLevel);
4272 printf(
"Received invalid value for setting loglevel (%d), ignoring...\n",
4282 printf(
"Received unknown flag in setting FeeProperty (%d), ignoring...\n",
4300 struct timeval tStamp;
4301 char benchmark_msg[200];
4305 status = gettimeofday(&tStamp, 0);
4307 benchmark_msg[
sprintf(benchmark_msg,
4308 "Unable to get timestamp for benchmark!")] = 0;
4310 today = localtime(&(tStamp.tv_sec));
4311 benchmark_msg[
sprintf(benchmark_msg,
4312 "%s: \t%.8s - %ld us", msg, asctime(today) + 11,
4313 tStamp.tv_usec)] = 0;
4316 if (getenv(
"FEE_BENCHMARK_FILENAME")) {
4317 pFile = fopen(getenv(
"FEE_BENCHMARK_FILENAME"),
"a+");
4319 fprintf(pFile, benchmark_msg);
4320 fprintf(pFile,
"\n");
4324 printf(
"Unable to open benchmark file.\n");
4325 printf(
"%s\n", benchmark_msg);
4349 if ((data == 0) || (size == 0)) {
4353 for (i = start; (i <
size) || (iBackUp < size); ++
i) {
4355 printf(
"%c", data[i]);
4357 printf(
"\nData - Size: %d", size);
4366 const int getState() {
4370 const ItemNode* getFirstNode() {
4374 const ItemNode* getLastNode() {
4389 const char* getCmndACK() {
4393 void setState(
int newState) {
4397 void setCmndACK(
char* newData) {
4401 void setCmndACKSize(
int size) {
4405 void setServerName(
char* name) {
4406 if (serverName != 0) {
4409 serverName = (
char*)
malloc(strlen(name) + 1);
4410 if (serverName == 0) {
4411 printf(
" No memory available !\n");
4413 strcpy(serverName, name);
4416 void clearServerName() {
4417 if (serverName != 0) {
4428 pthread_cond_t* getInitCondPtr() {
4433 pthread_mutex_t* getWaitInitMutPtr() {
4441 --------------------------------------------------------------------------------
4442 for assistance refer to the CVS howto. Maintained by Matthias Richter Powered by
bool checkIntLocation(IntItemNode *node)
Return to feeserver c CVS log Up to[MAIN] dcscvs FeeServer feeserver src Wed FeeServer_v0 v0 FeeServer_v0 v0 FeeServer_v0 v0 FeeServer_v0 v0 FeeServer_v0 v0 FeeServer_v0 v0
unsigned int calculateChecksum(unsigned char *buffer, int size)
static MemoryNode * lastMemoryNode
ItemNode * findItem(char *name)
#define dis_remove_service
unsigned int setDeadbandBroadcast(char *name, float newDeadbandBC)
void initIntItemNode(IntItemNode *intItemNode)
static unsigned short updateRate
void add_item_node(unsigned int _id, Item *_item)
static bool ceReadySignaled
static unsigned int charNodesAmount
-— NEW FEATURE SINCE VERSION 0.8.2b [Char Channel] (2007-07-28) –— ///
int setUpdateRate(IssueStruct *issueParam)
static MessageStruct message
int freeMemory(void *addr)
static IntItemNode * firstIntNode
static pthread_t thread_mon_int
int publishChar(CharItem *charItem)
— NEW FEATURE SINCE VERSION 0.8.2b [Char channel] (2007-07-28) — ///
void add_char_item_node(unsigned int _id, CharItem *_char_item)
FloatItem * createFloatItem()
int publishFloat(FloatItem *floatItem)
void command_handler(int *tag, char *address, int *size)
CharItemNode * findCharItem(char *name)
void dim_dummy_exit_handler(int *bufp)
void fee_exit_handler(unsigned int state)
bool setFeeProperty(FeeProperty *prop)
– NEW FEATURE V. 0.9.1 [Set FeeServer Properies in CE] (2007-08-17) – ///
char * createHeader(unsigned int id, short errorCode, bool huffmanFlag, bool checksumFlag, int checksum)
void signalCEready(int ceState)
static bool intMonitorThreadStarted
void initIssueStruct(IssueStruct *issueStr)
char * mapDimErrorCodes(int errorCode)
int main(int argc, char **arg)
bool checkLogLevel(int event)
static unsigned int intNodesAmount
void threadInitializeCE()
int setLogLevel(IssueStruct *issueParam)
static unsigned int serviceACKID
MemoryNode * findMemoryNode(void *addr)
CharItem * fillCharItem(void(*funcPointer)(long *, int **, int *), char *itemName, long tag)
static int serverNameLength
void updateFeeServer(IssueStruct *issueParam)
void dis_add_error_handler(void(*user_routine)())
unsigned int dtq_sleep(int secs)
void createLogMessage(unsigned int type, char *description, char *origin)
int getUpdateRate(IssueStruct *issueParam)
static pthread_t thread_logWatchdog
void unpublishCharItemList()
Return to feeserver c CVS log Up to[MAIN] dcscvs FeeServer feeserver src Wed FeeServer_v0 v0 FeeServer_v0 v0 FeeServer_v0 v0 FeeServer_v0 v0 FeeServer_v0 v0 FeeServer_v0 FeeServer_v0 v0 FeeServer_v0 v0 FeeServer_v0 v0 FeeServer_v0 v0 FeeServer_v0 v0 dev Changes since
void initCharItemNode(CharItemNode *charItemNode)
void initItemNode(ItemNode *iNode)
void dim_error_msg_handler(int severity, int error_code, char *msg)
static pthread_mutex_t wait_mut
bool checkLocation(ItemNode *node)
void add_int_item_node(unsigned int _id, IntItem *_int_item)
static pthread_mutex_t command_mut
void ack_service(int *tag, char **address, int *size)
bool checkReplicatedLogMessage()
static ItemNode * firstNode
int startLogWatchDogThread()
static CharItemNode * lastCharNode
static unsigned int logWatchDogTimeout
int updateFeeService(char *serviceName)
static pthread_t thread_mon
CharItem * createCharItem()
Return to feeserver c CVS log Up to[MAIN] dcscvs FeeServer feeserver src Wed May
static pthread_mutex_t log_mut
static IntItemNode * lastIntNode
#define dis_start_serving
#define dis_update_service
static unsigned int logLevel
int setIssueTimeout(IssueStruct *issueParam)
Warning because the service this data corrsponds to might have been last updated longer ago than Local time
static MemoryNode * firstMemoryNode
static pthread_cond_t cond
int getDeadband(IssueStruct *issueParam)
void freeMemoryNode(MemoryNode *node)
int setDeadband(IssueStruct *issueParam)
static pthread_cond_t init_cond
static pthread_mutex_t wait_init_mut
void createBenchmark(char *msg)
void unpublishIntItemList()
Item * fillItem(float *floatLocation, char *itemName, float defDeadband)
static pthread_t thread_init
static bool monitorThreadStarted
Return to feeserver c CVS log Up to[MAIN] dcscvs FeeServer feeserver src Wed FeeServer_v0 v0 dev
void triggerRestart(int exitVal)
void leaveCommandHandler(unsigned int id, short errorCode, unsigned int msgType, char *message)
#define dis_add_exit_handler
int publishInt(IntItem *intItem)
Return to feeserver c CVS log Up to[MAIN] dcscvs FeeServer feeserver src File
static unsigned int nodesAmount
MemoryNode * createMemoryNode(unsigned int size, char type, char *module, unsigned int preSize)
FloatItem * fillFloatItem(float *floatLocation, char *itemName, float defDeadband)
IntItem * createIntItem()
static unsigned int commandID
static unsigned long issueTimeout
IntItemNode * findIntItem(char *name)
void * threadIssue(void *threadParam)
static unsigned short replicatedMsgCount
bool checkCommand(char *payload, int size, unsigned int checksum)
static bool logWatchDogRunning
Return to feeserver c CVS log Up to[MAIN] dcscvs FeeServer feeserver src Wed HEAD
int allocateMemory(unsigned int size, char type, char *module, char prefixPurpose, void **ptr)
static MessageStruct lastMessage
sprintf(name1,"NewService%d", i)
static CharItemNode * firstCharNode
static unsigned int messageServiceID
char * marshallHeader(CommandHeader *pHeader)
int getIssueTimeout(IssueStruct *issueParam)
void interrupt_handler(int sig)
static ItemNode * lastNode
IntItem * fillIntItem(int *intLocation, char *itemName, int defDeadband)
int getLogLevel(IssueStruct *issueParam)