Class: Wizrb::Shared::Connection
- Inherits:
-
Object
- Object
- Wizrb::Shared::Connection
- Defined in:
- lib/wizrb/shared/connection.rb
Instance Attribute Summary collapse
-
#connection_id ⇒ Object
readonly
Returns the value of attribute connection_id.
-
#ip ⇒ Object
readonly
Returns the value of attribute ip.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
Instance Method Summary collapse
- #connect ⇒ Object
-
#initialize(ip, port) ⇒ Connection
constructor
A new instance of Connection.
- #receive(timeout: 2, max: 1024) ⇒ Object
- #send(data) ⇒ Object
- #test ⇒ Object
Constructor Details
#initialize(ip, port) ⇒ Connection
Returns a new instance of Connection.
11 12 13 14 15 16 17 |
# File 'lib/wizrb/shared/connection.rb', line 11 def initialize(ip, port) @ip = ip @port = port @connection_id = Time.now.to_i.to_s(16) @socket = UDPSocket.new log("Created") end |
Instance Attribute Details
#connection_id ⇒ Object (readonly)
Returns the value of attribute connection_id.
9 10 11 |
# File 'lib/wizrb/shared/connection.rb', line 9 def connection_id @connection_id end |
#ip ⇒ Object (readonly)
Returns the value of attribute ip.
9 10 11 |
# File 'lib/wizrb/shared/connection.rb', line 9 def ip @ip end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
9 10 11 |
# File 'lib/wizrb/shared/connection.rb', line 9 def port @port end |
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
9 10 11 |
# File 'lib/wizrb/shared/connection.rb', line 9 def socket @socket end |
Instance Method Details
#connect ⇒ Object
19 20 21 22 23 24 |
# File 'lib/wizrb/shared/connection.rb', line 19 def connect with_error_logging do socket.connect(ip, port) log("Connected") end end |
#receive(timeout: 2, max: 1024) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/wizrb/shared/connection.rb', line 34 def receive(timeout: 2, max: 1024) with_error_logging do connect ready = socket.wait_readable(timeout) raise Wizrb::ConnectionTimeoutError unless ready data, _addr = socket.recvfrom(max) log("Received: #{data}") parse_response(data) end end |
#send(data) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/wizrb/shared/connection.rb', line 26 def send(data) with_error_logging do connect log("Sending: #{data.to_json}") socket.send(data.to_json.encode("UTF-8"), 0) end end |
#test ⇒ Object
47 48 49 50 51 52 |
# File 'lib/wizrb/shared/connection.rb', line 47 def test with_error_logging do send({method: "getPilot", params: {}}) receive end end |