Search in sources :

Example 76 with SocketChannel

use of org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel in project apollo by apollo-rsps.

the class Server method init.

/**
 * Initialises the server.
 *
 * @param releaseName The class name of the current active {@link Release}.
 * @throws Exception If an error occurs.
 */
public void init(String releaseName) throws Exception {
    Class<?> clazz = Class.forName(releaseName);
    Release release = (Release) clazz.newInstance();
    int version = release.getReleaseNumber();
    logger.info("Initialized " + release + ".");
    serviceBootstrap.group(loopGroup);
    httpBootstrap.group(loopGroup);
    jaggrabBootstrap.group(loopGroup);
    World world = new World();
    ServiceManager services = new ServiceManager(world);
    IndexedFileSystem fs = new IndexedFileSystem(Paths.get("data/fs", Integer.toString(version)), true);
    ServerContext context = new ServerContext(release, services, fs);
    ApolloHandler handler = new ApolloHandler(context);
    ChannelInitializer<SocketChannel> service = new ServiceChannelInitializer(handler);
    serviceBootstrap.channel(NioServerSocketChannel.class);
    serviceBootstrap.childHandler(service);
    ChannelInitializer<SocketChannel> http = new HttpChannelInitializer(handler);
    httpBootstrap.channel(NioServerSocketChannel.class);
    httpBootstrap.childHandler(http);
    ChannelInitializer<SocketChannel> jaggrab = new JagGrabChannelInitializer(handler);
    jaggrabBootstrap.channel(NioServerSocketChannel.class);
    jaggrabBootstrap.childHandler(jaggrab);
    PluginManager manager = new PluginManager(world, new PluginContext(context));
    services.startAll();
    world.init(version, fs, manager);
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ServiceChannelInitializer(org.apollo.net.ServiceChannelInitializer) PluginContext(org.apollo.game.plugin.PluginContext) World(org.apollo.game.model.World) PluginManager(org.apollo.game.plugin.PluginManager) HttpChannelInitializer(org.apollo.net.HttpChannelInitializer) IndexedFileSystem(org.apollo.cache.IndexedFileSystem) ApolloHandler(org.apollo.game.session.ApolloHandler) JagGrabChannelInitializer(org.apollo.net.JagGrabChannelInitializer) Release(org.apollo.net.release.Release)

Example 77 with SocketChannel

use of org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel in project alien4cloud by alien4cloud.

the class StompConnection method init.

@SneakyThrows({ InterruptedException.class, URISyntaxException.class })
private void init() {
    if (this.stompChannel != null) {
        throw new IllegalStateException("The stomp connection has already been started");
    }
    String wsUrl = "ws://" + host + ":" + port + endPoint + "/websocket";
    if (log.isDebugEnabled()) {
        log.debug("Web socket url {}", wsUrl);
    }
    String loginUrl = null;
    if (user != null && password != null && loginPath != null) {
        loginUrl = "http://" + host + ":" + port + loginPath;
        if (log.isDebugEnabled()) {
            log.debug("Authentication url {}", loginUrl);
        }
    }
    this.eventLoopGroup = new NioEventLoopGroup();
    this.stompClientHandler = new StompClientHandler();
    DefaultHttpHeaders handshakeHeaders = new DefaultHttpHeaders();
    if (this.headers != null) {
        for (Map.Entry<String, String> header : this.headers.entrySet()) {
            handshakeHeaders.add(header.getKey(), header.getValue());
        }
    }
    final WebSocketClientHandler webSocketHandler = new WebSocketClientHandler(WebSocketClientHandshakerFactory.newHandshaker(new URI(wsUrl), WebSocketVersion.V13, null, false, handshakeHeaders), host, user, password, loginUrl);
    Bootstrap b = new Bootstrap();
    b.group(eventLoopGroup).channel(NioSocketChannel.class);
    b.handler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast(HttpClientCodec.class.getName(), new HttpClientCodec());
            pipeline.addLast(HttpObjectAggregator.class.getName(), new HttpObjectAggregator(8192));
            pipeline.addLast(WebSocketClientCompressionHandler.class.getName(), new WebSocketClientCompressionHandler());
            pipeline.addLast(WebSocketClientHandler.class.getName(), webSocketHandler);
            pipeline.addLast(StompSubframeDecoder.class.getName(), new StompSubframeDecoder());
            pipeline.addLast(StompSubframeEncoder.class.getName(), new StompSubframeEncoder());
            pipeline.addLast(StompSubframeAggregator.class.getName(), new StompSubframeAggregator(1048576));
            pipeline.addLast(StompClientHandler.class.getName(), stompClientHandler);
        }
    });
    this.stompChannel = b.connect(host, port).sync().channel();
    this.stompClientHandler.connectFuture(this.stompChannel.newPromise());
    webSocketHandler.handshakeFuture().addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(final ChannelFuture future) throws Exception {
            stompClientHandler.beginStomp(stompChannel);
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) WebSocketClientCompressionHandler(io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketClientCompressionHandler) StompSubframeAggregator(io.netty.handler.codec.stomp.StompSubframeAggregator) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) StompSubframeEncoder(io.netty.handler.codec.stomp.StompSubframeEncoder) URI(java.net.URI) ChannelFutureListener(io.netty.channel.ChannelFutureListener) URISyntaxException(java.net.URISyntaxException) ChannelPipeline(io.netty.channel.ChannelPipeline) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) StompSubframeDecoder(io.netty.handler.codec.stomp.StompSubframeDecoder) Bootstrap(io.netty.bootstrap.Bootstrap) Map(java.util.Map) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SneakyThrows(lombok.SneakyThrows)

Example 78 with SocketChannel

use of org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel in project jdepth by Crab2died.

the class C2DServer method bind.

private void bind(String host, int port) throws InterruptedException {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap bootstrap = new ServerBootstrap().group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast(new C2DHessianMsgDecoder(1024 * 1024, 4, 4, -8, 0)).addLast("MessageEncoder", new C2DHessianMsgEncoder()).addLast("ReadTimeoutHandler", new ReadTimeoutHandler(TIME_OUT)).addLast("LoginAuthResp", new LoginAuthRespHandler()).addLast("Pong", new PongHandler());
            }
        });
        ChannelFuture future = bootstrap.bind(host, port).sync();
        future.channel().closeFuture().sync();
        logger.info("server is started");
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) PongHandler(com.github.jvm.io.protocol.c2d.heart.PongHandler) C2DHessianMsgDecoder(com.github.jvm.io.protocol.c2d.codc.hessian.C2DHessianMsgDecoder) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) C2DHessianMsgEncoder(com.github.jvm.io.protocol.c2d.codc.hessian.C2DHessianMsgEncoder) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ReadTimeoutHandler(io.netty.handler.timeout.ReadTimeoutHandler) LoginAuthRespHandler(com.github.jvm.io.protocol.c2d.auth.LoginAuthRespHandler) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 79 with SocketChannel

use of org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel in project jdepth by Crab2died.

the class EchoServer method bind.

private void bind(int port) throws InterruptedException {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap bootstrap = new ServerBootstrap().group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast(new FixedLengthFrameDecoder(15)).addLast(new StringDecoder()).addLast(new EchoServerHandler());
            }
        });
        ChannelFuture future = bootstrap.bind(port).sync();
        future.channel().closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) StringDecoder(io.netty.handler.codec.string.StringDecoder) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) FixedLengthFrameDecoder(io.netty.handler.codec.FixedLengthFrameDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 80 with SocketChannel

use of org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel in project jdepth by Crab2died.

the class SubReqServer method bind.

public void bind(int port) throws InterruptedException {
    EventLoopGroup boosGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap bootstrap = new ServerBootstrap().group(boosGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast(new ObjectDecoder(1024 * 1024, ClassResolvers.weakCachingConcurrentResolver(this.getClass().getClassLoader()))).addLast(new ObjectEncoder()).addLast(new SubReqServerHandler());
            }
        });
        ChannelFuture future = bootstrap.bind(port).sync();
        future.channel().closeFuture().sync();
    } finally {
        boosGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ObjectDecoder(io.netty.handler.codec.serialization.ObjectDecoder) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ObjectEncoder(io.netty.handler.codec.serialization.ObjectEncoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

SocketChannel (io.netty.channel.socket.SocketChannel)283 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)151 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)114 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)107 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)105 Bootstrap (io.netty.bootstrap.Bootstrap)103 ChannelPipeline (io.netty.channel.ChannelPipeline)95 ChannelFuture (io.netty.channel.ChannelFuture)93 EventLoopGroup (io.netty.channel.EventLoopGroup)92 InetSocketAddress (java.net.InetSocketAddress)46 LoggingHandler (io.netty.handler.logging.LoggingHandler)45 IOException (java.io.IOException)44 Channel (io.netty.channel.Channel)42 LengthFieldBasedFrameDecoder (io.netty.handler.codec.LengthFieldBasedFrameDecoder)36 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)35 SslContext (io.netty.handler.ssl.SslContext)34 ByteBuf (io.netty.buffer.ByteBuf)31 StringDecoder (io.netty.handler.codec.string.StringDecoder)27 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)20 ChannelInitializer (io.netty.channel.ChannelInitializer)19