-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor.go
243 lines (232 loc) · 8.37 KB
/
processor.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package main
import (
"fmt"
"slices"
)
var leader_elected bool = false
var leader_id string = ""
type Processor struct {
ID string
channel chan Attachment
connected_edges []chan Attachment // names of connected edges
last_sent_channel chan Attachment
annexing_token Token
candidate Token
max_hop int
chased int
leader_id string
}
func CheckFinished(token_processor_id string) bool {
for _, proc := range list_processors {
if proc.annexing_token.processor_id != token_processor_id {
return false
}
}
return true
}
func GetLeader() *Processor {
if leader_elected {
for _, proc := range list_processors {
if proc.ID == leader_id {
return proc
}
}
}
return nil
}
func AnnounceLeader() {
if leader_elected {
for _, proc := range list_processors {
proc.leader_id = leader_id
}
}
}
func (self *Processor) Run() {
for {
select {
case msg := <-self.channel:
fmt.Println("Processor ", self.ID, " received token ", msg.token.processor_id, " with phase ", msg.token.phase)
if !leader_elected {
if msg.hop_counter != -1 {
// annexing mode
if self.annexing_token == msg.token && CheckFinished(msg.token.processor_id) {
// a2
fmt.Println("Leader elected: ", msg.token.processor_id)
leader_elected = true
leader_id = msg.token.processor_id
break
} else if msg.token.phase > self.annexing_token.phase ||
(msg.token == self.annexing_token && self.chased == -1 && self.candidate.processor_id == "") {
// a1
fmt.Println("Processor ", self.ID, " getting annexed by ", msg.token.processor_id, " with phase ", msg.token.phase)
self.annexing_token = msg.token
msg.hop_counter++
self.max_hop = msg.hop_counter
self.candidate = Token{
phase: -1,
processor_id: "",
}
msg.visited = append(msg.visited, self.channel)
for _, edge := range self.connected_edges {
if slices.Contains(msg.visited, edge) {
continue
}
msg.stack = append(msg.stack, edge)
}
if len(msg.stack) > 0 {
last_pos := len(msg.stack) - 1
popped_channel := msg.stack[last_pos]
msg.stack = msg.stack[:last_pos]
self.last_sent_channel = popped_channel
popped_channel <- msg
} else if len(msg.visited) == len(list_processors) && CheckFinished(self.annexing_token.processor_id) {
leader_id = msg.token.processor_id
AnnounceLeader()
leader_elected = true
fmt.Println("Leader elected: ", self.annexing_token.processor_id)
}
} else if msg.token.phase < self.annexing_token.phase {
// a3
fmt.Println("Processor", self.ID, "doing nothing")
// do nothing
} else if msg.token.phase == self.candidate.phase {
// a4
fmt.Println("Processor ", self.ID, " starting new annexing phase with phase ", msg.token.phase+1, " and processor id ", self.ID)
self.annexing_token = Token{
phase: msg.token.phase + 1,
processor_id: self.ID,
}
msg.hop_counter = 0
self.max_hop = msg.hop_counter
self.candidate = Token{
phase: -1,
processor_id: "",
}
temp_attachment := Attachment{
token: self.annexing_token,
hop_counter: self.max_hop,
last_max_hop: -1,
visited: make([]chan Attachment, 0),
stack: make([]chan Attachment, 0),
}
temp_attachment.visited = append(temp_attachment.visited, self.channel)
for _, edge := range self.connected_edges {
if slices.Contains(temp_attachment.visited, edge) {
continue
}
temp_attachment.stack = append(temp_attachment.stack, edge)
}
if len(temp_attachment.stack) > 0 {
last_pos := len(temp_attachment.stack) - 1
popped_channel := temp_attachment.stack[last_pos]
temp_attachment.stack = temp_attachment.stack[:last_pos]
self.last_sent_channel = popped_channel
popped_channel <- temp_attachment
} else if len(temp_attachment.visited) == len(list_processors) && CheckFinished(self.annexing_token.processor_id) {
leader_id = msg.token.processor_id
AnnounceLeader()
leader_elected = true
}
} else if msg.token.phase == self.annexing_token.phase && (self.chased == msg.token.phase || self.annexing_token.processor_id > msg.token.processor_id) {
// a5
fmt.Println("Processor ", self.ID, " new candidate ", msg.token.processor_id, " with phase ", msg.token.phase)
self.candidate = msg.token
} else if msg.token.phase == self.annexing_token.phase && self.annexing_token.processor_id < msg.token.processor_id && self.chased == -1 {
// a6
fmt.Println("Token ", msg.token.processor_id, " started chasing ", self.annexing_token.processor_id, " with phase ", msg.token.phase)
self.chased = msg.token.phase
self.last_sent_channel <- Attachment{
token: msg.token,
hop_counter: -1,
last_max_hop: self.max_hop,
}
}
} else if msg.last_max_hop != -1 {
// chasing mode
// c1
fmt.Println("Processor ", self.ID, " chasing ", msg.token.processor_id, " with phase ", msg.token.phase)
if msg.token.phase == self.annexing_token.phase && msg.token.processor_id == self.annexing_token.processor_id &&
msg.last_max_hop < self.max_hop &&
self.chased == -1 &&
self.candidate.phase != msg.token.phase {
self.chased = msg.token.phase
self.last_sent_channel <- Attachment{
token: msg.token,
hop_counter: -1,
last_max_hop: self.max_hop,
}
} else if msg.token.phase < self.annexing_token.phase {
// c2
fmt.Println("Processor ", self.ID, " chase stopped because ", msg.token.processor_id, " with phase ", msg.token.phase, " is in a lower phase")
// do nothing
} else if self.candidate.phase == msg.token.phase {
// c3
fmt.Println("Processor ", self.ID, " starting new annexing phase with phase ", msg.token.phase+1, " and processor id ", self.ID)
self.annexing_token = Token{
phase: msg.token.phase + 1,
processor_id: self.ID,
}
msg.hop_counter = 0
self.max_hop = msg.hop_counter
self.candidate = Token{
phase: -1,
processor_id: "",
}
temp_attachment := Attachment{
token: self.annexing_token,
hop_counter: self.max_hop,
last_max_hop: -1,
visited: make([]chan Attachment, 0),
stack: make([]chan Attachment, 0),
}
temp_attachment.visited = append(temp_attachment.visited, self.channel)
for _, edge := range self.connected_edges {
if slices.Contains(temp_attachment.visited, edge) {
continue
}
temp_attachment.stack = append(temp_attachment.stack, edge)
}
if len(temp_attachment.stack) > 0 {
last_pos := len(temp_attachment.stack) - 1
popped_channel := temp_attachment.stack[last_pos]
temp_attachment.stack = temp_attachment.stack[:last_pos]
self.last_sent_channel = popped_channel
popped_channel <- temp_attachment
} else if len(temp_attachment.visited) == len(list_processors) && CheckFinished(self.annexing_token.processor_id) {
leader_id = msg.token.processor_id
AnnounceLeader()
leader_elected = true
}
} else if (msg.token.phase == self.annexing_token.phase && msg.token.processor_id == self.annexing_token.processor_id &&
(self.chased == msg.token.phase || self.max_hop < msg.last_max_hop)) ||
((msg.token.phase != self.annexing_token.phase || msg.token.processor_id != self.annexing_token.processor_id) &&
msg.token.phase > self.annexing_token.phase) {
fmt.Println("Processor ", self.ID, " new candidate ", msg.token.processor_id, " with phase ", msg.token.phase)
self.candidate.phase = msg.token.phase
self.candidate.processor_id = msg.token.processor_id
}
}
}
}
}
}
func CreateProcessor(id string) *Processor {
temp_processor := Processor{
ID: id,
channel: make(chan Attachment),
connected_edges: make([]chan Attachment, 0),
last_sent_channel: make(chan Attachment),
annexing_token: Token{
phase: -1,
processor_id: "",
},
candidate: Token{
phase: -1,
processor_id: "",
},
max_hop: 0,
chased: -1,
leader_id: "",
}
return &temp_processor
}