Skip to content

Commit

Permalink
Merge pull request #221 from Comcast/heart_beat
Browse files Browse the repository at this point in the history
Heart beat Timer
  • Loading branch information
schmidtw committed Jul 18, 2018
2 parents 3df3876 + 6e4f8e9 commit 326e83c
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Expand Up @@ -11,7 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set(SOURCES main.c mutex.c networking.c nopoll_helpers.c nopoll_handlers.c
set(SOURCES main.c mutex.c networking.c nopoll_helpers.c heartBeat.c nopoll_handlers.c
ParodusInternal.c string_helpers.c time.c config.c conn_interface.c
connection.c spin_thread.c client_list.c service_alive.c
upstream.c downstream.c thread_tasks.c partners_check.c token.c
Expand Down
10 changes: 6 additions & 4 deletions src/conn_interface.c
Expand Up @@ -34,10 +34,12 @@
#include "service_alive.h"
#include "seshat_interface.h"
#include "crud_interface.h"
#include "heartBeat.h"
#ifdef FEATURE_DNS_QUERY
#include <ucresolv_log.h>
#endif
#include <time.h>

/*----------------------------------------------------------------------------*/
/* Macros */
/*----------------------------------------------------------------------------*/
Expand All @@ -49,7 +51,6 @@
/*----------------------------------------------------------------------------*/

bool close_retry = false;
volatile unsigned int heartBeatTimer = 0;
pthread_mutex_t close_mut=PTHREAD_MUTEX_INITIALIZER;

/*----------------------------------------------------------------------------*/
Expand All @@ -67,6 +68,7 @@ void createSocketConnection(void (* initKeypress)())
noPollCtx *ctx;
bool seshat_registered = false;
unsigned int webpa_ping_timeout_ms = 1000 * get_parodus_cfg()->webpa_ping_timeout;
unsigned int heartBeatTimer = 0;

//loadParodusCfg(tmpCfg,get_parodus_cfg());
#ifdef FEATURE_DNS_QUERY
Expand Down Expand Up @@ -115,7 +117,7 @@ void createSocketConnection(void (* initKeypress)())
time_taken_ms = diff.tv_sec * 1000 + (diff.tv_nsec / 1000000);

// ParodusInfo("nopoll_loop_wait() time %d msec\n", time_taken_ms);

heartBeatTimer = get_heartBeatTimer();
if(heartBeatTimer >= webpa_ping_timeout_ms)
{
ParodusInfo("heartBeatTimer %d webpa_ping_timeout_ms %d\n", heartBeatTimer, webpa_ping_timeout_ms);
Expand All @@ -133,10 +135,10 @@ void createSocketConnection(void (* initKeypress)())
{
ParodusPrint("heartBeatHandler - close_retry set to %d, hence resetting the heartBeatTimer\n",close_retry);
}
heartBeatTimer = 0;
reset_heartBeatTimer();
}
else {
heartBeatTimer += time_taken_ms;
increment_heartBeatTimer(time_taken_ms);
}

if( false == seshat_registered ) {
Expand Down
3 changes: 2 additions & 1 deletion src/connection.c
Expand Up @@ -28,6 +28,7 @@
#include "nopoll_helpers.h"
#include "mutex.h"
#include "spin_thread.h"
#include "heartBeat.h"

/*----------------------------------------------------------------------------*/
/* Macros */
Expand Down Expand Up @@ -441,7 +442,7 @@ int createNopollConnection(noPollCtx *ctx)
close_retry = false;
pthread_mutex_unlock (&close_mut);
ParodusPrint("createNopollConnection(): close_mut unlock\n");
heartBeatTimer = 0;
reset_heartBeatTimer();
// Reset connErr flag on successful connection
connErr = 0;
set_global_reconnect_reason("webpa_process_starts");
Expand Down
1 change: 0 additions & 1 deletion src/connection.h
Expand Up @@ -35,7 +35,6 @@ extern "C" {
/*----------------------------------------------------------------------------*/

extern bool close_retry;
extern volatile unsigned int heartBeatTimer;
extern pthread_mutex_t close_mut;

/*----------------------------------------------------------------------------*/
Expand Down
56 changes: 56 additions & 0 deletions src/heartBeat.c
@@ -0,0 +1,56 @@
/**
* Copyright 2018 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/**
* @file heartBeat.c
*
* @description This decribes functions required to manage heartBeatTimer.
*
*/

#include "heartBeat.h"

volatile unsigned int heartBeatTimer = 0;

pthread_mutex_t heartBeat_mut=PTHREAD_MUTEX_INITIALIZER;

// Get value of heartBeatTimer
unsigned int get_heartBeatTimer()
{
unsigned int tmp = 0;
pthread_mutex_lock (&heartBeat_mut);
tmp = heartBeatTimer;
pthread_mutex_unlock (&heartBeat_mut);
return tmp;
}

// Reset value of heartBeatTimer to 0
void reset_heartBeatTimer()
{
pthread_mutex_lock (&heartBeat_mut);
heartBeatTimer = 0;
pthread_mutex_unlock (&heartBeat_mut);
}

// Increment value of heartBeatTimer to desired value
void increment_heartBeatTimer(unsigned int inc_time_ms)
{
pthread_mutex_lock (&heartBeat_mut);
heartBeatTimer += inc_time_ms;
pthread_mutex_unlock (&heartBeat_mut);
}


46 changes: 46 additions & 0 deletions src/heartBeat.h
@@ -0,0 +1,46 @@
/**
* Copyright 2018 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/**
* @file heartBeat.h
*
* @description This decribes functions required to manage heartBeatTimer.
*
*/

#ifndef _HEARBEAT_H_
#define _HEARBEAT_H_

#include <pthread.h>

#ifdef __cplusplus
extern "C" {
#endif

// Get value of heartBeatTimer
unsigned int get_heartBeatTimer();

// Reset value of heartBeatTimer to 0
void reset_heartBeatTimer();

// Increment value of heartBeatTimer to desired value
void increment_heartBeatTimer(unsigned int inc_time_ms);

#ifdef __cplusplus
}
#endif

#endif
3 changes: 2 additions & 1 deletion src/nopoll_handlers.c
Expand Up @@ -24,6 +24,7 @@
#include "ParodusInternal.h"
#include "nopoll_handlers.h"
#include "connection.h"
#include "heartBeat.h"

/*----------------------------------------------------------------------------*/
/* Macros */
Expand Down Expand Up @@ -146,7 +147,7 @@ void listenerOnPingMessage (noPollCtx * ctx, noPollConn * conn, noPollMsg * msg,
ParodusInfo("Ping received with payload %s, opcode %d\n",(char *)payload, nopoll_msg_opcode(msg));
if (nopoll_msg_opcode(msg) == NOPOLL_PING_FRAME)
{
heartBeatTimer = 0;
reset_heartBeatTimer();
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/nopoll_handlers.h
Expand Up @@ -37,7 +37,6 @@ extern "C" {
extern pthread_mutex_t g_mutex;
extern pthread_cond_t g_cond;
extern pthread_mutex_t close_mut;
extern volatile unsigned int heartBeatTimer;
extern bool close_retry;

/*----------------------------------------------------------------------------*/
Expand Down
15 changes: 8 additions & 7 deletions tests/CMakeLists.txt
Expand Up @@ -109,15 +109,15 @@ target_link_libraries (test_string_helpers ${PARODUS_COMMON_LIBS} )
# test_nopoll_handlers
#-------------------------------------------------------------------------------
add_test(NAME test_nopoll_handlers COMMAND ${MEMORY_CHECK} ./test_nopoll_handlers)
add_executable(test_nopoll_handlers test_nopoll_handlers.c ../src/nopoll_handlers.c)
add_executable(test_nopoll_handlers test_nopoll_handlers.c ../src/nopoll_handlers.c ../src/heartBeat.c)
target_link_libraries (test_nopoll_handlers -lnopoll -lcunit -lcimplog -Wl,--no-as-needed -lrt -lpthread -lm)


#-------------------------------------------------------------------------------
# test_nopoll_handlers_fragment
#-------------------------------------------------------------------------------
add_test(NAME test_nopoll_handlers_fragment COMMAND ${MEMORY_CHECK} ./test_nopoll_handlers_fragment)
add_executable(test_nopoll_handlers_fragment test_nopoll_handlers_fragment.c ../src/nopoll_handlers.c)
add_executable(test_nopoll_handlers_fragment test_nopoll_handlers_fragment.c ../src/nopoll_handlers.c ../src/heartBeat.c)
target_link_libraries (test_nopoll_handlers_fragment -lnopoll -lcunit -lcimplog -Wl,--no-as-needed -lrt -lpthread -lm -lcmocka)

#-------------------------------------------------------------------------------
Expand All @@ -126,7 +126,7 @@ target_link_libraries (test_nopoll_handlers_fragment -lnopoll -lcunit -lcimplog
add_test(NAME test_connection COMMAND ${MEMORY_CHECK} ./test_connection)
#add_executable(test_connection test_connection.c ../src/connection.c ${PARODUS_COMMON_SRC})
#target_link_libraries (test_connection ${PARODUS_COMMON_LIBS} -lcmocka)
set(CONN_SRC test_connection.c ../src/connection.c ${PARODUS_COMMON_SRC})
set(CONN_SRC test_connection.c ../src/connection.c ../src/heartBeat.c ${PARODUS_COMMON_SRC})
add_executable(test_connection ${CONN_SRC})
#target_link_libraries (test_connection ${PARODUS_CONN_LIBS} ${PARODUS_COMMON_LIBS} -lcmocka)
target_link_libraries (test_connection ${PARODUS_COMMON_LIBS} -lcmocka)
Expand All @@ -137,7 +137,7 @@ target_link_libraries (test_connection ${PARODUS_COMMON_LIBS} -lcmocka)
add_test(NAME test_createConnection COMMAND ${MEMORY_CHECK} ./test_createConnection)
#add_executable(test_createConnection test_createConnection.c ../src/connection.c ../src/string_helpers.c ../src/config.c)
#target_link_libraries (test_createConnection ${PARODUS_COMMON_LIBS} -lcmocka)
add_executable(test_createConnection test_createConnection.c ../src/connection.c ../src/string_helpers.c ../src/config.c)
add_executable(test_createConnection test_createConnection.c ../src/connection.c ../src/string_helpers.c ../src/config.c ../src/heartBeat.c)
#target_link_libraries (test_createConnection ${PARODUS_CONN_LIBS} ${PARODUS_COMMON_LIBS} -lcmocka )
target_link_libraries (test_createConnection ${PARODUS_COMMON_LIBS} -lcmocka )

Expand All @@ -149,7 +149,7 @@ add_test(NAME test_client_list COMMAND ${MEMORY_CHECK} ./test_client_list)
#target_link_libraries (test_client_list ${PARODUS_COMMON_LIBS})
set(CLIST_SRC test_client_list.c ../src/client_list.c
../src/service_alive.c ../src/upstream.c ../src/networking.c ../src/nopoll_helpers.c
../src/downstream.c ../src/connection.c ../src/nopoll_handlers.c
../src/downstream.c ../src/connection.c ../src/nopoll_handlers.c ../src/heartBeat.c
../src/ParodusInternal.c ../src/thread_tasks.c ../src/conn_interface.c
../src/partners_check.c ../src/crud_interface.c ../src/crud_tasks.c ../src/crud_internal.c ${PARODUS_COMMON_SRC})

Expand All @@ -169,7 +169,7 @@ target_link_libraries (test_client_list ${PARODUS_COMMON_LIBS})
add_test(NAME test_service_alive COMMAND ${MEMORY_CHECK} ./test_service_alive)
#add_executable(test_service_alive test_service_alive.c ../src/client_list.c ../src/service_alive.c ../src/upstream.c ../src/networking.c ../src/nopoll_helpers.c ../src/nopoll_handlers.c ../src/config.c ../src/connection.c ../src/ParodusInternal.c ../src/downstream.c ../src/thread_tasks.c ../src/conn_interface.c ../src/partners_check.c ${PARODUS_COMMON_SRC})
#target_link_libraries (test_service_alive ${PARODUS_COMMON_LIBS})
set(SVA_SRC test_service_alive.c ../src/client_list.c ../src/service_alive.c ../src/upstream.c ../src/networking.c ../src/nopoll_helpers.c ../src/nopoll_handlers.c ../src/config.c ../src/connection.c ../src/ParodusInternal.c ../src/downstream.c ../src/thread_tasks.c ../src/conn_interface.c ../src/partners_check.c ${PARODUS_COMMON_SRC})
set(SVA_SRC test_service_alive.c ../src/client_list.c ../src/service_alive.c ../src/upstream.c ../src/networking.c ../src/nopoll_helpers.c ../src/nopoll_handlers.c ../src/config.c ../src/connection.c ../src/ParodusInternal.c ../src/downstream.c ../src/thread_tasks.c ../src/conn_interface.c ../src/partners_check.c ../src/heartBeat.c ${PARODUS_COMMON_SRC})
if (ENABLE_SESHAT)
set(SVA_SRC ${SVA_SRC} ../src/seshat_interface.c)
else()
Expand Down Expand Up @@ -250,6 +250,7 @@ set(CONIFC_SRC test_conn_interface.c
../src/token.c
../src/string_helpers.c
../src/mutex.c
../src/heartBeat.c
)
if (ENABLE_SESHAT)
set(CONIFC_SRC ${CONIFC_SRC} ../src/seshat_interface.c)
Expand Down Expand Up @@ -286,7 +287,7 @@ set(TOKEN_SRC ../src/conn_interface.c ../src/config.c
../src/networking.c
../src/thread_tasks.c ../src/time.c
../src/string_helpers.c ../src/mutex.c
../src/token.c
../src/token.c ../src/heartBeat.c
)

if (ENABLE_SESHAT)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_conn_interface.c
Expand Up @@ -27,6 +27,7 @@
#include "../src/conn_interface.h"
#include "../src/connection.h"
#include "../src/config.h"
#include "../src/heartBeat.h"

/*----------------------------------------------------------------------------*/
/* File Scoped Variables */
Expand All @@ -35,7 +36,6 @@ UpStreamMsg *UpStreamMsgQ;
ParodusMsg *ParodusMsgQ;
extern bool close_retry;
extern pthread_mutex_t close_mut;
extern volatile unsigned int heartBeatTimer;
pthread_mutex_t nano_mut;
pthread_cond_t nano_con;

Expand Down Expand Up @@ -339,7 +339,7 @@ void err_createSocketConnection()
pthread_mutex_lock (&close_mut);
close_retry = true;
pthread_mutex_unlock (&close_mut);
heartBeatTimer = 0;
reset_heartBeatTimer();
expect_function_call(nopoll_thread_handlers);

will_return(nopoll_ctx_new, (intptr_t)NULL);
Expand Down

0 comments on commit 326e83c

Please sign in to comment.