PageRenderTime 16ms CodeModel.GetById 2ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/hazelcast/src/main/java/com/hazelcast/impl/ascii/memcache/StatsCommand.java

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 92 lines | 65 code | 11 blank | 16 comment | 2 complexity | ba472c10ba530b24f17196b08df26154 MD5 | raw file
 1/*
 2 * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
 3 *
 4 * Licensed under the Apache License, Version 2.0 (the "License");
 5 * you may not use this file except in compliance with the License.
 6 * You may obtain a copy of the License at
 7 *
 8 * http://www.apache.org/licenses/LICENSE-2.0
 9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.hazelcast.impl.ascii.memcache;
18
19import com.hazelcast.impl.ascii.AbstractTextCommand;
20import com.hazelcast.nio.IOUtil;
21
22import java.nio.ByteBuffer;
23
24public class StatsCommand extends AbstractTextCommand {
25    ByteBuffer response;
26    static final byte[] STAT = "STAT ".getBytes();
27    static final byte[] UPTIME = "uptime ".getBytes();
28    static final byte[] BYTES = "bytes ".getBytes();
29    static final byte[] CMD_SET = "cmd_set ".getBytes();
30    static final byte[] CMD_GET = "cmd_get ".getBytes();
31    static final byte[] CMD_DELETE = "cmd_delete ".getBytes();
32    static final byte[] THREADS = "threads ".getBytes();
33    static final byte[] WAITING_REQUESTS = "waiting_requests ".getBytes();
34    static final byte[] GET_HITS = "get_hits ".getBytes();
35    static final byte[] GET_MISSES = "get_misses ".getBytes();
36    static final byte[] CURR_CONNECTIONS = "curr_connections ".getBytes();
37    static final byte[] TOTAL_CONNECTIONS = "total_connections ".getBytes();
38
39    public StatsCommand() {
40        super(TextCommandType.STATS);
41    }
42
43    public boolean doRead(ByteBuffer cb) {
44        return true;
45    }
46
47    public void setResponse(Stats stats) {
48        response = ByteBuffer.allocate(1000);
49        putInt(UPTIME, stats.uptime);
50        putInt(THREADS, stats.threads);
51        putInt(WAITING_REQUESTS, stats.waiting_requests);
52        putInt(CURR_CONNECTIONS, stats.curr_connections);
53        putInt(TOTAL_CONNECTIONS, stats.total_connections);
54        putLong(BYTES, stats.bytes);
55        putLong(CMD_GET, stats.cmd_get);
56        putLong(CMD_SET, stats.cmd_set);
57        putLong(CMD_DELETE, stats.cmd_delete);
58        putLong(GET_HITS, stats.get_hits);
59        putLong(GET_MISSES, stats.get_misses);
60        response.put(END);
61        response.flip();
62//        System.out.println(new String(response.array(), 0, response.remaining()));
63    }
64
65    private void putInt(byte[] name, int value) {
66        response.put(STAT);
67        response.put(name);
68        response.put(String.valueOf(value).getBytes());
69        response.put(RETURN);
70    }
71
72    private void putLong(byte[] name, long value) {
73        response.put(STAT);
74        response.put(name);
75        response.put(String.valueOf(value).getBytes());
76        response.put(RETURN);
77    }
78
79    public boolean writeTo(ByteBuffer bb) {
80        if (response == null) {
81            response = ByteBuffer.allocate(0);
82        }
83        IOUtil.copyToHeapBuffer(response, bb);
84        return !response.hasRemaining();
85    }
86
87    @Override
88    public String toString() {
89        return "StatsCommand{" +
90                '}' + super.toString();
91    }
92}