Sha256: c3c1124767134d31803e5a9dfe294ada1cec3ca02aba02b8fc54ed82fb601aef

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

import java.net.InetSocketAddress
import org.jboss.netty.channel.group.DefaultChannelGroup
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory
import org.jboss.netty.bootstrap.ServerBootstrap
require_relative 'pipeline'

module Foxbat

  class Server
    def initialize(host, port, klass, options, &block)
      if options[:secure]
        @context = Security.setup_ssl_context(options[:keystore])
      end

      @group = DefaultChannelGroup.new
      @address = InetSocketAddress.new(host, port)
      @pipeline = Pipeline.new(klass, @group, false, options, @context, &block)
    end
    
    def start(threadpool)
      sp = java.util.concurrent.Executors.newSingleThreadExecutor
      factory = NioServerSocketChannelFactory.new(sp, threadpool)
      @bootstrap = ServerBootstrap.new(factory)
      @bootstrap.setPipelineFactory(@pipeline)
      @bootstrap.setOption("child.tcpNoDelay", true)
      server_channel = @bootstrap.bind(@address)
      @group.add(server_channel)
    end

    def connection_count
      @group.size - 1 # -1 to exclude the server's channel
    end

    def stop
      @group.close.awaitUninterruptibly
    end
  end
  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
foxbat-0.2.6 lib/foxbat/server.rb
foxbat-0.2.5 lib/foxbat/server.rb