Module: Server::InstanceMethods

Includes:
DefaultHandler
Included in:
Server
Defined in:
lib/server/instance_methods.rb

Overview

The InstanceMethods module

Instance Method Summary collapse

Methods included from DefaultHandler

#channel_active, #exception_caught, #message_received

Instance Method Details

#<<(handler) ⇒ Object



109
110
111
# File 'lib/server/instance_methods.rb', line 109

def <<(handler)
  channel_initializer << handler
end

#add_listener(*listener) ⇒ Object



113
114
115
# File 'lib/server/instance_methods.rb', line 113

def add_listener(*listener)
  channel_initializer.add_listener(*listener)
end

#bootstrapObject

rubocop: disable Metrics/AbcSize



35
36
37
38
39
40
41
42
43
# File 'lib/server/instance_methods.rb', line 35

def bootstrap
  @bootstrap = ServerBootstrap.new
  @bootstrap.group(boss_group, worker_group)
  @bootstrap.channel(channel_type)
  @bootstrap.option(ChannelOption::SO_BACKLOG, max_queued_incoming_connections)
  @bootstrap.childOption(ChannelOption::SO_KEEPALIVE, keep_alive) if keep_alive
  @bootstrap.handler(logging_handler) if options.fetch(:log_requests, false)
  @bootstrap.childHandler(channel_initializer)
end

#boss_groupObject



59
60
61
# File 'lib/server/instance_methods.rb', line 59

def boss_group
  @boss_group ||= NioEventLoopGroup.new(2)
end

#channel_groupObject



67
68
69
# File 'lib/server/instance_methods.rb', line 67

def channel_group
  @channel_group ||= DefaultChannelGroup.new('server_channels', GlobalEventExecutor::INSTANCE)
end

#channel_initializerObject



55
56
57
# File 'lib/server/instance_methods.rb', line 55

def channel_initializer
  @channel_initializer ||= ::Server::ChannelInitializer.new(channel_group, @options)
end

#channel_typeObject



51
52
53
# File 'lib/server/instance_methods.rb', line 51

def channel_type
  @channel_type ||= Server::CHANNEL_TYPE
end

#configure_handlers(*handlers, &block) ⇒ Object

rubocop: enable Metrics/AbcSize



46
47
48
49
# File 'lib/server/instance_methods.rb', line 46

def configure_handlers(*handlers, &block)
  channel_initializer << block if block_given?
  add_listener(*handlers)
end

#keep_aliveObject



121
122
123
124
125
126
# File 'lib/server/instance_methods.rb', line 121

def keep_alive
  @keep_alive ||= begin
    value = @options[:keep_alive]
    value || DEFAULT_KEEP_ALIVE
  end
end

#logging_handlerObject



71
72
73
# File 'lib/server/instance_methods.rb', line 71

def logging_handler
  @logging_handler ||= LoggingHandler.new(LogLevel::INFO)
end

#max_queued_incoming_connectionsObject



128
129
130
131
132
133
134
# File 'lib/server/instance_methods.rb', line 128

def max_queued_incoming_connections
  @max_queued_incoming_connections ||= begin
    value = @options[:max_queued_incoming_connections]
    value = DEFAULT_MAX_QUEUED_INCOMING_CONNECTIONS if value.nil?
    value.to_java(java.lang.Integer)
  end
end

#portObject

rubocop: enable Metrics/AbcSize rubocop: enable Metrics/MethodLength



95
96
97
# File 'lib/server/instance_methods.rb', line 95

def port
  @port ||= (@options[:port] || DEFAULT_PORT).to_i
end

#replace_listeners(*listener) ⇒ Object



117
118
119
# File 'lib/server/instance_methods.rb', line 117

def replace_listeners(*listener)
  channel_initializer.replace_listeners(*listener)
end

#run(port = self.port) ⇒ Object

rubocop: disable Metrics/AbcSize rubocop: disable Metrics/MethodLength



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/server/instance_methods.rb', line 77

def run(port = self.port)
  channel = bootstrap.bind(port).sync().channel()
  channel_group.add(channel)
  ::Server::ShutdownHook.new(self)
  log.info "Listening on #{channel.local_address}"
  channel.closeFuture().sync()
rescue java.lang.IllegalArgumentException => e
  raise "Invalid argument: #{e.message}"
rescue java.net.BindException => e
  raise "Bind error: #{e.message}: #{options[:host]}:#{port}"
rescue java.net.SocketException => e
  raise "Socket error: #{e.message}: #{options[:host]}:#{port}"
ensure
  stop
end

#shutdownObject



99
100
101
102
# File 'lib/server/instance_methods.rb', line 99

def shutdown
  channel_group.disconnect().awaitUninterruptibly()
  channel_group.close().awaitUninterruptibly()
end

#stopObject



104
105
106
107
# File 'lib/server/instance_methods.rb', line 104

def stop
  boss_group&.shutdownGracefully()
  worker_group&.shutdownGracefully()
end

#worker_groupObject



63
64
65
# File 'lib/server/instance_methods.rb', line 63

def worker_group
  @worker_group ||= NioEventLoopGroup.new
end