Class: LIFX::TransportManager::LAN Private

Inherits:
Base
  • Object
show all
Defined in:
lib/lifx/transport_manager/lan.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary collapse

DISCOVERY_INTERVAL_WHEN_NO_SITES_FOUND =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

seconds

1
DISCOVERY_INTERVAL =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

seconds

15

Instance Method Summary collapse

Constructor Details

#initialize(bind_ip: '0.0.0.0', send_ip: '255.255.255.255', port: 56700, peer_port: 56750) ⇒ LAN

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of LAN.



6
7
8
9
10
11
12
13
14
15
# File 'lib/lifx/transport_manager/lan.rb', line 6

def initialize(bind_ip: '0.0.0.0', send_ip: '255.255.255.255', port: 56700, peer_port: 56750)
  super
  @bind_ip   = bind_ip
  @send_ip   = send_ip
  @port      = port
  @peer_port = peer_port
  
  @sites = {}
  initialize_transports
end

Instance Method Details

#broadcast(message) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



77
78
79
80
81
82
# File 'lib/lifx/transport_manager/lan.rb', line 77

def broadcast(message)
  if !@transport.connected?
    create_broadcast_transport
  end
  @transport.write(message)
end

#broadcast_to_peers(message) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



84
85
86
87
88
89
# File 'lib/lifx/transport_manager/lan.rb', line 84

def broadcast_to_peers(message)
  if !@peer_transport.connected?
    create_peer_transport
  end
  @peer_transport.write(message)
end

#discoverObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/lifx/transport_manager/lan.rb', line 27

def discover
  stop_discovery
  Thread.abort_on_exception = true
  @discovery_thread = Thread.new do
    @last_request_seen = Time.at(0)
    message = Message.new(path: ProtocolPath.new(tagged: true), payload: Protocol::Device::GetPanGateway.new)
    logger.info("Discovering gateways on #{@bind_ip}:#{@port}")
    loop do
      interval = @sites.empty? ?
        DISCOVERY_INTERVAL_WHEN_NO_SITES_FOUND :
        DISCOVERY_INTERVAL
      if Time.now - @last_request_seen > interval
        write(message)
      end
      sleep(interval / 2.0)
    end
  end
end

#flush(**options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



17
18
19
20
21
22
23
# File 'lib/lifx/transport_manager/lan.rb', line 17

def flush(**options)
  @sites.values.map do |site|
    Thread.new do
      site.flush(**options)
    end
  end.each(&:join)
end

#gatewaysObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



95
96
97
# File 'lib/lifx/transport_manager/lan.rb', line 95

def gateways
  @sites.values.map(&:gateways)
end

#on_network?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


73
74
75
# File 'lib/lifx/transport_manager/lan.rb', line 73

def on_network?
  Socket.getifaddrs.any? { |ifaddr| ifaddr.broadaddr }
end

#sitesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



91
92
93
# File 'lib/lifx/transport_manager/lan.rb', line 91

def sites
  @sites.dup
end

#stopObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



50
51
52
53
54
55
56
# File 'lib/lifx/transport_manager/lan.rb', line 50

def stop
  stop_discovery
  @transport.close
  @sites.values.each do |site|
    site.stop
  end
end

#stop_discoveryObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



46
47
48
# File 'lib/lifx/transport_manager/lan.rb', line 46

def stop_discovery
  Thread.kill(@discovery_thread) if @discovery_thread
end

#write(message) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/lifx/transport_manager/lan.rb', line 58

def write(message)
  return unless on_network?
  if message.path.all_sites?
    broadcast(message)
  else
    site = @sites[message.path.site_id]
    if site
      site.write(message)
    else
      broadcast(message)
    end
  end
  broadcast_to_peers(message)
end