/
chord.go
197 lines (169 loc) · 4.35 KB
/
chord.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
package types
import (
"fmt"
"errors"
"runtime"
)
func (c *Chord_t) GetSuccessors (num uint, reply *Host_t) error {
elem := c.Successorlist.First
if num > c.Successorlist.Size || num > c.Successorlist.Maxsize {
return errors.New ("Index out of bound")
}
for i := uint(0); i < num && elem.Next != nil; i++ {
elem = elem.Next
}
if elem == nil {
return errors.New ("NIL ELEM")
}
*reply = *elem.Host
return nil
}
/**
* If returned error is nil, then the successor is the callers new successor,
* otherwise, iterate further
*/
func (c *Chord_t) FindSuccessor (callerID ID_t, successor *Host_t) error {
mysuccessor := c.Successor
if mysuccessor == nil {
panic (nil)
}
if callerID.InIntervalIncludeUpper (c.Node.ID, mysuccessor.ID) {
*successor = *c.Successor
return nil
}
return mysuccessor.SingleCall ("Chord.FindSuccessor", callerID, successor)
}
/**
* Caller is predecessor if I have no predecessor OR it is in the interval
*/
func (c *Chord_t) Notify (caller *Host_t, _ign *string) error {
c.mutex.Lock()
defer c.mutex.Unlock()
if c.Predecessor == nil || caller.ID.InInterval(c.Predecessor.ID, c.Node.ID) {
c.Predecessor = caller
println ("New predecessor:", c.Predecessor.String())
}
return nil
}
/**
* Returns my predecessor, or an error if I do not have one
*/
func (c *Chord_t) GetPredecessor (_ign string, predecessor *Host_t) error {
c.mutex.Lock()
defer c.mutex.Unlock()
if c.Predecessor == nil {
return errors.New ("I have no predecessor")
}
*predecessor = *c.Predecessor
return nil
}
func (c *Chord_t) CheckPredecessor (ign *string, _ign *string) error {
if c.Predecessor != nil && ! c.Predecessor.Alive () {
c.mutex.Lock ()
c.Predecessor = nil
c.mutex.Unlock ()
}
return nil
}
func (c *Chord_t) Join (h *Host_t, ign *string) error {
c.Predecessor = nil
var successor Host_t
err := h.SingleCall("Chord.FindSuccessor", &c.Node.ID, &successor)
if err != nil {
return err
}
c.Successor = &successor
err = successor.SingleCall("Chord.Notify", c.Node, nil)
return err
}
func (c *Chord_t) Stabilize (ign *string, change *bool) error {
c.mutex.Lock()
runtime.Gosched ()
c.mutex.Unlock()
var successorsPredecessor Host_t
var ch = false
if c.Successor == nil {
*change = false
return nil
}
conn := c.Successor.Connect ()
if conn == nil {
c.Organizer.ReportDead (c.Successor.ID)
c.mutex.Lock()
c.Successor = c.Node
*change = true
c.mutex.Unlock()
return nil
}
err := conn.Call("Chord.GetPredecessor", "", &successorsPredecessor)
if err == nil {
if successorsPredecessor.ID.InInterval(c.Node.ID, c.Successor.ID) {
c.Successor = &successorsPredecessor
ch = true
}
}
conn.Call("Chord.Notify", c.Node, nil)
conn.Close()
*change = ch
return nil
}
func (c *Chord_t) ClosestPrecedingNode (id ID_t, reply *Host_t) error {
for i := 0; i < len (c.Finger); i++ {
if c.Finger[i].ID.InInterval (c.Node.ID, id) {
*reply = *c.Finger[i]
return nil
}
}
return nil
}
func (c *Chord_t) FixFingers (n *int, ign *string) error {
next := uint(*n + 1)
if next >= uint(len (c.Finger)) {
next = 0
}
nextid := int(c.Node.ID) + int(2<<(next - 1))
var finger Host_t
err := c.FindSuccessor (ID_t(nextid), &finger)
if err != nil {
println (err.Error())
return nil
}
c.Finger[next] = &finger
fmt.Println (c.Finger)
*n = int (next)
return nil
}
func (c *Chord_t) NewPredecessor (predecessor Host_t, ign *string) error {
c.mutex.Lock ()
if !c.Alive {
c.mutex.Unlock ()
c.Successor.SingleCall ("Chord.NewPredecessor", predecessor, nil)
return nil
}
println ("New predecessor!")
c.Predecessor = &predecessor
c.mutex.Unlock()
return nil
}
func (c *Chord_t) NewSuccessor (successor Host_t, ign *string) error {
c.mutex.Lock ()
if !c.Alive {
c.mutex.Unlock ()
return c.Predecessor.SingleCall ("Chord.NewSuccessor", successor, nil)
}
println ("New successor!")
c.Successor = &successor
c.mutex.Unlock ()
c.Successor.SingleCall ("Chord.NewPredecessor", *c.Node, nil)
c.Organizer.Report (c)
return nil
}
func (c *Chord_t) Leave (ign *string, _ign *string) error {
c.Alive = false
if c.Successor.Equal (c.Node) {
return nil
}
c.Predecessor.SingleCall ("Chord.NewSuccessor", *c.Successor, nil)
return nil
}
//END Functions