Skip to content
Snippets Groups Projects
Commit 36285a67 authored by Hans Moog's avatar Hans Moog
Browse files

Refactor: refactored autopeering module

parent 9bd059c3
No related branches found
No related tags found
No related merge requests found
Showing
with 214 additions and 174 deletions
package chosenneighborcandidates
import (
"github.com/iotaledger/goshimmer/plugins/autopeering/instances/neighborhood"
"github.com/iotaledger/goshimmer/plugins/autopeering/instances/outgoingrequest"
"github.com/iotaledger/goshimmer/plugins/autopeering/types/peer"
"github.com/iotaledger/goshimmer/plugins/autopeering/types/request"
"github.com/iotaledger/goshimmer/plugins/autopeering/types/peerlist"
)
var INSTANCE peerlist.PeerList
var DISTANCE = func(req *request.Request) func(p *peer.Peer) float64 {
return func(p *peer.Peer) float64 {
return 1
}
}
func init() {
updateNeighborCandidates()
neighborhood.Events.Update.Attach(updateNeighborCandidates)
}
func updateNeighborCandidates() {
INSTANCE = neighborhood.LIST_INSTANCE.Sort(DISTANCE(outgoingrequest.INSTANCE))
}
package peermanager package entrynodes
import ( import (
"github.com/iotaledger/goshimmer/packages/identity" "github.com/iotaledger/goshimmer/packages/identity"
"github.com/iotaledger/goshimmer/plugins/autopeering/parameters" "github.com/iotaledger/goshimmer/plugins/autopeering/parameters"
peermanagerTypes "github.com/iotaledger/goshimmer/plugins/autopeering/peermanager/types" "github.com/iotaledger/goshimmer/plugins/autopeering/types/peer"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/peer" "github.com/iotaledger/goshimmer/plugins/autopeering/types/peerlist"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/types"
"net" "net"
"strconv" "strconv"
"strings" "strings"
) )
var ENTRY_NODES = parseEntryNodes() var INSTANCE = parseEntryNodes()
func parseEntryNodes() peermanagerTypes.PeerList { func parseEntryNodes() peerlist.PeerList {
result := make(peermanagerTypes.PeerList, 0) result := make(peerlist.PeerList, 0)
for _, entryNodeDefinition := range strings.Fields(*parameters.ENTRY_NODES.Value) { for _, entryNodeDefinition := range strings.Fields(*parameters.ENTRY_NODES.Value) {
if entryNodeDefinition == "" { if entryNodeDefinition == "" {
...@@ -25,18 +24,7 @@ func parseEntryNodes() peermanagerTypes.PeerList { ...@@ -25,18 +24,7 @@ func parseEntryNodes() peermanagerTypes.PeerList {
Identity: nil, Identity: nil,
} }
protocolBits := strings.Split(entryNodeDefinition, "://") identityBits := strings.Split(entryNodeDefinition, "@")
if len(protocolBits) != 2 {
panic("invalid entry in list of trusted entry nodes: " + entryNodeDefinition)
}
switch protocolBits[0] {
case "tcp":
entryNode.PeeringProtocolType = types.PROTOCOL_TYPE_TCP
case "udp":
entryNode.PeeringProtocolType = types.PROTOCOL_TYPE_UDP
}
identityBits := strings.Split(protocolBits[1], "@")
if len(identityBits) != 2 { if len(identityBits) != 2 {
panic("error while parsing identity of entry node: " + entryNodeDefinition) panic("error while parsing identity of entry node: " + entryNodeDefinition)
} }
......
package knownpeers
import (
"github.com/iotaledger/goshimmer/plugins/autopeering/instances/entrynodes"
"github.com/iotaledger/goshimmer/plugins/autopeering/types/peerregister"
)
var INSTANCE = initKnownPeers()
func initKnownPeers() peerregister.PeerRegister {
knownPeers := make(peerregister.PeerRegister)
for _, entryNode := range entrynodes.INSTANCE {
knownPeers.AddOrUpdate(entryNode)
}
return knownPeers
}
package peermanager package neighborhood
import "reflect" import "reflect"
var Events = moduleEvents{ var Events = moduleEvents{
UpdateNeighborhood: &callbackEvent{make(map[uintptr]Callback)}, Update: &callbackEvent{make(map[uintptr]Callback)},
} }
type moduleEvents struct { type moduleEvents struct {
UpdateNeighborhood *callbackEvent Update *callbackEvent
} }
type callbackEvent struct { type callbackEvent struct {
...@@ -28,4 +28,4 @@ func (this *callbackEvent) Trigger() { ...@@ -28,4 +28,4 @@ func (this *callbackEvent) Trigger() {
} }
} }
type Callback = func() type Callback = func()
\ No newline at end of file
package peermanager package neighborhood
import ( import (
"github.com/iotaledger/goshimmer/packages/timeutil" "github.com/iotaledger/goshimmer/packages/timeutil"
"github.com/iotaledger/goshimmer/plugins/autopeering/peermanager/types" "github.com/iotaledger/goshimmer/plugins/autopeering/instances/knownpeers"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/request" "github.com/iotaledger/goshimmer/plugins/autopeering/instances/outgoingrequest"
"github.com/iotaledger/goshimmer/plugins/autopeering/types/request"
"github.com/iotaledger/goshimmer/plugins/autopeering/types/peerlist"
"github.com/iotaledger/goshimmer/plugins/autopeering/types/peerregister"
"time" "time"
) )
var NEIGHBORHOOD types.PeerRegister var INSTANCE peerregister.PeerRegister
var NEIGHBORHOOD_LIST types.PeerList var LIST_INSTANCE peerlist.PeerList
// Selects a fixed neighborhood from all known peers - this allows nodes to "stay in the same circles" that share their // Selects a fixed neighborhood from all known peers - this allows nodes to "stay in the same circles" that share their
// view on the ledger an is a preparation for economic clustering // view on the ledger an is a preparation for economic clustering
var NEIGHBORHOOD_SELECTOR = func(this types.PeerRegister, req *request.Request) types.PeerRegister { var NEIGHBORHOOD_SELECTOR = func(this peerregister.PeerRegister, req *request.Request) peerregister.PeerRegister {
filteredPeers := make(types.PeerRegister) filteredPeers := make(peerregister.PeerRegister)
for id, peer := range this { for id, peer := range this {
filteredPeers[id] = peer filteredPeers[id] = peer
} }
...@@ -31,12 +34,12 @@ func init() { ...@@ -31,12 +34,12 @@ func init() {
} }
func updateNeighborHood() { func updateNeighborHood() {
if float64(len(NEIGHBORHOOD)) * 1.2 <= float64(len(KNOWN_PEERS)) || lastUpdate.Before(time.Now().Add(-300 * time.Second)) { if float64(len(INSTANCE)) * 1.2 <= float64(len(knownpeers.INSTANCE)) || lastUpdate.Before(time.Now().Add(-300 * time.Second)) {
NEIGHBORHOOD = KNOWN_PEERS.Filter(NEIGHBORHOOD_SELECTOR, request.OUTGOING_REQUEST) INSTANCE = knownpeers.INSTANCE.Filter(NEIGHBORHOOD_SELECTOR, outgoingrequest.INSTANCE)
NEIGHBORHOOD_LIST = NEIGHBORHOOD.List() LIST_INSTANCE = INSTANCE.List()
lastUpdate = time.Now() lastUpdate = time.Now()
Events.UpdateNeighborhood.Trigger() Events.Update.Trigger()
} }
} }
\ No newline at end of file
package outgoingrequest
import (
"github.com/iotaledger/goshimmer/plugins/autopeering/instances/ownpeer"
"github.com/iotaledger/goshimmer/plugins/autopeering/types/request"
"github.com/iotaledger/goshimmer/plugins/autopeering/types/salt"
"github.com/iotaledger/goshimmer/plugins/autopeering/saltmanager"
)
var INSTANCE *request.Request
func init() {
INSTANCE = &request.Request{
Issuer: ownpeer.INSTANCE,
}
INSTANCE.Sign()
saltmanager.Events.UpdatePublicSalt.Attach(func(salt *salt.Salt) {
INSTANCE.Sign()
})
}
package ownpeer
import (
"github.com/iotaledger/goshimmer/packages/accountability"
"github.com/iotaledger/goshimmer/plugins/autopeering/parameters"
"github.com/iotaledger/goshimmer/plugins/autopeering/types/peer"
"github.com/iotaledger/goshimmer/plugins/autopeering/saltmanager"
"net"
)
var INSTANCE = &peer.Peer{
Identity: accountability.OWN_ID,
PeeringPort: uint16(*parameters.PORT.Value),
GossipPort: uint16(*parameters.PORT.Value),
Address: net.IPv4(0, 0, 0, 0),
Salt: saltmanager.PUBLIC_SALT,
}
...@@ -4,7 +4,6 @@ import "github.com/iotaledger/goshimmer/packages/parameter" ...@@ -4,7 +4,6 @@ import "github.com/iotaledger/goshimmer/packages/parameter"
var ( var (
ADDRESS = parameter.AddString("AUTOPEERING/ADDRESS", "0.0.0.0", "address to bind for incoming peering requests") ADDRESS = parameter.AddString("AUTOPEERING/ADDRESS", "0.0.0.0", "address to bind for incoming peering requests")
ENTRY_NODES = parameter.AddString("AUTOPEERING/ENTRY_NODES", "tcp://0d828930890386f036eb77982cc067c5429f7b8f@82.165.29.179:14626", "list of trusted entry nodes for auto peering") ENTRY_NODES = parameter.AddString("AUTOPEERING/ENTRY_NODES", "0d828930890386f036eb77982cc067c5429f7b8f@82.165.29.179:14626", "list of trusted entry nodes for auto peering")
TCP_PORT = parameter.AddInt("AUTOPEERING/TCP_PORT", 14626, "tcp port for incoming peering requests") PORT = parameter.AddInt("AUTOPEERING/PORT", 14626, "tcp port for incoming peering requests")
UDP_PORT = parameter.AddInt("AUTOPEERING/UDP_PORT", 14626, "udp port for incoming peering requests")
) )
package peermanager
import (
"github.com/iotaledger/goshimmer/plugins/autopeering/peermanager/types"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/peer"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/request"
)
var CHOSEN_NEIGHBOR_CANDIDATES types.PeerList
var CHOSEN_NEIGHBOR_DISTANCE = func(req *request.Request) func(p *peer.Peer) float64 {
return func(p *peer.Peer) float64 {
return 1
}
}
func init() {
updateNeighborCandidates()
Events.UpdateNeighborhood.Attach(updateNeighborCandidates)
}
func updateNeighborCandidates() {
CHOSEN_NEIGHBOR_CANDIDATES = NEIGHBORHOOD.List().Sort(CHOSEN_NEIGHBOR_DISTANCE(request.OUTGOING_REQUEST))
}
package peermanager
import (
"github.com/iotaledger/goshimmer/plugins/autopeering/peermanager/types"
)
var KNOWN_PEERS = initKnownPeers()
func initKnownPeers() types.PeerRegister {
knownPeers := make(types.PeerRegister)
for _, entryNode := range ENTRY_NODES {
knownPeers.AddOrUpdate(entryNode)
}
return knownPeers
}
\ No newline at end of file
...@@ -3,11 +3,11 @@ package autopeering ...@@ -3,11 +3,11 @@ package autopeering
import ( import (
"github.com/iotaledger/goshimmer/packages/daemon" "github.com/iotaledger/goshimmer/packages/daemon"
"github.com/iotaledger/goshimmer/packages/node" "github.com/iotaledger/goshimmer/packages/node"
"github.com/iotaledger/goshimmer/plugins/autopeering/peermanager" "github.com/iotaledger/goshimmer/plugins/autopeering/instances/knownpeers"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol" "github.com/iotaledger/goshimmer/plugins/autopeering/protocol"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/peer" "github.com/iotaledger/goshimmer/plugins/autopeering/types/peer"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/request" "github.com/iotaledger/goshimmer/plugins/autopeering/types/request"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/response" "github.com/iotaledger/goshimmer/plugins/autopeering/types/response"
"github.com/iotaledger/goshimmer/plugins/autopeering/server" "github.com/iotaledger/goshimmer/plugins/autopeering/server"
"github.com/iotaledger/goshimmer/plugins/gossip/neighbormanager" "github.com/iotaledger/goshimmer/plugins/gossip/neighbormanager"
) )
...@@ -33,7 +33,7 @@ func configure(plugin *node.Plugin) { ...@@ -33,7 +33,7 @@ func configure(plugin *node.Plugin) {
}) })
protocol.Events.DiscoverPeer.Attach(func(p *peer.Peer) { protocol.Events.DiscoverPeer.Attach(func(p *peer.Peer) {
if peermanager.KNOWN_PEERS.AddOrUpdate(p) { if knownpeers.INSTANCE.AddOrUpdate(p) {
plugin.LogInfo("new peer detected: " + p.Address.String() + " / " + p.Identity.StringIdentifier) plugin.LogInfo("new peer detected: " + p.Address.String() + " / " + p.Identity.StringIdentifier)
} }
}) })
......
...@@ -5,7 +5,14 @@ import "time" ...@@ -5,7 +5,14 @@ import "time"
const ( const (
NEIGHBOR_COUNT = 8 NEIGHBOR_COUNT = 8
FIND_NEIGHBOR_INTERVAL = 5 * time.Second FIND_NEIGHBOR_INTERVAL = 5 * time.Second
PING_RANDOM_PEERS_INTERVAL = 1 * time.Second
PING_RANDOM_PEERS_COUNT = 3 // How often does the outgoing ping processor check if new pings should be sent.
PING_PROCESS_INTERVAL = 1 * time.Second
// The amount of times each neighbor should be contacted in this cycle.
PING_CONTACT_COUNT_PER_CYCLE = 2
// The length of a ping cycle (after this time we have sent randomized pings to all of our neighbors).
PING_CYCLE_LENGTH = 900 * time.Second
) )
package protocol package protocol
import ( import (
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/peer" "github.com/iotaledger/goshimmer/plugins/autopeering/types/peer"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/request" "github.com/iotaledger/goshimmer/plugins/autopeering/types/request"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/response" "github.com/iotaledger/goshimmer/plugins/autopeering/types/response"
"reflect" "reflect"
) )
......
...@@ -2,7 +2,7 @@ package protocol ...@@ -2,7 +2,7 @@ package protocol
import ( import (
"github.com/iotaledger/goshimmer/packages/node" "github.com/iotaledger/goshimmer/packages/node"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/ping" "github.com/iotaledger/goshimmer/plugins/autopeering/types/ping"
) )
func createIncomingPingProcessor(plugin *node.Plugin) func(p *ping.Ping) { func createIncomingPingProcessor(plugin *node.Plugin) func(p *ping.Ping) {
......
...@@ -2,11 +2,12 @@ package protocol ...@@ -2,11 +2,12 @@ package protocol
import ( import (
"github.com/iotaledger/goshimmer/packages/node" "github.com/iotaledger/goshimmer/packages/node"
"github.com/iotaledger/goshimmer/plugins/autopeering/peermanager" "github.com/iotaledger/goshimmer/plugins/autopeering/instances/chosenneighborcandidates"
"github.com/iotaledger/goshimmer/plugins/autopeering/peermanager/types" "github.com/iotaledger/goshimmer/plugins/autopeering/instances/neighborhood"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/constants" "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/constants"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/peer" "github.com/iotaledger/goshimmer/plugins/autopeering/types/peer"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/request" "github.com/iotaledger/goshimmer/plugins/autopeering/types/request"
"github.com/iotaledger/goshimmer/plugins/autopeering/types/peerlist"
"github.com/iotaledger/goshimmer/plugins/gossip/neighbormanager" "github.com/iotaledger/goshimmer/plugins/gossip/neighbormanager"
) )
...@@ -40,8 +41,8 @@ func createIncomingRequestProcessor(plugin *node.Plugin) func(req *request.Reque ...@@ -40,8 +41,8 @@ func createIncomingRequestProcessor(plugin *node.Plugin) func(req *request.Reque
} }
} }
func proposedPeeringCandidates(req *request.Request) types.PeerList { func proposedPeeringCandidates(req *request.Request) peerlist.PeerList {
return peermanager.NEIGHBORHOOD.List().Filter(func(p *peer.Peer) bool { return neighborhood.LIST_INSTANCE.Filter(func(p *peer.Peer) bool {
return p.Identity.PublicKey != nil return p.Identity.PublicKey != nil
}).Sort(peermanager.CHOSEN_NEIGHBOR_DISTANCE(req)) }).Sort(chosenneighborcandidates.DISTANCE(req))
} }
\ No newline at end of file
...@@ -2,7 +2,7 @@ package protocol ...@@ -2,7 +2,7 @@ package protocol
import ( import (
"github.com/iotaledger/goshimmer/packages/node" "github.com/iotaledger/goshimmer/packages/node"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/response" "github.com/iotaledger/goshimmer/plugins/autopeering/types/response"
"strconv" "strconv"
) )
......
...@@ -4,33 +4,35 @@ import ( ...@@ -4,33 +4,35 @@ import (
"github.com/iotaledger/goshimmer/packages/accountability" "github.com/iotaledger/goshimmer/packages/accountability"
"github.com/iotaledger/goshimmer/packages/daemon" "github.com/iotaledger/goshimmer/packages/daemon"
"github.com/iotaledger/goshimmer/packages/node" "github.com/iotaledger/goshimmer/packages/node"
"github.com/iotaledger/goshimmer/plugins/autopeering/instances/neighborhood"
"github.com/iotaledger/goshimmer/plugins/autopeering/parameters" "github.com/iotaledger/goshimmer/plugins/autopeering/parameters"
"github.com/iotaledger/goshimmer/plugins/autopeering/peermanager"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/constants" "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/constants"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/peer"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/ping"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/salt"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/types" "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/types"
"github.com/iotaledger/goshimmer/plugins/autopeering/saltmanager" "github.com/iotaledger/goshimmer/plugins/autopeering/saltmanager"
"github.com/iotaledger/goshimmer/plugins/autopeering/types/peer"
"github.com/iotaledger/goshimmer/plugins/autopeering/types/ping"
"github.com/iotaledger/goshimmer/plugins/autopeering/types/salt"
"github.com/iotaledger/goshimmer/plugins/gossip/neighbormanager" "github.com/iotaledger/goshimmer/plugins/gossip/neighbormanager"
"math/rand" "math/rand"
"net" "net"
"time" "time"
) )
var lastPing time.Time
func createOutgoingPingProcessor(plugin *node.Plugin) func() { func createOutgoingPingProcessor(plugin *node.Plugin) func() {
return func() { return func() {
plugin.LogInfo("Starting Ping Processor ...") plugin.LogInfo("Starting Ping Processor ...")
plugin.LogSuccess("Starting Ping Processor ... done") plugin.LogSuccess("Starting Ping Processor ... done")
lastPing = time.Now().Add(-constants.PING_CYCLE_LENGTH)
outgoingPing := &ping.Ping{ outgoingPing := &ping.Ping{
Issuer: &peer.Peer{ Issuer: &peer.Peer{
Identity: accountability.OWN_ID, Identity: accountability.OWN_ID,
PeeringProtocolType: types.PROTOCOL_TYPE_TCP,
PeeringPort: uint16(*parameters.UDP_PORT.Value),
GossipProtocolType: types.PROTOCOL_TYPE_TCP,
GossipPort: uint16(*parameters.UDP_PORT.Value),
Address: net.IPv4(0, 0, 0, 0), Address: net.IPv4(0, 0, 0, 0),
PeeringPort: uint16(*parameters.PORT.Value),
GossipPort: uint16(*parameters.PORT.Value),
Salt: saltmanager.PUBLIC_SALT, Salt: saltmanager.PUBLIC_SALT,
}, },
} }
...@@ -42,7 +44,7 @@ func createOutgoingPingProcessor(plugin *node.Plugin) func() { ...@@ -42,7 +44,7 @@ func createOutgoingPingProcessor(plugin *node.Plugin) func() {
pingPeers(plugin, outgoingPing) pingPeers(plugin, outgoingPing)
ticker := time.NewTicker(constants.PING_RANDOM_PEERS_INTERVAL) ticker := time.NewTicker(constants.PING_PROCESS_INTERVAL)
ticker: ticker:
for { for {
select { select {
...@@ -60,24 +62,30 @@ func createOutgoingPingProcessor(plugin *node.Plugin) func() { ...@@ -60,24 +62,30 @@ func createOutgoingPingProcessor(plugin *node.Plugin) func() {
} }
func pingPeers(plugin *node.Plugin, outgoingPing *ping.Ping) { func pingPeers(plugin *node.Plugin, outgoingPing *ping.Ping) {
chosenPeers := make(map[string]*peer.Peer) pingDelay := constants.PING_CYCLE_LENGTH / time.Duration(len(neighborhood.LIST_INSTANCE))
for i := 0; i < constants.PING_RANDOM_PEERS_COUNT; i++ { if lastPing.Add(pingDelay).Before(time.Now()) {
randomNeighborHoodPeer := peermanager.NEIGHBORHOOD_LIST[rand.Intn(len(peermanager.NEIGHBORHOOD_LIST))] chosenPeers := make(map[string]*peer.Peer)
nodeId := randomNeighborHoodPeer.Identity.StringIdentifier for i := 0; i < constants.PING_CONTACT_COUNT_PER_CYCLE; i++ {
randomNeighborHoodPeer := neighborhood.LIST_INSTANCE[rand.Intn(len(neighborhood.LIST_INSTANCE))]
if !neighbormanager.ACCEPTED_NEIGHBORS.Contains(nodeId) && !neighbormanager.CHOSEN_NEIGHBORS.Contains(nodeId) && nodeId := randomNeighborHoodPeer.Identity.StringIdentifier
if !neighbormanager.ACCEPTED_NEIGHBORS.Contains(nodeId) && !neighbormanager.CHOSEN_NEIGHBORS.Contains(nodeId) &&
nodeId != accountability.OWN_ID.StringIdentifier { nodeId != accountability.OWN_ID.StringIdentifier {
chosenPeers[randomNeighborHoodPeer.Identity.StringIdentifier] = randomNeighborHoodPeer chosenPeers[randomNeighborHoodPeer.Identity.StringIdentifier] = randomNeighborHoodPeer
}
} }
}
for _, chosenPeer := range chosenPeers { for _, chosenPeer := range chosenPeers {
go func(chosenPeer *peer.Peer) { go func(chosenPeer *peer.Peer) {
chosenPeer.Send(outgoingPing.Marshal(), false) chosenPeer.Send(outgoingPing.Marshal(), types.PROTOCOL_TYPE_UDP, false)
plugin.LogDebug("sent ping to " + chosenPeer.String())
}(chosenPeer)
}
plugin.LogDebug("sent ping to " + chosenPeer.String()) lastPing = time.Now()
}(chosenPeer)
} }
} }
\ No newline at end of file
...@@ -4,21 +4,22 @@ import ( ...@@ -4,21 +4,22 @@ import (
"github.com/iotaledger/goshimmer/packages/accountability" "github.com/iotaledger/goshimmer/packages/accountability"
"github.com/iotaledger/goshimmer/packages/daemon" "github.com/iotaledger/goshimmer/packages/daemon"
"github.com/iotaledger/goshimmer/packages/node" "github.com/iotaledger/goshimmer/packages/node"
"github.com/iotaledger/goshimmer/plugins/autopeering/peermanager" "github.com/iotaledger/goshimmer/plugins/autopeering/instances/chosenneighborcandidates"
"github.com/iotaledger/goshimmer/plugins/autopeering/instances/outgoingrequest"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/constants" "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/constants"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/peer" "github.com/iotaledger/goshimmer/plugins/autopeering/types/peer"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/request" "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/types"
"github.com/iotaledger/goshimmer/plugins/autopeering/server/tcp" "github.com/iotaledger/goshimmer/plugins/autopeering/server/tcp"
"github.com/iotaledger/goshimmer/plugins/gossip/neighbormanager" "github.com/iotaledger/goshimmer/plugins/gossip/neighbormanager"
"time" "time"
) )
func createChosenNeighborProcessor(plugin *node.Plugin) func() { func createOutgoingRequestProcessor(plugin *node.Plugin) func() {
return func() { return func() {
plugin.LogInfo("Starting Chosen Neighbor Processor ...") plugin.LogInfo("Starting Chosen Neighbor Processor ...")
plugin.LogSuccess("Starting Chosen Neighbor Processor ... done") plugin.LogSuccess("Starting Chosen Neighbor Processor ... done")
chooseNeighbors(plugin) sendOutgoingRequests(plugin)
ticker := time.NewTicker(constants.FIND_NEIGHBOR_INTERVAL) ticker := time.NewTicker(constants.FIND_NEIGHBOR_INTERVAL)
ticker: ticker:
...@@ -29,7 +30,7 @@ func createChosenNeighborProcessor(plugin *node.Plugin) func() { ...@@ -29,7 +30,7 @@ func createChosenNeighborProcessor(plugin *node.Plugin) func() {
break ticker break ticker
case <- ticker.C: case <- ticker.C:
chooseNeighbors(plugin) sendOutgoingRequests(plugin)
} }
} }
...@@ -37,8 +38,8 @@ func createChosenNeighborProcessor(plugin *node.Plugin) func() { ...@@ -37,8 +38,8 @@ func createChosenNeighborProcessor(plugin *node.Plugin) func() {
} }
} }
func chooseNeighbors(plugin *node.Plugin) { func sendOutgoingRequests(plugin *node.Plugin) {
for _, chosenNeighborCandidate := range peermanager.CHOSEN_NEIGHBOR_CANDIDATES { for _, chosenNeighborCandidate := range chosenneighborcandidates.INSTANCE {
go func(peer *peer.Peer) { go func(peer *peer.Peer) {
nodeId := peer.Identity.StringIdentifier nodeId := peer.Identity.StringIdentifier
...@@ -46,14 +47,14 @@ func chooseNeighbors(plugin *node.Plugin) { ...@@ -46,14 +47,14 @@ func chooseNeighbors(plugin *node.Plugin) {
!neighbormanager.CHOSEN_NEIGHBORS.Contains(nodeId) && !neighbormanager.CHOSEN_NEIGHBORS.Contains(nodeId) &&
accountability.OWN_ID.StringIdentifier != nodeId { accountability.OWN_ID.StringIdentifier != nodeId {
if connectionAlive, err := request.Send(peer); err != nil { if dialed, err := peer.Send(outgoingrequest.INSTANCE.Marshal(), types.PROTOCOL_TYPE_TCP, true); err != nil {
plugin.LogDebug(err.Error()) plugin.LogDebug(err.Error())
} else if connectionAlive {
plugin.LogDebug("sent TCP peering request to " + peer.String())
tcp.HandleConnection(peer.Conn)
} else { } else {
plugin.LogDebug("sent UDP peering request to " + peer.String()) plugin.LogDebug("sent peering request to " + peer.String())
if dialed {
tcp.HandleConnection(peer.Conn)
}
} }
} }
}(chosenNeighborCandidate) }(chosenNeighborCandidate)
......
...@@ -25,6 +25,6 @@ func Configure(plugin *node.Plugin) { ...@@ -25,6 +25,6 @@ func Configure(plugin *node.Plugin) {
} }
func Run(plugin *node.Plugin) { func Run(plugin *node.Plugin) {
daemon.BackgroundWorker(createChosenNeighborProcessor(plugin)) daemon.BackgroundWorker(createOutgoingRequestProcessor(plugin))
daemon.BackgroundWorker(createOutgoingPingProcessor(plugin)) daemon.BackgroundWorker(createOutgoingPingProcessor(plugin))
} }
package request
import (
"github.com/iotaledger/goshimmer/packages/accountability"
"github.com/iotaledger/goshimmer/plugins/autopeering/parameters"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/peer"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/salt"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/types"
"github.com/iotaledger/goshimmer/plugins/autopeering/saltmanager"
"github.com/pkg/errors"
"net"
)
var OUTGOING_REQUEST *Request
func Send(peer *peer.Peer) (bool, error) {
var keepAlive bool
switch peer.PeeringProtocolType {
case types.PROTOCOL_TYPE_TCP:
keepAlive = true
case types.PROTOCOL_TYPE_UDP:
keepAlive = false
default:
return false, errors.New("peer uses invalid protocol")
}
if err := peer.Send(OUTGOING_REQUEST.Marshal(), keepAlive); err != nil {
return false, err
}
return keepAlive, nil
}
func init() {
OUTGOING_REQUEST = &Request{
Issuer: &peer.Peer{
Identity: accountability.OWN_ID,
PeeringProtocolType: types.PROTOCOL_TYPE_TCP,
PeeringPort: uint16(*parameters.UDP_PORT.Value),
GossipProtocolType: types.PROTOCOL_TYPE_TCP,
GossipPort: uint16(*parameters.UDP_PORT.Value),
Address: net.IPv4(0, 0, 0, 0),
Salt: saltmanager.PUBLIC_SALT,
},
}
OUTGOING_REQUEST.Sign()
saltmanager.Events.UpdatePublicSalt.Attach(func(salt *salt.Salt) {
OUTGOING_REQUEST.Sign()
})
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment