Module: Telnet::InstanceMethods

Included in:
Server
Defined in:
lib/telnet/instance_methods.rb

Overview

The InstanceMethods module

Instance Method Summary collapse

Instance Method Details

#channel_active(ctx) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/telnet/instance_methods.rb', line 23

def channel_active(ctx)
  host_name = InetAddress.local_host.host_name
  # Send greeting for a new connection.
  ctx.write "Welcome to #{host_name}!\r\n"
  ctx.write "The time is #{Time.now}.\r\n"
  ctx.write @options[:prompt]
  ctx.flush
end

#channel_read_complete(ctx) ⇒ Object



66
67
68
69
# File 'lib/telnet/instance_methods.rb', line 66

def channel_read_complete(ctx)
  log.trace "##{__method__} channel: #{ctx.channel}"
  ctx.flush
end

#close(ctx) ⇒ Object



60
61
62
63
64
# File 'lib/telnet/instance_methods.rb', line 60

def close(ctx)
  future = ctx.write "Bye!\r\n"
  # Close the connection
  future.addListener(ChannelFutureListener::CLOSE)
end

#exception_caught(ctx, cause) ⇒ Object



71
72
73
74
# File 'lib/telnet/instance_methods.rb', line 71

def exception_caught(ctx, cause)
  cause.printStackTrace()
  ctx.close
end

#handle_empty_request(ctx) ⇒ Object



51
52
53
54
# File 'lib/telnet/instance_methods.rb', line 51

def handle_empty_request(ctx)
  ctx.write "Please type something.\r\n"
  ctx.write @options[:prompt]
end

#handle_message(ctx, message) ⇒ Object

Generate and write a response. Writing to a ChannelBuffer is not needed here. The encoder will do the conversion.



43
44
45
46
47
48
49
# File 'lib/telnet/instance_methods.rb', line 43

def handle_message(ctx, message)
  request = message.to_s.strip
  return handle_empty_request(ctx) if request.empty?
  return close(ctx) if quit_command?(request)
  ctx.write "Server echo: #{request}\r\n"
  ctx.write @options[:prompt]
end

#message_received(ctx, msg) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/telnet/instance_methods.rb', line 32

def message_received(ctx, msg)
  log.trace "##{__method__} channel: #{ctx.channel}, message: #{msg.inspect}"
  msg&.chomp!
  log.info "Received message: #{msg}"
  return super(ctx, msg) unless respond_to?(:handle_message)
  handle_message(ctx, msg)
end

#quit_command?(request) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/telnet/instance_methods.rb', line 56

def quit_command?(request)
  @options[:quit_commands].include?(request.to_s.downcase.to_sym)
end