summaryrefslogtreecommitdiffstats
path: root/extras/emacs/periodic-skel.el
blob: a8f3ef6db8295af129a4ea283dcd0e0cf0c51545 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
;;; pipe-skel.el - pipelined graph node skeleton

(require 'skeleton)

(define-skeleton skel-periodic
"Insert a skeleton periodic process node"
nil
'(setq node-name (skeleton-read "Name: "))
'(setq uc-node-name (upcase node-name))
'(setq poll-period (skeleton-read "Poll period (f64 seconds, e.g. 10.0): "))

"
#define " uc-node-name "_POLL_PERIOD " poll-period "

static uword
" node-name "_process (vlib_main_t * vm,
                       vlib_node_runtime_t * rt,
                       vlib_frame_t * f)
{
    f64 poll_time_remaining;
    uword event_type, * event_data = 0;

    poll_time_remaining = " uc-node-name "_POLL_PERIOD;
    while (1) {
        int i;

        /* 
         * Sleep until next periodic call due, or until we receive event(s) 
         */
        poll_time_remaining = 
            vlib_process_wait_for_event_or_clock (vm, poll_time_remaining);
        
        event_type = vlib_process_get_events (vm, &event_data);
        switch (event_type) {
        case ~0:                /* no events => timeout */
            break;

        /* 
         * $$$$ FIXME: add cases / handlers for each event type 
         */
        case EVENT1:
            for (i = 0; i < vec_len (event_data); i++) 
                handle_event1 (mm, event_data[i]);
            break;

        case EVENT2:
            for (i = 0; i < vec_len (event_data); i++) 
                handle_event2 (vm, event_data[i]);
	    break;

        /* ... and so forth for each event type */

        default:
            /* This should never happen... */
            clib_warning (\"BUG: unhandled event type %d\", event_type);
            break;
        }
        if (event_data)
            _vec_len (event_data) = 0;

        /* Timer expired, call periodic function */
        if (vlib_process_suspend_time_is_zero (poll_time_remaining)) {
            " node-name "_periodic (vm);
            poll_time_remaining = " uc-node-name "_POLL_PERIOD;
        }
    }

    return 0;
}

/*
 * " node-name " periodic node declaration 
 */
static VLIB_REGISTER_NODE (" node-name "_node) = {
    .function = " node-name "_process,
    .type = VLIB_NODE_TYPE_PROCESS,
    .name = \"" node-name "-process\",
};

/*
 * To signal an event:
 *
 * vlib_process_signal_event (vm, " node-name "_node.index, EVENTn, datum);
 *
 */
")