Module: Client::InstanceMethods

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

Overview

The InstanceMethods module

Constant Summary collapse

IdentiferTemplate =
'#<%<class>s:0x%<id>s @options=%<opts>s>'.freeze

Instance Method Summary collapse

Instance Method Details

#channel_unregistered(ctx) ⇒ Object



178
179
180
181
# File 'lib/client.rb', line 178

def channel_unregistered(ctx)
  log.trace "##{__method__} channel: #{ctx.channel}"
  # shutdown
end

#client_has_shut_downObject



170
171
172
173
174
175
176
# File 'lib/client.rb', line 170

def client_has_shut_down
  shut_down_callbacks.take_while do |callback|
    callback[:block]&.call(*callback.fetch(:args, []))
  end
rescue StandardError => e
  log.error e.message
end

#close(channel = @channel) ⇒ Object



129
130
131
132
133
134
135
136
# File 'lib/client.rb', line 129

def close(channel = @channel)
  log.debug "Closing client channel: #{channel}"
  channel.closeFuture().sync()
  # Wait until all messages are flushed before closing the channel.
  @last_write_future&.sync()
ensure
  shutdown
end

#connect(host = @options[:host], port = @options[:port]) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/client.rb', line 119

def connect(host = @options[:host], port = @options[:port])
  return unless @channel.nil?
  # Start the connection attempt.
  @channel = bootstrap.connect(host, port).sync().channel()
rescue AbstractChannel::AnnotatedConnectException => e
  raise e.message
rescue StandardError => e
  raise "Connection failure: #{e.message}"
end

#execute_command(str, client = self) ⇒ Object



192
193
194
195
196
# File 'lib/client.rb', line 192

def execute_command(str, client = self)
  return if str.empty?
  client.puts str
  close if @options[:quit_commands].include?(str.downcase.to_sym)
end

#getsObject



107
108
109
110
111
112
113
# File 'lib/client.rb', line 107

def gets
  log.debug 'Waiting for response from server'
  @queue.take
rescue StandardError => e
  warn "Unexpected error waiting for message: #{e.message}"
  nil
end

#invoke_user_appObject



152
153
154
155
156
# File 'lib/client.rb', line 152

def invoke_user_app
  @user_app&.call(self)
ensure
  close
end

#message_received(ctx, message) ⇒ Object



183
184
185
186
187
188
189
190
# File 'lib/client.rb', line 183

def message_received(ctx, message)
  notify :message_received, ctx, message
  if @application_handler.nil?
    $stdout.print message.chomp unless message.nil?
  else
    @application_handler.call(ctx, message)
  end
end

#puts(msg) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/client.rb', line 99

def puts(msg)
  wait_until_channel_is_active
  msg.chomp!
  log.trace "#puts msg: #{msg.inspect}"
  raise 'Message is empty!' if msg.nil? || msg.empty?
  @last_write_future = @channel.writeAndFlush("#{msg}\n")
end

#read_user_commandsObject



198
199
200
201
202
203
204
205
206
207
# File 'lib/client.rb', line 198

def read_user_commands
  log.trace 'Reading user commands'
  loop do
    log.debug 'Waiting for user input...'
    input = $stdin.gets
    raise 'Poll failure from stdin' if input.nil?
    break unless @channel.active?
    break unless execute_command(input).nil?
  end
end

#sessionObject



145
146
147
148
149
150
# File 'lib/client.rb', line 145

def session
  when_client_has_shut_down(@client_group) do |group|
    log.debug "Channel group has shut down: #{group}"
  end
  @user_app.nil? ? read_user_commands : invoke_user_app
end

#shut_down_callbacksObject



158
159
160
# File 'lib/client.rb', line 158

def shut_down_callbacks
  @shut_down_callbacks ||= []
end

#shutdownObject



138
139
140
141
142
143
# File 'lib/client.rb', line 138

def shutdown
  log.debug 'Shutting down gracefully'
  @client_group&.shutdownGracefully()
ensure
  client_has_shut_down
end

#to_sObject



211
212
213
# File 'lib/client.rb', line 211

def to_s
  format(IdentiferTemplate, class: self.class.name, id: object_id.to_s(16), opts: @options.to_s)
end

#wait_until_channel_is_active(timeout = 5, give_up = Time.now + timeout) ⇒ Object



115
116
117
# File 'lib/client.rb', line 115

def wait_until_channel_is_active(timeout = 5, give_up = Time.now + timeout)
  sleep 0.1 until @channel.active? || Time.now > give_up
end

#when_client_has_shut_down(*args, &block) ⇒ Object



162
163
164
165
166
167
168
# File 'lib/client.rb', line 162

def when_client_has_shut_down(*args, &block)
  shut_down_callbacks << {
    block: block,
    args: args
  }
  self
end