From 2cfed29565359411fd07a333cd5f25f7dd15b839 Mon Sep 17 00:00:00 2001 From: Ken Zangelin Date: Sun, 2 Jul 2023 09:47:51 +0200 Subject: [PATCH] Just to keep working on this in my other laptop, in Athens - doesn't even compile --- CMakeLists.txt | 2 + src/app/orionld/orionld.cpp | 4 + src/lib/orionld/pernot/CMakeLists.txt | 34 +++++ src/lib/orionld/pernot/PernotSubscription.h | 72 +++++++++ src/lib/orionld/pernot/pernotLoop.cpp | 153 ++++++++++++++++++++ src/lib/orionld/pernot/pernotLoop.h | 37 +++++ 6 files changed, 302 insertions(+) create mode 100644 src/lib/orionld/pernot/CMakeLists.txt create mode 100644 src/lib/orionld/pernot/PernotSubscription.h create mode 100644 src/lib/orionld/pernot/pernotLoop.cpp create mode 100644 src/lib/orionld/pernot/pernotLoop.h diff --git a/CMakeLists.txt b/CMakeLists.txt index fa986db3af..217c8c80f7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -208,6 +208,7 @@ SET (ORION_LIBS orionld_troe orionld_dbModel orionld_apiModel + orionld_pernot serviceRoutines serviceRoutinesV2 ngsiNotify @@ -366,6 +367,7 @@ if (error EQUAL 0) ADD_SUBDIRECTORY(src/lib/jsonParse) ADD_SUBDIRECTORY(src/lib/jsonParseV2) ADD_SUBDIRECTORY(src/lib/rest) + ADD_SUBDIRECTORY(src/lib/orionld/pernot) ADD_SUBDIRECTORY(src/lib/orionld/socketService) ADD_SUBDIRECTORY(src/lib/orionld/notifications) ADD_SUBDIRECTORY(src/lib/orionld/forwarding) diff --git a/src/app/orionld/orionld.cpp b/src/app/orionld/orionld.cpp index 3b18a4be03..f881de1619 100644 --- a/src/app/orionld/orionld.cpp +++ b/src/app/orionld/orionld.cpp @@ -129,6 +129,7 @@ extern "C" #include "orionld/regCache/regCacheInit.h" // regCacheInit #include "orionld/regCache/regCacheCreate.h" // regCacheCreate #include "orionld/regCache/regCacheRelease.h" // regCacheRelease +#include "orionld/pernot/pernotLoop.h" // pernotLoopStart #include "orionld/version.h" #include "orionld/orionRestServices.h" @@ -1315,6 +1316,9 @@ int main(int argC, char* argV[]) kaBufferReset(&orionldState.kalloc, KFALSE); + // Start the thread for periodic notifications + pernotLoopStart(); + if (socketService == true) { int fd; diff --git a/src/lib/orionld/pernot/CMakeLists.txt b/src/lib/orionld/pernot/CMakeLists.txt new file mode 100644 index 0000000000..6d92b4a5b2 --- /dev/null +++ b/src/lib/orionld/pernot/CMakeLists.txt @@ -0,0 +1,34 @@ +# Copyright 2023 FIWARE Foundation e.V. +# +# This file is part of Orion-LD Context Broker. +# +# Orion-LD Context Broker is free software: you can redistribute it and/or +# modify it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# Orion-LD Context Broker is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero +# General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with Orion-LD Context Broker. If not, see http://www.gnu.org/licenses/. +# +# For those usages not covered by this license please contact with +# orionld at fiware dot org + +CMAKE_MINIMUM_REQUIRED(VERSION 3.5) + +SET (SOURCES + pernotLoop.cpp +) + +# Include directories +# ----------------------------------------------------------------- +include_directories("${PROJECT_SOURCE_DIR}/src/lib") + + +# Library declaration +# ----------------------------------------------------------------- +ADD_LIBRARY(orionld_pernot STATIC ${SOURCES}) diff --git a/src/lib/orionld/pernot/PernotSubscription.h b/src/lib/orionld/pernot/PernotSubscription.h new file mode 100644 index 0000000000..4db3c7635d --- /dev/null +++ b/src/lib/orionld/pernot/PernotSubscription.h @@ -0,0 +1,72 @@ +#ifndef SRC_LIB_ORIONLD_PERNOT_PERNOTSUBSCRIPTION_H_ +#define SRC_LIB_ORIONLD_PERNOT_PERNOTSUBSCRIPTION_H_ + +/* +* +* Copyright 2023 FIWARE Foundation e.V. +* +* This file is part of Orion-LD Context Broker. +* +* Orion-LD Context Broker is free software: you can redistribute it and/or +* modify it under the terms of the GNU Affero General Public License as +* published by the Free Software Foundation, either version 3 of the +* License, or (at your option) any later version. +* +* Orion-LD Context Broker is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero +* General Public License for more details. +* +* You should have received a copy of the GNU Affero General Public License +* along with Orion-LD Context Broker. If not, see http://www.gnu.org/licenses/. +* +* For those usages not covered by this license please contact with +* orionld at fiware dot org +* +* Author: Ken Zangelin +*/ + + + +// ----------------------------------------------------------------------------- +// +// PernotState - +// +typedef enum PernotState +{ + SubActive = 1, + SubPaused = 2, + SubErroneous = 3, + SubExpired = 4 +} PernotState; + + + +// ----------------------------------------------------------------------------- +// +// PernotSubscription - Periodic Notification Subscription +// +typedef struct PernotSubscription +{ + char* subscriptionId; + PernotState state; + char tenant[64]; + KjNode* kjSubP; // OR, I split the entire tree into binary fields inside PernotSubscription ... + + // Timestamps + double lastNotificationAttempt; // In seconds + double lastSuccessTime; + double lastFailureTime; + double expiresAt; + + // Error handling + uint32_t consecutiveErrors; + uint32_t timeInterval; // In seconds (Subscription::timeInterval is an integer in seconds) + uint32_t cooldown; + + CURL* curlHandle; + + struct PernotSubscription* next; +} PernotSubscription; + +#endif // SRC_LIB_ORIONLD_PERNOT_PERNOTSUBSCRIPTION_H_ diff --git a/src/lib/orionld/pernot/pernotLoop.cpp b/src/lib/orionld/pernot/pernotLoop.cpp new file mode 100644 index 0000000000..ff0eb8e019 --- /dev/null +++ b/src/lib/orionld/pernot/pernotLoop.cpp @@ -0,0 +1,153 @@ +/* +* +* Copyright 2023 FIWARE Foundation e.V. +* +* This file is part of Orion-LD Context Broker. +* +* Orion-LD Context Broker is free software: you can redistribute it and/or +* modify it under the terms of the GNU Affero General Public License as +* published by the Free Software Foundation, either version 3 of the +* License, or (at your option) any later version. +* +* Orion-LD Context Broker is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero +* General Public License for more details. +* +* You should have received a copy of the GNU Affero General Public License +* along with Orion-LD Context Broker. If not, see http://www.gnu.org/licenses/. +* +* For those usages not covered by this license please contact with +* orionld at fiware dot org +* +* Author: Ken Zangelin +*/ +#include // strerror +#include // gettimeofday +#include // pthread_create + +#include "logMsg/logMsg.h" // LM_x + +#include "orionld/common/orionldState.h" // orionldState +#include "orionld/pernot/PernotSubscription.h" // PernotSubscription +#include "orionld/pernot/pernotLoop.h" // Own interface + + + +// ----------------------------------------------------------------------------- +// +// PernotSubCache - move to pernot/PernotSubCache.h +// +typedef struct PernotSubCache +{ + PernotSubscription* head; + PernotSubscription* tail; +} PernotSubCache; + + +PernotSubCache pernotSubCache; + + + +// ----------------------------------------------------------------------------- +// +// pernotSubCacheInit - move to pernot/pernotSubCacheInit.h/cpp +// +void pernotSubCacheInit(void) +{ + bzero(&pernotSubCache, sizeof(pernotSubCache)); +} + + + +// ----------------------------------------------------------------------------- +// +// pernotSubInsert - move to pernot/pernotSubInsert.h/cpp +// +bool pernotSubInsert(void) +{ + return true; +} + + + +// ----------------------------------------------------------------------------- +// +// currentTime - +// +static double currentTime(void) +{ + // int gettimeofday(struct timeval *tv, struct timezone *tz); + struct timeval tv; + + if (gettimeofday(&tv, NULL) != 0) + LM_RE(0, ("gettimeofday error: %s", strerror(errno))); + + return tv.tv_sec + tv.tv_usec / 1000000; +} + + + +// ----------------------------------------------------------------------------- +// +// pernotLoop - +// +static void* pernotLoop(void* vP) +{ + while (1) + { + for (PernotSubscription* subP = pernotSubCache.head; subP != NULL; subP = subP->next) + { + double now = currentTime(); + + if (subP->state == SubPaused) + continue; + + if (subP->expiresAt <= now) + subP->state = SubExpired; + + if (subP->state == SubExpired) + continue; + + if (subP->state == SubErroneous) + { + // Check for cooldown - take it out of error if cooldown time has passwd + if (subP->lastFailureTime + subP->cooldown < now) + subP->state = SubActive; + else + continue; + } + + if (subP->lastNotificationAttempt + subP->timeInterval < now) + { + subP->lastNotificationAttempt = now; // Either it works or fails, the timestamp needs to be updated + if (pernotTreat(subP) == true) // Just send the notification, don't await any response + { + subP->lastSuccessTime = now; + subP->consecutiveErrors = 0; + } + else + { + subP->lastFailureTime = now; + subP->consecutiveErrors += 1; + + if (subP->consecutiveErrors >= 3) + { + subP->state = SubErroneous; + } + } + } + } + } +} + + + +// ----------------------------------------------------------------------------- +// +// pernotLoopStart - +// +void pernotLoopStart(void) +{ + pthread_create(&pernotThreadID, NULL, pernotLoop, NULL); +} diff --git a/src/lib/orionld/pernot/pernotLoop.h b/src/lib/orionld/pernot/pernotLoop.h new file mode 100644 index 0000000000..6936c2858a --- /dev/null +++ b/src/lib/orionld/pernot/pernotLoop.h @@ -0,0 +1,37 @@ +#ifndef SRC_LIB_ORIONLD_PERNOT_PERNOTSTART_H_ +#define SRC_LIB_ORIONLD_PERNOT_PERNOTSTART_H_ + +/* +* +* Copyright 2023 FIWARE Foundation e.V. +* +* This file is part of Orion-LD Context Broker. +* +* Orion-LD Context Broker is free software: you can redistribute it and/or +* modify it under the terms of the GNU Affero General Public License as +* published by the Free Software Foundation, either version 3 of the +* License, or (at your option) any later version. +* +* Orion-LD Context Broker is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero +* General Public License for more details. +* +* You should have received a copy of the GNU Affero General Public License +* along with Orion-LD Context Broker. If not, see http://www.gnu.org/licenses/. +* +* For those usages not covered by this license please contact with +* orionld at fiware dot org +* +* Author: Ken Zangelin +*/ + + + +// ----------------------------------------------------------------------------- +// +// pernotStart - +// +extern void pernotStart(void); + +#endif // SRC_LIB_ORIONLD_PERNOT_PERNOTSTART_H_