1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2005-2008 Jean Privat <jean@pryen.org>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
18 redef meth to_s
: String
27 meth wake_up
is abstract
29 meth wake_up_in
(d
: Int)
30 # Ask to be weaked up in `d' time units
32 _scheduler
.add_event
(self, d
)
34 attr _scheduler
: Scheduler
39 attr _nexts
: ArraySet[NodeSink]
40 meth attach
(n
: NodeSink)
41 # Add the sink `n' the the connected nodes
42 # Do nothing if `n' is already connected
45 # Create the collection if needed
46 if _nexts
== null then
47 _nexts
= new ArraySet[NodeSink]
52 meth detach
(n
: NodeSink)
53 # Remove the sink `n' from the connected nodes
54 # Do nothing if `n' is not connected
61 # Notify the sinks by calling the recieve(1) method of each sink
63 if _nexts
== null then
74 meth recieve
(n
: NodeSource) is abstract
75 # the `n' has emeted a signal
81 attr _time_list
: Array[Couple[Int, WakeUpNode]]
82 attr _time
: Int # What time is it ?
83 meth add_event
(n
: WakeUpNode, d
: Int)
84 # The node `n' whant to be weaked up in `d' time units
86 var entry
= new Couple[Int, WakeUpNode](d
+_time
, n
)
90 meth next_event
: WakeUpNode
93 if _time_list
.is_empty
then
98 var entry
= _time_list
.first
99 for e
in _time_list
do
100 if e
.first
< entry
.first
then
104 _time_list
.remove
(entry
)
110 meth run_for
(time_limit
: Int)
113 var node
= next_event
114 if _time
> time_limit
then
119 print
("No more event.")
128 _time_list
= new Array[Couple[Int, WakeUpNode]]
148 init(name
: String, s
: Scheduler, delay
: Int)
158 readable attr _count
: Int
159 redef meth recieve
(n
: NodeSource)
165 class SimpleCountSink
177 attr _last
: NodeSource
178 redef meth recieve
(n
: NodeSource)
196 redef meth recieve
(n
: NodeSource)
207 init(name
: String, limit
: Int)
219 redef meth recieve
(n
: NodeSource)
228 init(name
: String, scheduler
: Scheduler, delay
: Int)
231 _scheduler
= scheduler
238 var s
= new Scheduler
240 var b1
= new BeepSource("Beep 1", s
, 10)
241 var b2
= new BeepSource("Beep 2", s
, 7)
242 var a1
= new NodeAlternate("Alternate 1")
243 var a2
= new NodeAlternate("Alternate 2")
244 var a3
= new NodeAlternate("Alternate 3")
245 var d1
= new NodeDelay("Delay 1", s
, 2)
246 var e1
= new NodeEat("Eat 1", 5)
247 var c1
= new SimpleCountSink("Count 1")
274 if not args
.is_empty
then