File: timer.c

package info (click to toggle)
redict 7.3.6%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,112 kB
  • sloc: ansic: 129,434; tcl: 46,164; makefile: 930; python: 815; ruby: 572; sh: 482; javascript: 30
file content (107 lines) | stat: -rw-r--r-- 3,344 bytes parent folder | download | duplicates (3)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// SPDX-FileCopyrightText: 2024 Redict Contributors
// SPDX-FileCopyrightText: 2024 Salvatore Sanfilippo <antirez at gmail dot com>
//
// SPDX-License-Identifier: BSD-3-Clause
// SPDX-License-Identifier: LGPL-3.0-only

#include "redictmodule.h"

static void timer_callback(RedictModuleCtx *ctx, void *data)
{
    RedictModuleString *keyname = data;
    RedictModuleCallReply *reply;

    reply = RedictModule_Call(ctx, "INCR", "s", keyname);
    if (reply != NULL)
        RedictModule_FreeCallReply(reply);
    RedictModule_FreeString(ctx, keyname);
}

int test_createtimer(RedictModuleCtx *ctx, RedictModuleString **argv, int argc)
{
    if (argc != 3) {
        RedictModule_WrongArity(ctx);
        return REDICTMODULE_OK;
    }

    long long period;
    if (RedictModule_StringToLongLong(argv[1], &period) == REDICTMODULE_ERR) {
        RedictModule_ReplyWithError(ctx, "Invalid time specified.");
        return REDICTMODULE_OK;
    }

    RedictModuleString *keyname = argv[2];
    RedictModule_RetainString(ctx, keyname);

    RedictModuleTimerID id = RedictModule_CreateTimer(ctx, period, timer_callback, keyname);
    RedictModule_ReplyWithLongLong(ctx, id);

    return REDICTMODULE_OK;
}

int test_gettimer(RedictModuleCtx *ctx, RedictModuleString **argv, int argc)
{
    if (argc != 2) {
        RedictModule_WrongArity(ctx);
        return REDICTMODULE_OK;
    }

    long long id;
    if (RedictModule_StringToLongLong(argv[1], &id) == REDICTMODULE_ERR) {
        RedictModule_ReplyWithError(ctx, "Invalid id specified.");
        return REDICTMODULE_OK;
    }

    uint64_t remaining;
    RedictModuleString *keyname;
    if (RedictModule_GetTimerInfo(ctx, id, &remaining, (void **)&keyname) == REDICTMODULE_ERR) {
        RedictModule_ReplyWithNull(ctx);
    } else {
        RedictModule_ReplyWithArray(ctx, 2);
        RedictModule_ReplyWithString(ctx, keyname);
        RedictModule_ReplyWithLongLong(ctx, remaining);
    }

    return REDICTMODULE_OK;
}

int test_stoptimer(RedictModuleCtx *ctx, RedictModuleString **argv, int argc)
{
    if (argc != 2) {
        RedictModule_WrongArity(ctx);
        return REDICTMODULE_OK;
    }

    long long id;
    if (RedictModule_StringToLongLong(argv[1], &id) == REDICTMODULE_ERR) {
        RedictModule_ReplyWithError(ctx, "Invalid id specified.");
        return REDICTMODULE_OK;
    }

    int ret = 0;
    RedictModuleString *keyname;
    if (RedictModule_StopTimer(ctx, id, (void **) &keyname) == REDICTMODULE_OK) {
        RedictModule_FreeString(ctx, keyname);
        ret = 1;
    }

    RedictModule_ReplyWithLongLong(ctx, ret);
    return REDICTMODULE_OK;
}


int RedictModule_OnLoad(RedictModuleCtx *ctx, RedictModuleString **argv, int argc) {
    REDICTMODULE_NOT_USED(argv);
    REDICTMODULE_NOT_USED(argc);
    if (RedictModule_Init(ctx,"timer",1,REDICTMODULE_APIVER_1)== REDICTMODULE_ERR)
        return REDICTMODULE_ERR;

    if (RedictModule_CreateCommand(ctx,"test.createtimer", test_createtimer,"",0,0,0) == REDICTMODULE_ERR)
        return REDICTMODULE_ERR;
    if (RedictModule_CreateCommand(ctx,"test.gettimer", test_gettimer,"",0,0,0) == REDICTMODULE_ERR)
        return REDICTMODULE_ERR;
    if (RedictModule_CreateCommand(ctx,"test.stoptimer", test_stoptimer,"",0,0,0) == REDICTMODULE_ERR)
        return REDICTMODULE_ERR;

    return REDICTMODULE_OK;
}