mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2024-11-27 12:05:23 +00:00
test dial/listen in the sim
This commit is contained in:
parent
5db93be4df
commit
72afa05029
2
build
2
build
@ -45,7 +45,7 @@ elif [ $ANDROID ]; then
|
||||
github.com/yggdrasil-network/yggdrasil-extras/src/mobile \
|
||||
github.com/yggdrasil-network/yggdrasil-extras/src/dummy
|
||||
else
|
||||
for CMD in yggdrasil yggdrasilctl yggdrasilsim; do
|
||||
for CMD in yggdrasil yggdrasilctl; do
|
||||
echo "Building: $CMD"
|
||||
go build $ARGS -ldflags="$LDFLAGS" -gcflags="$GCFLAGS" ./cmd/$CMD
|
||||
|
||||
|
60
cmd/yggdrasilsim/dial.go
Normal file
60
cmd/yggdrasilsim/dial.go
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
@ -9,7 +9,5 @@ import (
|
||||
|
||||
func main() {
|
||||
store := makeStoreSquareGrid(4)
|
||||
var block chan struct{}
|
||||
<-block
|
||||
panic(store)
|
||||
dialStore(store)
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ package yggdrasil
|
||||
import (
|
||||
"errors"
|
||||
"github.com/Arceliar/phony"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/util"
|
||||
)
|
||||
|
||||
type Simlink struct {
|
||||
@ -44,7 +43,7 @@ func (s *Simlink) writeMsgs(msgs [][]byte) (int, error) {
|
||||
var size int
|
||||
for _, msg := range msgs {
|
||||
size += len(msg)
|
||||
bs := append(util.GetBytes(), msg...)
|
||||
bs := append([]byte(nil), msg...)
|
||||
phony.Block(s, func() {
|
||||
s.dest.Act(s, func() {
|
||||
defer func() { recover() }()
|
||||
|
Loading…
Reference in New Issue
Block a user