Class: Gibson::Connection

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

Constant Summary collapse

DEFAULTS =

Connection default options.

{
  # The UNIX socket path, if this option is set a UNIX socket connection will be used.
  :socket    => '/var/run/gibson.sock',
  # The ip address to connect to, if this option is set a TCP socket connection will be used.
  :address   => nil,
  # The tcp port to connect to.
  :port      => 10128,
  # The connection and I/O timeout in milliseconds.
  :timeout   => 100,
  # If a TCP connection will be used, set this to true to use the SO_KEEPALIVE flag on the socket.
  :keepalive => false
}

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Connection

Create a new Connection instance with custom options. If no options are specified, Connection.DEFAULTS will be used. For instance:

Gibson::Client.new                         # will create a connection to the default /var/run/gibson.sock UNIX socket.
Gibson::Client.new :address => '127.0.0.1' # will connect to localhost:10128


27
28
29
30
31
# File 'lib/gibson/connection.rb', line 27

def initialize(opts = {})
  @sock = nil
  @connected = false
  @options = DEFAULTS.merge(opts)
end

Instance Method Details

#closeObject

Close the connection.



58
59
60
# File 'lib/gibson/connection.rb', line 58

def close
  @sock.close if connected?
end

#connectObject

Attempt a connection with the specified options until @options is reached.



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/gibson/connection.rb', line 42

def connect
  Timeout.timeout(@options[:timeout]) do
    if @options[:address] != nil
      @sock = TCPSocket.new( @options[:address], @options[:port] ) 
      @sock.setsockopt( Socket::IPPROTO_TCP, Socket::TCP_NODELAY, true )
      @sock.setsockopt( Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true ) if @options[:keepalive]
    else
      @sock = UNIXSocket.open( @options[:socket] )
    end

    @connected = true
  end
end

#connected?Boolean

Return true if connection is enstablished, otherwise false.

Returns:

  • (Boolean)


35
36
37
# File 'lib/gibson/connection.rb', line 35

def connected?
  @connected
end

#read(n) ⇒ Object

Read specified amount of data from the socket.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/gibson/connection.rb', line 83

def read(n)
  read = 0
  data = ""

  while read < n do
    wait_readable

    left = n - read
    
    data += @sock.recv left
    read = data.size
  end

  data
end

#wait_readableObject

Wait for the socket to be in a readable state for @options milliseconds.



70
71
72
# File 'lib/gibson/connection.rb', line 70

def wait_readable
  IO.select( [@sock], nil, nil, @options[:timeout] ) || raise(Timeout::Error, "IO timeout")
end

#wait_writableObject

Wait for the socket to be in a writable state for @options milliseconds.



64
65
66
# File 'lib/gibson/connection.rb', line 64

def wait_writable
  IO.select(nil, [@sock], nil, @options[:timeout] ) || raise(Timeout::Error, "IO timeout")
end

#write(data) ⇒ Object

Write data to the socket.



76
77
78
79
# File 'lib/gibson/connection.rb', line 76

def write(data)
  wait_writable
  @sock.write data
end