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.bootstrap.Bootstrap;
19  import io.netty.channel.Channel;
20  import io.netty.channel.ChannelFuture;
21  import io.netty.channel.ChannelInitializer;
22  import io.netty.channel.ChannelPipeline;
23  import io.netty.channel.EventLoopGroup;
24  import io.netty.channel.nio.NioEventLoopGroup;
25  import io.netty.channel.socket.SocketChannel;
26  import io.netty.channel.socket.nio.NioSocketChannel;
27  import io.netty.handler.codec.http2.Http2OrHttpChooser.SelectedProtocol;
28  import io.netty.handler.codec.http2.Http2SecurityUtil;
29  import io.netty.handler.codec.memcache.binary.BinaryMemcacheClientCodec;
30  import io.netty.handler.codec.memcache.binary.BinaryMemcacheObjectAggregator;
31  import io.netty.handler.ssl.ApplicationProtocolConfig;
32  import io.netty.handler.ssl.ApplicationProtocolConfig.Protocol;
33  import io.netty.handler.ssl.ApplicationProtocolConfig.SelectedListenerFailureBehavior;
34  import io.netty.handler.ssl.ApplicationProtocolConfig.SelectorFailureBehavior;
35  import io.netty.handler.ssl.SslContext;
36  import io.netty.handler.ssl.SslProvider;
37  import io.netty.handler.ssl.SupportedCipherSuiteFilter;
38  import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
39  
40  import java.io.BufferedReader;
41  import java.io.InputStreamReader;
42  
43  /**
44   * Simple memcache client that demonstrates get and set commands against a memcache server.
45   */
46  public final class MemcacheClient {
47  
48      static final boolean SSL = System.getProperty("ssl") != null;
49      static final String HOST = System.getProperty("host", "127.0.0.1");
50      static final int PORT = Integer.parseInt(System.getProperty("port", "11211"));
51  
52      public static void main(String[] args) throws Exception {
53          // Configure SSL.
54          final SslContext sslCtx;
55          if (SSL) {
56              sslCtx = SslContext.newClientContext(SslProvider.JDK,
57                      null, InsecureTrustManagerFactory.INSTANCE, Http2SecurityUtil.CIPHERS,
58                      /* NOTE: the following filter may not include all ciphers required by the HTTP/2 specification
59                       * Please refer to the HTTP/2 specification for cipher requirements. */
60                      SupportedCipherSuiteFilter.INSTANCE,
61                      new ApplicationProtocolConfig(
62                              Protocol.ALPN,
63                              SelectorFailureBehavior.FATAL_ALERT,
64                              SelectedListenerFailureBehavior.FATAL_ALERT,
65                              SelectedProtocol.HTTP_2.protocolName(),
66                              SelectedProtocol.HTTP_1_1.protocolName()),
67                      0, 0);
68          } else {
69              sslCtx = null;
70          }
71  
72          EventLoopGroup group = new NioEventLoopGroup();
73          try {
74              Bootstrap b = new Bootstrap();
75              b.group(group)
76                      .channel(NioSocketChannel.class)
77                      .handler(new ChannelInitializer<SocketChannel>() {
78                          @Override
79                          protected void initChannel(SocketChannel ch) throws Exception {
80                              ChannelPipeline p = ch.pipeline();
81                              if (sslCtx != null) {
82                                  p.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT));
83                              }
84                              p.addLast(new BinaryMemcacheClientCodec());
85                              p.addLast(new BinaryMemcacheObjectAggregator(Integer.MAX_VALUE));
86                              p.addLast(new MemcacheClientHandler());
87                          }
88                      });
89  
90              // Start the connection attempt.
91              Channel ch = b.connect(HOST, PORT).sync().channel();
92  
93              // Read commands from the stdin.
94              System.out.println("Enter commands (quit to end)");
95              System.out.println("get <key>");
96              System.out.println("set <key> <value>");
97              ChannelFuture lastWriteFuture = null;
98              BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
99              for (;;) {
100                 String line = in.readLine();
101                 if (line == null) {
102                     break;
103                 }
104                 if ("quit".equals(line.toLowerCase())) {
105                     ch.close().sync();
106                     break;
107                 }
108                 // Sends the received line to the server.
109                 lastWriteFuture = ch.writeAndFlush(line);
110             }
111 
112             // Wait until all messages are flushed before closing the channel.
113             if (lastWriteFuture != null) {
114                 lastWriteFuture.sync();
115             }
116         } finally {
117             group.shutdownGracefully();
118         }
119     }
120 }