Class: Orchestrator::Device::UdpConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/orchestrator/device/transport_udp.rb

Instance Method Summary collapse

Constructor Details

#initialize(manager, processor) ⇒ UdpConnection

Returns a new instance of UdpConnection.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/orchestrator/device/transport_udp.rb', line 4

def initialize(manager, processor)
    @manager = manager
    @loop = manager.thread
    @processor = processor

    settings = manager.settings
    @ip = settings.ip
    @port = settings.port
    @on_read = method(:on_read)

    # One per loop unless port specified
    @udp_server = @loop.udp_service

    if IPAddress.valid? @ip
        @attached_ip = @ip
        @udp_server.attach(@attached_ip, @port, @on_read)
        @loop.next_tick do
            # Call connected (we only need to do this once)
            @processor.connected
        end
    else
        variation = 1 + rand(60000 * 5)  # 5min
        @checker = @manager.get_scheduler.in(60000 * 5 + variation) do
            find_ip(@ip)
        end
        find_ip(@ip)
    end
end

Instance Method Details

#disconnectObject



58
# File 'lib/orchestrator/device/transport_udp.rb', line 58

def disconnect; end

#on_read(data) ⇒ Object



38
39
40
41
42
43
# File 'lib/orchestrator/device/transport_udp.rb', line 38

def on_read(data)
    # We schedule as UDP server may be on a different thread
    @loop.schedule do
        @processor.buffer(data)
    end
end

#terminateObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/orchestrator/device/transport_udp.rb', line 45

def terminate
    #@processor.disconnected   # Disconnect should never be called
    @terminated = true
    if @searching
        @searching.cancel
        @searching = nil
    else
        @udp_server.detach(@attached_ip, @port)
    end

    @checker.cancel if @checker
end

#transmit(cmd) ⇒ Object



33
34
35
36
# File 'lib/orchestrator/device/transport_udp.rb', line 33

def transmit(cmd)
    return if @terminated
    @udp_server.send(@attached_ip, @port, cmd[:data])
end