Class: Orocos::Async::NameServiceBase

Inherits:
ObjectBase
  • Object
show all
Extended by:
ObjectBase::Periodic::ClassMethods, Utilrb::EventLoop::Forwardable
Includes:
ObjectBase::Periodic, Namespace
Defined in:
lib/orocos/async/name_service.rb

Constant Summary

Constants included from Namespace

Namespace::DELIMATOR

Instance Attribute Summary collapse

Attributes included from ObjectBase::Periodic::ClassMethods

#default_period

Attributes inherited from ObjectBase

#emitting, #event_loop, #name, #options, #pending_adds

Instance Method Summary collapse

Methods included from Namespace

#basename, #map_to_namespace, #namespace, #namespace=, #same_namespace?, #split_name, split_name, validate_namespace_name, #verify_same_namespace

Methods included from ObjectBase::Periodic

#default_period, #period, #period=

Methods inherited from ObjectBase

#add_listener, define_event, define_events, #disable_emitting, #event, event_names, #event_names, #invalidate_delegator!, #listener?, #listeners, #number_of_listeners, #on_event, #proxy_event, #reachable!, #reachable?, #remove_all_listeners, #remove_proxy_event, #unreachable!, #valid_delegator?, valid_event?, #valid_event?, validate_event, #validate_event, #wait

Constructor Details

#initialize(name_service, options = Hash.new) ⇒ NameServiceBase

Returns a new instance of NameServiceBase



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/orocos/async/name_service.rb', line 53

def initialize(name_service,options = Hash.new)
    @options ||= Kernel.validate_options options,:period => default_period,:start => false,:sync_key => nil,:known_errors => Orocos::Async::KNOWN_ERRORS,:event_loop => Orocos::Async.event_loop
    @stored_names ||= Set.new
    _,options_async = Kernel.filter_options @options,:event_loop=>nil
    super(name_service.name,@options[:event_loop])
    disable_emitting do
        reachable! name_service
    end
    @watchdog_timer = @event_loop.async_every method(:names),options_async do |names|
        names.each do |name|
            n = @stored_names.add? name
            event :task_added,name if n
        end
        @stored_names.delete_if do |name|
            if !names.include?(name)
                event :task_removed,name
                true
            else
                false
            end
        end
    end
    @watchdog_timer.doc = name
    @task_context_proxies = Array.new
end

Instance Attribute Details

#task_context_proxiesObject (readonly)

Returns the value of attribute task_context_proxies



49
50
51
# File 'lib/orocos/async/name_service.rb', line 49

def task_context_proxies
  @task_context_proxies
end

Instance Method Details

#on_task_added {|name| ... } ⇒ void

This method returns an undefined value.

Registers an event callback that will receive task names when the task got deregistered from the name service(s)

Yield Parameters:

  • name (String)


38
# File 'lib/orocos/async/name_service.rb', line 38

define_events :task_added

#proxy(name, options = Hash.new) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/orocos/async/name_service.rb', line 99

def proxy(name,options = Hash.new)
    name = if name.respond_to?(:name)
               name.name
           else
               name
           end
    options[:event_loop] ||= @event_loop
    options[:name_service] ||= self
    ns,base_name = split_name(name)
    ns ||= ""
    task = @task_context_proxies.find do |t|
        ns2,base_name2= split_name(t.name)
        ns2 ||= ""
        ns == ns2 && base_name == base_name2 && t.event_loop == options[:event_loop] && t.name_service == options[:name_service]
    end
    if task
        options.each_pair do |key,value|
            if task.options[key] != value
                # TODO add proper pretty_print methods to display options otherwise console will be flooded
                Orocos.warn "TaskContextProxy #{name} is already initialized with different options."
               # Orocos.warn "Ignoring options: #{options}."
                break
            end
        end
        task
    else
        @task_context_proxies << Orocos::Async::TaskContextProxy.new(name,options)
        @task_context_proxies.last
    end
end

#really_add_listener(listener) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/orocos/async/name_service.rb', line 79

def really_add_listener(listener)
    if listener.event == :task_added || listener.event == :task_removed 
        @watchdog_timer.start unless @watchdog_timer.running?
        if listener.use_last_value? && !@stored_names.empty?
            @stored_names.each do |name|
                listener.call name
            end
        end
    end
    super
end

#remove_listener(listener) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/orocos/async/name_service.rb', line 91

def remove_listener(listener)
    super
    if number_of_listeners(:task_removed) == 0 && number_of_listeners(:task_added) == 0
        @watchdog_timer.cancel
        @stored_names.clear
    end
end