class PjLink::Client::Gateway

Constants

DEFAULT_PORT

Attributes

address[R]
connection[R]
connection_key[R]
port[R]

Public Class Methods

new(address, port=DEFAULT_PORT) click to toggle source
# File lib/pj_link/client.rb, line 95
def initialize(address, port=DEFAULT_PORT)
  @address, @port = address, port.to_i
end

Public Instance Methods

send_message(message, password) click to toggle source
# File lib/pj_link/client.rb, line 99
def send_message(message, password)
  retry_count = 0
  begin
    connect(password)
    @connection.write("#{@password_hash}%1#{message}\r")
    response = @connection.waitfor("Prompt" => /%1/).
      gsub(/^.*=/, "")

    response
  rescue Errno::EPIPE
    if retry_count < 4
      disconnect
      retry_count += 1
      retry
    else
      raise
    end
  rescue Net::ReadTimeout
    raise PasswordRequiredError,
      "You must provide a password to access this device"
  ensure
    disconnect
  end
end

Private Instance Methods

connect(password) click to toggle source
# File lib/pj_link/client.rb, line 124
def connect(password)
  retry_count = 0
  begin
    @connection = Net::Telnet.new(
      'Host' => @address,
      'Port' => @port,
      'Telnetmode' => false,
      'Promp' => /%1/)
    _, auth_required, key = *@connection.sock.recvmsg[0].split
  rescue Errno::ECONNRESET
    if retry_count < 4
      retry_count += 1
      sleep 1
      retry
    else
      raise
    end
  end

  @password_hash = Digest::MD5.hexdigest("#{key}#{password}") if auth_required
  self
end
disconnect() click to toggle source
# File lib/pj_link/client.rb, line 147
def disconnect
  begin
    @connection.close if @connection
  rescue IOError; nil
  ensure
    @connection = nil
  end
end