CreateWallTimer

Principles

Figure 1 Process of invoking the interfaces for creating a wall timer shows the process of creating a wall timer.

Figure 1 Process of invoking the interfaces for creating a wall timer
  1. Initialize resources.

    The process is similar to that of Timers > CreateTimer > Initialize resources. You must invoke the OpenHiva::Init interface to initialize the timer.

  2. Create a wall timer.
    1. Before creating a timer, you must register a node, that is, create a node handle OpenHiva::Node n.
    2. Use the created node handle n to invoke the Hiva::CreateWallTimer interface to create a wall timer. After the interface is invoked, the Hiva::WallTimer object is returned.

      When the CreateWallTimer interface is invoked, the input parameter groupName must be the same as that set in the OpenHiva::Init interface. In addition, the settings of the input parameters oneshot and autostart affect the number of times that the wall timer can be triggered and the startup mode of the wall timer.

      • oneshot: indicates the number of times that the wall timer can be triggered.
        • True: The timer uses the one-off oneshot mode. During the running time of the process, the timer is triggered only once.
        • False (default value): indicates that the timer uses the periodic mode. During the running time of the process, the timer is triggered periodically until the process resources are cleared.
      • autostart: indicates the startup mode of the wall timer.
        • True (default value): indicates automatic startup. When the CreateWallTimer interface is returned, the timer is started.
        • False: indicates that automatic startup is disabled. When the CreateWallTimer interface is returned, the timer is not started. You need to manually start the timer by invoking the Hiva::WallTimer::Start interface.
    3. You can use the Hiva::WallTimer object to start or stop the timer (Hiva::WallTimer::Start/Hiva::WallTimer::Stop) and set the timer triggering period (Hiva::WallTimer::SetPeriod).

      The timer triggering period is the period for triggering the callback function. When the timer expires, the callback function of the user is executed.

  3. Release resources.

    Before the process ends, invoke the OpenHiva::Shutdown interface to clear resources. After resources are released, the defined OpenHiva interfaces cannot be used.

Sample Code

The following is a code example of key steps for creating a wall timer (for reference only):

#include <cstdlib>
#include <chrono>
#include <thread>
#include <string>
#include <stdlib.h>
#include <Hiva.h>
#include "Hiva_time/Hiva_time.h"
#include "Hiva_timer/wall_timer.h"
#include "open/init.h"
#include "open/node.h"

// Subfunction of the callback function, which is user-defined.
/static void PrintEvent(const Hiva::WallTimerEvent &evt)
{
    std::cout<<"\tlastExpected:"<<evt.lastExpected<<std::endl;       // Time when the last callback should occur
    std::cout<<"\tlastReal:"<<evt.lastReal<<std::endl;               // Time when the last callback occurred
    std::cout<<"\tlastExpired:"<<evt.lastExpired<<std::endl;         // Time when the last timer expires on the timer server and a notification is sent to the application
    std::cout<<"\tcurrentExpected:"<<evt.currentExpected<<std::endl; // Time when the current callback should occur
    std::cout<<"\tcurrenReal:"<<evt.currentReal<<std::endl;          // Time when the current callback occurred
    std::cout<<"\tcurrenExpired:"<<evt.currentExpired<<std::endl;    // Time when the current timer expires on the timer server and a notification is sent to the application
}

/ Users define callback functions based on their own service logic for processing after the timer expires.
static void callback1(const Hiva::WallTimerEvent &evt)
{
    HIVA_INFO("callback1 called:");
    PrintEvent(evt);
}

int main(int argc, char **argv)
{
    // 1. Initialize resources.
    bool oneshot = false;                // Indicates the triggering mode, which can be one-off triggering or periodic triggering.
    bool autostart = true;              // Indicates whether the timer is automatically started or manually started.
    int32_t sec = 1;                    // Indicates timer triggering period.
    int32_t sleepDuration = 60;         // Indicates run time of the simulation process.
    if (argc == 1) {
        HIVA_INFO("Default: oneshot: %d, autostart: %d, duration: %d, process sleep time:%d", (int)oneshot, (int)autostart, sec, sleepDuration);
    } elseif (argc == 5) {
        oneshot = (bool)strtol(argv[1], NULL, 10);
        autostart = (bool)strtol(argv[2], NULL, 10);
        sec = (int32_t)strtol(argv[3], NULL, 10);
        sleepDuration = (int32_t)strtol(argv[4], NULL, 10);
        HIVA_INFO("oneshot: %d, autostart: %d, duration: %d, process sleep time:%d", (int)oneshot, (int)autostart, sec, sleepDuration);
    } else {
        HIVA_ERROR("Input para is wrong, should be no input or 4 paras: oneshot, autostart, duration, process sleep time\n");
        return -1;
}
    // Define a thread group.
    std::vector<OpenHiva::ScheduleGroup> threadGroup;
    OpenHiva::ScheduleGroup testGroup;
    testGroup.groupName =  "timer";
    testGroup.scheduleType = OpenHiva::ScheduleType::UNBIND_AICPU;
    threadGroup.push_back(testGroup);
    // Invoke the resource initialization interface.
    OpenHiva::Init(argc,argv,threadGroup);

    // 2. Create a timer.
    // Construct the Node object.
    OpenHiva::Node node("timer_client_test");
    HIVA_INFO("timer test: Client test start timer");
    // Use the CreateWallTimer interface to create a timer. The value of groupName must be the same as that of ThreadGroup.groupName in the Init interface.
    Hiva::WallTimer t = Hiva::CreateWallTimer(Hiva::WallDuration(sec), callback1, "timer", oneshot, autostart);
    HIVA_INFO("timer test: client test start to sleep");
    std::this_thread::sleep_for(std::chrono::seconds(sleepDuration));
    HIVA_INFO("timer test: finish sleep");

    // 3. Release resources.
    // When the process ends, invoke the ShutDown function to release resources.
    OpenHiva::Shutdown();
    return0;
}