forked from naver/arcus-memcached
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_protocol.c
114 lines (100 loc) · 3.8 KB
/
example_protocol.c
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
108
109
110
111
112
113
114
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "protocol_extension.h"
/*
* This is an example on how you can add your own commands into the
* ascii protocol. You load the extensions into memcached by using
* the -X option:
* ./memcached -X .libs/example_protocol.so -E .libs/default_engine.so
*
* @todo add an example that require extra userspace data, and communicates
* with the engine by getting the engine descriptor.
*/
static const char *get_name(const void *cmd_cookie);
static bool accept_command(const void *cmd_cookie, void *cookie,
int argc, token_t *argv, size_t *ndata,
char **ptr);
static bool execute_command(const void *cmd_cookie, const void *cookie,
int argc, token_t *argv,
bool (*response_handler)(const void *cookie,
int nbytes,
const char *dta));
static void abort_command(const void *cmd_cookie, const void *cookie);
static EXTENSION_ASCII_PROTOCOL_DESCRIPTOR noop_descriptor = {
.get_name = get_name,
.accept = accept_command,
.execute = execute_command,
.abort = abort_command,
.cookie = &noop_descriptor
};
static EXTENSION_ASCII_PROTOCOL_DESCRIPTOR echo_descriptor = {
.get_name = get_name,
.accept = accept_command,
.execute = execute_command,
.abort = abort_command,
.cookie = &echo_descriptor
};
static const char *get_name(const void *cmd_cookie) {
if (cmd_cookie == &noop_descriptor) {
return "noop";
} else {
return "echo";
}
}
static bool accept_command(const void *cmd_cookie, void *cookie,
int argc, token_t *argv, size_t *ndata,
char **ptr) {
if (cmd_cookie == &noop_descriptor) {
return strcmp(argv[0].value, "noop") == 0;
} else {
return strcmp(argv[0].value, "echo") == 0;
}
}
static bool execute_command(const void *cmd_cookie, const void *cookie,
int argc, token_t *argv,
bool (*response_handler)(const void *cookie,
int nbytes,
const char *dta)) {
if (cmd_cookie == &noop_descriptor) {
return response_handler(cookie, 4, "OK\r\n");
} else {
if (!response_handler(cookie, argv[0].length, argv[0].value)) {
return false;
}
for (int ii = 1; ii < argc; ++ii) {
if (!response_handler(cookie, 2, " [") ||
!response_handler(cookie, argv[ii].length, argv[ii].value) ||
!response_handler(cookie, 1, "]")) {
return false;
}
}
return response_handler(cookie, 2, "\r\n");
}
}
static void abort_command(const void *cmd_cookie, const void *cookie)
{
/* EMPTY */
}
#if defined (__SUNPRO_C) && (__SUNPRO_C >= 0x550)
__global
#elif defined __GNUC__
__attribute__ ((visibility("default")))
#endif
EXTENSION_ERROR_CODE memcached_extensions_initialize(const char *config,
GET_SERVER_API get_server_api) {
SERVER_HANDLE_V1 *server = get_server_api();
if (server == NULL) {
return EXTENSION_FATAL;
}
if (!server->extension->register_extension(EXTENSION_ASCII_PROTOCOL,
&noop_descriptor)) {
return EXTENSION_FATAL;
}
if (!server->extension->register_extension(EXTENSION_ASCII_PROTOCOL,
&echo_descriptor)) {
return EXTENSION_FATAL;
}
return EXTENSION_SUCCESS;
}