libs5-go/net/peer.go

124 lines
2.6 KiB
Go
Raw Normal View History

2024-01-06 11:33:46 +00:00
package net
import (
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
"go.uber.org/zap"
"net/url"
)
2024-01-08 10:32:06 +00:00
//go:generate mockgen -source=peer.go -destination=../mocks/net/peer.go -package=net
var (
_ Peer = (*BasePeer)(nil)
)
2024-01-06 11:33:46 +00:00
// EventCallback type for the callback function
type EventCallback func(event []byte) error
// CloseCallback type for the OnClose callback
type CloseCallback func()
2024-01-06 11:33:46 +00:00
// ErrorCallback type for the onError callback
type ErrorCallback func(args ...interface{})
// ListenerOptions struct for options
type ListenerOptions struct {
OnClose *CloseCallback
2024-01-06 11:33:46 +00:00
OnError *ErrorCallback
Logger *zap.Logger
}
type Peer interface {
SendMessage(message []byte) error
RenderLocationURI() string
ListenForMessages(callback EventCallback, options ListenerOptions)
End() error
SetId(id *encoding.NodeId)
Id() *encoding.NodeId
2024-01-06 11:33:46 +00:00
SetChallenge(challenge []byte)
Challenge() []byte
SetSocket(socket interface{})
Socket() interface{}
SetConnected(isConnected bool)
IsConnected() bool
SetConnectionURIs(uris []*url.URL)
ConnectionURIs() []*url.URL
IsHandshakeDone() bool
SetHandshakeDone(status bool)
GetIP() string
2024-01-06 11:33:46 +00:00
}
type BasePeer struct {
connectionURIs []*url.URL
isConnected bool
2024-01-06 11:33:46 +00:00
challenge []byte
socket interface{}
id *encoding.NodeId
handshaked bool
}
func (b *BasePeer) IsConnected() bool {
return b.isConnected
}
func (b *BasePeer) SetConnected(isConnected bool) {
b.isConnected = isConnected
}
func (b *BasePeer) SendMessage(message []byte) error {
panic("must implement in child class")
}
func (b *BasePeer) RenderLocationURI() string {
panic("must implement in child class")
}
func (b *BasePeer) ListenForMessages(callback EventCallback, options ListenerOptions) {
panic("must implement in child class")
}
func (b *BasePeer) End() error {
panic("must implement in child class")
}
func (b *BasePeer) GetIP() string {
panic("must implement in child class")
}
func (b *BasePeer) Challenge() []byte {
return b.challenge
}
func (b *BasePeer) SetChallenge(challenge []byte) {
b.challenge = challenge
}
func (b *BasePeer) Socket() interface{} {
return b.socket
}
func (b *BasePeer) SetSocket(socket interface{}) {
b.socket = socket
}
func (b *BasePeer) Id() *encoding.NodeId {
return b.id
}
func (b *BasePeer) SetId(id *encoding.NodeId) {
b.id = id
2024-01-06 11:33:46 +00:00
}
func (b *BasePeer) SetConnectionURIs(uris []*url.URL) {
b.connectionURIs = uris
}
func (b *BasePeer) ConnectionURIs() []*url.URL {
return b.connectionURIs
}
func (b *BasePeer) IsHandshakeDone() bool {
return b.handshaked
}
func (b *BasePeer) SetHandshakeDone(status bool) {
b.handshaked = status
}