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



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

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



24
25
26
# File 'lib/specjour/connection.rb', line 24

def connect
  timeout { connect_socket }
end

#disconnectObject



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

def disconnect
  socket.close
end

#next_testObject



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

def next_test
  will_reconnect do
    send_message(:ready)
    load_object socket.gets(TERMINATOR)
  end
end


49
50
51
52
53
# File 'lib/specjour/connection.rb', line 49

def print(arg)
  will_reconnect do
    socket.print dump_object(arg)
  end
end

#puts(arg) ⇒ Object



55
56
57
# File 'lib/specjour/connection.rb', line 55

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

#send_message(method_name, *args) ⇒ Object



59
60
61
62
# File 'lib/specjour/connection.rb', line 59

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

#timeout(&block) ⇒ Object



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

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