Class: Fnord::TCPConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/fnord/tcp_connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port = 1337, options = {}) ⇒ TCPConnection

Returns a new instance of TCPConnection.



6
7
8
# File 'lib/fnord/tcp_connection.rb', line 6

def initialize(host, port = 1337, options = {})
  @host, @port, @options = host, port, options
end

Instance Attribute Details

#socketObject

Returns the value of attribute socket.



4
5
6
# File 'lib/fnord/tcp_connection.rb', line 4

def socket
  @socket
end

Instance Method Details

#connectObject



18
19
20
# File 'lib/fnord/tcp_connection.rb', line 18

def connect
  @socket = TCPSocket.new(@host, @port)
end

#connected?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/fnord/tcp_connection.rb', line 40

def connected?
  !!@socket
end

#disconnectObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fnord/tcp_connection.rb', line 28

def disconnect
  # untested
  return unless connected?

  begin
    @socket.close
  rescue
  ensure
    @socket = nil
  end
end

#reconnectObject



22
23
24
25
26
# File 'lib/fnord/tcp_connection.rb', line 22

def reconnect
  # untested
  disconnect
  connect
end

#send_data(data) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/fnord/tcp_connection.rb', line 10

def send_data(data)
  ensure_connected(:retry => true) do
    # Rails.logger.info "[FNORD] Sending data\n#{data}"
    @socket.puts(data)
    @socket.flush
  end
end