mirror of
https://github.com/outbackdingo/parodus.git
synced 2026-01-28 02:20:02 +00:00
Remove nopoll and fix some memory leaks
This commit is contained in:
@@ -44,8 +44,8 @@ include_directories(${INCLUDE_DIR}
|
||||
add_definitions(-std=c99)
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_GNU_SOURCE -DNOPOLL_LOGGER ")
|
||||
|
||||
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror -Wall -Wno-missing-field-initializers")
|
||||
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wall")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror -Wall -Wno-missing-field-initializers")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wall")
|
||||
|
||||
|
||||
# pthread external dependency
|
||||
@@ -70,39 +70,13 @@ add_dependencies(libtrower-base64 trower-base64)
|
||||
ExternalProject_Add(lws
|
||||
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/_prefix/lws
|
||||
GIT_REPOSITORY https://github.com/warmcat/libwebsockets.git
|
||||
GIT_TAG "master"
|
||||
GIT_TAG "ed92b6dfe75ad65a78dadfa4dc96da4568d95d69"
|
||||
CMAKE_ARGS += -DCMAKE_INSTALL_PREFIX=${INSTALL_DIR}
|
||||
-DCMAKE_BUILD_TYPE=DEBUG
|
||||
)
|
||||
add_library(libwebsockets STATIC SHARED IMPORTED)
|
||||
add_dependencies(libwebsockets lws)
|
||||
|
||||
|
||||
# nopoll external dependency
|
||||
#-------------------------------------------------------------------------------
|
||||
set(PATCHES_DIR ${CMAKE_CURRENT_SOURCE_DIR}/patches)
|
||||
set(NOPOLL_LOG_SRC ${PREFIX_DIR}/nopoll/src/nopoll/src/nopoll_log.c)
|
||||
ExternalProject_Add(nopoll
|
||||
PREFIX ${PREFIX_DIR}/nopoll
|
||||
GIT_REPOSITORY https://github.com/ASPLes/nopoll.git
|
||||
GIT_TAG "b18aacc06b4dc9700e0a261efc201a8e125e4328"
|
||||
PATCH_COMMAND patch -p1 < ${PATCHES_DIR}/nopoll.patch
|
||||
COMMAND touch NEWS README AUTHORS ChangeLog
|
||||
COMMAND libtoolize --force
|
||||
COMMAND aclocal
|
||||
COMMAND autoheader --warnings=error
|
||||
COMMAND automake --add-missing -Werror
|
||||
COMMAND autoconf --force --warnings=error
|
||||
CONFIGURE_COMMAND COMMAND <SOURCE_DIR>/configure --prefix=${PREFIX}
|
||||
--includedir=${INCLUDE_DIR}
|
||||
--libdir=${LIBRARY_DIR}
|
||||
${CUSTOM_HOST}
|
||||
BUILD_IN_SOURCE 1
|
||||
)
|
||||
add_library(libnopoll STATIC SHARED IMPORTED)
|
||||
add_dependencies(libnopoll nopoll)
|
||||
|
||||
|
||||
# nanoMsg external dependency
|
||||
#-------------------------------------------------------------------------------
|
||||
ExternalProject_Add(nanomsg
|
||||
|
||||
1495
patches/nopoll.patch
1495
patches/nopoll.patch
File diff suppressed because it is too large
Load Diff
@@ -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_handlers.c ParodusInternal.c
|
||||
set(SOURCES main.c networking.c lws_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)
|
||||
|
||||
add_executable(parodus ${SOURCES})
|
||||
@@ -21,7 +21,6 @@ target_link_libraries (parodus
|
||||
-lwrp-c
|
||||
-lmsgpackc
|
||||
-ltrower-base64
|
||||
-lnopoll
|
||||
-llibseshat
|
||||
-luuid
|
||||
-lm
|
||||
|
||||
@@ -74,8 +74,7 @@ char* getWebpaConveyHeader()
|
||||
|
||||
buffer = cJSON_PrintUnformatted(response);
|
||||
ParodusInfo("X-WebPA-Convey Header: [%zd]%s\n", strlen(buffer), buffer);
|
||||
/*TODO Remove nopoll related API*/
|
||||
if(nopoll_base64_encode (buffer, strlen(buffer), encodedData, &encodedDataSize) != nopoll_true)
|
||||
if(lws_b64_encode_string (buffer, strlen(buffer), encodedData, encodedDataSize) < 0)
|
||||
{
|
||||
ParodusError("Base64 Encoding failed for Connection Header\n");
|
||||
}
|
||||
|
||||
@@ -25,10 +25,10 @@
|
||||
#include <sys/wait.h>
|
||||
|
||||
#include <cJSON.h>
|
||||
#include <nopoll.h>
|
||||
#include <nanomsg/nn.h>
|
||||
#include <nanomsg/pipeline.h>
|
||||
#include <wrp-c.h>
|
||||
#include <libwebsockets.h>
|
||||
|
||||
#include "parodus_log.h"
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
@@ -13,8 +13,6 @@
|
||||
#include "upstream.h"
|
||||
#include "downstream.h"
|
||||
#include "thread_tasks.h"
|
||||
#include "nopoll_helpers.h"
|
||||
#include "mutex.h"
|
||||
#include "spin_thread.h"
|
||||
#include "service_alive.h"
|
||||
#include <libseshat.h>
|
||||
@@ -33,8 +31,6 @@
|
||||
|
||||
bool conn_retry = false;
|
||||
bool LastReasonStatus = false;
|
||||
volatile unsigned int heartBeatTimer = 0;
|
||||
pthread_mutex_t close_mut=PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Function Prototypes */
|
||||
@@ -67,6 +63,7 @@ void createLWSsocket(void *config_in, void (* initKeypress)())
|
||||
}
|
||||
|
||||
seshat_registered = __registerWithSeshat();
|
||||
UNUSED(seshat_registered);
|
||||
|
||||
do
|
||||
{
|
||||
|
||||
@@ -10,11 +10,8 @@
|
||||
#include "time.h"
|
||||
#include "config.h"
|
||||
#include "upstream.h"
|
||||
#include "nopoll_helpers.h"
|
||||
#include "mutex.h"
|
||||
#include "spin_thread.h"
|
||||
#include "nopoll_handlers.h"
|
||||
#include <libwebsockets.h>
|
||||
#include "lws_handlers.h"
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Macros */
|
||||
@@ -94,9 +91,12 @@ dump_handshake_info(struct lws *wsi)
|
||||
char * join_fragment_msg (char *firstMsg,int firstSize,char *secondMsg,int secondSize,int * result)
|
||||
{
|
||||
*result = firstSize + secondSize;
|
||||
fragmentSize = *result;
|
||||
char *tmpMsg = (char *)malloc(sizeof(char)* (*result));
|
||||
memcpy(tmpMsg,firstMsg,firstSize);
|
||||
memcpy (tmpMsg + (firstSize), secondMsg, secondSize);
|
||||
free(firstMsg);
|
||||
free(secondMsg);
|
||||
return tmpMsg;
|
||||
}
|
||||
|
||||
@@ -106,7 +106,7 @@ parodus_callback(struct lws *wsi, enum lws_callback_reasons reason,
|
||||
{
|
||||
int n;
|
||||
char * payload = NULL;
|
||||
char * out = NULL;
|
||||
unsigned char * out = NULL;
|
||||
switch (reason) {
|
||||
|
||||
case LWS_CALLBACK_CLIENT_ESTABLISHED:
|
||||
@@ -133,34 +133,36 @@ parodus_callback(struct lws *wsi, enum lws_callback_reasons reason,
|
||||
char * tmpMsg = NULL;
|
||||
payload = (char *)malloc(sizeof(char)*len);
|
||||
strcpy(payload,(char *)in);
|
||||
if (lws_is_final_fragment(wsi))
|
||||
{
|
||||
|
||||
if(fragmentMsg == NULL)
|
||||
{
|
||||
listenerOnrequest_queue(payload,len);
|
||||
}
|
||||
else
|
||||
{
|
||||
tmpMsg = join_fragment_msg(fragmentMsg,fragmentSize,payload,len,&payloadSize);
|
||||
len = payloadSize;
|
||||
listenerOnrequest_queue(tmpMsg,len);
|
||||
fragmentMsg = NULL;
|
||||
len = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(fragmentMsg == NULL)
|
||||
{
|
||||
fragmentMsg = payload;
|
||||
fragmentSize = len;
|
||||
}
|
||||
else
|
||||
{
|
||||
fragmentMsg = join_fragment_msg(fragmentMsg,fragmentSize,payload,len,&payloadSize);
|
||||
}
|
||||
}
|
||||
|
||||
if (lws_is_final_fragment(wsi))
|
||||
{
|
||||
|
||||
if(fragmentMsg == NULL)
|
||||
{
|
||||
listenerOnrequest_queue(payload,len);
|
||||
}
|
||||
else
|
||||
{
|
||||
tmpMsg = join_fragment_msg(fragmentMsg,fragmentSize,payload,len,&payloadSize);
|
||||
len = payloadSize;
|
||||
listenerOnrequest_queue(tmpMsg,len);
|
||||
fragmentMsg = NULL;
|
||||
fragmentSize = 0;
|
||||
payloadSize = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(fragmentMsg == NULL)
|
||||
{
|
||||
fragmentMsg = payload;
|
||||
fragmentSize = len;
|
||||
}
|
||||
else
|
||||
{
|
||||
fragmentMsg = join_fragment_msg(fragmentMsg,fragmentSize,payload,len,&payloadSize);
|
||||
}
|
||||
}
|
||||
|
||||
lws_callback_on_writable(wsi);
|
||||
break;
|
||||
@@ -176,22 +178,21 @@ parodus_callback(struct lws *wsi, enum lws_callback_reasons reason,
|
||||
if(ResponseMsgQ != NULL)
|
||||
{
|
||||
//Read response data from queue
|
||||
pthread_mutex_lock (&res_mutex);
|
||||
while(ResponseMsgQ)
|
||||
while(ResponseMsgQ)
|
||||
{
|
||||
pthread_mutex_lock (&res_mutex);
|
||||
UpStreamMsg *message = ResponseMsgQ;
|
||||
ResponseMsgQ = ResponseMsgQ->next;
|
||||
pthread_mutex_unlock (&res_mutex);
|
||||
|
||||
out = (char *)malloc(sizeof(char) * (LWS_PRE + message->len));
|
||||
out = (unsigned char *)malloc(sizeof(unsigned char) * (LWS_PRE + message->len));
|
||||
memcpy (LWS_PRE + out, message->msg, message->len);
|
||||
char * tmpPtr = LWS_PRE + out;
|
||||
n = lws_write(wsi, tmpPtr, message->len, LWS_WRITE_BINARY);
|
||||
n = lws_write(wsi, LWS_PRE + out, message->len, LWS_WRITE_BINARY);
|
||||
if (n < 0)
|
||||
{
|
||||
ParodusError("Failed to send to server\n");
|
||||
free(message);
|
||||
message = NULL;
|
||||
free(message->msg);
|
||||
message->msg = NULL;
|
||||
return 1;
|
||||
}
|
||||
if (n < message->len) {
|
||||
@@ -200,8 +201,8 @@ parodus_callback(struct lws *wsi, enum lws_callback_reasons reason,
|
||||
}
|
||||
ParodusInfo("Sent %d bytes of data to server successfully \n",n);
|
||||
free(out);
|
||||
free(message);
|
||||
message = NULL;
|
||||
free(message->msg);
|
||||
message->msg = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -224,8 +225,7 @@ parodus_callback(struct lws *wsi, enum lws_callback_reasons reason,
|
||||
((0 != strlen(get_parodus_cfg()->hw_manufacturer)) ? get_parodus_cfg()->hw_manufacturer : "unknown"));
|
||||
|
||||
ParodusInfo("User-Agent: %s\n",user_agent);
|
||||
/*TODO Implement base64 encoding, currently we are using nopoll API to encode data*/
|
||||
//conveyHeader = getWebpaConveyHeader();
|
||||
conveyHeader = getWebpaConveyHeader();
|
||||
|
||||
if (lws_add_http_header_by_name(wsi,
|
||||
(unsigned char *)"X-WebPA-Device-Name:",
|
||||
@@ -240,13 +240,13 @@ parodus_callback(struct lws *wsi, enum lws_callback_reasons reason,
|
||||
(unsigned char *)user_agent,strlen(user_agent),p,end))
|
||||
return -1;
|
||||
|
||||
/* if(strlen(conveyHeader) > 0)
|
||||
if(strlen(conveyHeader) > 0)
|
||||
{
|
||||
if (lws_add_http_header_by_name(wsi,
|
||||
(unsigned char *)"X-WebPA-Convey:",
|
||||
(unsigned char *)conveyHeader,strlen(conveyHeader),p,end))
|
||||
return -1;
|
||||
}*/
|
||||
}
|
||||
break;
|
||||
case LWS_CALLBACK_OPENSSL_PERFORM_SERVER_CERT_VERIFICATION:
|
||||
/* Disable self signed verification */
|
||||
@@ -272,7 +272,7 @@ static const struct lws_protocols protocols[] = {
|
||||
|
||||
LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
|
||||
{
|
||||
ParodusInfo(" %s\n",line);
|
||||
ParodusInfo(" %s",line);
|
||||
}
|
||||
|
||||
void createLWSconnection()
|
||||
@@ -280,7 +280,6 @@ void createLWSconnection()
|
||||
struct lws_context_creation_info info;
|
||||
struct lws_client_connect_info i;
|
||||
struct lws_context *context;
|
||||
const char *prot, *p;
|
||||
int port = 8080,use_ssl =0;
|
||||
|
||||
memset(&info, 0, sizeof info);
|
||||
@@ -291,7 +290,7 @@ void createLWSconnection()
|
||||
info.protocols = protocols;
|
||||
info.gid = -1;
|
||||
info.uid = -1;
|
||||
lws_set_log_level(LLL_INFO | LLL_NOTICE | LLL_WARN | LLL_LATENCY | LLL_CLIENT | LLL_COUNT,NULL);
|
||||
lws_set_log_level(LLL_INFO | LLL_NOTICE | LLL_WARN | LLL_LATENCY | LLL_CLIENT | LLL_COUNT,lwsl_emit_syslog);
|
||||
|
||||
info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
|
||||
|
||||
|
||||
@@ -19,10 +19,7 @@ extern "C" {
|
||||
/* File Scoped Variables */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
extern bool close_retry;
|
||||
extern bool conn_retry;
|
||||
extern volatile unsigned int heartBeatTimer;
|
||||
extern pthread_mutex_t close_mut;
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Function Prototypes */
|
||||
|
||||
@@ -145,6 +145,7 @@ void listenerOnMessage(void * msg, size_t msgSize)
|
||||
ParodusError( "Failure in msgpack decoding for receivdMsg: rv is %d\n", rv );
|
||||
}
|
||||
ParodusPrint("free for downstream decoded msg\n");
|
||||
free(msg);
|
||||
wrp_free_struct(message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/**
|
||||
* @file nopoll_handlers.c
|
||||
* @file lws_handlers.c
|
||||
*
|
||||
* @description This describes nopoll handler functions.
|
||||
* @description This describes lws handler functions.
|
||||
*
|
||||
* Copyright (c) 2015 Comcast
|
||||
*/
|
||||
|
||||
#include "ParodusInternal.h"
|
||||
#include "nopoll_handlers.h"
|
||||
#include "lws_handlers.h"
|
||||
#include "connection.h"
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
41
src/lws_handlers.h
Normal file
41
src/lws_handlers.h
Normal file
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* @file lws_handlers.h
|
||||
*
|
||||
* @description This header defines lws handler functions.
|
||||
*
|
||||
* Copyright (c) 2015 Comcast
|
||||
*/
|
||||
|
||||
#ifndef _LWS_HANDLERS_H_
|
||||
#define _LWS_HANDLERS_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* File Scoped Variables */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
extern pthread_mutex_t g_mutex;
|
||||
extern pthread_cond_t g_cond;
|
||||
extern bool close_retry;
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Function Prototypes */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* @brief listenerOnrequest_queue function to add messages to the queue
|
||||
*
|
||||
* @param[in] reqSize size of the incoming message
|
||||
* @param[in] requestMsg The message received from server for various process requests
|
||||
*/
|
||||
void listenerOnrequest_queue(void *requestMsg,int reqSize);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
159
src/mutex.c
159
src/mutex.c
@@ -1,159 +0,0 @@
|
||||
/**
|
||||
* Copyright 2016 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.
|
||||
*
|
||||
*/
|
||||
#include "mutex.h"
|
||||
#include "parodus_log.h"
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Macros */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* none */
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Data Structures */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* none */
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* File Scoped Variables */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* none */
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Function Prototypes */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* none */
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* External Functions */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* @brief createMutex Nopoll create mutex handler
|
||||
*/
|
||||
noPollPtr createMutex()
|
||||
{
|
||||
pthread_mutexattr_t attr;
|
||||
pthread_mutex_t * mutex;
|
||||
int rtn;
|
||||
|
||||
mutex = (pthread_mutex_t*) malloc(sizeof(pthread_mutex_t));
|
||||
|
||||
if (mutex == NULL) {
|
||||
ParodusError("Failed to create mutex\n");
|
||||
return NULL;
|
||||
}
|
||||
pthread_mutexattr_init( &attr);
|
||||
/*pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_ERRORCHECK);*/
|
||||
pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE);
|
||||
|
||||
/* init the mutex using default values */
|
||||
rtn = pthread_mutex_init (mutex, &attr);
|
||||
pthread_mutexattr_destroy (&attr);
|
||||
if (rtn != 0) {
|
||||
ParodusError("Error in init Mutex\n");
|
||||
free(mutex);
|
||||
return NULL;
|
||||
} else {
|
||||
ParodusPrint("mutex init successfully\n");
|
||||
}
|
||||
|
||||
return mutex;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief lockMutex Nopoll lock mutex handler
|
||||
*/
|
||||
void lockMutex(noPollPtr _mutex)
|
||||
{
|
||||
int rtn;
|
||||
char errbuf[100];
|
||||
|
||||
if (_mutex == NULL) {
|
||||
ParodusError("Received null mutex\n");
|
||||
return;
|
||||
}
|
||||
pthread_mutex_t * mutex = _mutex;
|
||||
|
||||
/* lock the mutex */
|
||||
rtn = pthread_mutex_lock (mutex);
|
||||
if (rtn != 0) {
|
||||
strerror_r (rtn, errbuf, 100);
|
||||
ParodusError("Error in Lock mutex: %s\n", errbuf);
|
||||
/* do some reporting */
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief unlockMutex Nopoll unlock mutex handler
|
||||
*/
|
||||
void unlockMutex(noPollPtr _mutex)
|
||||
{
|
||||
int rtn;
|
||||
char errbuf[100];
|
||||
|
||||
if (_mutex == NULL) {
|
||||
ParodusError("Received null mutex\n");
|
||||
return;
|
||||
}
|
||||
pthread_mutex_t * mutex = _mutex;
|
||||
|
||||
/* unlock mutex */
|
||||
rtn = pthread_mutex_unlock (mutex);
|
||||
if (rtn != 0) {
|
||||
/* do some reporting */
|
||||
strerror_r (rtn, errbuf, 100);
|
||||
ParodusError("Error in unlock mutex: %s\n", errbuf);
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief destroyMutex Nopoll destroy mutex handler
|
||||
*/
|
||||
void destroyMutex(noPollPtr _mutex)
|
||||
{
|
||||
if (_mutex == NULL) {
|
||||
ParodusError("Received null mutex\n");
|
||||
return;
|
||||
}
|
||||
pthread_mutex_t * mutex = _mutex;
|
||||
|
||||
if (pthread_mutex_destroy (mutex) != 0) {
|
||||
/* do some reporting */
|
||||
ParodusError("problem in destroy\n");
|
||||
return;
|
||||
} else {
|
||||
ParodusPrint("Mutex destroyed \n");
|
||||
}
|
||||
free(mutex);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Internal functions */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* none */
|
||||
58
src/mutex.h
58
src/mutex.h
@@ -1,58 +0,0 @@
|
||||
/**
|
||||
* Copyright 2016 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <nopoll.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#ifndef _MUTEX_H_
|
||||
#define _MUTEX_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Function Prototypes */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* @brief createMutex Nopoll create mutex handler
|
||||
*/
|
||||
noPollPtr createMutex();
|
||||
|
||||
/**
|
||||
* @brief lockMutex Nopoll lock mutex handler
|
||||
*/
|
||||
void lockMutex(noPollPtr _mutex);
|
||||
|
||||
/**
|
||||
* @brief unlockMutex Nopoll unlock mutex handler
|
||||
*/
|
||||
void unlockMutex(noPollPtr _mutex);
|
||||
|
||||
/**
|
||||
* @brief destroyMutex Nopoll destroy mutex handler
|
||||
*/
|
||||
void destroyMutex(noPollPtr _mutex);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
@@ -1,60 +0,0 @@
|
||||
/**
|
||||
* @file nopoll_handlers.h
|
||||
*
|
||||
* @description This header defines nopoll handler functions.
|
||||
*
|
||||
* Copyright (c) 2015 Comcast
|
||||
*/
|
||||
|
||||
#ifndef _NOPOLL_HANDLERS_H_
|
||||
#define _NOPOLL_HANDLERS_H_
|
||||
|
||||
#include "nopoll.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* File Scoped Variables */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
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;
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Function Prototypes */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* @brief listenerOnMessage_queue function to add messages to the queue
|
||||
*
|
||||
* @param[in] ctx The context where the connection happens.
|
||||
* @param[in] conn The Websocket connection object
|
||||
* @param[in] msg The message received from server for various process requests
|
||||
* @param[out] user_data data which is to be sent
|
||||
*/
|
||||
|
||||
void listenerOnrequest_queue(void *requestMsg,int reqSize);
|
||||
|
||||
/**
|
||||
* @brief listenerOnPingMessage function to create WebSocket listener to receive heartbeat ping messages
|
||||
*
|
||||
* @param[in] ctx The context where the connection happens.
|
||||
* @param[in] conn Websocket connection object
|
||||
* @param[in] msg The ping message received from the server
|
||||
* @param[out] user_data data which is to be sent
|
||||
*/
|
||||
void listenerOnPingMessage (noPollCtx * ctx, noPollConn * conn, noPollMsg * msg, noPollPtr user_data);
|
||||
|
||||
|
||||
void listenerOnCloseMessage (noPollCtx * ctx, noPollConn * conn, noPollPtr user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,112 +0,0 @@
|
||||
/**
|
||||
* @file nopoll_helpers.c
|
||||
*
|
||||
* @description This file is used to manage incomming and outgoing messages.
|
||||
*
|
||||
* Copyright (c) 2015 Comcast
|
||||
*/
|
||||
|
||||
#include "ParodusInternal.h"
|
||||
#include "connection.h"
|
||||
#include "nopoll_helpers.h"
|
||||
#include "nopoll_handlers.h"
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Macros */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#define MAX_SEND_SIZE (60 * 1024)
|
||||
#define FLUSH_WAIT_TIME (2000000LL)
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* External functions */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
void setMessageHandlers()
|
||||
{
|
||||
nopoll_conn_set_on_msg(get_global_conn(), (noPollOnMessageHandler) listenerOnMessage_queue, NULL);
|
||||
nopoll_conn_set_on_ping_msg(get_global_conn(), (noPollOnMessageHandler)listenerOnPingMessage, NULL);
|
||||
nopoll_conn_set_on_close(get_global_conn(), (noPollOnCloseHandler)listenerOnCloseMessage, NULL);
|
||||
}
|
||||
|
||||
/** To send upstream msgs to server ***/
|
||||
|
||||
void sendMessage(noPollConn *conn, void *msg, size_t len)
|
||||
{
|
||||
int bytesWritten = 0;
|
||||
|
||||
ParodusInfo("sendMessage length %zu\n", len);
|
||||
if(nopoll_conn_is_ok(conn) && nopoll_conn_is_ready(conn))
|
||||
{
|
||||
//bytesWritten = nopoll_conn_send_binary(conn, msg, len);
|
||||
bytesWritten = sendResponse(conn, msg, len);
|
||||
ParodusPrint("Number of bytes written: %d\n", bytesWritten);
|
||||
if (bytesWritten != (int) len)
|
||||
{
|
||||
ParodusError("Failed to send bytes %zu, bytes written were=%d (errno=%d, %s)..\n", len, bytesWritten, errno, strerror(errno));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ParodusError("Failed to send msg upstream as connection is not OK\n");
|
||||
}
|
||||
}
|
||||
|
||||
int sendResponse(noPollConn * conn, void * buffer, size_t length)
|
||||
{
|
||||
char *cp = buffer;
|
||||
int final_len_sent = 0;
|
||||
noPollOpCode frame_type = NOPOLL_BINARY_FRAME;
|
||||
|
||||
while (length > 0)
|
||||
{
|
||||
int bytes_sent, len_to_send;
|
||||
|
||||
len_to_send = length > MAX_SEND_SIZE ? MAX_SEND_SIZE : length;
|
||||
length -= len_to_send;
|
||||
bytes_sent = __nopoll_conn_send_common(conn, cp, len_to_send, length > 0 ? nopoll_false : nopoll_true, 0, frame_type);
|
||||
|
||||
if (bytes_sent != len_to_send)
|
||||
{
|
||||
if (-1 == bytes_sent || (bytes_sent = nopoll_conn_flush_writes(conn, FLUSH_WAIT_TIME, bytes_sent)) != len_to_send)
|
||||
{
|
||||
ParodusError("sendResponse() Failed to send all the data\n");
|
||||
cp = NULL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
cp += len_to_send;
|
||||
final_len_sent += len_to_send;
|
||||
frame_type = NOPOLL_CONTINUATION_FRAME;
|
||||
}
|
||||
return final_len_sent;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief __report_log Nopoll log handler
|
||||
* Nopoll log handler for integrating nopoll logs
|
||||
*/
|
||||
void __report_log (noPollCtx * ctx, noPollDebugLevel level, const char * log_msg, noPollPtr user_data)
|
||||
{
|
||||
UNUSED(ctx);
|
||||
UNUSED(user_data);
|
||||
|
||||
if (level == NOPOLL_LEVEL_DEBUG)
|
||||
{
|
||||
//ParodusPrint("%s\n", log_msg);
|
||||
}
|
||||
if (level == NOPOLL_LEVEL_INFO)
|
||||
{
|
||||
ParodusInfo ("%s\n", log_msg);
|
||||
}
|
||||
if (level == NOPOLL_LEVEL_WARNING)
|
||||
{
|
||||
ParodusPrint("%s\n", log_msg);
|
||||
}
|
||||
if (level == NOPOLL_LEVEL_CRITICAL)
|
||||
{
|
||||
ParodusError("%s\n", log_msg );
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
/**
|
||||
* @file nopoll_handlers.h
|
||||
*
|
||||
* @description This header defines functions to manage incomming and outgoing messages.
|
||||
*
|
||||
* Copyright (c) 2015 Comcast
|
||||
*/
|
||||
|
||||
#ifndef _NOPOLL_HELPERS_H_
|
||||
#define _NOPOLL_HELPERS_H_
|
||||
|
||||
#include "nopoll.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Function Prototypes */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* @brief Interface to create WebSocket client connections.
|
||||
* Loads the WebPA config file, if not provided by the caller,
|
||||
* and creates the intial connection and manages the connection wait, close mechanisms.
|
||||
*/
|
||||
int sendResponse(noPollConn * conn,void *str, size_t bufferSize);
|
||||
void setMessageHandlers();
|
||||
void sendMessage(noPollConn *conn, void *msg, size_t len);
|
||||
|
||||
/**
|
||||
* @brief __report_log Nopoll log handler
|
||||
* Nopoll log handler for integrating nopoll logs
|
||||
*/
|
||||
void __report_log (noPollCtx * ctx, noPollDebugLevel level, const char * log_msg, noPollPtr user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -37,8 +37,8 @@ void *messageHandlerTask()
|
||||
|
||||
listenerOnMessage(message->payload, message->len);
|
||||
|
||||
//free(message);
|
||||
//message = NULL;
|
||||
free(message);
|
||||
message = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
#include "partners_check.h"
|
||||
#include "connection.h"
|
||||
#include "client_list.h"
|
||||
#include "nopoll_helpers.h"
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Macros */
|
||||
|
||||
Reference in New Issue
Block a user