Skip to content

Commit 1057f57

Browse files
committed
added callbacks
1 parent b0d5a1c commit 1057f57

File tree

5 files changed

+160
-159
lines changed

5 files changed

+160
-159
lines changed

CHANGELOG

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2010.10.21, Version 0.0.5
2+
* added unit tests for all operations
3+
* need to fix increment/decrement 64 bit issue
14
2010.10.21, Version 0.0.4
25
* added basic memcached client library and test script
36
2010.10.20, Version 0.0.3

README.md

+89-97
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@ If false, the body will be allocated on the message object as it is parsed. It w
1616

1717
### `encoding`
1818

19-
<code>
20-
"encodings": {
21-
"BINARY": 0,
22-
"ASCII": 1,
23-
"UTF8": 2
24-
},
25-
</code>
19+
"encodings": {
20+
"BINARY": 0,
21+
"ASCII": 1,
22+
"UTF8": 2
23+
}
2624

2725
Default is BINARY.
2826

@@ -36,12 +34,10 @@ Only applies if chunked is false. When chunked is true, only raw buffers will be
3634

3735
You can set encoding and chunked by passing the constructor an options object as follows:
3836

39-
<code>
40-
{
41-
encoding: 0|1|2,
42-
chunked: true|false
43-
}
44-
</code>
37+
{
38+
encoding: 0|1|2,
39+
chunked: true|false
40+
}
4541

4642
### `execute(buffer, start, end)`
4743

@@ -80,103 +76,99 @@ fired if chunked = true.
8076

8177
## Message format
8278

83-
<code>
84-
{
85-
[m]header: {
86-
magic: int,
87-
opcode: int,
88-
keylen: int16,
89-
exlen: int,
90-
datatype: int,
91-
status/reserved: int16,
92-
totlen: int32,
93-
opaque: int32,
94-
cashi: int32,
95-
caslo: int32,
96-
bodylen: int32
97-
},
98-
[o]key: string,
99-
[o]extra: {},
100-
[o]body: string
101-
}
102-
</code>
79+
{
80+
[m]header: {
81+
magic: int,
82+
opcode: int,
83+
keylen: int16,
84+
exlen: int,
85+
datatype: int,
86+
status/reserved: int16,
87+
totlen: int32,
88+
opaque: int32,
89+
cashi: int32,
90+
caslo: int32,
91+
bodylen: int32
92+
},
93+
[o]key: string,
94+
[o]extra: {},
95+
[o]body: string
96+
}
10397

10498
# Example
105-
<code>
106-
var memc = require("../lib/parser");
10799

108-
...
109-
110-
var current = null;
100+
var memc = require("../lib/parser");
101+
102+
...
111103

112-
var parser = new memc.parser({
113-
"chunked": true,
114-
"encoding": memc.constants.encodings.BINARY
115-
});
104+
var current = null;
105+
106+
var parser = new memc.parser({
107+
"chunked": true,
108+
"encoding": memc.constants.encodings.BINARY
109+
});
110+
111+
parser.onMessage = function(message) {
112+
// Will fire when a message has completed fully (i.e. body has been fully parsed),
113+
// even if the message is only a header with no body. This means for a message
114+
// that is only a header, you will get it in th onHeader and the onMessage callbacks
115+
sys.puts("message\n" + JSON.stringify(message, null, "\t"));
116+
switch(message.header.opcode) {
117+
case memc.constants.opcodes.SET:
118+
break;
119+
case memc.constants.opcodes.GET:
120+
break;
121+
case memc.constants.opcodes.QUIT:
122+
break;
123+
}
124+
};
116125

117-
parser.onMessage = function(message) {
118-
// Will fire when a message has completed fully (i.e. body has been fully parsed),
119-
// even if the message is only a header with no body. This means for a message
120-
// that is only a header, you will get it in th onHeader and the onMessage callbacks
121-
sys.puts("message\n" + JSON.stringify(message, null, "\t"));
122-
switch(message.header.opcode) {
123-
case memc.constants.opcodes.SET:
124-
break;
125-
case memc.constants.opcodes.GET:
126-
break;
127-
case memc.constants.opcodes.QUIT:
128-
break;
129-
}
130-
};
131-
132-
parser.onHeader = function(message) {
133-
// Will fire after the header (first 24 bytes) of a message has been parsed.
134-
sys.puts("header\n" + JSON.stringify(message, null, "\t"));
135-
switch(message.header.opcode) {
136-
case memc.constants.opcodes.SET:
137-
break;
138-
case memc.constants.opcodes.GET:
139-
break;
140-
case memc.constants.opcodes.QUIT:
141-
break;
142-
}
143-
if(message.header.bodylen > 0) {
144-
if(parser.chunked) {
145-
// save a pointer to the message in current so we can access it the onBody callback
146-
// as we will not get anything in the body when onMessage fires while in chunked mode
147-
current = message;
148-
current.body = [];
126+
parser.onHeader = function(message) {
127+
// Will fire after the header (first 24 bytes) of a message has been parsed.
128+
sys.puts("header\n" + JSON.stringify(message, null, "\t"));
129+
switch(message.header.opcode) {
130+
case memc.constants.opcodes.SET:
131+
break;
132+
case memc.constants.opcodes.GET:
133+
break;
134+
case memc.constants.opcodes.QUIT:
135+
break;
149136
}
150-
else {
151-
// we will get the body on the message returned in the onMessage callback
137+
if(message.header.bodylen > 0) {
138+
if(parser.chunked) {
139+
// save a pointer to the message in current so we can access it the onBody callback
140+
// as we will not get anything in the body when onMessage fires while in chunked mode
141+
current = message;
142+
current.body = [];
143+
}
144+
else {
145+
// we will get the body on the message returned in the onMessage callback
146+
}
152147
}
153-
}
154-
};
155-
156-
parser.onBody = function(buffer, start, end) {
157-
// this will only fire if chunked is set to true. the parser will not set the body of
158-
// the message and will just forward on the chunks of the body in this callback.
159-
// NOTE: the parser does not use Buffer.slice(), it is giving you the actual buffer which was passed into it.
160-
sys.puts("chunk: " + (end-start));
161-
current.body.push(buffer.slice(start, end));
162-
};
163-
164-
parser.onError = function(err) {
165-
sys.puts("error\n" + JSON.stringify(err, null, "\t"));
166-
};
148+
};
167149

168-
...
169-
170-
stream.ondata = function (buffer, start, end) {
171-
parser.execute(buffer, start, end);
172-
};
150+
parser.onBody = function(buffer, start, end) {
151+
// this will only fire if chunked is set to true. the parser will not set the body of
152+
// the message and will just forward on the chunks of the body in this callback.
153+
// NOTE: the parser does not use Buffer.slice(), it is giving you the actual buffer which was passed into it.
154+
sys.puts("chunk: " + (end-start));
155+
current.body.push(buffer.slice(start, end));
156+
};
157+
158+
parser.onError = function(err) {
159+
sys.puts("error\n" + JSON.stringify(err, null, "\t"));
160+
};
161+
162+
...
173163

174-
</code>
164+
stream.ondata = function (buffer, start, end) {
165+
parser.execute(buffer, start, end);
166+
};
175167

176168
# Dependencies
177169

178170
For the test script in examples/test.js you need to copy binary.node from this project:
179171

180-
http://github.com/billywhizz/node-binary
172+
- <http://github.com/billywhizz/node-binary>
181173

182174
to the lib directory so that the binary messages for sending to memcached can be created.

examples/unit.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ var value = "hello";
4040
{"method": "flushq", "params":"expiration, cb"}
4141
*/
4242

43-
client.connect("/tmp/memcached.sock", null, function() {
43+
client.connect(process.ARGV[2], null, function() {
4444
sys.puts("connected");
4545
client.flushq(null, function(message) {
4646
// we should only get a callback if there is an error
@@ -141,17 +141,18 @@ client.connect("/tmp/memcached.sock", null, function() {
141141
sys.puts("GET: " + message.body);
142142
});
143143
client.get(key, function(message) {
144+
sys.puts(JSON.stringify(message, null, "\t"));
144145
sys.puts("GET: " + message.body);
145146
var stat = {};
146147
client.stat(null, function(message) {
147148
if(message.header.bodylen > 0) {
148149
stat[message.key] = message.body;
149150
}
150151
else {
151-
sys.puts("STAT:\n" + JSON.stringify(stat, null, "\t"));
152152
client.delete(key, function(message) {
153153
sys.puts("DELETE: " + (message.header.status == memc.constants.status.NO_ERROR?"OK":"FAIL"));
154154
client.quit(function(message) {
155+
sys.puts("QUIT:\n" + JSON.stringify(message, null, "\t"));
155156
sys.puts("QUIT: " + (message.header.status == memc.constants.status.NO_ERROR?"OK":"FAIL"));
156157
});
157158
});

0 commit comments

Comments
 (0)