Skip to content

Commit

Permalink
Just to keep working on this in my other laptop, in Athens - doesn't …
Browse files Browse the repository at this point in the history
…even compile
  • Loading branch information
kzangeli committed Jul 2, 2023
1 parent 0def54a commit 2cfed29
Show file tree
Hide file tree
Showing 6 changed files with 302 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ SET (ORION_LIBS
orionld_troe
orionld_dbModel
orionld_apiModel
orionld_pernot
serviceRoutines
serviceRoutinesV2
ngsiNotify
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions src/app/orionld/orionld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down
34 changes: 34 additions & 0 deletions src/lib/orionld/pernot/CMakeLists.txt
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})
72 changes: 72 additions & 0 deletions src/lib/orionld/pernot/PernotSubscription.h
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_
153 changes: 153 additions & 0 deletions src/lib/orionld/pernot/pernotLoop.cpp
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);
}
37 changes: 37 additions & 0 deletions src/lib/orionld/pernot/pernotLoop.h
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_

0 comments on commit 2cfed29

Please sign in to comment.