LCOV - code coverage report
Current view: top level - src/plugins/zeromqrecv - zeromqrecv.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 57 59 96.6 %
Date: 2019-09-12 12:28:41 Functions: 7 8 87.5 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief Source for zeromqrecv plugin
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  *
       8             :  */
       9             : 
      10             : #ifndef HAVE_KDBCONFIG
      11             : #include "kdbconfig.h"
      12             : #endif
      13             : 
      14             : #include "zeromqrecv.h"
      15             : 
      16             : #include <kdbhelper.h>
      17             : #include <kdblogger.h>
      18             : 
      19             : #include <stdio.h>
      20             : 
      21             : 
      22             : /**
      23             :  * @see ElektraIoPluginSetBinding (kdbioplugin.h)
      24             :  */
      25           4 : void elektraZeroMqRecvSetIoBinding (Plugin * handle, KeySet * parameters)
      26             : {
      27           4 :         ELEKTRA_NOT_NULL (handle);
      28           4 :         ELEKTRA_NOT_NULL (parameters);
      29           4 :         ElektraZeroMqRecvPluginData * data = elektraPluginGetData (handle);
      30           4 :         ELEKTRA_NOT_NULL (data);
      31             : 
      32           4 :         Key * ioBindingKey = ksLookupByName (parameters, "/ioBinding", 0);
      33           4 :         ELEKTRA_NOT_NULL (ioBindingKey);
      34           4 :         ElektraIoInterface * binding = *(ElektraIoInterface **) keyValue (ioBindingKey);
      35             : 
      36           4 :         data->ioBinding = binding;
      37           4 : }
      38             : 
      39             : /**
      40             :  * @see ElektraNotificationOpenNotification (kdbnotificationinternal.h)
      41             :  */
      42           4 : void elektraZeroMqRecvOpenNotification (Plugin * handle, KeySet * parameters)
      43             : {
      44           4 :         ELEKTRA_NOT_NULL (handle);
      45           4 :         ElektraZeroMqRecvPluginData * pluginData = elektraPluginGetData (handle);
      46           4 :         ELEKTRA_NOT_NULL (pluginData);
      47             : 
      48             :         ElektraNotificationCallback callback;
      49           4 :         Key * callbackKey = ksLookupByName (parameters, "/callback", 0);
      50           4 :         ELEKTRA_NOT_NULL (callbackKey);
      51           4 :         callback = *(ElektraNotificationCallback *) keyValue (callbackKey);
      52             : 
      53             :         ElektraNotificationCallbackContext * context;
      54           4 :         Key * contextKey = ksLookupByName (parameters, "/context", 0);
      55           4 :         if (contextKey != NULL)
      56             :         {
      57           0 :                 context = *(ElektraNotificationCallbackContext **) keyValue (contextKey);
      58             :         }
      59             :         else
      60             :         {
      61             :                 context = NULL;
      62             :         }
      63             : 
      64           4 :         pluginData->notificationCallback = callback;
      65           4 :         pluginData->notificationContext = context;
      66             : 
      67             :         // init dbus connections
      68           4 :         if (pluginData->ioBinding)
      69             :         {
      70           4 :                 elektraZeroMqRecvSetup (pluginData);
      71             :         }
      72             :         else
      73             :         {
      74             :                 ELEKTRA_LOG_DEBUG ("no I/O binding present. plugin in noop mode");
      75             :         }
      76           4 : }
      77             : 
      78             : /**
      79             :  * @see ElektraNotificationCloseNotification (kdbnotificationinternal.h)
      80             :  */
      81           4 : void elektraZeroMqRecvCloseNotification (Plugin * handle, KeySet * parameters ELEKTRA_UNUSED)
      82             : {
      83           4 :         ElektraZeroMqRecvPluginData * pluginData = elektraPluginGetData (handle);
      84           4 :         pluginData->notificationCallback = NULL;
      85           4 :         pluginData->notificationContext = NULL;
      86             : 
      87           4 :         elektraZeroMqRecvTeardown (pluginData);
      88           4 : }
      89             : 
      90          24 : int elektraZeroMqRecvOpen (Plugin * handle, Key * errorKey ELEKTRA_UNUSED)
      91             : {
      92          24 :         Key * endpointKey = ksLookupByName (elektraPluginGetConfig (handle), "/endpoint", 0);
      93             :         const char * endpoint;
      94          24 :         if (endpointKey)
      95             :         {
      96           0 :                 endpoint = keyString (endpointKey);
      97             :         }
      98             :         else
      99             :         {
     100             :                 endpoint = ELEKTRA_ZEROMQ_DEFAULT_SUB_ENDPOINT;
     101             :         }
     102             : 
     103          24 :         ElektraZeroMqRecvPluginData * data = elektraPluginGetData (handle);
     104          24 :         if (!data)
     105             :         {
     106          24 :                 data = elektraMalloc (sizeof (*data));
     107          24 :                 data->ioBinding = NULL;
     108          24 :                 data->zmqContext = NULL;
     109          24 :                 data->zmqSubscriber = NULL;
     110          24 :                 data->zmqAdapter = NULL;
     111          24 :                 data->endpoint = endpoint;
     112             :         }
     113          24 :         elektraPluginSetData (handle, data);
     114             : 
     115          24 :         return 1; /* success */
     116             : }
     117             : 
     118          32 : int elektraZeroMqRecvGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey)
     119             : {
     120          32 :         if (!strcmp (keyName (parentKey), "system/elektra/modules/zeromqrecv"))
     121             :         {
     122          32 :                 KeySet * contract = ksNew (
     123             :                         30, keyNew ("system/elektra/modules/zeromqrecv", KEY_VALUE, "zeromqrecv plugin waits for your orders", KEY_END),
     124             :                         keyNew ("system/elektra/modules/zeromqrecv/exports", KEY_END),
     125             :                         keyNew ("system/elektra/modules/zeromqrecv/exports/open", KEY_FUNC, elektraZeroMqRecvOpen, KEY_END),
     126             :                         keyNew ("system/elektra/modules/zeromqrecv/exports/get", KEY_FUNC, elektraZeroMqRecvGet, KEY_END),
     127             :                         keyNew ("system/elektra/modules/zeromqrecv/exports/close", KEY_FUNC, elektraZeroMqRecvClose, KEY_END),
     128             :                         keyNew ("system/elektra/modules/zeromqrecv/exports/setIoBinding", KEY_FUNC, elektraZeroMqRecvSetIoBinding, KEY_END),
     129             :                         keyNew ("system/elektra/modules/zeromqrecv/exports/openNotification", KEY_FUNC, elektraZeroMqRecvOpenNotification,
     130             :                                 KEY_END),
     131             :                         keyNew ("system/elektra/modules/zeromqrecv/exports/closeNotification", KEY_FUNC, elektraZeroMqRecvCloseNotification,
     132             :                                 KEY_END),
     133             : #include ELEKTRA_README
     134             :                         keyNew ("system/elektra/modules/zeromqrecv/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END);
     135          32 :                 ksAppend (returned, contract);
     136          32 :                 ksDel (contract);
     137             : 
     138          32 :                 return 1; /* success */
     139             :         }
     140             : 
     141             :         return 1; /* success */
     142             : }
     143             : 
     144          24 : int elektraZeroMqRecvClose (Plugin * handle, Key * parentKey ELEKTRA_UNUSED)
     145             : {
     146          24 :         ElektraZeroMqRecvPluginData * pluginData = elektraPluginGetData (handle);
     147          24 :         if (pluginData == NULL)
     148             :         {
     149             :                 return 1;
     150             :         }
     151             : 
     152          24 :         elektraFree (pluginData);
     153          24 :         elektraPluginSetData (handle, NULL);
     154             : 
     155          24 :         return 1; /* success */
     156             : }
     157             : 
     158          24 : Plugin * ELEKTRA_PLUGIN_EXPORT
     159             : {
     160             :         // clang-format off
     161          24 :         return elektraPluginExport("zeromqrecv",
     162             :                 ELEKTRA_PLUGIN_OPEN,    &elektraZeroMqRecvOpen,
     163             :                 ELEKTRA_PLUGIN_GET,     &elektraZeroMqRecvGet,
     164             :                 ELEKTRA_PLUGIN_CLOSE,   &elektraZeroMqRecvClose,
     165             :                 ELEKTRA_PLUGIN_END);
     166             : }

Generated by: LCOV version 1.13