1 | // Copyright (c) 2006 Dustin Sallings <dustin@spy.net> |
2 | |
3 | package net.spy.memcached.protocol.ascii; |
4 | |
5 | import java.nio.ByteBuffer; |
6 | |
7 | import net.spy.memcached.ops.OperationState; |
8 | import net.spy.memcached.ops.OperationStatus; |
9 | import net.spy.memcached.ops.StatsOperation; |
10 | |
11 | /** |
12 | * Operation to retrieve statistics from a memcached server. |
13 | */ |
14 | final class StatsOperationImpl extends OperationImpl |
15 | implements StatsOperation { |
16 | |
17 | private static final OperationStatus END=new OperationStatus(true, "END"); |
18 | |
19 | private static final byte[] MSG="stats\r\n".getBytes(); |
20 | |
21 | private final byte[] msg; |
22 | private final StatsOperation.Callback cb; |
23 | |
24 | public StatsOperationImpl(String arg, StatsOperation.Callback c) { |
25 | super(c); |
26 | cb=c; |
27 | if(arg == null) { |
28 | msg=MSG; |
29 | } else { |
30 | msg=("stats " + arg + "\r\n").getBytes(); |
31 | } |
32 | } |
33 | |
34 | @Override |
35 | public void handleLine(String line) { |
36 | if(line.equals("END")) { |
37 | cb.receivedStatus(END); |
38 | transitionState(OperationState.COMPLETE); |
39 | } else { |
40 | String[] parts=line.split(" ", 3); |
41 | assert parts.length == 3; |
42 | cb.gotStat(parts[1], parts[2]); |
43 | } |
44 | } |
45 | |
46 | @Override |
47 | public void initialize() { |
48 | setBuffer(ByteBuffer.wrap(msg)); |
49 | } |
50 | |
51 | @Override |
52 | protected void wasCancelled() { |
53 | cb.receivedStatus(CANCELLED); |
54 | } |
55 | |
56 | } |