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
21
# File 'lib/specjour/connection.rb', line 18

def initialize(uri)
  @uri = uri
  @retries = 0
end

Instance Attribute Details

#retriesObject (readonly)

Returns the value of attribute retries.



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

def retries
  @retries
end

#socketObject



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

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



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

def connect
  timeout { connect_socket }
end

#disconnectObject



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

def disconnect
  socket.close if socket && !socket.closed?
end

#next_testObject



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

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


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

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

#puts(arg = '') ⇒ Object



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

def puts(arg='')
  will_reconnect do
    print(arg << "\n")
  end
end

#send_message(method_name, *args) ⇒ Object



56
57
58
59
60
61
# File 'lib/specjour/connection.rb', line 56

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