mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2025-04-28 06:05:06 +03:00
Merge branch 'future' into develop-future
This commit is contained in:
commit
fdb296047b
28 changed files with 1049 additions and 880 deletions
61
cmd/yggdrasilsim/dial.go
Normal file
61
cmd/yggdrasilsim/dial.go
Normal file
|
@ -0,0 +1,61 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/crypto"
|
||||
)
|
||||
|
||||
func doListen(recvNode *simNode) {
|
||||
// TODO be able to stop the listeners somehow so they don't leak across different tests
|
||||
for {
|
||||
c, err := recvNode.listener.Accept()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
c.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func dialTest(sendNode, recvNode *simNode) {
|
||||
if sendNode.id == recvNode.id {
|
||||
fmt.Println("Skipping dial to self")
|
||||
return
|
||||
}
|
||||
var mask crypto.NodeID
|
||||
for idx := range mask {
|
||||
mask[idx] = 0xff
|
||||
}
|
||||
for {
|
||||
c, err := sendNode.dialer.DialByNodeIDandMask(nil, &recvNode.nodeID, &mask)
|
||||
if c != nil {
|
||||
c.Close()
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Println("Dial failed:", err)
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func dialStore(store nodeStore) {
|
||||
var nodeIdxs []int
|
||||
for idx, n := range store {
|
||||
nodeIdxs = append(nodeIdxs, idx)
|
||||
go doListen(n)
|
||||
}
|
||||
sort.Slice(nodeIdxs, func(i, j int) bool {
|
||||
return nodeIdxs[i] < nodeIdxs[j]
|
||||
})
|
||||
for _, idx := range nodeIdxs {
|
||||
sendNode := store[idx]
|
||||
for _, jdx := range nodeIdxs {
|
||||
recvNode := store[jdx]
|
||||
fmt.Printf("Dialing from node %d to node %d / %d...\n", idx, jdx, len(store))
|
||||
dialTest(sendNode, recvNode)
|
||||
}
|
||||
}
|
||||
}
|
6
cmd/yggdrasilsim/main.go
Normal file
6
cmd/yggdrasilsim/main.go
Normal file
|
@ -0,0 +1,6 @@
|
|||
package main
|
||||
|
||||
func main() {
|
||||
store := makeStoreSquareGrid(4)
|
||||
dialStore(store)
|
||||
}
|
28
cmd/yggdrasilsim/node.go
Normal file
28
cmd/yggdrasilsim/node.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/gologme/log"
|
||||
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/config"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/crypto"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/yggdrasil"
|
||||
)
|
||||
|
||||
type simNode struct {
|
||||
core yggdrasil.Core
|
||||
id int
|
||||
nodeID crypto.NodeID
|
||||
dialer *yggdrasil.Dialer
|
||||
listener *yggdrasil.Listener
|
||||
}
|
||||
|
||||
func newNode(id int) *simNode {
|
||||
n := simNode{id: id}
|
||||
n.core.Start(config.GenerateConfig(), log.New(ioutil.Discard, "", 0))
|
||||
n.nodeID = *n.core.NodeID()
|
||||
n.dialer, _ = n.core.ConnDialer()
|
||||
n.listener, _ = n.core.ConnListen()
|
||||
return &n
|
||||
}
|
41
cmd/yggdrasilsim/store.go
Normal file
41
cmd/yggdrasilsim/store.go
Normal file
|
@ -0,0 +1,41 @@
|
|||
package main
|
||||
|
||||
type nodeStore map[int]*simNode
|
||||
|
||||
func makeStoreSingle() nodeStore {
|
||||
s := make(nodeStore)
|
||||
s[0] = newNode(0)
|
||||
return s
|
||||
}
|
||||
|
||||
func linkNodes(a *simNode, b *simNode) {
|
||||
la := a.core.NewSimlink()
|
||||
lb := b.core.NewSimlink()
|
||||
la.SetDestination(lb)
|
||||
lb.SetDestination(la)
|
||||
la.Start()
|
||||
lb.Start()
|
||||
}
|
||||
|
||||
func makeStoreSquareGrid(sideLength int) nodeStore {
|
||||
store := make(nodeStore)
|
||||
nNodes := sideLength * sideLength
|
||||
idxs := make([]int, 0, nNodes)
|
||||
// TODO shuffle nodeIDs
|
||||
for idx := 1; idx <= nNodes; idx++ {
|
||||
idxs = append(idxs, idx)
|
||||
}
|
||||
for _, idx := range idxs {
|
||||
n := newNode(idx)
|
||||
store[idx] = n
|
||||
}
|
||||
for idx := 0; idx < nNodes; idx++ {
|
||||
if (idx % sideLength) != 0 {
|
||||
linkNodes(store[idxs[idx]], store[idxs[idx-1]])
|
||||
}
|
||||
if idx >= sideLength {
|
||||
linkNodes(store[idxs[idx]], store[idxs[idx-sideLength]])
|
||||
}
|
||||
}
|
||||
return store
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue