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
- #channel_unregistered(ctx) ⇒ Object
- #client_has_shut_down ⇒ Object
- #close(channel = @channel) ⇒ Object
- #connect(host = @options[:host], port = @options[:port]) ⇒ Object
- #execute_command(str, client = self) ⇒ Object
- #gets ⇒ Object
- #invoke_user_app ⇒ Object
- #message_received(ctx, message) ⇒ Object
- #puts(msg) ⇒ Object
- #read_user_commands ⇒ Object
- #session ⇒ Object
- #shut_down_callbacks ⇒ Object
- #shutdown ⇒ Object
- #to_s ⇒ Object
- #wait_until_channel_is_active(timeout = 5, give_up = Time.now + timeout) ⇒ Object
- #when_client_has_shut_down(*args, &block) ⇒ Object
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_down ⇒ Object
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. 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. rescue StandardError => e raise "Connection failure: #{e.}" 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 |
#gets ⇒ Object
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.}" nil end |
#invoke_user_app ⇒ Object
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 (ctx, ) notify :message_received, ctx, if @application_handler.nil? $stdout.print .chomp unless .nil? else @application_handler.call(ctx, ) 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_commands ⇒ Object
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 |
#session ⇒ Object
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_callbacks ⇒ Object
158 159 160 |
# File 'lib/client.rb', line 158 def shut_down_callbacks @shut_down_callbacks ||= [] end |
#shutdown ⇒ Object
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_s ⇒ Object
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 |