Class: PatronusFati::Connection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, port) ⇒ Connection

Returns a new instance of Connection.



5
6
7
8
9
10
11
# File 'lib/patronus_fati/connection.rb', line 5

def initialize(server, port)
  @server = server
  @port = port

  self.read_queue = Queue.new
  self.write_queue = Queue.new
end

Instance Attribute Details

#portvoid (readonly)

Returns the value of attribute port.



3
4
5
# File 'lib/patronus_fati/connection.rb', line 3

def port
  @port
end

#read_queuevoid

Returns the value of attribute read_queue.



3
4
5
# File 'lib/patronus_fati/connection.rb', line 3

def read_queue
  @read_queue
end

#read_threadvoid (protected)

Returns the value of attribute read_thread.



46
47
48
# File 'lib/patronus_fati/connection.rb', line 46

def read_thread
  @read_thread
end

#servervoid (readonly)

Returns the value of attribute server.



3
4
5
# File 'lib/patronus_fati/connection.rb', line 3

def server
  @server
end

#socketvoid (protected)

Returns the value of attribute socket.



46
47
48
# File 'lib/patronus_fati/connection.rb', line 46

def socket
  @socket
end

#write_queuevoid

Returns the value of attribute write_queue.



3
4
5
# File 'lib/patronus_fati/connection.rb', line 3

def write_queue
  @write_queue
end

#write_threadvoid (protected)

Returns the value of attribute write_thread.



46
47
48
# File 'lib/patronus_fati/connection.rb', line 46

def write_thread
  @write_thread
end

Instance Method Details

#connectvoid



13
14
15
16
17
18
19
# File 'lib/patronus_fati/connection.rb', line 13

def connect
  establish_connection
  return unless connected?

  start_read_thread
  start_write_thread
end

#connected?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/patronus_fati/connection.rb', line 21

def connected?
  !(socket.nil? || socket.closed?)
end

#disconnectvoid



25
26
27
28
29
30
31
32
33
34
# File 'lib/patronus_fati/connection.rb', line 25

def disconnect
  return unless socket

  Thread.kill(read_thread)
  Thread.kill(write_thread)

  socket.close unless socket.closed?

  self.socket = nil
end

#establish_connectionvoid (protected)



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

def establish_connection
  return if connected?
  @socket = TCPSocket.new(server, port)
end

#readvoid



36
37
38
# File 'lib/patronus_fati/connection.rb', line 36

def read
  read_queue.pop
end

#start_read_threadvoid (protected)



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/patronus_fati/connection.rb', line 54

def start_read_thread
  self.read_thread = Thread.new do
    begin
      while (line = socket.readline)
        read_queue << line
      end
    rescue IOError, EOFError => e
      raise DisconnectError
    rescue => e
      PatronusFati.logger.error(format('Error in read thread: %s %s', e.class.to_s, e.message))
      e.backtrace.each do |l|
        PatronusFati.logger.error(l)
      end
    ensure
      socket.close if socket && !socket.closed?
    end
  end
end

#start_write_threadvoid (protected)



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/patronus_fati/connection.rb', line 73

def start_write_thread
  self.write_thread = Thread.new do
    begin
      count = 0
      while (msg = write_queue.pop)
        socket.write("!%i %s\r\n" % [count, msg])
        count += 1
      end
    rescue => e
      PatronusFati.logger.error(format('Error in write thread: %s %s', e.class.to_s, e.message))
      e.backtrace.each do |l|
        PatronusFati.logger.error(l)
      end
    ensure
      socket.close if socket && !socket.closed?
    end
  end
end

#write(msg) ⇒ void



40
41
42
# File 'lib/patronus_fati/connection.rb', line 40

def write(msg)
  write_queue.push(msg)
end