Class: KJess::Connection

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

Overview

Connection

Defined Under Namespace

Classes: Error

Instance Method Summary collapse

Constructor Details

#initialize(host, port = 22133, options = {}) ⇒ Connection

TODO: make port an option at next major version number change



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/kjess/connection.rb', line 59

def initialize( host, port = 22133, options = {} )
  if port.is_a?(Hash)
    options = port
    port = 22133
  end

  @options         = options.dup
  @options[:host] = host
  @options[:port] = Float( port ).to_i
  @socket          = nil
  @pid             = nil
  @read_buffer     = ''
end

Instance Method Details

#closeObject

Internal: close the socket if it is not already closed

Returns nothing



107
108
109
110
111
# File 'lib/kjess/connection.rb', line 107

def close
  @socket.close if @socket and not @socket.closed?
  @read_buffer = ''
  @socket = nil
end

#closed?Boolean

Internal: is the socket closed

Returns true or false

Returns:

  • (Boolean)


116
117
118
119
120
# File 'lib/kjess/connection.rb', line 116

def closed?
  return true if @socket.nil?
  return true if @socket.closed?
  return false
end

#connect_timeoutObject

Public: The timeout for connecting in seconds. Defaults to 2



23
24
25
# File 'lib/kjess/connection.rb', line 23

def connect_timeout
  socket.connect_timeout
end

#hostObject

Public: The hostname/ip address to connect to.



13
14
15
# File 'lib/kjess/connection.rb', line 13

def host
  socket.host
end

#keepalive_active?Boolean

Internal: return thekeepalive timeout

Returns:

  • (Boolean)


38
39
40
# File 'lib/kjess/connection.rb', line 38

def keepalive_active?
  socket.keepalive_active?
end

#keepalive_countObject

Internal: return the keepalive count The keepalive count



44
45
46
# File 'lib/kjess/connection.rb', line 44

def keepalive_count
  socket.keepalive_count
end

#keepalive_idleObject

Internal: return the keepalive idle



54
55
56
# File 'lib/kjess/connection.rb', line 54

def keepalive_idle
  socket.keepalive_idle
end

#keepalive_intervalObject

Internal: return the keepalive interval



49
50
51
# File 'lib/kjess/connection.rb', line 49

def keepalive_interval
  socket.keepalive_interval
end

#portObject

Public: The port number to connect to. Default 22133



18
19
20
# File 'lib/kjess/connection.rb', line 18

def port
  socket.port
end

#read(nbytes) ⇒ Object

Internal: Read from the socket

nbytes - this method takes the number of bytes to read

Returns what IO#read returns



167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/kjess/connection.rb', line 167

def read( nbytes )
  while @read_buffer.length < nbytes
    @read_buffer << socket.readpartial(nbytes - @read_buffer.length)
  end

  result = @read_buffer.slice!(0, nbytes)

  $stderr.puts "<-- #{result}" if $DEBUG
  return result
rescue KJess::Error
  close
  raise
end

#read_timeoutObject

Public: The timeout for reading in seconds. Defaults to 2



28
29
30
# File 'lib/kjess/connection.rb', line 28

def read_timeout
  socket.read_timeout
end

#readline(eom = Protocol::CRLF) ⇒ Object

Internal: read a single line from the socket

eom - the End Of Mesasge delimiter (default: “rn”)

Returns a String



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/kjess/connection.rb', line 140

def readline( eom = Protocol::CRLF )
  while true
    while (idx = @read_buffer.index(eom)) == nil
      @read_buffer << socket.readpartial(10240)
    end

    line = @read_buffer.slice!(0, idx + eom.length)
    $stderr.puts "<-- #{line}" if $DEBUG
    break unless line.strip.length == 0
  end
  return line
rescue KJess::Error
  close
  raise
rescue EOFError
  close
  return "EOF"
rescue => e
  close
  raise Error, "Could not read from #{host}:#{port}: #{e.class}: #{e.message}", e.backtrace
end

#socketObject

Internal: Return the socket that is connected to the Kestrel server

Returns the socket. If the socket is not connected it will connect and then return it.

Make sure that we close the socket if we are not the same process that opened that socket to begin with.

Returns a KJess::Socket



95
96
97
98
99
100
101
102
# File 'lib/kjess/connection.rb', line 95

def socket
  close if @pid && @pid != Process.pid
  return @socket if @socket and not @socket.closed?
  @socket      = Socket.connect( @options )
  @pid         = Process.pid
  @read_buffer = ''
  return @socket
end

#with_additional_read_timeout(additional_timeout, &block) ⇒ Object

Internal: Adds time to the read timeout

additional_timeout - additional number of seconds to the read timeout

Returns nothing



78
79
80
81
82
83
84
# File 'lib/kjess/connection.rb', line 78

def with_additional_read_timeout(additional_timeout, &block)
  old_read_timeout = socket.read_timeout
  socket.read_timeout += additional_timeout
  block.call
ensure
  @read_timeout = old_read_timeout
end

#write(msg) ⇒ Object

Internal: write the given item to the socket

msg - the message to write

Returns nothing



127
128
129
130
131
132
133
# File 'lib/kjess/connection.rb', line 127

def write( msg )
  $stderr.puts "--> #{msg}" if $DEBUG
  socket.write( msg )
rescue KJess::Error
  close
  raise
end

#write_timeoutObject

Public: The timeout for writing in seconds. Defaults to 2



33
34
35
# File 'lib/kjess/connection.rb', line 33

def write_timeout
  socket.write_timeout
end