Class: PatronusFati::Connection
- Inherits:
-
Object
- Object
- PatronusFati::Connection
- Defined in:
- lib/patronus_fati/connection.rb
Instance Attribute Summary collapse
-
#port ⇒ void
readonly
Returns the value of attribute port.
-
#read_queue ⇒ void
Returns the value of attribute read_queue.
-
#read_thread ⇒ void
protected
Returns the value of attribute read_thread.
-
#server ⇒ void
readonly
Returns the value of attribute server.
-
#socket ⇒ void
protected
Returns the value of attribute socket.
-
#write_queue ⇒ void
Returns the value of attribute write_queue.
-
#write_thread ⇒ void
protected
Returns the value of attribute write_thread.
Instance Method Summary collapse
- #connect ⇒ void
- #connected? ⇒ Boolean
- #disconnect ⇒ void
- #establish_connection ⇒ void protected
-
#initialize(server, port) ⇒ Connection
constructor
A new instance of Connection.
- #read ⇒ void
- #start_read_thread ⇒ void protected
- #start_write_thread ⇒ void protected
- #write(msg) ⇒ void
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
#port ⇒ void (readonly)
Returns the value of attribute port.
3 4 5 |
# File 'lib/patronus_fati/connection.rb', line 3 def port @port end |
#read_queue ⇒ void
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_thread ⇒ void (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 |
#server ⇒ void (readonly)
Returns the value of attribute server.
3 4 5 |
# File 'lib/patronus_fati/connection.rb', line 3 def server @server end |
#socket ⇒ void (protected)
Returns the value of attribute socket.
46 47 48 |
# File 'lib/patronus_fati/connection.rb', line 46 def socket @socket end |
#write_queue ⇒ void
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_thread ⇒ void (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
#connect ⇒ void
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
21 22 23 |
# File 'lib/patronus_fati/connection.rb', line 21 def connected? !(socket.nil? || socket.closed?) end |
#disconnect ⇒ void
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_connection ⇒ void (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 |
#read ⇒ void
36 37 38 |
# File 'lib/patronus_fati/connection.rb', line 36 def read read_queue.pop end |
#start_read_thread ⇒ void (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.)) e.backtrace.each do |l| PatronusFati.logger.error(l) end ensure socket.close if socket && !socket.closed? end end end |
#start_write_thread ⇒ void (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.)) 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 |