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

Refactor: added ping messages for auto peering and refactored code

parent d93335f2
No related branches found
No related tags found
No related merge requests found
package udp package udp
import ( import (
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/ping"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/request" "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/request"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/response" "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/response"
"net" "net"
...@@ -8,17 +9,37 @@ import ( ...@@ -8,17 +9,37 @@ import (
) )
var Events = &pluginEvents{ var Events = &pluginEvents{
ReceiveRequest: &requestEvent{make(map[uintptr]ConnectionPeeringRequestConsumer)}, ReceivePing: &pingEvent{make(map[uintptr]PingConsumer)},
ReceiveRequest: &requestEvent{make(map[uintptr]ConnectionPeeringRequestConsumer)},
ReceiveResponse: &responseEvent{make(map[uintptr]ConnectionPeeringResponseConsumer)}, ReceiveResponse: &responseEvent{make(map[uintptr]ConnectionPeeringResponseConsumer)},
Error: &ipErrorEvent{make(map[uintptr]IPErrorConsumer)}, Error: &ipErrorEvent{make(map[uintptr]IPErrorConsumer)},
} }
type pluginEvents struct { type pluginEvents struct {
ReceivePing *pingEvent
ReceiveRequest *requestEvent ReceiveRequest *requestEvent
ReceiveResponse *responseEvent ReceiveResponse *responseEvent
Error *ipErrorEvent Error *ipErrorEvent
} }
type pingEvent struct {
callbacks map[uintptr]PingConsumer
}
func (this *pingEvent) Attach(callback PingConsumer) {
this.callbacks[reflect.ValueOf(callback).Pointer()] = callback
}
func (this *pingEvent) Detach(callback PingConsumer) {
delete(this.callbacks, reflect.ValueOf(callback).Pointer())
}
func (this *pingEvent) Trigger(ping *ping.Ping) {
for _, callback := range this.callbacks {
callback(ping)
}
}
type requestEvent struct { type requestEvent struct {
callbacks map[uintptr]ConnectionPeeringRequestConsumer callbacks map[uintptr]ConnectionPeeringRequestConsumer
} }
...@@ -72,4 +93,3 @@ func (this *ipErrorEvent) Trigger(ip net.IP, err error) { ...@@ -72,4 +93,3 @@ func (this *ipErrorEvent) Trigger(ip net.IP, err error) {
callback(ip, err) callback(ip, err)
} }
} }
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"github.com/iotaledger/goshimmer/packages/network/udp" "github.com/iotaledger/goshimmer/packages/network/udp"
"github.com/iotaledger/goshimmer/packages/node" "github.com/iotaledger/goshimmer/packages/node"
"github.com/iotaledger/goshimmer/plugins/autopeering/parameters" "github.com/iotaledger/goshimmer/plugins/autopeering/parameters"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/ping"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/request" "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/request"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/response" "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/response"
"github.com/pkg/errors" "github.com/pkg/errors"
...@@ -72,6 +73,14 @@ func processReceivedData(addr *net.UDPAddr, data []byte) { ...@@ -72,6 +73,14 @@ func processReceivedData(addr *net.UDPAddr, data []byte) {
Events.ReceiveResponse.Trigger(peeringResponse) Events.ReceiveResponse.Trigger(peeringResponse)
} }
case ping.MARSHALLED_PACKET_HEADER:
if ping, err := ping.Unmarshal(data); err != nil {
Events.Error.Trigger(addr.IP, err)
} else {
ping.Issuer.Address = addr.IP
Events.ReceivePing.Trigger(ping)
}
default: default:
Events.Error.Trigger(addr.IP, errors.New("invalid UDP peering packet from " + addr.IP.String())) Events.Error.Trigger(addr.IP, errors.New("invalid UDP peering packet from " + addr.IP.String()))
} }
......
package udp package udp
import ( import (
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/ping"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/request" "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/request"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/response" "github.com/iotaledger/goshimmer/plugins/autopeering/protocol/response"
"net" "net"
) )
type PingConsumer = func(p *ping.Ping)
type ConnectionPeeringRequestConsumer = func(request *request.Request) type ConnectionPeeringRequestConsumer = func(request *request.Request)
type ConnectionPeeringResponseConsumer = func(peeringResponse *response.Response) type ConnectionPeeringResponseConsumer = func(peeringResponse *response.Response)
......
package neighbormanager
import (
"github.com/iotaledger/goshimmer/plugins/autopeering/peermanager/types"
)
var ACCEPTED_NEIGHBORS = make(types.PeerRegister)
package neighbormanager
import (
"github.com/iotaledger/goshimmer/plugins/autopeering/peermanager/types"
)
var CHOSEN_NEIGHBORS = make(types.PeerRegister)
package neighbormanager
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
"github.com/iotaledger/goshimmer/packages/accountability" "github.com/iotaledger/goshimmer/packages/accountability"
"github.com/iotaledger/goshimmer/plugins/autopeering/peermanager" "github.com/iotaledger/goshimmer/plugins/autopeering/peermanager"
"github.com/iotaledger/goshimmer/plugins/gossip/neighbormanager"
"github.com/rivo/tview" "github.com/rivo/tview"
"math" "math"
"strconv" "strconv"
...@@ -62,10 +63,11 @@ func (headerBar *UIHeaderBar) Update() { ...@@ -62,10 +63,11 @@ func (headerBar *UIHeaderBar) Update() {
fmt.Fprintln(headerBar.InfoContainer) fmt.Fprintln(headerBar.InfoContainer)
fmt.Fprintln(headerBar.InfoContainer) fmt.Fprintln(headerBar.InfoContainer)
fmt.Fprintln(headerBar.InfoContainer) fmt.Fprintln(headerBar.InfoContainer)
fmt.Fprintln(headerBar.InfoContainer)
fmt.Fprintf(headerBar.InfoContainer, "[::b]Node ID: [::d]%40v ", accountability.OWN_ID.StringIdentifier) fmt.Fprintf(headerBar.InfoContainer, "[::b]Node ID: [::d]%40v ", accountability.OWN_ID.StringIdentifier)
fmt.Fprintln(headerBar.InfoContainer) fmt.Fprintln(headerBar.InfoContainer)
fmt.Fprintf(headerBar.InfoContainer, "[::b]Known Peers: [::d]%40v ", strconv.Itoa(len(peermanager.KNOWN_PEERS.Peers))) fmt.Fprintf(headerBar.InfoContainer, "[::b]Neighbors: [::d]%40v ", strconv.Itoa(len(neighbormanager.CHOSEN_NEIGHBORS)) + " chosen / " + strconv.Itoa(len(neighbormanager.ACCEPTED_NEIGHBORS)) + " accepted")
fmt.Fprintln(headerBar.InfoContainer)
fmt.Fprintf(headerBar.InfoContainer, "[::b]Known Peers: [::d]%40v ", strconv.Itoa(len(peermanager.KNOWN_PEERS)) + " total / " + strconv.Itoa(len(peermanager.NEIGHBORHOOD)) + " neighborhood")
fmt.Fprintln(headerBar.InfoContainer) fmt.Fprintln(headerBar.InfoContainer)
fmt.Fprintf(headerBar.InfoContainer, "[::b]Uptime: [::d]"); fmt.Fprintf(headerBar.InfoContainer, "[::b]Uptime: [::d]");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment