-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathruntime.go
59 lines (51 loc) · 1019 Bytes
/
runtime.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package gosm
import "sync"
import "time"
type Runtime struct {
components []*Component
wg *sync.WaitGroup
}
func (rt *Runtime) startComponent(c *Component) {
//FIXME: This should probably be done in a thread-safe manner
rt.components = append(rt.components, c)
c.Runtime = rt
rt.wg.Add(1)
go c.loop()
}
func (rt *Runtime) Activity() bool {
activity := false
for _, c := range rt.components {
activity = activity || c.Active()
}
return activity
}
func (rt *Runtime) WaitUntilInactive() {
for {
time.Sleep(receiveTimeout)
if !rt.Activity() {
break
}
}
}
func (rt *Runtime) StopWhenInactive() {
go func() {
rt.WaitUntilInactive()
for _, c := range rt.components {
c.Stop()
}
}()
}
func LaunchComponents(cs ...*Component) *Runtime {
rt := &Runtime{
components: make([]*Component, 0),
wg: &sync.WaitGroup{},
}
for _, c := range cs {
rt.startComponent(c)
}
return rt
}
func RunComponents(cs ...*Component) {
rt := LaunchComponents(cs...)
rt.wg.Wait()
}