Class: Specjour::Connection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Protocol
Defined in:
lib/specjour/connection.rb

Constant Summary

Constants included from Protocol

Protocol::TERMINATOR, Protocol::TERMINATOR_REGEXP

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Protocol

#dump_object, #load_object

Constructor Details

#initialize(uri) ⇒ Connection

Returns a new instance of Connection.



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

def initialize(uri)
  @uri = uri
end

Instance Attribute Details

#socketObject



26
27
28
# File 'lib/specjour/connection.rb', line 26

def socket
  @socket ||= connect
end

#uriObject (readonly)

Returns the value of attribute uri.



6
7
8
# File 'lib/specjour/connection.rb', line 6

def uri
  @uri
end

Class Method Details

.wrap(established_connection) ⇒ Object



11
12
13
14
15
16
# File 'lib/specjour/connection.rb', line 11

def self.wrap(established_connection)
  host, port = established_connection.peeraddr.values_at(3,1)
  connection = new URI::Generic.build(:host => host, :port => port)
  connection.socket = established_connection
  connection
end

Instance Method Details

#connectObject



22
23
24
# File 'lib/specjour/connection.rb', line 22

def connect
  timeout { connect_socket }
end


36
37
38
39
40
41
# File 'lib/specjour/connection.rb', line 36

def print(arg)
  socket.print dump_object(arg)
rescue SystemCallError => error
  reconnect
  retry
end

#puts(arg) ⇒ Object



43
44
45
# File 'lib/specjour/connection.rb', line 43

def puts(arg)
  print(arg << "\n")
end

#send_message(method_name, *args) ⇒ Object



47
48
49
50
# File 'lib/specjour/connection.rb', line 47

def send_message(method_name, *args)
  print([method_name, *args])
  flush
end

#timeout(&block) ⇒ Object



30
31
32
33
34
# File 'lib/specjour/connection.rb', line 30

def timeout(&block)
  Timeout.timeout(5, &block)
rescue Timeout::Error
  raise Error, "Connection to dispatcher timed out"
end