Class: Communicator
- Inherits:
-
Object
- Object
- Communicator
- Defined in:
- lib/ruby-fcp/communicator.rb
Instance Attribute Summary collapse
-
#ConnectionIdentifier ⇒ Object
readonly
Returns the value of attribute ConnectionIdentifier.
-
#heartbeat ⇒ Object
Returns the value of attribute heartbeat.
-
#responses ⇒ Object
Returns the value of attribute responses.
Instance Method Summary collapse
- #close ⇒ Object
- #connect ⇒ Object
- #grab_response ⇒ Object
-
#initialize(client, host, port, version = 2.0) ⇒ Communicator
constructor
A new instance of Communicator.
- #keep_alive ⇒ Object
- #send_packet(message) ⇒ Object
- #sock_thrd ⇒ Object
- #sort_out(packet) ⇒ Object
Constructor Details
#initialize(client, host, port, version = 2.0) ⇒ Communicator
Returns a new instance of Communicator.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/ruby-fcp/communicator.rb', line 9 def initialize(client,host, port, version = 2.0) @utils = Utils.new @version = version @ConnectionIdentifier = "" @host = host @port = port @client = client @responses = { peers: [],dda: [], default: [], error: [], datalengths: {} } @tex = Mutex.new @state = false @queue = Queue.new @heartbeat = 300 connect end |
Instance Attribute Details
#ConnectionIdentifier ⇒ Object (readonly)
Returns the value of attribute ConnectionIdentifier.
6 7 8 |
# File 'lib/ruby-fcp/communicator.rb', line 6 def ConnectionIdentifier @ConnectionIdentifier end |
#heartbeat ⇒ Object
Returns the value of attribute heartbeat.
7 8 9 |
# File 'lib/ruby-fcp/communicator.rb', line 7 def heartbeat @heartbeat end |
#responses ⇒ Object
Returns the value of attribute responses.
7 8 9 |
# File 'lib/ruby-fcp/communicator.rb', line 7 def responses @responses end |
Instance Method Details
#close ⇒ Object
128 129 130 131 |
# File 'lib/ruby-fcp/communicator.rb', line 128 def close @tex.synchronize{@sock.write "Disconnect EndMessage\n"} @sock.close end |
#connect ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/ruby-fcp/communicator.rb', line 24 def connect @sock = TCPSocket.new @host ,@port @sock.write @utils.packet_mangler({"Name" => @client,"ExpectedVersion" => @version},"ClientHello") response = grab_response unless response[:state] == -1 @sock_thrd = Thread.new {sock_thrd} @ConnectionIdentifier = response["ConnectionIdentifier"] @state = true keep_alive end end |
#grab_response ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/ruby-fcp/communicator.rb', line 104 def grab_response response = { state: 1 } line = @sock.readline response[:state] = -1 if line =~ /ClosedConnectionDuplicateClientName|ProtocolError/ response[:head] = line.chomp until line =~ /EndMessage|^Data$/ response[line.split('=')[0]] = line.split('=')[1].chomp if line.split('=').size == 2 line = @sock.readline end @responses[:datalengths][response["Identifier"]] = response["DataLength"].to_i if response.has_key? "DataLength" response[:data] = @sock.read @responses[:datalengths][response["Identifier"]] if response[:head] =~ /AllData/ response end |
#keep_alive ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/ruby-fcp/communicator.rb', line 118 def keep_alive Thread.start do loop do send_packet "Void\nEndMessage\n" sleep @heartbeat break if @state == false end end end |
#send_packet(message) ⇒ Object
100 101 102 |
# File 'lib/ruby-fcp/communicator.rb', line 100 def send_packet() @queue.push end |
#sock_thrd ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/ruby-fcp/communicator.rb', line 36 def sock_thrd @threads = [] loop do @threads.each do |thrd| begin @threads.delete thrd if thrd.join(0.5) rescue RequestFinished => req @responses[:error].push req rescue Exception => excpt puts "#{excpt}" @threads.delete thrd end if thrd.status == false @threads.delete thrd elsif thrd.status == nil @threads.delete thrd end end @sock.close Thread.exit if @state == false begin while packet = @queue.pop(true) @tex.synchronize{@sock.write packet} #sort_out(packet) end rescue ThreadError => err end begin if select([@sock], nil,nil,2) packet = @tex.synchronize{grab_response} sort_out(packet) end rescue @sock.close Thread.exit state = false connect end end end |
#sort_out(packet) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/ruby-fcp/communicator.rb', line 78 def sort_out(packet) if packet[:head].include? "NodeHello" @ConnectionIdentifier = packet["ConnectionIdentifier"] elsif packet[:head].include? "CloseConnectionDuplicateClientName" @state = false elsif packet.has_key? "Identifier" if @responses.has_key? packet["Identifier"] @responses[packet["Identifier"]].push packet else @responses[packet["Identifier"]] = [packet] end elsif packet[:head].include? "DDA" @responses[:dda].push packet elsif packet[:state] == -1 @responses[:error].push packet elsif packet[:head].include? "Peer" @responses[:peers].push packet else packet @responses[:default].push packet end end |