Class: Orocos::Avahi::NameService
- Inherits:
-
NameServiceBase
- Object
- NameServiceBase
- Orocos::Avahi::NameService
- 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
Instance Method Summary collapse
-
#deregister(task) ⇒ Object
Deregisters the given name or task from the name service.
-
#get(name, options = Hash.new) ⇒ Orocos::TaskContext, Orocos::Log::TaskContext
Gets an handle to a local/remote Orocos Task having the given name.
-
#initialize(searchdomain) ⇒ NameService
constructor
A new instance of NameService which is listening on the given search domain.
-
#ior(name) ⇒ String
Gets the IOR for the given Orocos Task having the given name.
-
#names ⇒ Array<String>
Returns all Orocos Task names known by the name service inclusive the namespace of the NameService instance.
-
#register(task, name = task.name) ⇒ Object
Registers the IOR of the given TaskContext on the Avahi name service.
- #same_namespace?(name) ⇒ Boolean
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.
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
This only works for tasks which were registered by the same ruby instance.
Deregisters the given name or task from the name service.
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.
760 761 762 763 764 765 766 767 |
# File 'lib/orocos/name_service.rb', line 760 def get(name, = Hash.new) = Kernel. ,:name,:namespace,:process ns,_ = Namespace.split_name(name) [:namespace] ||= ns Orocos::TaskContext.new(ior(name),) 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.
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 |
#names ⇒ Array<String>
Returns all Orocos Task names known by the name service inclusive the namespace of the NameService instance.
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
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
710 711 712 |
# File 'lib/orocos/name_service.rb', line 710 def same_namespace?(name) true end |