-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbehaviour.go
164 lines (143 loc) · 3.19 KB
/
behaviour.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
package main
/*
import (
"fmt"
"io"
"math/rand"
"time"
imp "github.com/jbenet/go-ipfs/importer"
mdag "github.com/jbenet/go-ipfs/merkledag"
util "github.com/jbenet/go-ipfs/util"
)
type milestone struct {
interest float64
point time.Duration
}
type interestCurve struct {
milestones []milestone
}
func (ic *interestCurve) getCurrentInterest(offset time.Duration) float64 {
previ := float64(0)
for _, m := range ic.milestones {
if offset < m.point {
delta := float64(offset) / float64(m.point)
incr := m.interest - previ
return (previ + (incr * delta)) / 100
}
previ = m.interest
offset -= m.point
}
return previ
}
type Behaviour struct {
Type int
// Multiplier for interest curve
Magnitude float64
// ....
Lifespan time.Duration
// interest curve, describing interest in this object over time
curve *interestCurve
// description of the content
Key util.Key
ValSize int64
// Attributes about the distribution of values in the data
// affects compressability of the data
DataType int
// Node who first adds this content to the network
Initiator int
involvedNodes map[int]struct{}
unaddedNodes map[int]struct{}
}
func (b *Behaviour) Start() {
for i, _ := range nodes {
if i != b.Initiator {
b.unaddedNodes[i] = struct{}{}
}
}
b.involvedNodes[b.Initiator] = struct{}{}
nd := b.genData()
err := nodes[b.Initiator].DAG.AddRecursive(nd)
if err != nil {
panic(err)
}
tick := time.NewTicker(time.Millisecond * 50)
begin := time.Now()
for _ = range tick.C {
inter := b.curve.getCurrentInterest(time.Now().Sub(begin))
req := int(inter * float64(len(nodes)))
if req > len(b.involvedNodes) {
b.AddNode()
} else if req < len(b.involvedNodes) {
fmt.Printf("invnodes = %d, req = %d\n", len(b.involvedNodes), req)
if len(b.involvedNodes) > 1 {
b.RemoveNode()
}
}
if time.Now().After(begin.Add(b.Lifespan)) {
fmt.Println("Behaviour over!")
return
}
}
}
func (b *Behaviour) RemoveNode() {
fmt.Println("Removing node from behaviour!")
i := rand.Intn(len(b.involvedNodes))
var which int
for k, _ := range b.unaddedNodes {
if i == 0 {
which = k
break
}
i--
}
delete(b.involvedNodes, which)
b.unaddedNodes[which] = struct{}{}
go func(i int) {
nd, err := nodes[i].DAG.Get(b.Key)
if err != nil {
fmt.Printf("Remove Node Error: %s\n", err)
return
}
err = nodes[i].DAG.Remove(nd)
if err != nil {
fmt.Printf("Remove Node Error: %s\n", err)
return
}
}(which)
}
func (b *Behaviour) AddNode() {
fmt.Println("Adding node to behaviour!")
if len(b.unaddedNodes) == 0 {
fmt.Println("Tried to add more nodes than exist...")
return
}
i := rand.Intn(len(b.unaddedNodes))
var which int
for k, _ := range b.unaddedNodes {
if i == 0 {
which = k
break
}
i--
}
delete(b.unaddedNodes, which)
b.involvedNodes[which] = struct{}{}
go func(i int) {
nd, err := nodes[i].DAG.Get(b.Key)
if err != nil {
fmt.Printf("DAG GET ERROR: %s\n", err)
return
}
_ = nd
fmt.Printf("Got node!")
}(which)
}
func (b *Behaviour) genData() *mdag.Node {
read := io.LimitReader(util.NewTimeSeededRand(), b.ValSize)
nd, err := imp.NewDagFromReader(read)
if err != nil {
panic(err)
}
return nd
}
*/