class Alien::AlienConnection

Attributes

raise_errors[RW]

Public Class Methods

new() click to toggle source
# File lib/alien/alienconnection.rb, line 19
def initialize
  @connected = false
  @raise_errors = true
end

Public Instance Methods

close(send_quit=true) click to toggle source

Close the socket.

# File lib/alien/alienconnection.rb, line 172
def close(send_quit=true)
  if @connected
    if send_quit
      @sock.write("quit\r\n")
      sleep 1
    end
    @sock.close()
  end

  @connected = false
  return true
end
connect(ipaddress='localhost', port=23) click to toggle source

Try to open a socket connection to the reader.

# File lib/alien/alienconnection.rb, line 108
def connect(ipaddress='localhost', port=23)
  @connected = false

  #connect to a reader and grab the welcome message...
  begin
    timeout(3) {
      @sock = TCPSocket.new(ipaddress, port)
    }

    s = receive() #Grab the welcome message
    if s.include?("later.")  #Reader is busy. Please call back later.
      raise "Trouble Connecting to #{ipaddress}. (Someone else is talking to the reader.)"
    end
    @connected = true
  rescue RuntimeError
    raise
  rescue Timeout::Error, Errno::ETIMEDOUT
    raise "Trouble Connecting to #{ipaddress}. (No reader at this IP?)"
  rescue Errno::ECONNREFUSED
    raise "Trouble Connecting to #{ipaddress}. (Connection refused.)"
  end

  return @connected
end
connected() click to toggle source

Returns the connected status of this AlienReader.

# File lib/alien/alienconnection.rb, line 25
def connected
  return @connected
end
open(ipaddress="localhost", port=23, username="alien", password="password") click to toggle source

Execute both the connect and login methods in one call.

# File lib/alien/alienconnection.rb, line 160
def open(ipaddress="localhost", port=23, username="alien", password="password")
  connect(ipaddress,port)

  if @connected
    login(username,password)
  end

  return @connected
end
receive(opts={}) click to toggle source

Read from the open socket until a null character is found. Strips off the trailing rn from the response

# File lib/alien/alienconnection.rb, line 32
def receive(opts={})
  timeout = opts.fetch(:timeout, 40).to_i
  wait_for_null = opts.fetch(:wait_for_null, true)

  s = ""

  # Wait for data to become available on the socket
  res = select([@sock], nil, nil, timeout)
  if (res == nil)
    raise "Timeout waiting for reader response." if @raise_errors
  end

  if (wait_for_null)
    # Some readers don't put a null on the end of the 'call back later' message.
    # Check for 'later' to know when to punt on the read.
    begin
      timeout(timeout) {
        char = @sock.recv(1)
        while (char != "\0")
          s << char
          char = @sock.recv(1)
        end
      }
    rescue Timeout::Error
      raise "Timeout waiting for reader response." if @raise_errors
    end

    s.strip!

    if s.include? "Error"
      raise s if @raise_errors
    end

    # If this is a response to a Quit command, the reader will close the socket.
    # If there is an active connection, the reader will reject our attempt to connect.
    # Either way, we're not connected anymore...
    if (s.include? "Goodbye!")
      close(false)
    end

    return s
  else
    # Simply try to read up to 1 kB and return it
    return @sock.recv(1024)
  end
end
send(msg="") click to toggle source

Send a message over an open socket (Alien terse msg format to suppress prompts on reply)

# File lib/alien/alienconnection.rb, line 81
def send (msg="")
  if @connected
    @sock.write "\1#{msg}\r\n" # leading \1 for terse reply
  end
end
sendreceive(msg="", opts={}) click to toggle source

Send a message over an open socket and wait for a reply

# File lib/alien/alienconnection.rb, line 89
def sendreceive(msg="", opts={})
  timeout = opts.fetch(:timeout, 40)
  wait_for_null = opts.fetch(:wait_for_null, true)

  begin
    if @connected
      send(msg)
      receive(:timeout=>timeout, :wait_for_null=>wait_for_null)
    else
      raise "Not connected to reader."
    end
  rescue
    err = "Error in alienconnection:\nTried to send:\"#{msg}\"\nand got:\n\"" + String($!) +"\""
    raise err
  end
end

Private Instance Methods

login(username="alien", password="password") click to toggle source

Login to the reader on an open socket connection. Call connect first.

# File lib/alien/alienconnection.rb, line 136
def login (username="alien", password="password")
  if @connected
    begin
      @sock.write "#{username}\r\n"
      receive()
      @sock.write "#{password}\r\n"
      s = receive()

      if s.include? "Error:"
        err_msg = s.scan(/(Error: )(.*)/).flatten[1]
        close()
        raise "Trouble logging in. " << err_msg
        @connected = false
      end
    rescue
      raise
    end
  end

  return @connected
end