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)

705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
# File 'lib/orocos/name_service.rb', line 705

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:


757
758
759
760
761
762
763
764
# File 'lib/orocos/name_service.rb', line 757

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:


780
781
782
783
784
785
786
787
# File 'lib/orocos/name_service.rb', line 780

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:


767
768
769
770
771
772
773
774
775
776
777
# File 'lib/orocos/name_service.rb', line 767

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")
    raise Orocos::NotFound, "Avahi nameservice could not retrieve an ior for task #{name}" unless ior
    ior
end

#namesArray<String>

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

Returns:

  • (Array<String>)

727
728
729
# File 'lib/orocos/name_service.rb', line 727

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.


739
740
741
742
743
744
745
746
747
748
749
750
751
# File 'lib/orocos/name_service.rb', line 739

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

#same_namespace?(name) ⇒ Boolean

Returns:

  • (Boolean)

731
732
733
# File 'lib/orocos/name_service.rb', line 731

def same_namespace?(name)
    true
end