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 fun to_s
: String
22 var _name
: String = "noname"
27 fun wake_up
is abstract
29 fun wake_up_in
(d
: Int)
30 # Ask to be weaked up in `d' time units
32 _scheduler
.add_event
(self, d
)
34 var _scheduler
: Scheduler
39 var _nexts
: nullable ArraySet[NodeSink] = null
40 fun attach
(n
: NodeSink)
41 # Add the sink `n' the the connected nodes
42 # Do nothing if `n' is already connected
44 # Create the collection if needed
45 if _nexts
== null then
46 _nexts
= new ArraySet[NodeSink]
51 fun detach
(n
: NodeSink)
52 # Remove the sink `n' from the connected nodes
53 # Do nothing if `n' is not connected
59 # Notify the sinks by calling the recieve(1) method of each sink
61 if _nexts
== null then
64 for n
in _nexts
.as(not null) do
72 fun recieve
(n
: NodeSource) is abstract
73 # the `n' has emeted a signal
79 var _time_list
: Array[Couple[Int, WakeUpNode]] = new Array[Couple[Int, WakeUpNode]]
80 var _time
: Int = 0 # What time is it ?
81 fun add_event
(n
: WakeUpNode, d
: Int)
82 # The node `n' whant to be weaked up in `d' time units
84 var entry
= new Couple[Int, WakeUpNode](d
+_time
, n
)
88 fun next_event
: nullable WakeUpNode
91 if _time_list
.is_empty
then
96 var entry
= _time_list
.first
97 for e
in _time_list
do
98 if e
.first
< entry
.first
then
102 _time_list
.remove
(entry
)
108 fun run_for
(time_limit
: Int)
111 var node
= next_event
112 if _time
> time_limit
then
117 print
("No more event.")
145 init(name
: String, s
: Scheduler, delay
: Int)
156 redef fun recieve
(n
: NodeSource)
162 class SimpleCountSink
174 var _last
: nullable NodeSource
175 redef fun recieve
(n
: NodeSource)
193 redef fun recieve
(n
: NodeSource)
204 init(name
: String, limit
: Int)
216 redef fun recieve
(n
: NodeSource)
225 init(name
: String, scheduler
: Scheduler, delay
: Int)
228 _scheduler
= scheduler
235 var s
= new Scheduler
237 var b1
= new BeepSource("Beep 1", s
, 10)
238 var b2
= new BeepSource("Beep 2", s
, 7)
239 var a1
= new NodeAlternate("Alternate 1")
240 var a2
= new NodeAlternate("Alternate 2")
241 var a3
= new NodeAlternate("Alternate 3")
242 var d1
= new NodeDelay("Delay 1", s
, 2)
243 var e1
= new NodeEat("Eat 1", 5)
244 var c1
= new SimpleCountSink("Count 1")
271 if not args
.is_empty
then