libs5-go/net/peer.go

137 lines
2.8 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"
2024-03-10 11:19:49 +00:00
"net"
2024-01-06 11:33:46 +00:00
"net/url"
)
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
EndForAbuse() error
2024-01-06 11:33:46 +00:00
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)
2024-03-10 11:16:37 +00:00
GetIPString() string
2024-03-10 11:19:49 +00:00
GetIP() net.Addr
2024-01-15 18:50:04 +00:00
Abuser() bool
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) EndForAbuse() error {
panic("must implement in child class")
}
2024-03-10 11:16:37 +00:00
func (b *BasePeer) GetIPString() string {
panic("must implement in child class")
}
2024-03-10 11:19:49 +00:00
func (b *BasePeer) GetIP() net.Addr {
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
}
2024-01-15 18:50:04 +00:00
func (b *BasePeer) Abuser() bool {
panic("must implement in child class")
}