Class: RobotLab::Discovery::Advertiser

Inherits:
Object
  • Object
show all
Defined in:
lib/robot_lab/discovery/advertiser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, port:, path:, hostname: Socket.gethostname, capabilities: []) ⇒ Advertiser

Returns a new instance of Advertiser.



11
12
13
14
15
16
17
18
19
# File 'lib/robot_lab/discovery/advertiser.rb', line 11

def initialize(name:, port:, path:, hostname: Socket.gethostname, capabilities: [])
  @name         = name
  @port         = port
  @path         = path
  @hostname     = hostname
  @capabilities = Array(capabilities).map(&:to_s)
  @service      = nil
  @thread       = nil
end

Instance Attribute Details

#capabilitiesObject (readonly)

Returns the value of attribute capabilities.



9
10
11
# File 'lib/robot_lab/discovery/advertiser.rb', line 9

def capabilities
  @capabilities
end

#hostnameObject (readonly)

Returns the value of attribute hostname.



9
10
11
# File 'lib/robot_lab/discovery/advertiser.rb', line 9

def hostname
  @hostname
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/robot_lab/discovery/advertiser.rb', line 9

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/robot_lab/discovery/advertiser.rb', line 9

def path
  @path
end

#portObject (readonly)

Returns the value of attribute port.



9
10
11
# File 'lib/robot_lab/discovery/advertiser.rb', line 9

def port
  @port
end

Instance Method Details

#startObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/robot_lab/discovery/advertiser.rb', line 21

def start
  @service = ZeroConf::Service.new(
    SERVICE_TYPE,
    @port,
    @hostname,
    instance_name: @name,
    text:     TxtRecord.encode(path: @path, capabilities: @capabilities),
    subtypes: @capabilities.map { |c| "_#{RobotLab::Discovery.dns_label(c)}" }
  )
  @thread = Thread.new { @service.start }
  self
end

#started?Boolean

Returns:

  • (Boolean)


42
# File 'lib/robot_lab/discovery/advertiser.rb', line 42

def started? = @thread&.alive? || false

#stopObject



34
35
36
37
38
39
40
# File 'lib/robot_lab/discovery/advertiser.rb', line 34

def stop
  @service&.stop
  @thread&.join
  @service = nil
  @thread  = nil
  self
end