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
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
61 # Notify the sinks by calling the recieve(1) method of each sink
63 if _nexts
== null then
66 for n
in _nexts
.as(not null) do
74 fun recieve
(n
: NodeSource) is abstract
75 # the `n' has emeted a signal
81 var _time_list
: Array[Couple[Int, WakeUpNode]] = new Array[Couple[Int, WakeUpNode]]
82 var _time
: Int = 0 # What time is it ?
83 fun 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 fun next_event
: nullable 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 fun run_for
(time_limit
: Int)
113 var node
= next_event
114 if _time
> time_limit
then
119 print
("No more event.")
147 init(name
: String, s
: Scheduler, delay
: Int)
157 readable var _count
: Int = 0
158 redef fun recieve
(n
: NodeSource)
164 class SimpleCountSink
176 var _last
: nullable NodeSource
177 redef fun recieve
(n
: NodeSource)
195 redef fun recieve
(n
: NodeSource)
206 init(name
: String, limit
: Int)
218 redef fun recieve
(n
: NodeSource)
227 init(name
: String, scheduler
: Scheduler, delay
: Int)
230 _scheduler
= scheduler
237 var s
= new Scheduler
239 var b1
= new BeepSource("Beep 1", s
, 10)
240 var b2
= new BeepSource("Beep 2", s
, 7)
241 var a1
= new NodeAlternate("Alternate 1")
242 var a2
= new NodeAlternate("Alternate 2")
243 var a3
= new NodeAlternate("Alternate 3")
244 var d1
= new NodeDelay("Delay 1", s
, 2)
245 var e1
= new NodeEat("Eat 1", 5)
246 var c1
= new SimpleCountSink("Count 1")
273 if not args
.is_empty
then