Module: TcpServer::InstanceMethods

Includes:
DefaultHandler
Included in:
Server
Defined in:
lib/tcp_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



148
149
150
# File 'lib/tcp_server/instance_methods.rb', line 148

def <<(handler)
  channelizer << handler
end

#add_listener(*listener) ⇒ Object



152
153
154
# File 'lib/tcp_server/instance_methods.rb', line 152

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

#backlog_optionObject

rubocop: enable Metrics/AbcSize



45
46
47
# File 'lib/tcp_server/instance_methods.rb', line 45

def backlog_option
  ChannelOption::SO_BACKLOG
end

#bootstrapObject

rubocop: disable Metrics/AbcSize



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

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

#boss_groupObject



66
67
68
# File 'lib/tcp_server/instance_methods.rb', line 66

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

#channel_groupObject



74
75
76
77
# File 'lib/tcp_server/instance_methods.rb', line 74

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

#channel_typeObject



58
59
60
# File 'lib/tcp_server/instance_methods.rb', line 58

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

#channelizerObject



62
63
64
# File 'lib/tcp_server/instance_methods.rb', line 62

def channelizer
  @channelizer ||= TcpServer::Channelizer.new(channel_group, @options)
end

#configure_handlers(*handlers, &block) ⇒ Object



53
54
55
56
# File 'lib/tcp_server/instance_methods.rb', line 53

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

#handle_interruptObject



115
116
117
118
119
# File 'lib/tcp_server/instance_methods.rb', line 115

def handle_interrupt
  $stdout.write "\r\e[0K"
  $stdout.flush
  shutdown
end

#keep_aliveObject



160
161
162
163
164
165
# File 'lib/tcp_server/instance_methods.rb', line 160

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

#keep_alive_optionObject



49
50
51
# File 'lib/tcp_server/instance_methods.rb', line 49

def keep_alive_option
  ChannelOption::SO_KEEPALIVE
end

#logging_handlerObject



79
80
81
# File 'lib/tcp_server/instance_methods.rb', line 79

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

#max_queued_incoming_connectionsObject



167
168
169
170
171
172
173
# File 'lib/tcp_server/instance_methods.rb', line 167

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



111
112
113
# File 'lib/tcp_server/instance_methods.rb', line 111

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

#quietly(preserved_verbose = $VERBOSE) ⇒ Object



133
134
135
136
137
138
# File 'lib/tcp_server/instance_methods.rb', line 133

def quietly(preserved_verbose = $VERBOSE)
  $VERBOSE = nil
  yield
ensure
  $VERBOSE = preserved_verbose
end

#replace_listeners(*listener) ⇒ Object



156
157
158
# File 'lib/tcp_server/instance_methods.rb', line 156

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

#run(port = self.port) ⇒ Object

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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/tcp_server/instance_methods.rb', line 89

def run(port = self.port)
  channel = server_channel(port)
  channel_group.add(channel)
  Signal.trap('INT') { handle_interrupt }
  Signal.trap('TERM') { handle_interrupt }
  log.info "Listening on #{channel.local_address}"
  channel.closeFuture().sync()
rescue Interrupt => _e
  $stdout.write "\r\e[0K"
  $stdout.flush
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

#server_channel(port = self.port) ⇒ Object



83
84
85
# File 'lib/tcp_server/instance_methods.rb', line 83

def server_channel(port = self.port)
  @server_channel ||= bootstrap.bind(port).sync().channel()
end

#shutdownObject



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/tcp_server/instance_methods.rb', line 121

def shutdown
  logger.info 'Shutting down'
  quietly do
    server_channel&.close()
    channel_group.disconnect().awaitUninterruptibly()
    channel_group.close().awaitUninterruptibly()
  end
rescue StandardError => e
  warn e.message
  $stderr.flush
end

#stopObject



140
141
142
143
144
145
146
# File 'lib/tcp_server/instance_methods.rb', line 140

def stop
  boss_group&.shutdownGracefully()
  worker_group&.shutdownGracefully()
rescue StandardError => e
  warn e.message
  $stderr.flush
end

#worker_groupObject



70
71
72
# File 'lib/tcp_server/instance_methods.rb', line 70

def worker_group
  @worker_group ||= NioEventLoopGroup.new
end