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
40 var _nexts
: nullable ArraySet[NodeSink] = null
41 fun attach
(n
: NodeSink)
42 # Add the sink `n' the the connected nodes
43 # Do nothing if `n' is already connected
45 # Create the collection if needed
46 if _nexts
== null then
47 _nexts
= new ArraySet[NodeSink]
52 fun detach
(n
: NodeSink)
53 # Remove the sink `n' from the connected nodes
54 # Do nothing if `n' is not connected
60 # Notify the sinks by calling the recieve(1) method of each sink
62 if _nexts
== null then
65 for n
in _nexts
.as(not null) do
73 fun recieve
(n
: NodeSource) is abstract
74 # the `n' has emeted a signal
80 var _time_list
: Array[Couple[Int, WakeUpNode]] = new Array[Couple[Int, WakeUpNode]]
81 var _time
: Int = 0 # What time is it ?
82 fun add_event
(n
: WakeUpNode, d
: Int)
83 # The node `n' whant to be weaked up in `d' time units
85 var entry
= new Couple[Int, WakeUpNode](d
+_time
, n
)
89 fun next_event
: nullable WakeUpNode
92 if _time_list
.is_empty
then
97 var entry
= _time_list
.first
98 for e
in _time_list
do
99 if e
.first
< entry
.first
then
103 _time_list
.remove
(entry
)
109 fun run_for
(time_limit
: Int)
112 var node
= next_event
113 if _time
> time_limit
then
118 print
("No more event.")
146 init(name
: String, s
: Scheduler, delay
: Int)
157 redef fun recieve
(n
: NodeSource)
163 class SimpleCountSink
175 var _last
: nullable NodeSource
176 redef fun recieve
(n
: NodeSource)
194 redef fun recieve
(n
: NodeSource)
205 init(name
: String, limit
: Int)
217 redef fun recieve
(n
: NodeSource)
226 init(name
: String, scheduler
: Scheduler, delay
: Int)
229 _scheduler
= scheduler
236 var s
= new Scheduler
238 var b1
= new BeepSource("Beep 1", s
, 10)
239 var b2
= new BeepSource("Beep 2", s
, 7)
240 var a1
= new NodeAlternate("Alternate 1")
241 var a2
= new NodeAlternate("Alternate 2")
242 var a3
= new NodeAlternate("Alternate 3")
243 var d1
= new NodeDelay("Delay 1", s
, 2)
244 var e1
= new NodeEat("Eat 1", 5)
245 var c1
= new SimpleCountSink("Count 1")
272 if not args
.is_empty
then