5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-26 04:51:38 +00:00

test dial/listen in the sim

This commit is contained in:
Arceliar 2020-05-02 10:01:09 -05:00
parent 5db93be4df
commit 72afa05029
4 changed files with 63 additions and 6 deletions

2
build
View File

@ -45,7 +45,7 @@ elif [ $ANDROID ]; then
github.com/yggdrasil-network/yggdrasil-extras/src/mobile \ github.com/yggdrasil-network/yggdrasil-extras/src/mobile \
github.com/yggdrasil-network/yggdrasil-extras/src/dummy github.com/yggdrasil-network/yggdrasil-extras/src/dummy
else else
for CMD in yggdrasil yggdrasilctl yggdrasilsim; do for CMD in yggdrasil yggdrasilctl; do
echo "Building: $CMD" echo "Building: $CMD"
go build $ARGS -ldflags="$LDFLAGS" -gcflags="$GCFLAGS" ./cmd/$CMD go build $ARGS -ldflags="$LDFLAGS" -gcflags="$GCFLAGS" ./cmd/$CMD

60
cmd/yggdrasilsim/dial.go Normal file
View File

@ -0,0 +1,60 @@
package main
import (
"fmt"
"sort"
"time"
"github.com/yggdrasil-network/yggdrasil-go/src/crypto"
)
func doListen(recvNode *simNode) {
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)
}
}
}

View File

@ -9,7 +9,5 @@ import (
func main() { func main() {
store := makeStoreSquareGrid(4) store := makeStoreSquareGrid(4)
var block chan struct{} dialStore(store)
<-block
panic(store)
} }

View File

@ -3,7 +3,6 @@ package yggdrasil
import ( import (
"errors" "errors"
"github.com/Arceliar/phony" "github.com/Arceliar/phony"
"github.com/yggdrasil-network/yggdrasil-go/src/util"
) )
type Simlink struct { type Simlink struct {
@ -44,7 +43,7 @@ func (s *Simlink) writeMsgs(msgs [][]byte) (int, error) {
var size int var size int
for _, msg := range msgs { for _, msg := range msgs {
size += len(msg) size += len(msg)
bs := append(util.GetBytes(), msg...) bs := append([]byte(nil), msg...)
phony.Block(s, func() { phony.Block(s, func() {
s.dest.Act(s, func() { s.dest.Act(s, func() {
defer func() { recover() }() defer func() { recover() }()