/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. package com.hazelcast.impl.ascii.memcache;
  17. import com.hazelcast.impl.ascii.AbstractTextCommand;
  18. import com.hazelcast.nio.IOUtil;
  19. import java.nio.ByteBuffer;
  20. public class StatsCommand extends AbstractTextCommand {
  21. ByteBuffer response;
  22. static final byte[] STAT = "STAT ".getBytes();
  23. static final byte[] UPTIME = "uptime ".getBytes();
  24. static final byte[] BYTES = "bytes ".getBytes();
  25. static final byte[] CMD_SET = "cmd_set ".getBytes();
  26. static final byte[] CMD_GET = "cmd_get ".getBytes();
  27. static final byte[] CMD_DELETE = "cmd_delete ".getBytes();
  28. static final byte[] THREADS = "threads ".getBytes();
  29. static final byte[] WAITING_REQUESTS = "waiting_requests ".getBytes();
  30. static final byte[] GET_HITS = "get_hits ".getBytes();
  31. static final byte[] GET_MISSES = "get_misses ".getBytes();
  32. static final byte[] CURR_CONNECTIONS = "curr_connections ".getBytes();
  33. static final byte[] TOTAL_CONNECTIONS = "total_connections ".getBytes();
  34. public StatsCommand() {
  35. super(TextCommandType.STATS);
  36. }
  37. public boolean doRead(ByteBuffer cb) {
  38. return true;
  39. }
  40. public void setResponse(Stats stats) {
  41. response = ByteBuffer.allocate(1000);
  42. putInt(UPTIME, stats.uptime);
  43. putInt(THREADS, stats.threads);
  44. putInt(WAITING_REQUESTS, stats.waiting_requests);
  45. putInt(CURR_CONNECTIONS, stats.curr_connections);
  46. putInt(TOTAL_CONNECTIONS, stats.total_connections);
  47. putLong(BYTES, stats.bytes);
  48. putLong(CMD_GET, stats.cmd_get);
  49. putLong(CMD_SET, stats.cmd_set);
  50. putLong(CMD_DELETE, stats.cmd_delete);
  51. putLong(GET_HITS, stats.get_hits);
  52. putLong(GET_MISSES, stats.get_misses);
  53. response.put(END);
  54. response.flip();
  55. // System.out.println(new String(response.array(), 0, response.remaining()));
  56. }
  57. private void putInt(byte[] name, int value) {
  58. response.put(STAT);
  59. response.put(name);
  60. response.put(String.valueOf(value).getBytes());
  61. response.put(RETURN);
  62. }
  63. private void putLong(byte[] name, long value) {
  64. response.put(STAT);
  65. response.put(name);
  66. response.put(String.valueOf(value).getBytes());
  67. response.put(RETURN);
  68. }
  69. public boolean writeTo(ByteBuffer bb) {
  70. if (response == null) {
  71. response = ByteBuffer.allocate(0);
  72. }
  73. IOUtil.copyToHeapBuffer(response, bb);
  74. return !response.hasRemaining();
  75. }
  76. @Override
  77. public String toString() {
  78. return "StatsCommand{" +
  79. '}' + super.toString();
  80. }
  81. }