View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. 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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.example.memcache.binary;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.buffer.Unpooled;
20  import io.netty.channel.ChannelHandlerAdapter;
21  import io.netty.channel.ChannelHandlerContext;
22  import io.netty.channel.ChannelPromise;
23  import io.netty.handler.codec.memcache.binary.BinaryMemcacheOpcodes;
24  import io.netty.handler.codec.memcache.binary.BinaryMemcacheRequest;
25  import io.netty.handler.codec.memcache.binary.DefaultBinaryMemcacheRequest;
26  import io.netty.handler.codec.memcache.binary.DefaultFullBinaryMemcacheRequest;
27  import io.netty.handler.codec.memcache.binary.FullBinaryMemcacheResponse;
28  import io.netty.util.CharsetUtil;
29  
30  public class MemcacheClientHandler extends ChannelHandlerAdapter {
31  
32      /**
33       * Transforms basic string requests to binary memcache requests
34       */
35      @Override
36      public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
37          String command = (String) msg;
38          if (command.startsWith("get ")) {
39              String key = command.substring("get ".length());
40  
41              BinaryMemcacheRequest req = new DefaultBinaryMemcacheRequest(key);
42              req.setOpcode(BinaryMemcacheOpcodes.GET);
43              req.setKeyLength((short) key.length());
44              req.setTotalBodyLength(key.length());
45  
46              ctx.write(req, promise);
47          } else if (command.startsWith("set ")) {
48              String[] parts = command.split(" ", 3);
49              if (parts.length < 3) {
50                  throw new IllegalArgumentException("Malformed Command: " + command);
51              }
52              String key = parts[1];
53              String value = parts[2];
54  
55              ByteBuf content = Unpooled.wrappedBuffer(value.getBytes(CharsetUtil.UTF_8));
56              ByteBuf extras = ctx.alloc().buffer(8);
57              extras.writeZero(8);
58  
59              BinaryMemcacheRequest req = new DefaultFullBinaryMemcacheRequest(key, extras, content);
60              req.setOpcode(BinaryMemcacheOpcodes.SET);
61              req.setKeyLength((short) key.length());
62              req.setExtrasLength((byte) 8);
63              req.setTotalBodyLength(key.length() + 8 + value.length());
64  
65              ctx.write(req, promise);
66          } else {
67              throw new IllegalStateException("Unknown Message: " + msg);
68          }
69      }
70  
71      @Override
72      public void channelRead(ChannelHandlerContext ctx, Object msg) {
73          FullBinaryMemcacheResponse res = (FullBinaryMemcacheResponse) msg;
74          System.out.println(res.content().toString(CharsetUtil.UTF_8));
75      }
76  
77      @Override
78      public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
79          cause.printStackTrace();
80          ctx.close();
81      }
82  }