forked from telefonicaid/fiware-orion
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Just to keep working on this in my other laptop, in Athens - doesn't …
…even compile
- Loading branch information
Showing
6 changed files
with
302 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <string.h> // strerror | ||
#include <sys/time.h> // gettimeofday | ||
#include <pthread.h> // 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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_ |