Class: Orocos::Avahi::NameService

Inherits:
NameServiceBase show all
Defined in:
lib/orocos/name_service.rb

Overview

Name service to access Orocos Tasks which are publishing their IOR via Avahi

Instance Attribute Summary

Attributes inherited from NameServiceBase

#name

Instance Method Summary collapse

Methods inherited from NameServiceBase

#each_task, #find_one_running, #get_provides, #reachable?, #task_reachable?, #validate

Constructor Details

#initialize(searchdomain) ⇒ NameService

A new instance of NameService which is listening on the given search domain. The serach domain must have a ._tcp or ._udp at the end for a protocol type.

Parameters:

  • searchdomain (String)

    The search domain the name service is listening to (_myrobot._tcp / _myrobot._udp)

Raises:

  • (ArgumentError)


684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
# File 'lib/orocos/name_service.rb', line 684

def initialize(searchdomain)

    raise ArgumentError,"no serachdomain is given" unless searchdomain

    @registered_tasks = Hash.new
    @searchdomain = searchdomain

    begin
        require 'servicediscovery'
        @avahi_nameserver = ::Avahi::ServiceDiscovery.new
    rescue LoadError
        raise LoadError, "NameService: 'distributed_nameserver' needs to be installed for Avahi nameservice support"
    end
    # Start listening on the given domains (this does refer to the _myservice._tcp service domain and not(!) the .local domain)
    begin
        @avahi_nameserver.listen_on(Array(@searchdomain))
    rescue RuntimeError
        raise ArgumentError, "given search domain #{searchdomain} is invalid. Use '_myservice._tcp'."
    end
end

Instance Method Details

#deregister(task) ⇒ Object

Note:

This only works for tasks which were registered by the same ruby instance.

Deregisters the given name or task from the name service.

Parameters:



735
736
737
738
739
740
741
742
# File 'lib/orocos/name_service.rb', line 735

def deregister(task)
    name = if task.respond_to? :name
               task.name
           else
               task
           end
    @registered_tasks.delete name
end

#get(name, options = Hash.new) ⇒ Orocos::TaskContext, Orocos::Log::TaskContext

Gets an handle to a local/remote Orocos Task having the given name.

Parameters:

  • name (String)

    the name of the TaskContext

  • options (Hash) (defaults to: Hash.new)

    the options used by the name service to find the TaskContext

Options Hash (options):

  • :name (String)

    Overwrites The real name of the task

  • :process (Orocos::Process)

    The process supporting the task

Returns:

Raises:

See Also:



760
761
762
763
764
765
766
767
# File 'lib/orocos/name_service.rb', line 760

def get(name,options = Hash.new)
    options = Kernel.validate_options options,:name,:namespace,:process
    ns,_ = Namespace.split_name(name)
    options[:namespace] ||= ns
    Orocos::TaskContext.new(ior(name),options)
rescue Orocos::CORBA::ComError => e
    raise Orocos::NotFound, "task context #{name} is registered but cannot be reached."
end

#ior(name) ⇒ String

Gets the IOR for the given Orocos Task having the given name.

Parameters:

  • name (String)

    the name of the TaskContext

Returns:

  • (String)

Raises:



745
746
747
748
749
750
751
752
753
754
755
756
757
# File 'lib/orocos/name_service.rb', line 745

def ior(name)
    services = @avahi_nameserver.find_services(name)
    if services.empty?
        raise Orocos::NotFound, "Avahi nameservice could not find a task named '#{name}'"
    elsif services.size > 1
        warn "Avahi: '#{name}' found multiple times. Possibly due to publishing on IPv4 and IPv6, or on multiple interfaces -- picking first one in list"
    end
    ior = services.first.get_description("IOR")
    if !ior || ior.empty?
        raise Orocos::NotFound, "Avahi nameservice could not retrieve an ior for task #{name}"
    end
    ior
end

#namesArray<String>

Returns all Orocos Task names known by the name service inclusive the namespace of the NameService instance.

Returns:

  • (Array<String>)


706
707
708
# File 'lib/orocos/name_service.rb', line 706

def names
    @avahi_nameserver.get_all_services.uniq
end

#register(task, name = task.name) ⇒ Object

Registers the IOR of the given TaskContext on the Avahi name service

Parameters:

  • task (Orocos::TaskContext)

    The task.

  • name (String) (defaults to: task.name)

    The name which is used to register the task.



718
719
720
721
722
723
724
725
726
727
728
729
# File 'lib/orocos/name_service.rb', line 718

def register(task,name=task.name)
    existing_service = @registered_tasks[name]
    service = existing_service || ::Avahi::ServiceDiscovery.new
    service.set_description("IOR",task.ior)
    if existing_service
        service.update
    else
        @registered_tasks[name] = service
        service.publish(name, @searchdomain)
    end
    service
end

#same_namespace?(name) ⇒ Boolean

Returns:

  • (Boolean)


710
711
712
# File 'lib/orocos/name_service.rb', line 710

def same_namespace?(name)
    true
end