author | Tero Marttila <terom@fixme.fi> |
Thu, 28 Aug 2008 01:34:14 +0300 | |
changeset 44 | 03a7e064f833 |
parent 43 | e5b714190dee |
child 46 | 8a832c0e01ee |
permissions | -rw-r--r-- |
40 | 1 |
|
2 |
#include <stdlib.h> |
|
3 |
#include <assert.h> |
|
4 |
||
5 |
#include "request.h" |
|
6 |
#include "memcache.h" |
|
7 |
#include "../common.h" |
|
8 |
||
44
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
9 |
struct memcache_req *memcache_req_alloc (struct memcache *mc, enum memcache_command cmd_type, const struct memcache_key *key, const struct memcache_obj *obj, const struct memcache_buf *buf, void *cb_arg) { |
40 | 10 |
struct memcache_req *req = NULL; |
44
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
11 |
|
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
12 |
// ensure key is provided |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
13 |
assert(key != NULL); |
40 | 14 |
|
15 |
// allocate it |
|
16 |
if ((req = calloc(1, sizeof(*req))) == NULL) |
|
17 |
ERROR("calloc"); |
|
18 |
||
19 |
// state |
|
41
540737bf6bac
sending requests, and partial support for receiving -- incomplete, not tested
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
20 |
req->state = MEMCACHE_STATE_INVALID; |
40 | 21 |
|
22 |
// copy the key |
|
23 |
if ((req->key.buf = malloc(key->len)) == NULL) |
|
24 |
ERROR("malloc key buf"); |
|
25 |
||
26 |
memcpy(req->key.buf, key->buf, key->len); |
|
27 |
req->key.len = key->len; |
|
44
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
28 |
|
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
29 |
// copy the obj if provided |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
30 |
if (obj) { |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
31 |
memcpy(&req->obj, obj, sizeof(req->obj)); |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
32 |
req->have_obj = 1; |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
33 |
} |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
34 |
|
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
35 |
// copy the buf if provided |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
36 |
if (buf) { |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
37 |
memcpy(&req->buf, buf, sizeof(req->buf)); |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
38 |
req->have_buf = 1; |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
39 |
} |
40 | 40 |
|
41
540737bf6bac
sending requests, and partial support for receiving -- incomplete, not tested
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
41 |
// store the other data |
40 | 42 |
req->mc = mc; |
41
540737bf6bac
sending requests, and partial support for receiving -- incomplete, not tested
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
43 |
req->cmd_type = cmd_type; |
40 | 44 |
req->cb_arg = cb_arg; |
45 |
||
46 |
// success |
|
47 |
return req; |
|
48 |
||
49 |
error: |
|
50 |
if (req) { |
|
51 |
free(req->key.buf); |
|
52 |
free(req); |
|
53 |
} |
|
54 |
||
55 |
return NULL; |
|
56 |
} |
|
57 |
||
44
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
58 |
// accessors |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
59 |
enum memcache_req_state memcache_req_state (struct memcache_req *req) { |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
60 |
return req->state; |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
61 |
} |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
62 |
|
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
63 |
enum memcache_command memcache_req_cmd (struct memcache_req *req) { |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
64 |
return req->cmd_type; |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
65 |
} |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
66 |
|
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
67 |
enum memcache_reply memcache_req_reply (struct memcache_req *req) { |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
68 |
return req->reply_type; |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
69 |
} |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
70 |
|
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
71 |
const struct memcache_key *keymemcache_req_key (struct memcache_req *req) { |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
72 |
return &req->key; |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
73 |
} |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
74 |
|
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
75 |
const struct memcache_obj *memcache_req_obj (struct memcache_req *req) { |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
76 |
return req->have_obj ? &req->obj : NULL; |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
77 |
} |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
78 |
|
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
79 |
const struct memcache_buf *memcache_req_buf (struct memcache_req *req) { |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
80 |
return req->have_buf ? &req->buf : NULL; |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
81 |
} |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
82 |
|
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
83 |
// events |
42
0e503189af2f
more reply-receiving code, but still incomplete
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
84 |
static void _memcache_req_notify (struct memcache_req *req) { |
0e503189af2f
more reply-receiving code, but still incomplete
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
85 |
req->mc->cb_fn(req, req->cb_arg); |
40 | 86 |
} |
87 |
||
42
0e503189af2f
more reply-receiving code, but still incomplete
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
88 |
void memcache_req_queued (struct memcache_req *req) { |
0e503189af2f
more reply-receiving code, but still incomplete
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
89 |
req->state = MEMCACHE_STATE_QUEUED; |
0e503189af2f
more reply-receiving code, but still incomplete
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
90 |
|
0e503189af2f
more reply-receiving code, but still incomplete
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
91 |
// _memcache_req_notify(req); |
0e503189af2f
more reply-receiving code, but still incomplete
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
92 |
} |
0e503189af2f
more reply-receiving code, but still incomplete
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
93 |
|
0e503189af2f
more reply-receiving code, but still incomplete
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
94 |
void memcache_req_send (struct memcache_req *req) { |
0e503189af2f
more reply-receiving code, but still incomplete
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
95 |
req->state = MEMCACHE_STATE_SEND; |
0e503189af2f
more reply-receiving code, but still incomplete
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
96 |
|
0e503189af2f
more reply-receiving code, but still incomplete
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
97 |
// _memcache_req_notify(req); |
0e503189af2f
more reply-receiving code, but still incomplete
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
98 |
} |
0e503189af2f
more reply-receiving code, but still incomplete
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
99 |
|
44
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
100 |
void memcache_req_recv (struct memcache_req *req, enum memcache_reply reply_type) { |
42
0e503189af2f
more reply-receiving code, but still incomplete
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
101 |
req->state = MEMCACHE_STATE_REPLY; |
0e503189af2f
more reply-receiving code, but still incomplete
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
102 |
req->reply_type = reply_type; |
0e503189af2f
more reply-receiving code, but still incomplete
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
103 |
|
44
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
104 |
// we must surely have a valid obj now |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
105 |
req->have_obj = 1; |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
106 |
|
42
0e503189af2f
more reply-receiving code, but still incomplete
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
107 |
_memcache_req_notify(req); |
0e503189af2f
more reply-receiving code, but still incomplete
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
108 |
} |
0e503189af2f
more reply-receiving code, but still incomplete
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
109 |
|
43
e5b714190dee
the request/reply code should be complete now, but still needs testing
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
110 |
void memcache_req_data (struct memcache_req *req) { |
e5b714190dee
the request/reply code should be complete now, but still needs testing
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
111 |
assert(req->state == MEMCACHE_STATE_REPLY || req->state == MEMCACHE_STATE_REPLY_DATA); |
e5b714190dee
the request/reply code should be complete now, but still needs testing
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
112 |
|
e5b714190dee
the request/reply code should be complete now, but still needs testing
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
113 |
req->state = MEMCACHE_STATE_REPLY_DATA; |
44
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
114 |
|
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
115 |
// we must surely have a valid buf now |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
116 |
req->have_buf = 1; |
43
e5b714190dee
the request/reply code should be complete now, but still needs testing
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
117 |
|
e5b714190dee
the request/reply code should be complete now, but still needs testing
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
118 |
_memcache_req_notify(req); |
e5b714190dee
the request/reply code should be complete now, but still needs testing
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
119 |
} |
e5b714190dee
the request/reply code should be complete now, but still needs testing
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
120 |
|
42
0e503189af2f
more reply-receiving code, but still incomplete
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
121 |
void memcache_req_done (struct memcache_req *req) { |
43
e5b714190dee
the request/reply code should be complete now, but still needs testing
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
122 |
// make sure we are in the REPLY/REPLY_DATA state |
e5b714190dee
the request/reply code should be complete now, but still needs testing
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
123 |
assert(req->state == MEMCACHE_STATE_REPLY || req->state == MEMCACHE_STATE_REPLY_DATA); |
44
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
124 |
|
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
125 |
// are we supposed to have data? |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
126 |
if (req->buf.data) { |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
127 |
// make sure we really have the full data, if applicable |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
128 |
assert(req->buf.offset == req->buf.len); |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
129 |
|
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
130 |
// yes... |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
131 |
req->have_buf = 1; |
43
e5b714190dee
the request/reply code should be complete now, but still needs testing
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
132 |
|
44
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
133 |
// have data |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
134 |
req->state = MEMCACHE_STATE_DATA_DONE; |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
135 |
|
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
136 |
} else { |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
137 |
// no data |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
138 |
req->state = MEMCACHE_STATE_DONE; |
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
139 |
} |
42
0e503189af2f
more reply-receiving code, but still incomplete
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
140 |
|
0e503189af2f
more reply-receiving code, but still incomplete
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
141 |
// forget the connection |
0e503189af2f
more reply-receiving code, but still incomplete
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
142 |
req->conn = NULL; |
43
e5b714190dee
the request/reply code should be complete now, but still needs testing
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
143 |
|
44
03a7e064f833
stub functions and documentation
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
144 |
_memcache_req_notify(req); |
43
e5b714190dee
the request/reply code should be complete now, but still needs testing
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
145 |
} |
42
0e503189af2f
more reply-receiving code, but still incomplete
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
146 |
|
43
e5b714190dee
the request/reply code should be complete now, but still needs testing
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
147 |
void memcache_req_error (struct memcache_req *req) { |
e5b714190dee
the request/reply code should be complete now, but still needs testing
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
148 |
// forget our connection |
e5b714190dee
the request/reply code should be complete now, but still needs testing
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
149 |
req->conn = NULL; |
e5b714190dee
the request/reply code should be complete now, but still needs testing
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
150 |
|
e5b714190dee
the request/reply code should be complete now, but still needs testing
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
151 |
req->state = MEMCACHE_STATE_ERROR; |
e5b714190dee
the request/reply code should be complete now, but still needs testing
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
152 |
|
e5b714190dee
the request/reply code should be complete now, but still needs testing
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
153 |
_memcache_req_notify(req); |
40 | 154 |
} |
155 |
||
156 |
void memcache_req_free (struct memcache_req *req) { |
|
157 |
// must be unused |
|
158 |
assert(req->conn == NULL); |
|
43
e5b714190dee
the request/reply code should be complete now, but still needs testing
Tero Marttila <terom@fixme.fi>
parents:
42
diff
changeset
|
159 |
assert(req->state == MEMCACHE_STATE_INVALID || req->state == MEMCACHE_STATE_ERROR || req->state == MEMCACHE_STATE_DONE || req->state == MEMCACHE_STATE_DATA_DONE); |
40 | 160 |
|
161 |
free(req->key.buf); |
|
162 |
free(req); |
|
163 |
} |
|
164 |