#include <cnet.h>

/*  This is a simple cnet protocol source file which demonstrates the use
    of timer events in cnet.

    The supplied function reboot_node() will first be called for each node.
    reboot_node() informs cnet that the node is interested in the EV_TIMER1
    events and that cnet should call timeouts() each time EV_TIMER1 occurs.
    Finally, reboot_node() requests that EV_TIMER1 occurs in 1000ms (1s).

    When EV_TIMER1 occurs, cnet will call timeouts(), which will print
    either tick or tock. As each timer event occurs only once, timeouts()
    must finally reschedule EV_TIMER1 to occur again in another 1000ms.
 */



void timeouts(CnetEvent ev, CnetTimer ts, CnetData data)
{
    static int which = 0;

    printf("%3d.\t%s\n", which, which%2 ? "\ttock" : "tick");
    ++which;

/*  Reschedule EV_TIMER1 to occur again in 1000ms */

    CNET_start_timer(EV_TIMER1, 1000, 0);
}


void reboot_node(CnetEvent ev, CnetTimer ts, CnetData data)
{

/*  Indicate that we are interested in the EV_TIMER1 event */

    CHECK(CNET_set_handler(EV_TIMER1, timeouts, 0));

/*  Request that EV_TIMER1 occur in 1000ms, ignoring the return value */

    CNET_start_timer(EV_TIMER1, 1000, 0);
}
