⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rvwatchdog.c

📁 基于h323协议的软phone
💻 C
字号:
/***********************************************************************
        Copyright (c) 2002 RADVISION Ltd.
************************************************************************
NOTICE:
This document contains information that is confidential and proprietary
to RADVISION Ltd.. No part of this document may be reproduced in any
form whatsoever without written prior approval by RADVISION Ltd..

RADVISION Ltd. reserve the right to revise this publication and make
changes without obligation to notify any person of such revisions or
changes.
***********************************************************************/

#include "rvstdio.h"
#include "rvmemory.h"
#include "rvwatchdog.h"

#ifdef __cplusplus
extern "C" {
#endif



/********************************************************************************
    RvWatchdogAllocate
    this function allocates the internal resource table of thewatchdog. it is the
    first thing initialize in this module. cmInitialize calls it at the beginnig
    of its operation
    INPUT -
     watchdog       - Watchdog object to initialize
     numOfResources - the numbers of resources the watchdog support. it is used
                      to detarmine the size of the table.
    OUTPUT - none
    RETURN -
        Non-negative value on success, other on failure
*********************************************************************************/
int RvWatchdogAllocate(IN RvWatchdog* watchdog, IN int numOfResources)
{
    RvSize_t tableSize;

    tableSize = numOfResources * sizeof(RvWatchdogTable_S);

    /*allocate place for the internal table*/
    if (RvMemoryAlloc(NULL, (void**)&watchdog->hTable, tableSize) != RV_OK)
        return RV_ERROR_OUTOFRESOURCES;

    memset(watchdog->hTable, 0, tableSize);
    watchdog->nextFreeEntrance = 0;
    watchdog->maxEntrances = numOfResources;
    watchdog->isInit = RV_FALSE;

    return RV_OK;
}


/********************************************************************************
    RvWatchdogInit
    this it the main initialization of the watchdog.
    cmInitialize calls it after initializing all its modules.
    the function used the callback function allready set in the internal table,
    and set initial and current values of each resource .
    INPUT -
        Hwd         - the watchdog handle
    OUTPUT - none
    RETURN -
            non negative value - if success.
            negative value     - if failed to initialize the ss instance.
*********************************************************************************/
int RvWatchdogInit(
    IN RvWatchdog*  Hwd)
{
    RvUint32 index;
    int result;
    RvWatchdogTable_S* entrance;
    RvUint32         value;

    for (index = 0; index < Hwd->nextFreeEntrance; index++)
    {
        entrance = &(Hwd->hTable[index]);
        if (entrance->initialized)
        {
            result = entrance->cbFunc(entrance->context, index, RvWatchdogCurrent, &value);
            if (result < 0)
                return result;
            entrance->currentValue = entrance->initialValue = value;
        }
    }
    Hwd->isInit = RV_TRUE;

    return 0;
}


/********************************************************************************
    RvWatchdogAddResource
    this function is called by the varios module s while initializing. it set a
    resource data in the internal table.
    INPUT -
        hApp             - the application handle - used to get the watchdog
                           handle
        name             - the name of the resource - to put in the table
        group            - the name of the group this resource belongs to
        description      - the description of the resource - to put in the table
        resourceCallback - the module callback function, whith which the watchdog
                           will get a resource value when needed
        context          - the application handle that will be sent to the call
                           back function
    OUTPUT -
        resourceEnumeration - the enumeratin of resources that will be sent to
                              the callback function
    RETURN -
        the recource value
*********************************************************************************/
int RvWatchdogAddResource(
    IN  RvWatchdog*                         hWD,
    IN  const char*                         name,
    IN  const char*                         group,
    IN  const char*                         description,
    IN  RvWatchdogResourceCallback_T        resourceCallback,
    IN  void*                               context,
    OUT RvUint32*                           resourceEnumeration)
{
    RvWatchdogTable_S *entrance;
    int result = 0;
    RvUint32 index;

    index = hWD->nextFreeEntrance;

    /*check if the table if full*/
    if (hWD->nextFreeEntrance == hWD->maxEntrances)
        return RV_ERROR_OUTOFRANGE;

    entrance              = &(hWD->hTable[index]);
    memset(entrance, 0, sizeof(RvWatchdogTable_S));
    entrance->initialized = RV_TRUE;
    entrance->context     = context;
    entrance->cbFunc      = resourceCallback;

    strncpy(entrance->name , name, sizeof(entrance->name));
    strncpy(entrance->group, group, sizeof(entrance->group));
    entrance->description = description;
    hWD->nextFreeEntrance++;

    *resourceEnumeration = index;

    /* if table was already initialized, we should init this resource's data now*/
    if (hWD->isInit)
    {
        RvUint32         value;
        result = entrance->cbFunc(entrance->context, index, RvWatchdogCurrent, &value);
        if (result < 0)
            return result;
        entrance->currentValue = entrance->initialValue = value;
    }

    return result;
}



/********************************************************************************
    RvWatchdogDeleteResource
    this function is called by the varios modules while to delete resources from
    the watchdog.
    INPUT -
        hApp                - the application handle - used to get the watchdog
                              handle
        resourceEnumeration - the enumeratino of the resource to delete
    OUTPUT - none
    RETURN -
            non negative value - if success.
            negative value     - if failed to initialize the ss instance.
*********************************************************************************/
int RvWatchdogDeleteResource(
    IN  RvWatchdog* hWD,
    IN  RvUint32    resourceEnumeration)
{
    if (hWD->hTable[resourceEnumeration].initialized)
    {
        hWD->hTable[resourceEnumeration].initialized = RV_FALSE;
        return RV_OK;
    }

    return RV_ERROR_UNINITIALIZED;
}



/********************************************************************************
    RvWatchdogPrint
    this function goes over all recources from the table, get thier values and
    print it at the log file.
    INPUT -
        hWD - Watchdog handle
        logSource - Log source to print to
    OUTPUT - none
    RETURN - non negative value - if success.
             negative value     - if failed to initialize the ss instance.
*********************************************************************************/
int RvWatchdogPrint(IN RvWatchdog* hWD, IN RvLogSource* logSource)
{
    RvUint32 index;
    RvUint32 maxVal, maxUse, curVal, deltaVal;
    int useMax, useMaxUsage, useCur;
    char buf[200];
    char* bufPtr;

    RvWatchdogTable_S* entrance;

    RvLogDebug(logSource, (logSource, "Resource name       |Initial data| Current   |Max usage |Max       |Delta     |"));

    for (index = 0; index < hWD->nextFreeEntrance; index++)
    {
        entrance = &(hWD->hTable[index]);

        if (entrance->initialized)
        {
            useMax = entrance->cbFunc(entrance->context, index, RvWatchdogMaxVal, &maxVal);
            useMaxUsage = entrance->cbFunc(entrance->context, index, RvWatchdogMaxUsage, &maxUse);
            useCur = entrance->cbFunc(entrance->context, index, RvWatchdogCurrent, &curVal);
            deltaVal = curVal - entrance->currentValue;

            bufPtr = buf;
            bufPtr += RvSprintf(bufPtr, "%19s | %11d| ", entrance->name, entrance->initialValue);
            if (useCur >= 0)
                bufPtr += RvSprintf(bufPtr, "%10d|", curVal);
            else
                bufPtr += RvSprintf(bufPtr, "        NA|");
            if (useMaxUsage >= 0)
                bufPtr += RvSprintf(bufPtr, "%10d|", maxUse);
            else
                bufPtr += RvSprintf(bufPtr, "        NA|");
            if (useMax >= 0)
                bufPtr += RvSprintf(bufPtr, "%10d|", maxVal);
            else
                bufPtr += RvSprintf(bufPtr, "        NA|");
            RvSprintf(bufPtr, "%10d|", deltaVal);

            RvLogDebug(logSource, (logSource, buf));
        }
    }

    return 0;
}




/********************************************************************************
    RvWatchdogNextResource
    this function gets the names of resources from the stack. It can be used
    to loop on all the resources of the stack with RvWatchdogGetResource().
    To stop looping, make sure the return value is non-negative.
    INPUT -
        hApp            - the application handle
        prevResource    - Handle to previous resource.
                          First time should be called with NULL
    OUTPUT -
        resourceHandle  - Handle to the current resource
                          Should be used as prevResourcefor the next call to this
                          function.
        resourceName    - Name of the resource (maximum of 40 bytes)
                          Can be used for RvWatchdogGetResource()
    RETURN - non negative value - if success.
             negative value     - if failed or if there are no more resource.
*********************************************************************************/
int RvWatchdogNextResource(
    IN  RvWatchdog* hWD,
    IN  void*   prevResource,
    OUT void**  resourceHandle,
    OUT char*   resourceName)
{
    RvUint32 index = (RvUint32)prevResource;

    *resourceHandle = NULL;
    resourceName[0] = '\0';

    if (hWD->nextFreeEntrance == 0)
        return RV_ERROR_UNKNOWN;

    /* Find the next unused entry */
    while ((index < hWD->nextFreeEntrance) && (!hWD->hTable[index].initialized))
        index++;

    if (index >= hWD->nextFreeEntrance)
    {
        /* We're done... */
        return RV_ERROR_UNKNOWN;
    }

    *resourceHandle = (void*)RvUint32Const(index+1);
    strncpy(resourceName, hWD->hTable[index].name, 40);

    return 0;
}


/********************************************************************************
RvWatchdogGetResource
this function get the value of a specific resource and return it
    INPUT -
        hApp  - the application handle
        name  - the resource name, used to locate resource in the table
        type  - the type of data needed (current/max/....)
    OUTPUT -
        value - the value of the resource requested
        resourceGroupName - name of group this resource belongs to. Can be passed
                            as NULL. Up to 40 characters
    RETURN - non negative value - if success.
             negative value     - if failed to initialize the ss instance.
*********************************************************************************/
int RvWatchdogGetResource(
    IN  RvWatchdog*                 hWD,
    IN  const char*                 name,
    IN  RvWatchdogResourceType      type,
    OUT RvUint32*                   value,
    OUT char*                       resourceGroupName)
{
    RvUint32 index;
    int result = RV_ERROR_UNKNOWN;
    RvWatchdogTable_S* entrance;

    for (index = 0; index < hWD->nextFreeEntrance; index++)
    {
        entrance = &(hWD->hTable[index]);
        if ((entrance->initialized) && (!strcmp(name,entrance->name)))
        {
            /* Found! */
            if (resourceGroupName != NULL)
                strncpy(resourceGroupName, entrance->group, 40);

            switch (type)
            {
            case RvWatchdogInitialVal:
                {
                    *value = entrance->initialValue;
                    result = 0;
                }
                break;
            case RvWatchdogMaxVal:
            case RvWatchdogMaxUsage:
            case RvWatchdogCurrent:
                {
                    result = entrance->cbFunc(entrance->context, index, type, value);
                }
                break;
            case RvWatchdogDeltaVal:
                {
                    result = entrance->cbFunc(entrance->context, index, type, value);
                    if (result >= 0)
                        *value -= entrance->currentValue;
                }
                break;
            }
        }
    }

    return result;
}



/********************************************************************************
    RvWatchdogEnd
        this function  free allocations made in teh module.
    INPUT -
     watchdog       - Watchdog object to initialize
    OUTPUT - none
    RETURN -
        Non-negative value
*********************************************************************************/
int RvWatchdogEnd(IN RvWatchdog* watchdog)
{
    RvMemoryFree(watchdog->hTable);
    return 0;
}



#ifdef __cplusplus
}
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -