Module: Telnet::ClientInstanceMethods
- Included in:
- Client
- Defined in:
- lib/telnet_client.rb
Overview
The ClientInstanceMethods module
Instance Method Summary collapse
- #channel_active(ctx) ⇒ Object
- #channel_inactive(ctx) ⇒ Object
- #channel_unregistered(ctx) ⇒ Object
- #client_has_shut_down ⇒ Object
- #close(channel = @channel) ⇒ Object
- #connect ⇒ Object
- #execute_command(str, client = self) ⇒ Object
- #gets(timeout = nil) ⇒ 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
- #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_active(ctx) ⇒ Object
190 191 192 |
# File 'lib/telnet_client.rb', line 190 def channel_active(ctx) log.info "Connected to #{ctx.channel().remoteAddress0()}" end |
#channel_inactive(ctx) ⇒ Object
194 195 196 |
# File 'lib/telnet_client.rb', line 194 def channel_inactive(ctx) log.info "Disconnected from #{ctx.channel().remoteAddress0()}" end |
#channel_unregistered(ctx) ⇒ Object
185 186 187 188 |
# File 'lib/telnet_client.rb', line 185 def channel_unregistered(ctx) log.trace "##{__method__} channel: #{ctx.channel}" shutdown end |
#client_has_shut_down ⇒ Object
177 178 179 180 181 182 183 |
# File 'lib/telnet_client.rb', line 177 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
138 139 140 141 142 143 |
# File 'lib/telnet_client.rb', line 138 def close(channel = @channel) log.debug 'Closing primary channel' channel.closeFuture().sync() ensure shutdown end |
#connect ⇒ Object
129 130 131 132 133 134 135 136 |
# File 'lib/telnet_client.rb', line 129 def connect @channel = bootstrap.connect(@host, @port).sync().channel() @last_write_future&.sync() rescue AbstractChannel::AnnotatedConnectException => e raise e. rescue StandardError => e raise "Connection failure: #{e.message}" end |
#execute_command(str, client = self) ⇒ Object
208 209 210 211 212 |
# File 'lib/telnet_client.rb', line 208 def execute_command(str, client = self) return if str.empty? client.puts "#{str}\r\n" close if [:quit_commands].include?(str.downcase.to_sym) end |
#gets(timeout = nil) ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/telnet_client.rb', line 117 def gets(timeout = nil) log.debug 'Waiting for response from server' timeout.nil? ? @queue.take : @queue.take(timeout) rescue StandardError => e warn "Unexpected error waiting for message: #{e.message}" nil end |
#invoke_user_app ⇒ Object
159 160 161 162 163 |
# File 'lib/telnet_client.rb', line 159 def invoke_user_app @user_app&.call(self) ensure close end |
#message_received(ctx, message) ⇒ Object
198 199 200 201 202 203 204 205 206 |
# File 'lib/telnet_client.rb', line 198 def (ctx, ) log.debug "##{__method__} message: #{message}" notify :message_received, ctx, if @application_handler.nil? $stdout.print .chomp unless .nil? else @application_handler.call(ctx, ) end end |
#puts(msg) ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/telnet_client.rb', line 109 def puts(msg) wait_until_channel_is_active msg.chomp! log.trace "#puts msg: #{msg.inspect}" return if msg.nil? || msg.empty? @last_write_future = @channel.writeAndFlush("#{msg}\r\n") end |
#read_user_commands ⇒ Object
214 215 216 217 218 219 220 221 222 223 |
# File 'lib/telnet_client.rb', line 214 def read_user_commands log.trace 'Reading user commands' loop do log.debug 'Waiting for user input...' input = $stdin.gets&.chomp raise 'Poll failure from stdin' if input.nil? break unless @channel.active? break if execute_command(input).is_a?(AbstractChannel::CloseFuture) end end |
#session ⇒ Object
152 153 154 155 156 157 |
# File 'lib/telnet_client.rb', line 152 def session when_client_has_shut_down(@client_group) do |group| log.debug "Channel group has shut down: #{group.inspect}" end @user_app.nil? ? read_user_commands : invoke_user_app end |
#shut_down_callbacks ⇒ Object
165 166 167 |
# File 'lib/telnet_client.rb', line 165 def shut_down_callbacks @shut_down_callbacks ||= [] end |
#shutdown ⇒ Object
145 146 147 148 149 150 |
# File 'lib/telnet_client.rb', line 145 def shutdown log.debug 'Shutting down gracefully' @client_group&.shutdownGracefully() ensure client_has_shut_down end |
#wait_until_channel_is_active(timeout = 5, give_up = Time.now + timeout) ⇒ Object
125 126 127 |
# File 'lib/telnet_client.rb', line 125 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
169 170 171 172 173 174 175 |
# File 'lib/telnet_client.rb', line 169 def when_client_has_shut_down(*args, &block) shut_down_callbacks << { block: block, args: args } self end |