Class: Orchestrator::UdpService
- Inherits:
-
UV::DatagramConnection
- Object
- UV::DatagramConnection
- Orchestrator::UdpService
- Defined in:
- lib/orchestrator/datagram_server.rb
Instance Method Summary collapse
- #attach(ip, port, callback) ⇒ Object
- #detach(ip_raw, port) ⇒ Object
-
#initialize(*args) ⇒ UdpService
constructor
A new instance of UdpService.
- #on_read(data, ip, port, transport) ⇒ Object
- #send(ip, port, data) ⇒ Object
Constructor Details
#initialize(*args) ⇒ UdpService
Returns a new instance of UdpService.
4 5 6 7 8 9 10 |
# File 'lib/orchestrator/datagram_server.rb', line 4 def initialize(*args) super(*args) @callbacks = { # ip => port => callback } end |
Instance Method Details
#attach(ip, port, callback) ⇒ Object
12 13 14 15 16 17 |
# File 'lib/orchestrator/datagram_server.rb', line 12 def attach(ip, port, callback) @loop.schedule do ports = @callbacks[ip.to_sym] ||= {} ports[port.to_i] = callback end end |
#detach(ip_raw, port) ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/orchestrator/datagram_server.rb', line 19 def detach(ip_raw, port) @loop.schedule do ip = ip_raw.to_sym ip_ports = @callbacks[ip] if ip_ports ip_ports.delete(port.to_i) @callbacks.delete(ip) if ip_ports.empty? end end end |
#on_read(data, ip, port, transport) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/orchestrator/datagram_server.rb', line 30 def on_read(data, ip, port, transport) ip_ports = @callbacks[ip.to_sym] if ip_ports callback = ip_ports[port.to_i] if callback callback.call(data) end end end |
#send(ip, port, data) ⇒ Object
40 41 42 43 44 |
# File 'lib/orchestrator/datagram_server.rb', line 40 def send(ip, port, data) @loop.schedule do send_datagram(data, ip, port) end end |