mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2024-12-24 16:57:53 +00:00
Merge branch 'switch' into session
This commit is contained in:
commit
68dce0dd74
@ -6,13 +6,15 @@ import "os"
|
|||||||
import "strings"
|
import "strings"
|
||||||
import "strconv"
|
import "strconv"
|
||||||
import "time"
|
import "time"
|
||||||
import "log"
|
|
||||||
|
|
||||||
import "runtime"
|
import "runtime"
|
||||||
import "runtime/pprof"
|
import "runtime/pprof"
|
||||||
import "flag"
|
import "flag"
|
||||||
|
|
||||||
|
import "github.com/gologme/log"
|
||||||
|
|
||||||
import . "github.com/yggdrasil-network/yggdrasil-go/src/yggdrasil"
|
import . "github.com/yggdrasil-network/yggdrasil-go/src/yggdrasil"
|
||||||
|
import . "github.com/yggdrasil-network/yggdrasil-go/src/crypto"
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
@ -570,23 +570,23 @@ func (t *switchTable) start() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if a packet should go to the self node
|
// Return a map of ports onto distance, keeping only ports closer to the destination than this node
|
||||||
// This means there's no node closer to the destination than us
|
// If the map is empty (or nil), then no peer is closer
|
||||||
// This is mainly used to identify packets addressed to us, or that hit a blackhole
|
func (t *switchTable) getCloser(dest []byte) map[switchPort]int {
|
||||||
func (t *switchTable) selfIsClosest(dest []byte) bool {
|
|
||||||
table := t.getTable()
|
table := t.getTable()
|
||||||
myDist := table.self.dist(dest)
|
myDist := table.self.dist(dest)
|
||||||
if myDist == 0 {
|
if myDist == 0 {
|
||||||
// Skip the iteration step if it's impossible to be closer
|
// Skip the iteration step if it's impossible to be closer
|
||||||
return true
|
return nil
|
||||||
}
|
}
|
||||||
|
closer := make(map[switchPort]int, len(table.elems))
|
||||||
for _, info := range table.elems {
|
for _, info := range table.elems {
|
||||||
dist := info.locator.dist(dest)
|
dist := info.locator.dist(dest)
|
||||||
if dist < myDist {
|
if dist < myDist {
|
||||||
return false
|
closer[info.port] = dist
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true
|
return closer
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if the peer is closer to the destination than ourself
|
// Returns true if the peer is closer to the destination than ourself
|
||||||
@ -640,25 +640,45 @@ func (t *switchTable) bestPortForCoords(coords []byte) switchPort {
|
|||||||
func (t *switchTable) handleIn(packet []byte, idle map[switchPort]struct{}) bool {
|
func (t *switchTable) handleIn(packet []byte, idle map[switchPort]struct{}) bool {
|
||||||
coords := switch_getPacketCoords(packet)
|
coords := switch_getPacketCoords(packet)
|
||||||
ports := t.core.peers.getPorts()
|
ports := t.core.peers.getPorts()
|
||||||
if t.selfIsClosest(coords) {
|
closer := t.getCloser(coords)
|
||||||
|
if len(closer) == 0 {
|
||||||
// TODO? call the router directly, and remove the whole concept of a self peer?
|
// TODO? call the router directly, and remove the whole concept of a self peer?
|
||||||
ports[0].sendPacket(packet)
|
ports[0].sendPacket(packet)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
table := t.getTable()
|
table := t.getTable()
|
||||||
myDist := table.self.dist(coords)
|
|
||||||
var best *peer
|
var best *peer
|
||||||
bestDist := myDist
|
var bestDist int
|
||||||
for port := range idle {
|
var bestCoordLen int
|
||||||
if to := ports[port]; to != nil {
|
for port, dist := range closer {
|
||||||
if info, isIn := table.elems[to.port]; isIn {
|
to := ports[port]
|
||||||
dist := info.locator.dist(coords)
|
_, isIdle := idle[port]
|
||||||
if !(dist < bestDist) {
|
coordLen := len(table.elems[port].locator.coords)
|
||||||
continue
|
var update bool
|
||||||
}
|
switch {
|
||||||
best = to
|
case to == nil:
|
||||||
bestDist = dist
|
//nothing
|
||||||
}
|
case !isIdle:
|
||||||
|
//nothing
|
||||||
|
case best == nil:
|
||||||
|
update = true
|
||||||
|
case dist < bestDist:
|
||||||
|
update = true
|
||||||
|
case dist > bestDist:
|
||||||
|
//nothing
|
||||||
|
case coordLen < bestCoordLen:
|
||||||
|
update = true
|
||||||
|
case coordLen > bestCoordLen:
|
||||||
|
//nothing
|
||||||
|
case port < best.port:
|
||||||
|
update = true
|
||||||
|
default:
|
||||||
|
//nothing
|
||||||
|
}
|
||||||
|
if update {
|
||||||
|
best = to
|
||||||
|
bestDist = dist
|
||||||
|
bestCoordLen = coordLen
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if best != nil {
|
if best != nil {
|
||||||
@ -697,7 +717,7 @@ func (b *switch_buffers) cleanup(t *switchTable) {
|
|||||||
// Remove queues for which we have no next hop
|
// Remove queues for which we have no next hop
|
||||||
packet := buf.packets[0]
|
packet := buf.packets[0]
|
||||||
coords := switch_getPacketCoords(packet.bytes)
|
coords := switch_getPacketCoords(packet.bytes)
|
||||||
if t.selfIsClosest(coords) {
|
if len(t.getCloser(coords)) == 0 {
|
||||||
for _, packet := range buf.packets {
|
for _, packet := range buf.packets {
|
||||||
util.PutBytes(packet.bytes)
|
util.PutBytes(packet.bytes)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user