io.netty.channel.ChannelInitializer Scala Examples
The following examples show how to use io.netty.channel.ChannelInitializer.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: NettyServer.scala From lila-ws with GNU Affero General Public License v3.0 | 5 votes |
package lila.ws package netty import com.typesafe.config.Config import com.typesafe.scalalogging.Logger import io.netty.bootstrap.ServerBootstrap import io.netty.channel.{ Channel, ChannelInitializer } import io.netty.channel.epoll.{ EpollEventLoopGroup, EpollServerSocketChannel } import io.netty.channel.nio.NioEventLoopGroup import io.netty.channel.socket.nio.NioServerSocketChannel import io.netty.handler.codec.http._ import scala.concurrent.ExecutionContext final class NettyServer( clients: ClientSystem, router: Router, config: Config )(implicit ec: ExecutionContext) { private val logger = Logger(getClass) def start(): Unit = { logger.info("Start") val port = config.getInt("http.port") val useEpoll = config.getBoolean("netty.useEpoll") val bossGroup = if (useEpoll) new EpollEventLoopGroup(1) else new NioEventLoopGroup(1) val workerGroup = if (useEpoll) new EpollEventLoopGroup else new NioEventLoopGroup val channelClz = if (useEpoll) classOf[EpollServerSocketChannel] else classOf[NioServerSocketChannel] try { val boot = new ServerBootstrap boot .group(bossGroup, workerGroup) .channel(channelClz) .childHandler(new ChannelInitializer[Channel] { override def initChannel(ch: Channel): Unit = { val pipeline = ch.pipeline() pipeline.addLast(new HttpServerCodec) pipeline.addLast(new HttpObjectAggregator(4096)) pipeline.addLast(new ProtocolHandler(clients, router)) pipeline.addLast(new FrameHandler) } }) val server = boot.bind(port).sync().channel() logger.info(s"Listening to $port") server.closeFuture().sync() logger.info(s"Closed $port") } finally { bossGroup.shutdownGracefully() workerGroup.shutdownGracefully() } } }
Example 2
Source File: RealmConnector.scala From wowchat with GNU General Public License v3.0 | 5 votes |
package wowchat.realm import java.net.InetSocketAddress import java.util.concurrent.TimeUnit import wowchat.common._ import com.typesafe.scalalogging.StrictLogging import io.netty.bootstrap.Bootstrap import io.netty.channel.socket.SocketChannel import io.netty.channel.socket.nio.NioSocketChannel import io.netty.channel.{Channel, ChannelInitializer, ChannelOption} import io.netty.handler.timeout.IdleStateHandler import io.netty.util.concurrent.Future import scala.util.Try class RealmConnector(realmConnectionCallback: RealmConnectionCallback) extends StrictLogging { private var channel: Option[Channel] = None private var connected: Boolean = false def connect: Unit = { logger.info(s"Connecting to realm server ${Global.config.wow.realmlist.host}:${Global.config.wow.realmlist.port}") val bootstrap = new Bootstrap bootstrap.group(Global.group) .channel(classOf[NioSocketChannel]) .option[java.lang.Integer](ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000) .option[java.lang.Boolean](ChannelOption.SO_KEEPALIVE, true) .remoteAddress(new InetSocketAddress(Global.config.wow.realmlist.host, Global.config.wow.realmlist.port)) .handler(new ChannelInitializer[SocketChannel]() { @throws[Exception] override protected def initChannel(socketChannel: SocketChannel): Unit = { val handler = if (WowChatConfig.getExpansion == WowExpansion.Vanilla) { new RealmPacketHandler(realmConnectionCallback) } else { new RealmPacketHandlerTBC(realmConnectionCallback) } socketChannel.pipeline.addLast( new IdleStateHandler(60, 120, 0), new IdleStateCallback, new RealmPacketDecoder, new RealmPacketEncoder, handler ) } }) channel = Some(bootstrap.connect.addListener((future: Future[_ >: Void]) => { Try { future.get(10, TimeUnit.SECONDS) }.fold(throwable => { logger.error(s"Failed to connect to realm server! ${throwable.getMessage}") realmConnectionCallback.disconnected }, _ => Unit) }).channel) } }
Example 3
Source File: EchoClient.scala From netty-in-action-scala with Apache License 2.0 | 5 votes |
package nia.chapter2.echoclient import io.netty.bootstrap.Bootstrap import io.netty.channel.ChannelInitializer import io.netty.channel.EventLoopGroup import io.netty.channel.nio.NioEventLoopGroup import io.netty.channel.socket.SocketChannel import io.netty.channel.socket.nio.NioSocketChannel import java.net.InetSocketAddress object EchoClient { @throws[Exception] def main(args: Array[String]): Unit = { if (args.length != 2) { System.err.println("Usage: " + classOf[EchoClient].getSimpleName + " <host> <port>") } else { val host = args(0) val port = args(1).toInt new EchoClient(host, port).start() } } } class EchoClient(val host: String, val port: Int) { @throws[Exception] def start(): Unit = { val group: EventLoopGroup = new NioEventLoopGroup try { //创建 Bootstrap val b = new Bootstrap //指定 EventLoopGroup 以处理客户端事件;需要适用于 NIO 的实现 b.group(group) //适用于 NIO 传输的Channel 类型 .channel(classOf[NioSocketChannel]) //设置服务器的InetSocketAddress .remoteAddress(new InetSocketAddress(host, port)) //在创建Channel时,向 ChannelPipeline中添加一个 EchoClientHandler实例 .handler { new ChannelInitializer[SocketChannel]() { @throws[Exception] override def initChannel(ch: SocketChannel): Unit = { ch.pipeline.addLast(new EchoClientHandler) } } } //连接到远程节点,阻塞等待直到连接完成 val f = b.connect.sync() //阻塞,直到Channel 关闭 f.channel.closeFuture.sync() } finally { //关闭线程池并且释放所有的资源 group.shutdownGracefully.sync() } } }
Example 4
Source File: GrpcGatewayServerBuilder.scala From grpcgateway with MIT License | 5 votes |
package grpcgateway.server import grpcgateway.handlers.{GrpcGatewayHandler, MethodNotFoundHandler, SwaggerHandler} import io.netty.bootstrap.ServerBootstrap import io.netty.channel.ChannelInitializer import io.netty.channel.nio.NioEventLoopGroup import io.netty.channel.socket.SocketChannel import io.netty.channel.socket.nio.NioServerSocketChannel import io.netty.handler.codec.http.{HttpObjectAggregator, HttpServerCodec} case class GrpcGatewayServerBuilder( port: Int = 80, services: Seq[GrpcGatewayHandler] = Nil ) { def forPort(port: Int): GrpcGatewayServerBuilder = { copy(port = port) } def addService(service: GrpcGatewayHandler): GrpcGatewayServerBuilder = { copy(services = services :+ service) } def build(): GrpcGatewayServer = { val masterGroup = new NioEventLoopGroup() val slaveGroup = new NioEventLoopGroup() val bootstrap = new ServerBootstrap() bootstrap .group(masterGroup, slaveGroup) .channel(classOf[NioServerSocketChannel]) .childHandler(new ChannelInitializer[SocketChannel] { override def initChannel(ch: SocketChannel): Unit = { ch.pipeline().addLast("codec", new HttpServerCodec()) ch.pipeline().addLast("aggregator", new HttpObjectAggregator(512 * 1024)) ch.pipeline().addLast("swagger", new SwaggerHandler(services)) services.foreach { handler => ch.pipeline().addLast(handler.name, handler) } ch.pipeline().addLast(new MethodNotFoundHandler()) } }) new GrpcGatewayServer(port, bootstrap, masterGroup, slaveGroup, services.toList) } } object GrpcGatewayServerBuilder { def forPort(port: Int): GrpcGatewayServerBuilder = new GrpcGatewayServerBuilder().forPort(port) def addService(service: GrpcGatewayHandler): GrpcGatewayServerBuilder = new GrpcGatewayServerBuilder().addService(service) }
Example 5
Source File: ProxyIntegrationTest.scala From Neutrino with Apache License 2.0 | 5 votes |
package com.ebay.neutrino.integ import com.ebay.neutrino.config.{Configuration, LoadBalancer, VirtualPool, VirtualServer} import com.ebay.neutrino.handler.{ExampleCloseHandler, ExamplePipelineHandler} import com.ebay.neutrino.{NettyClientSupport, NeutrinoCore} import io.netty.channel.{Channel, ChannelInitializer} import io.netty.handler.codec.http.{DefaultFullHttpRequest, HttpMethod, HttpVersion} import org.scalatest.{BeforeAndAfterAll, FlatSpec, Ignore, Matchers} import scala.concurrent.Await import scala.concurrent.duration._ @Ignore class ProxyIntegrationTest extends FlatSpec with NettyClientSupport with Matchers with BeforeAndAfterAll { // Create a new balancer val config = Configuration.load("proxy.conf") val core = NeutrinoCore(config) val server = new NettyEchoServer() override def beforeAll() = { val servers = Seq(VirtualServer("id", "localhost", 8081)) val pools = Seq(VirtualPool(servers=servers)) // Start running the downstream server server.start() // Start running the proxy. This will run until the process is interrupted... core.configure(LoadBalancer("id", pools)) Await.ready(core.start(), 5 seconds) } override def afterAll() = { Await.ready(core.shutdown(), 5 seconds) server.shutdown() } it should "run 10000 requests" in { // We'll have to connect as well val client = HttpClient(port=8080) val request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/") for (i <- 0 until 10000) { val conn = client.send(request) conn.channel.close() } } } class ProxyIntegrationInitializer extends ChannelInitializer[Channel] { // Initialize the user-configurable pipeline protected def initChannel(ch: Channel): Unit = { ch.pipeline.addLast(new ExampleCloseHandler()) ch.pipeline.addLast(new ExamplePipelineHandler()) //pipeline.addLast(new ExampleCustomHandler()) } }
Example 6
Source File: TransportServer.scala From aloha with Apache License 2.0 | 5 votes |
package me.jrwang.aloha.transport.server import java.io.Closeable import java.net.InetSocketAddress import java.util.concurrent.TimeUnit import io.netty.bootstrap.ServerBootstrap import io.netty.channel.{ChannelFuture, ChannelInitializer, ChannelOption} import io.netty.channel.socket.SocketChannel import me.jrwang.aloha.common.Logging import me.jrwang.aloha.common.util.Utils import me.jrwang.aloha.transport.TransportContext import me.jrwang.aloha.transport.util.{IOMode, NettyUtils} class TransportServer( transportContext: TransportContext, hostToBind: String, portToBind: Int, appRpcHandler: RpcHandler, bootstraps: List[TransportServerBootstrap] ) extends Closeable with Logging { private val conf = transportContext.conf private var port: Int = -1 private var bootstrap: ServerBootstrap = _ private var channelFuture: ChannelFuture = _ try init() catch { case e: RuntimeException => Utils.closeQuietly(this) throw e } def init(): Unit = { val ioMode = IOMode.valueOf(conf.ioMode) val bossGroup = NettyUtils.createEventLoop(ioMode, conf.serverThreads, conf.module + "-server") val workerGroup = bossGroup val allocator = NettyUtils.createPooledByteBufAllocator(conf.preferDirectBufs, true , conf.serverThreads) bootstrap = new ServerBootstrap() .group(bossGroup, workerGroup) .channel(NettyUtils.getServerChannelClass(ioMode)) .option(ChannelOption.ALLOCATOR, allocator) .childOption(ChannelOption.ALLOCATOR, allocator) if (conf.backLog > 0) bootstrap.option[java.lang.Integer](ChannelOption.SO_BACKLOG, conf.backLog) if (conf.receiveBuf > 0) bootstrap.childOption[java.lang.Integer](ChannelOption.SO_RCVBUF, conf.receiveBuf) if (conf.sendBuf > 0) bootstrap.childOption[java.lang.Integer](ChannelOption.SO_SNDBUF, conf.sendBuf) bootstrap.childHandler(new ChannelInitializer[SocketChannel]() { override protected def initChannel(ch: SocketChannel): Unit = { val rpcHandler = bootstraps.foldLeft[RpcHandler](appRpcHandler)((r, b) => { b.doBootstrap(ch, r) }) transportContext.initializePipeline(ch, rpcHandler) } }) val address = if (hostToBind == null) new InetSocketAddress(portToBind) else new InetSocketAddress(hostToBind, portToBind) channelFuture = bootstrap.bind(address) channelFuture.syncUninterruptibly port = channelFuture.channel.localAddress.asInstanceOf[InetSocketAddress].getPort logDebug(s"Transport server started on port: $port") } def getPort: Int = { if (port == -1) throw new IllegalStateException("Server not initialized") port } def awaitTermination(): Unit = { channelFuture.channel().closeFuture().sync() } override def close(): Unit = { if (channelFuture != null) { // close is a local operation and should finish within milliseconds; timeout just to be safe channelFuture.channel.close.awaitUninterruptibly(10, TimeUnit.SECONDS) channelFuture = null } if (bootstrap != null && bootstrap.config().group() != null) bootstrap.config().group().shutdownGracefully if (bootstrap != null && bootstrap.config().childGroup() != null) bootstrap.config().childGroup().shutdownGracefully bootstrap = null } }
Example 7
Source File: MySQLSocket.scala From asyncdb with Apache License 2.0 | 5 votes |
package io.asyncdb package netty package mysql import cats.syntax.all._ import cats.effect._ import cats.effect.concurrent._ import cats.data.NonEmptyList import io.netty.bootstrap.Bootstrap import io.netty.channel.{Channel, ChannelInitializer} import java.nio.charset.Charset import protocol.client._ import protocol.server._ case class MySQLSocketConfig( bootstrap: Bootstrap, username: String, password: Option[String], database: Option[String], charset: Short, authMethod: Option[String] ) extends NettySocketConfig class MySQLSocket[F[_]]( config: MySQLSocketConfig, channelHolder: Deferred[F, Either[Throwable, Channel]], ref: MsgRef[F] )(implicit F: Concurrent[F]) extends NettySocket[F, Message](config, channelHolder) { def connect = { open.flatMap(_.read).as(this) } def disconnect = { close.void } def write(n: Message) = { channel.flatMap(_.write(n).to[F]).void } def read = ref.take.flatMap { case OrErr(value) => F.fromEither(value) case v => F.pure(v) } } object MySQLSocket { def apply[F[_]: ConcurrentEffect](config: MySQLSocketConfig) = { for { msgRef <- MVar[F].empty[Message] clientCS <- Deferred[F, Charset] initCtx = ChannelContext( ChannelState.Handshake.WaitHandshakeInit, clientCS ) ctxRef <- Ref[F].of(initCtx) decoder = new FrameDecoder[F](config, ctxRef, msgRef) encoder = new FrameEncoder(config) initHandler = new ChannelInitializer[Channel] { override def initChannel(channel: Channel): Unit = { channel .pipeline() .addLast("MySQLFrameDecoder", decoder) .addLast("MySQLFrameEncoder", encoder) } } _ = config.bootstrap.handler(initHandler) channel <- Deferred[F, Either[Throwable, Channel]] } yield new MySQLSocket[F](config, channel, msgRef) } }
Example 8
Source File: FrontendService.scala From spark-sql-server with Apache License 2.0 | 5 votes |
package org.apache.spark.sql.server.service import io.netty.bootstrap.ServerBootstrap import io.netty.channel.ChannelInitializer import io.netty.channel.nio.NioEventLoopGroup import io.netty.channel.socket.SocketChannel import io.netty.channel.socket.nio.NioServerSocketChannel import io.netty.handler.logging.{LoggingHandler, LogLevel} import org.apache.spark.sql.internal.SQLConf import org.apache.spark.sql.server.SQLServerConf._ private[service] abstract class FrontendService extends CompositeService { var port: Int = _ var workerThreads: Int = _ var bossGroup: NioEventLoopGroup = _ var workerGroup: NioEventLoopGroup = _ def messageHandler: ChannelInitializer[SocketChannel] override def doInit(conf: SQLConf): Unit = { port = conf.sqlServerPort workerThreads = conf.sqlServerWorkerThreads bossGroup = new NioEventLoopGroup(1) workerGroup = new NioEventLoopGroup(workerThreads) } override def doStart(): Unit = { try { val b = new ServerBootstrap() // .option(ChannelOption.SO_KEEPALIVE, true) .group(bossGroup, workerGroup) .channel(classOf[NioServerSocketChannel]) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(messageHandler) // Binds and starts to accept incoming connections val f = b.bind(port).sync() // Blocked until the server socket is closed logInfo(s"Start running the SQL server (port=$port, workerThreads=$workerThreads)") f.channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully() workerGroup.shutdownGracefully() } } }