libs5-go/protocol/signed/accounce_peers.go

158 lines
3.3 KiB
Go
Raw Normal View History

2024-01-06 17:51:38 +00:00
package signed
import (
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
2024-01-07 08:58:22 +00:00
"git.lumeweb.com/LumeWeb/libs5-go/interfaces"
2024-01-06 17:51:38 +00:00
"git.lumeweb.com/LumeWeb/libs5-go/net"
"git.lumeweb.com/LumeWeb/libs5-go/protocol/base"
"git.lumeweb.com/LumeWeb/libs5-go/types"
2024-01-06 17:51:38 +00:00
"github.com/vmihailenco/msgpack/v5"
"net/url"
)
var (
_ base.IncomingMessageTyped = (*AnnouncePeers)(nil)
2024-01-06 17:51:38 +00:00
)
type AnnouncePeers struct {
peer net.Peer
2024-01-06 17:51:38 +00:00
connectionUris []*url.URL
peersToSend []net.Peer
base.IncomingMessageTypedImpl
2024-01-06 17:51:38 +00:00
}
func (a *AnnouncePeers) PeersToSend() []net.Peer {
return a.peersToSend
}
func (a *AnnouncePeers) SetPeersToSend(peersToSend []net.Peer) {
a.peersToSend = peersToSend
}
func NewAnnounceRequest(peer net.Peer, peersToSend []net.Peer) *AnnouncePeers {
return &AnnouncePeers{peer: peer, connectionUris: nil, peersToSend: peersToSend}
}
2024-01-06 17:51:38 +00:00
func NewAnnouncePeers() *AnnouncePeers {
return &AnnouncePeers{peer: nil, connectionUris: nil}
2024-01-06 17:51:38 +00:00
}
func (a *AnnouncePeers) DecodeMessage(dec *msgpack.Decoder) error {
// Decode the number of peers.
numPeers, err := dec.DecodeInt()
2024-01-06 17:51:38 +00:00
if err != nil {
return err
}
// Initialize the slice for storing connection URIs.
var connectionURIs []*url.URL
2024-01-06 17:51:38 +00:00
// Loop through each peer.
for i := 0; i < numPeers; i++ {
// Decode peer ID.
peerIdBytes, err := dec.DecodeBytes()
if err != nil {
return err
}
peerId := encoding.NewNodeId(peerIdBytes)
2024-01-06 17:51:38 +00:00
// Skip decoding connection status as it is not used.
_, err = dec.DecodeBool() // Connection status, not used.
2024-01-06 17:51:38 +00:00
if err != nil {
return err
}
// Decode the number of connection URIs for this peer.
numUris, err := dec.DecodeInt()
if err != nil {
return err
}
2024-01-06 17:51:38 +00:00
// Decode each connection URI for this peer.
for j := 0; j < numUris; j++ {
uriStr, err := dec.DecodeString()
if err != nil {
return err
}
2024-01-06 17:51:38 +00:00
uri, err := url.Parse(uriStr)
if err != nil {
return err
2024-01-06 17:51:38 +00:00
}
pid, err := peerId.ToString()
2024-01-06 17:51:38 +00:00
if err != nil {
return err
}
passwd, empty := uri.User.Password()
if empty {
passwd = ""
}
// Incorporate the peer ID into the URI.
uri.User = url.UserPassword(pid, passwd)
connectionURIs = append(connectionURIs, uri)
}
}
a.connectionUris = connectionURIs
return nil
}
func (a AnnouncePeers) HandleMessage(node interfaces.Node, peer net.Peer, verifyId bool) error {
if len(a.connectionUris) > 0 {
err := node.Services().P2P().ConnectToNode([]*url.URL{a.connectionUris[0]}, false)
if err != nil {
return err
2024-01-06 17:51:38 +00:00
}
}
return nil
}
func (a AnnouncePeers) EncodeMsgpack(enc *msgpack.Encoder) error {
err := enc.EncodeUint(uint64(types.ProtocolMethodAnnouncePeers))
if err != nil {
return err
}
// Encode the number of peers.
err = enc.EncodeInt(int64(len(a.peersToSend)))
if err != nil {
return err
}
// Loop through each peer.
for _, peer := range a.peersToSend {
err = enc.EncodeBytes(peer.Id().Raw())
if err != nil {
return err
}
// Encode connection status.
err = enc.EncodeBool(peer.IsConnected())
if err != nil {
return err
}
// Encode the number of connection URIs for this peer.
err = enc.EncodeInt(int64(len(peer.ConnectionURIs())))
if err != nil {
return err
}
// Encode each connection URI for this peer.
for _, uri := range peer.ConnectionURIs() {
err = enc.EncodeString(uri.String())
if err != nil {
return err
}
}
}
return nil
}