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
186 187 188 |
# File 'lib/telnet_client.rb', line 186 def channel_active(ctx) log.info "Connected to #{ctx.channel().remoteAddress0()}" end |
#channel_inactive(ctx) ⇒ Object
190 191 192 |
# File 'lib/telnet_client.rb', line 190 def channel_inactive(ctx) log.info "Disconnected from #{ctx.channel().remoteAddress0()}" end |
#channel_unregistered(ctx) ⇒ Object
181 182 183 184 |
# File 'lib/telnet_client.rb', line 181 def channel_unregistered(ctx) log.trace "##{__method__} channel: #{ctx.channel}" shutdown end |
#client_has_shut_down ⇒ Object
173 174 175 176 177 178 179 |
# File 'lib/telnet_client.rb', line 173 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
134 135 136 137 138 139 |
# File 'lib/telnet_client.rb', line 134 def close(channel = @channel) log.debug 'Closing primary channel' channel.closeFuture().sync() ensure shutdown end |
#connect ⇒ Object
125 126 127 128 129 130 131 132 |
# File 'lib/telnet_client.rb', line 125 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.}" end |
#execute_command(str, client = self) ⇒ Object
204 205 206 207 208 |
# File 'lib/telnet_client.rb', line 204 def execute_command(str, client = self) return if str.empty? client.puts "#{str}\r\n" close if @options[:quit_commands].include?(str.downcase.to_sym) end |
#gets(timeout = nil) ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/telnet_client.rb', line 113 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.}" nil end |
#invoke_user_app ⇒ Object
155 156 157 158 159 |
# File 'lib/telnet_client.rb', line 155 def invoke_user_app @user_app&.call(self) ensure close end |
#message_received(ctx, message) ⇒ Object
194 195 196 197 198 199 200 201 202 |
# File 'lib/telnet_client.rb', line 194 def (ctx, ) log.debug "##{__method__} message: #{}" notify :message_received, ctx, if @application_handler.nil? $stdout.print .chomp unless .nil? else @application_handler.call(ctx, ) end end |
#puts(msg) ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/telnet_client.rb', line 105 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
210 211 212 213 214 215 216 217 218 219 |
# File 'lib/telnet_client.rb', line 210 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
148 149 150 151 152 153 |
# File 'lib/telnet_client.rb', line 148 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
161 162 163 |
# File 'lib/telnet_client.rb', line 161 def shut_down_callbacks @shut_down_callbacks ||= [] end |
#shutdown ⇒ Object
141 142 143 144 145 146 |
# File 'lib/telnet_client.rb', line 141 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
121 122 123 |
# File 'lib/telnet_client.rb', line 121 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
165 166 167 168 169 170 171 |
# File 'lib/telnet_client.rb', line 165 def when_client_has_shut_down(*args, &block) shut_down_callbacks << { block: block, args: args } self end |