From 61822be619a49555f78ad22bb4c00dcead749336 Mon Sep 17 00:00:00 2001 From: Gayathri Date: Mon, 12 Nov 2018 09:41:35 +0530 Subject: [PATCH] Wrapper to send event messages is added --- CHANGELOG.md | 1 + CMakeLists.txt | 1 + parodusNotifier/CMakeLists.txt | 36 ++++ parodusNotifier/main.c | 134 +++++++++++++++ parodusNotifier/notifier_log.h | 53 ++++++ parodusNotifier/parodus_notifier.c | 265 +++++++++++++++++++++++++++++ parodusNotifier/parodus_notifier.h | 44 +++++ 7 files changed, 534 insertions(+) create mode 100644 parodusNotifier/CMakeLists.txt create mode 100644 parodusNotifier/main.c create mode 100644 parodusNotifier/notifier_log.h create mode 100644 parodusNotifier/parodus_notifier.c create mode 100644 parodusNotifier/parodus_notifier.h diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e6c0af..81e7f19 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Re-enabled the temporarily commented out setting that prevented cyclic building - Switched from nanomsg (Release 1.1.2) to NNG (Release v1.0.1) - Revert from NNG +- Wrapper to send event messages is added ## [1.0.0] - 2018-06-19 ### Added diff --git a/CMakeLists.txt b/CMakeLists.txt index df14914..265186e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -145,3 +145,4 @@ if (BUILD_TESTING) add_subdirectory(tests) endif (BUILD_TESTING) +add_subdirectory(parodusNotifier) diff --git a/parodusNotifier/CMakeLists.txt b/parodusNotifier/CMakeLists.txt new file mode 100644 index 0000000..09cd888 --- /dev/null +++ b/parodusNotifier/CMakeLists.txt @@ -0,0 +1,36 @@ +# 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. + +set(PROJ_PARODUS_NOTIFIER_LIB parodusNotifier) + +file(GLOB HEADERS parodus_notifier.h) +set(SOURCES parodus_notifier.c) +#------------------------------------------------------------------------------- +# parodusNotifier library +#------------------------------------------------------------------------------- +add_library(${PROJ_PARODUS_NOTIFIER_LIB} STATIC ${HEADERS} ${SOURCES}) +add_library(${PROJ_PARODUS_NOTIFIER_LIB}.shared SHARED ${HEADERS} ${SOURCES}) +set_target_properties(${PROJ_PARODUS_NOTIFIER_LIB}.shared PROPERTIES COMPILE_FLAGS -DNOTIFIER_LIB) +set_target_properties(${PROJ_PARODUS_NOTIFIER_LIB}.shared PROPERTIES OUTPUT_NAME ${PROJ_PARODUS_NOTIFIER_LIB}) +install (TARGETS ${PROJ_PARODUS_NOTIFIER_LIB} DESTINATION lib${LIB_SUFFIX}) +install (TARGETS ${PROJ_PARODUS_NOTIFIER_LIB}.shared DESTINATION lib${LIB_SUFFIX}) +install (FILES parodus_notifier.h DESTINATION include/${PROJ_PARODUS_NOTIFIER_LIB}) +#------------------------------------------------------------------------------- +# parodusNotifier binary +#------------------------------------------------------------------------------- +set(BIN_SOURCES parodus_notifier.c main.c) +add_executable(parodusNotifiercli ${BIN_SOURCES} ${HEADERS}) +target_link_libraries (parodusNotifiercli libparodus -lwrp-c -ltrower-base64 -lmsgpackc -lnanomsg -lpthread -lcimplog ${LOGGER_LIBS}) +install (TARGETS parodusNotifiercli DESTINATION bin) + diff --git a/parodusNotifier/main.c b/parodusNotifier/main.c new file mode 100644 index 0000000..f77cf5e --- /dev/null +++ b/parodusNotifier/main.c @@ -0,0 +1,134 @@ +/** + * 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 +#include +#include +#include +#include +#include "../src/libparodus.h" +#include "parodus_notifier.h" + +static void print_usage(); +int debugEnable = 0; + +int main( int argc, char **argv) +{ + notifier_t *msg = NULL; + msg = (notifier_t *) malloc(sizeof(notifier_t)); + memset(msg, 0, sizeof(notifier_t)); + const char *option_string = "n:s:d:p:c:D::"; + char *status = NULL; + + static const struct option long_options[] = { + {"service-name", required_argument, 0, 'n'}, + {"source", required_argument, 0, 's'}, + {"destination", required_argument, 0, 'd'}, + {"payload", required_argument, 0, 'p'}, + {"content-type", required_argument, 0, 'c'}, + {"DEBUG", no_argument, 0, 'D'}, + {0, 0, 0, 0} + }; + + int c; + optind = 1; + + while(1) + { + /* getopt_long stores the option index here. */ + int option_index = 0; + c = getopt_long (argc, argv, option_string, long_options, &option_index); + + /* Detect the end of the options. */ + if (c == -1) + break; + + switch (c) + { + case 'n': + msg->service_name = strdup(optarg); + break; + + case 's': + msg->source = strdup(optarg); + break; + + case 'd': + msg->destination = strdup(optarg); + break; + + case 'p': + msg->payload = strdup(optarg); + break; + + case 'c': + msg->content_type = strdup(optarg); + break; + + case 'D': + debugEnable = 1; + printf("DEBUG mode is ON\n"); + break; + + case '?': + /* getopt_long already printed an error message. */ + break; + + default: + fprintf(stderr,"Enter valid arguments..\n"); + print_usage(); + return -1; + } + } + + /* Print any remaining command line arguments (not options). */ + if (optind < argc) + { + fprintf(stderr,"non-option ARGV-elements: "); + while (optind < argc) + fprintf(stderr,"%s ", argv[optind++]); + putchar ('\n'); + } + + if(debugEnable) + { + printf("service_name: %s\nsource: %s\ndestination: %s\ncontent_type: %s\npayload: %s\n",msg->service_name, msg->source, msg->destination, msg->content_type, msg->payload); + } + + int ret = send_event_to_parodus(msg); + get_status_message(ret, &status); + if(ret == 0) + { + fprintf(stdout,"%s\n",status); + } + else + { + fprintf(stderr,"%s\n",status); + if(ret > 0) + { + print_usage(); + } + } + free(status); + free_notifier_struct(msg); + return ret; +} + +static void print_usage() +{ + fprintf(stderr, "Usage:\nparodusNotifiercli --service-name= --source= --destination= --payload= --content-type= [--DEBUG]\n\n" ); +} diff --git a/parodusNotifier/notifier_log.h b/parodusNotifier/notifier_log.h new file mode 100644 index 0000000..975513b --- /dev/null +++ b/parodusNotifier/notifier_log.h @@ -0,0 +1,53 @@ +/** + * 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. + * + */ +#ifndef _NOTIFIER_LOG_H +#define _NOTIFIER_LOG_H + +extern int debugEnable; + +#ifdef NOTIFIER_LIB +#define LOGGING_MODULE "PARODUSNOTIFIER" +#define NotifError(...) cimplog_error(LOGGING_MODULE, __VA_ARGS__) +#define NotifInfo(...) cimplog_info(LOGGING_MODULE, __VA_ARGS__) +#define NotifPrint(...) cimplog_debug(LOGGING_MODULE, __VA_ARGS__) +#else +#define NotifPrint(...) \ +{ \ + if(debugEnable) \ + { \ + printf(__VA_ARGS__); \ + } \ +} + +#define NotifInfo(...) \ +{ \ + if(debugEnable) \ + { \ + printf(__VA_ARGS__); \ + } \ +} + +#define NotifError(...) \ +{ \ + if(debugEnable) \ + { \ + printf(__VA_ARGS__); \ + } \ +} +#endif + +#endif diff --git a/parodusNotifier/parodus_notifier.c b/parodusNotifier/parodus_notifier.c new file mode 100644 index 0000000..1621fef --- /dev/null +++ b/parodusNotifier/parodus_notifier.c @@ -0,0 +1,265 @@ +/** + * 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 +#include +#include +#include +#include +#include "../src/libparodus.h" +#include "parodus_notifier.h" +#include "notifier_log.h" +#include +#include + +/*----------------------------------------------------------------------------*/ +/* Macros */ +/*----------------------------------------------------------------------------*/ +#define PARODUS_URL_DEFAULT "tcp://127.0.0.1:6666" +#define DEVICE_PROPS_FILE "/etc/device.properties" + +/*----------------------------------------------------------------------------*/ +/* Function Prototypes */ +/*----------------------------------------------------------------------------*/ +static void get_parodus_url(char **url); +static void getCurrentTime(struct timespec *timer); +static long timeValDiff(struct timespec *starttime, struct timespec *finishtime); +static int validate_notifier_struct(notifier_t *msg); +static libpd_instance_t client_instance; +/*----------------------------------------------------------------------------*/ +/* External Functions */ +/*----------------------------------------------------------------------------*/ +int send_event_to_parodus(notifier_t *msg) +{ + char *parodus_url = NULL; + int ret = -1; + wrp_msg_t *notif_wrp_msg = NULL; + struct timespec start,end,*startPtr,*endPtr; + startPtr = &start; + endPtr = &end; + static int init_done = 0; + + getCurrentTime(startPtr); + + ret = validate_notifier_struct(msg); + if(ret == SUCCESS) + { + NotifPrint("service_name: %s source: %s destination: %s payload: %s content_type: %s\n",msg->service_name, msg->source, msg->destination, msg->payload, msg->content_type); + if( !init_done ) + { + get_parodus_url(&parodus_url); + libpd_cfg_t cfg = {.service_name = msg->service_name, + .receive = false, + .keepalive_timeout_secs = 0, + .parodus_url = parodus_url, + .client_url = NULL + }; + + ret = libparodus_init (&client_instance, &cfg); + if(ret != 0) + { + exit(0); + } + NotifPrint("Init for parodus Success..!!\n"); + init_done = 1; + } + + notif_wrp_msg = (wrp_msg_t *)malloc(sizeof(wrp_msg_t)); + if(notif_wrp_msg != NULL) + { + memset(notif_wrp_msg, 0, sizeof(wrp_msg_t)); + notif_wrp_msg ->msg_type = WRP_MSG_TYPE__EVENT; + notif_wrp_msg ->u.event.source = strdup(msg->source); + notif_wrp_msg ->u.event.dest = strdup(msg->destination); + notif_wrp_msg->u.event.content_type = strdup(msg->content_type); + notif_wrp_msg ->u.event.payload = (void *)(strdup(msg->payload)); + notif_wrp_msg ->u.event.payload_size = strlen(notif_wrp_msg ->u.event.payload); + ret = libparodus_send(client_instance, notif_wrp_msg ); + if(ret == 0) + { + NotifPrint("Notification successfully sent to parodus\n"); + } + wrp_free_struct (notif_wrp_msg); + } + + getCurrentTime(endPtr); + NotifPrint("Elapsed time : %ld ms\n", timeValDiff(startPtr, endPtr)); + + } + return ret; +} + +void free_notifier_struct(notifier_t *msg) +{ + if(msg != NULL) + { + if(msg->source != NULL) + { + free(msg->source); + msg->source = NULL; + } + + if(msg->destination != NULL) + { + free(msg->destination); + msg->destination = NULL; + } + + if(msg->service_name != NULL) + { + free(msg->service_name); + msg->service_name = NULL; + } + + if(msg->content_type != NULL) + { + free(msg->content_type); + msg->content_type = NULL; + } + + if(msg->payload != NULL) + { + free(msg->payload); + msg->payload = NULL; + } + free(msg); + msg = NULL; + } +} + +void get_status_message(notifier_status_t status, char **message) +{ + if(status == SUCCESS) + { + *message = strdup("Event send success"); + } + else if(status == MESSAGE_IS_NULL) + { + *message = strdup("message is NULL"); + } + else if(status == SERVICE_NAME_IS_NULL) + { + *message = strdup("service_name is required"); + } + else if(status == SOURCE_IS_NULL) + { + *message = strdup("source is required"); + } + else if(status == DESTINATION_IS_NULL) + { + *message = strdup("destination is required"); + } + else if(status == PAYLOAD_IS_NULL) + { + *message = strdup("payload is required"); + } + else if(status == CONTENT_TYPE_IS_NULL) + { + *message = strdup("content_type is required"); + } + else + { + *message = strdup(libparodus_strerror((libpd_error_t)status)); + } +} + +const char *rdk_logger_module_fetch(void) +{ + return "LOG.RDK.PARODUSNOTIFIER"; +} + +/*----------------------------------------------------------------------------*/ +/* Internal Functions */ +/*----------------------------------------------------------------------------*/ +static void get_parodus_url(char **url) +{ + FILE *fp = fopen(DEVICE_PROPS_FILE, "r"); + + if( NULL != fp ) { + char str[255] = {'\0'}; + while( fscanf(fp,"%s", str) != EOF) { + char *value = NULL; + if(( value = strstr(str, "PARODUS_URL="))) + { + value = value + strlen("PARODUS_URL="); + *url = strdup(value); + NotifPrint("parodus url is %s\n", *url); + } + } + } else { + NotifError("Failed to open device.properties file:%s\n", DEVICE_PROPS_FILE); + NotifPrint("Adding default value for parodus_url\n"); + *url = strdup(PARODUS_URL_DEFAULT); + } + fclose(fp); + + if( NULL == *url ) { + NotifPrint("parodus url is not present in device.properties file, adding default parodus_url\n"); + *url = strdup(PARODUS_URL_DEFAULT); + } + NotifPrint("parodus url formed is %s\n", *url); +} + +static void getCurrentTime(struct timespec *timer) +{ + clock_gettime(CLOCK_REALTIME, timer); +} + +static long timeValDiff(struct timespec *starttime, struct timespec *finishtime) +{ + long msec; + msec=(finishtime->tv_sec-starttime->tv_sec)*1000; + msec+=(finishtime->tv_nsec-starttime->tv_nsec)/1000000; + return msec; +} + +static int validate_notifier_struct(notifier_t *msg) +{ + if(msg != NULL) + { + if(msg->service_name == NULL || strlen(msg->service_name) <= 0 ) + { + return SERVICE_NAME_IS_NULL; + } + + if(msg->source == NULL || strlen(msg->source) <= 0 ) + { + return SOURCE_IS_NULL; + } + + if(msg->destination == NULL || strlen(msg->destination) <= 0 ) + { + return DESTINATION_IS_NULL; + } + + if(msg->payload == NULL || strlen(msg->payload) <= 0 ) + { + return PAYLOAD_IS_NULL; + } + + if(msg->content_type == NULL || strlen(msg->content_type) <= 0 ) + { + return CONTENT_TYPE_IS_NULL; + } + } + else + { + return MESSAGE_IS_NULL; + } + + return SUCCESS; +} diff --git a/parodusNotifier/parodus_notifier.h b/parodusNotifier/parodus_notifier.h new file mode 100644 index 0000000..2cdb5a7 --- /dev/null +++ b/parodusNotifier/parodus_notifier.h @@ -0,0 +1,44 @@ +/** + * 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. + * + */ +#ifndef _PARODUS_NOTIFIER_H +#define _PARODUS_NOTIFIER_H + +typedef struct { + char *service_name; + char *content_type; + char *source; + char *destination; + char *payload; +} notifier_t; + +typedef enum { + SUCCESS = 0, + MESSAGE_IS_NULL, + SERVICE_NAME_IS_NULL, + SOURCE_IS_NULL, + DESTINATION_IS_NULL, + PAYLOAD_IS_NULL, + CONTENT_TYPE_IS_NULL +} notifier_status_t; + +int send_event_to_parodus(notifier_t *msg); + +void free_notifier_struct(notifier_t *msg); + +void get_status_message(notifier_status_t status, char **message); + +#endif