View Javadoc
1   /*
2    * Copyright 2016 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    *   https://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  
17  package io.netty.example.redis;
18  
19  import io.netty.buffer.ByteBufUtil;
20  import io.netty.channel.ChannelDuplexHandler;
21  import io.netty.channel.ChannelHandlerContext;
22  import io.netty.channel.ChannelPromise;
23  import io.netty.handler.codec.CodecException;
24  import io.netty.handler.codec.redis.ArrayRedisMessage;
25  import io.netty.handler.codec.redis.ErrorRedisMessage;
26  import io.netty.handler.codec.redis.FullBulkStringRedisMessage;
27  import io.netty.handler.codec.redis.IntegerRedisMessage;
28  import io.netty.handler.codec.redis.RedisMessage;
29  import io.netty.handler.codec.redis.SimpleStringRedisMessage;
30  import io.netty.util.CharsetUtil;
31  import io.netty.util.ReferenceCountUtil;
32  
33  import java.util.ArrayList;
34  import java.util.List;
35  
36  /**
37   * An example Redis client handler. This handler read input from STDIN and write output to STDOUT.
38   */
39  public class RedisClientHandler extends ChannelDuplexHandler {
40  
41      @Override
42      public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
43          String[] commands = ((String) msg).split("\\s+");
44          List<RedisMessage> children = new ArrayList<RedisMessage>(commands.length);
45          for (String cmdString : commands) {
46              children.add(new FullBulkStringRedisMessage(ByteBufUtil.writeUtf8(ctx.alloc(), cmdString)));
47          }
48          RedisMessage request = new ArrayRedisMessage(children);
49          ctx.write(request, promise);
50      }
51  
52      @Override
53      public void channelRead(ChannelHandlerContext ctx, Object msg) {
54          RedisMessage redisMessage = (RedisMessage) msg;
55          printAggregatedRedisResponse(redisMessage);
56          ReferenceCountUtil.release(redisMessage);
57      }
58  
59      @Override
60      public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
61          System.err.print("exceptionCaught: ");
62          cause.printStackTrace(System.err);
63          ctx.close();
64      }
65  
66      private static void printAggregatedRedisResponse(RedisMessage msg) {
67          if (msg instanceof SimpleStringRedisMessage) {
68              System.out.println(((SimpleStringRedisMessage) msg).content());
69          } else if (msg instanceof ErrorRedisMessage) {
70              System.out.println(((ErrorRedisMessage) msg).content());
71          } else if (msg instanceof IntegerRedisMessage) {
72              System.out.println(((IntegerRedisMessage) msg).value());
73          } else if (msg instanceof FullBulkStringRedisMessage) {
74              System.out.println(getString((FullBulkStringRedisMessage) msg));
75          } else if (msg instanceof ArrayRedisMessage) {
76              for (RedisMessage child : ((ArrayRedisMessage) msg).children()) {
77                  printAggregatedRedisResponse(child);
78              }
79          } else {
80              throw new CodecException("unknown message type: " + msg);
81          }
82      }
83  
84      private static String getString(FullBulkStringRedisMessage msg) {
85          if (msg.isNull()) {
86              return "(null)";
87          }
88          return msg.content().toString(CharsetUtil.UTF_8);
89      }
90  }