Module: Client::InitializationMethods

Included in:
TCP::Client
Defined in:
lib/tcp_server/client.rb

Overview

The InitializationMethods module

Instance Method Summary collapse

Instance Method Details

#bootstrapObject



66
67
68
69
70
71
72
73
# File 'lib/tcp_server/client.rb', line 66

def bootstrap
  @bootstrap = Bootstrap.new
  @bootstrap.group(client_group)
  @bootstrap.channel(::TCP::CHANNEL_TYPE)
  @bootstrap.option(ChannelOption::TCP_NODELAY, true)
  @bootstrap.handler(logging_handler) if @options[:log_requests]
  @bootstrap.handler(channelizer)
end

#channelizerObject



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

def channelizer
  @channelizer ||= ::Client::Channelizer.new(@options)
end

#client_groupObject



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

def client_group
  @client_group ||= NioEventLoopGroup.new
end

#configure_handlers(*handlers, &block) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/tcp_server/client.rb', line 87

def configure_handlers(*handlers, &block)
  channelizer.default_handler.add_listener(self)
  channelizer.default_handler.listeners.addAll(handlers)
  @user_app = block
  @application_handler = lambda do |ctx, msg|
    if @user_app.nil? || @user_app.arity == 1
      @queue.add(msg.chomp)
    else
      @user_app.call(ctx, msg)
    end
  end
end

#init(options) ⇒ Object



59
60
61
62
63
64
# File 'lib/tcp_server/client.rb', line 59

def init(options)
  @options = options
  @host = @options[:host]
  @port = @options[:port]
  @queue = LinkedBlockingQueue.new
end

#logging_handlerObject



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

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