Class: Net::DND::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/net/dnd/connection.rb

Overview

An internal class, used by the Session object, to manage the TCP Socket connection to the requested DND server. The Connection object contains the low-level protocol commands that are actually composed and sent down the socket. Once a command is sent the Connection object instantiates a new Response object to parse the returned data. The Response object is sent back to the Session as the result of calling the fields and lookup methods. The quit method is used to close down the socket connection. It doesn’t actually return anything back to the Session.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname) ⇒ Connection

Initialize the TCP connection to be used by the Session. Will raise errors if there’s no response from port 902 on the supplied host, or if the connection attempt times out. This constructor also verifies that the connected DND server is ready to respond to protocol commands.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/net/dnd/connection.rb', line 27

def initialize(hostname)
  @host = hostname
  @open = false
  begin
    @socket = Timeout::timeout(5) { TCPSocket.open(host, 902) }
  rescue Timeout::Error
    @error = "Connection attempt to DND server on host #{host} has timed out."
    return
  rescue Errno::ECONNREFUSED
    @error = "Could not connect to DND server on host #{host}."
    return
  end
  @response = Response.process(socket)
  @open = @response.ok?
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



20
21
22
# File 'lib/net/dnd/connection.rb', line 20

def error
  @error
end

#hostObject (readonly)

Returns the value of attribute host.



20
21
22
# File 'lib/net/dnd/connection.rb', line 20

def host
  @host
end

#responseObject (readonly)

Returns the value of attribute response.



20
21
22
# File 'lib/net/dnd/connection.rb', line 20

def response
  @response
end

#socketObject (readonly)

Returns the value of attribute socket.



20
21
22
# File 'lib/net/dnd/connection.rb', line 20

def socket
  @socket
end

Instance Method Details

#fields(field_list = []) ⇒ Object

Low-level protocol command for verifying a list of supplied fields. If no fields are supplied, the fields command will return verification data for all known fields.



52
53
54
55
# File 'lib/net/dnd/connection.rb', line 52

def fields(field_list=[])
  cmd = "fields #{field_list.join(' ')}".rstrip
  read_response(cmd)
end

#lookup(user, field_list) ⇒ Object

Low-level protocol command for performing a ‘find’ operation. Takes a user specifier and a list of fields.



60
61
62
63
64
# File 'lib/net/dnd/connection.rb', line 60

def lookup(user, field_list)
  user_spec = UserSpec.new(user)
  cmd = "lookup #{user_spec.to_s},#{field_list.join(' ')}"
  read_response(cmd)
end

#open?Boolean

Is the TCP socket still open/active?

Returns:

  • (Boolean)


45
46
47
# File 'lib/net/dnd/connection.rb', line 45

def open?
  @open
end

#quit(noargs = nil) ⇒ Object

Low-level protocol command for telling the DND server that you are closing the connection. Calling this method on the socket also closes the session’s TCP connection.



69
70
71
72
73
74
# File 'lib/net/dnd/connection.rb', line 69

def quit(noargs = nil)
  cmd = "quit"
  read_response(cmd)
  @socket.close
  response
end