-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspace.go
170 lines (133 loc) · 2.97 KB
/
space.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package tuplespace
import (
"code.google.com/p/go-uuid/uuid"
"sync"
)
type TupleSpace interface {
Write(tuple Tuple)
Read(tuple Tuple) chan Tuple
Take(tuple Tuple) chan Tuple
Watch(tuple Tuple, receiver chan Tuple) uuid.UUID
Cancel(id uuid.UUID) bool
Len() int
}
type tupleManager struct {
tuples []Tuple
mutex *sync.RWMutex
}
func (tM *tupleManager) Read(tuple Tuple, receiver chan Tuple) {
tM.mutex.RLock()
defer tM.mutex.RUnlock()
for i := len(tM.tuples) - 1; 0 <= i; i-- {
t := tM.tuples[i]
if t.Match(tuple) && !t.IsExpired() {
receiver <- t
}
}
}
func (tM *tupleManager) Take(tuple Tuple, receiver chan Tuple) {
tM.mutex.Lock()
defer tM.mutex.Unlock()
for i := len(tM.tuples) - 1; 0 <= i; i-- {
t := tM.tuples[i]
if t.Match(tuple) && !t.IsExpired() {
if i == 0 {
tM.tuples = make([]Tuple, 0)
} else {
tM.tuples = tM.tuples[:i]
}
receiver <- t
}
}
}
func (tM *tupleManager) Write(tuple Tuple) {
tM.mutex.Lock()
defer tM.mutex.Unlock()
tM.tuples = append(tM.tuples, tuple)
}
func (tM *tupleManager) Len() int {
tM.mutex.RLock()
defer tM.mutex.RUnlock()
return len(tM.tuples)
}
type callback struct {
Tuple Tuple
Receiver chan Tuple
}
type watchersManager struct {
watchers map[string]*callback
mutex *sync.RWMutex
}
func (wM *watchersManager) Register(tuple Tuple, receiver chan Tuple) uuid.UUID {
wM.mutex.Lock()
defer wM.mutex.Unlock()
id := uuid.NewRandom()
wM.watchers[id.String()] = &callback{
Tuple: tuple,
Receiver: receiver,
}
return id
}
func (wM *watchersManager) Cancel(id uuid.UUID) bool {
wM.mutex.Lock()
defer wM.mutex.Unlock()
key := id.String()
if _, ok := wM.watchers[key]; ok {
delete(wM.watchers, key)
return true
}
return false
}
func (wM *watchersManager) Match(t Tuple) chan Tuple {
wM.mutex.RLock()
defer wM.mutex.RUnlock()
for _, callback := range wM.watchers {
if t.Match(callback.Tuple) && !t.IsExpired() {
return callback.Receiver
}
}
return nil
}
type tupleSpace struct {
tuples *tupleManager
watchers *watchersManager
}
func NewSpace() TupleSpace {
return &tupleSpace{
tuples: &tupleManager{
tuples: make([]Tuple, 0),
mutex: new(sync.RWMutex),
},
watchers: &watchersManager{
watchers: make(map[string]*callback),
mutex: new(sync.RWMutex),
},
}
}
func (s *tupleSpace) Read(tuple Tuple) chan Tuple {
found := make(chan Tuple)
go func() { s.tuples.Read(tuple, found) }()
return found
}
func (s *tupleSpace) Take(tuple Tuple) chan Tuple {
found := make(chan Tuple)
go func() { s.tuples.Take(tuple, found) }()
return found
}
func (s *tupleSpace) Len() int {
return s.tuples.Len()
}
func (s *tupleSpace) Watch(tuple Tuple, receiver chan Tuple) uuid.UUID {
return s.watchers.Register(tuple, receiver)
}
func (s *tupleSpace) Cancel(id uuid.UUID) bool {
return s.watchers.Cancel(id)
}
func (s *tupleSpace) Write(tuple Tuple) {
recv := s.watchers.Match(tuple)
if recv != nil {
recv <- tuple
} else {
s.tuples.Write(tuple)
}
}