|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * Copyright (c) 2018 Microsoft Corporation |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in all |
| 13 | + * copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | + * SOFTWARE. |
| 22 | + * |
| 23 | + */ |
| 24 | + |
| 25 | +package com.azure.cosmos.implementation.directconnectivity.TcpServerMock; |
| 26 | + |
| 27 | +import com.azure.cosmos.implementation.Utils; |
| 28 | +import com.azure.cosmos.implementation.directconnectivity.TcpServerMock.rntbd.ServerRntbdContextEncoder; |
| 29 | +import com.azure.cosmos.implementation.directconnectivity.TcpServerMock.rntbd.ServerRntbdContextRequestDecoder; |
| 30 | +import com.azure.cosmos.implementation.directconnectivity.TcpServerMock.rntbd.ServerRntbdRequestDecoder; |
| 31 | +import com.azure.cosmos.implementation.directconnectivity.TcpServerMock.rntbd.ServerRntbdRequestFramer; |
| 32 | +import com.azure.cosmos.implementation.directconnectivity.TcpServerMock.rntbd.ServerRntbdRequestManager; |
| 33 | +import io.netty.bootstrap.ServerBootstrap; |
| 34 | +import io.netty.channel.ChannelFuture; |
| 35 | +import io.netty.channel.ChannelInitializer; |
| 36 | +import io.netty.channel.ChannelOption; |
| 37 | +import io.netty.channel.ChannelPipeline; |
| 38 | +import io.netty.channel.EventLoopGroup; |
| 39 | +import io.netty.channel.nio.NioEventLoopGroup; |
| 40 | +import io.netty.channel.socket.SocketChannel; |
| 41 | +import io.netty.channel.socket.nio.NioServerSocketChannel; |
| 42 | +import io.netty.handler.logging.LogLevel; |
| 43 | +import io.netty.handler.logging.LoggingHandler; |
| 44 | +import io.netty.handler.ssl.SslContext; |
| 45 | +import io.netty.handler.ssl.SslHandler; |
| 46 | +import io.netty.util.concurrent.Promise; |
| 47 | +import org.slf4j.Logger; |
| 48 | +import org.slf4j.LoggerFactory; |
| 49 | + |
| 50 | +import javax.net.ssl.SSLEngine; |
| 51 | + |
| 52 | +public class TcpServer { |
| 53 | + |
| 54 | + private final static Logger logger = LoggerFactory.getLogger(TcpServer.class); |
| 55 | + private static final String SERVER_KEYSTORE = "server.jks"; |
| 56 | + private final int port; |
| 57 | + private final EventLoopGroup parent; |
| 58 | + private final EventLoopGroup child; |
| 59 | + private final ServerRntbdRequestManager requestManager; // Use to inject fake server response. |
| 60 | + |
| 61 | + public TcpServer(int port) { |
| 62 | + this.port = port; |
| 63 | + this.parent = new NioEventLoopGroup(); |
| 64 | + this.child = new NioEventLoopGroup(); |
| 65 | + requestManager = new ServerRntbdRequestManager(); |
| 66 | + } |
| 67 | + |
| 68 | + public void start(Promise<Boolean> promise) throws InterruptedException { |
| 69 | + |
| 70 | + SslContext sslContext = SslContextUtils.CreateSslContext(SERVER_KEYSTORE, true); |
| 71 | + Utils.checkNotNullOrThrow(sslContext, "sslContext", ""); |
| 72 | + |
| 73 | + try { |
| 74 | + ServerBootstrap bootstrap = new ServerBootstrap(); |
| 75 | + |
| 76 | + bootstrap.group(parent, child) |
| 77 | + .channel(NioServerSocketChannel.class) |
| 78 | + .childHandler(new ChannelInitializer<SocketChannel>() { |
| 79 | + @Override |
| 80 | + public void initChannel(SocketChannel channel) throws Exception { |
| 81 | + |
| 82 | + SSLEngine engine = sslContext.newEngine(channel.alloc()); |
| 83 | + engine.setUseClientMode(false); |
| 84 | + |
| 85 | + ChannelPipeline pipeline = channel.pipeline(); |
| 86 | + pipeline.addLast( |
| 87 | + new SslHandler(engine), |
| 88 | + new ServerRntbdRequestFramer(), |
| 89 | + new ServerRntbdRequestDecoder(), |
| 90 | + new ServerRntbdContextRequestDecoder(), |
| 91 | + new ServerRntbdContextEncoder(), |
| 92 | + requestManager |
| 93 | + ); |
| 94 | + |
| 95 | + LogLevel logLevel = null; |
| 96 | + |
| 97 | + if (logger.isTraceEnabled()) { |
| 98 | + logLevel = LogLevel.TRACE; |
| 99 | + } else if (logger.isDebugEnabled()) { |
| 100 | + logLevel = LogLevel.DEBUG; |
| 101 | + } |
| 102 | + |
| 103 | + if (logLevel != null) { |
| 104 | + pipeline.addFirst(new LoggingHandler(logLevel)); |
| 105 | + } |
| 106 | + } |
| 107 | + }) |
| 108 | + .childOption(ChannelOption.SO_KEEPALIVE, true); |
| 109 | + |
| 110 | + ChannelFuture channelFuture = bootstrap.bind(port).sync().addListener((ChannelFuture f) -> { |
| 111 | + if (f.isSuccess()) { |
| 112 | + logger.info( |
| 113 | + "{} started and listening for connections on {}", |
| 114 | + TcpServer.class.getSimpleName(), |
| 115 | + f.channel().localAddress()); |
| 116 | + promise.setSuccess(Boolean.TRUE); |
| 117 | + } |
| 118 | + }); |
| 119 | + |
| 120 | + channelFuture.channel().closeFuture().sync().addListener((ChannelFuture f) -> { |
| 121 | + logger.info("Server channel closed."); |
| 122 | + }); |
| 123 | + |
| 124 | + } finally { |
| 125 | + parent.shutdownGracefully().sync(); |
| 126 | + child.shutdownGracefully().sync(); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + public void shutdown(Promise<Boolean> promise) { |
| 131 | + try { |
| 132 | + parent.shutdownGracefully().sync(); |
| 133 | + child.shutdownGracefully().sync(); |
| 134 | + promise.setSuccess(Boolean.TRUE); |
| 135 | + } catch (InterruptedException e) { |
| 136 | + logger.error("Error when shutting down server {}", e); |
| 137 | + promise.setFailure(e); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + public void injectServerResponse(RequestResponseType responseType) { |
| 142 | + this.requestManager.injectServerResponse(responseType); |
| 143 | + } |
| 144 | +} |
0 commit comments