2024-01-06 11:33:46 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-01-07 08:19:04 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/interfaces"
|
2024-01-06 11:33:46 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/net"
|
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
|
|
|
"github.com/vmihailenco/msgpack/v5"
|
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ EncodeableMessage = (*EncodeableMessageImpl)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
type IncomingMessage interface {
|
2024-01-07 08:58:22 +00:00
|
|
|
HandleMessage(node interfaces.Node, peer *net.Peer, verifyId bool) error
|
2024-01-06 11:33:46 +00:00
|
|
|
SetIncomingMessage(msg IncomingMessage)
|
|
|
|
msgpack.CustomDecoder
|
|
|
|
}
|
|
|
|
|
|
|
|
type IncomingMessageTyped interface {
|
|
|
|
DecodeMessage(dec *msgpack.Decoder) error
|
|
|
|
IncomingMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
type IncomingMessageImpl struct {
|
2024-01-06 15:54:38 +00:00
|
|
|
kind types.ProtocolMethod
|
|
|
|
data msgpack.RawMessage
|
|
|
|
original []byte
|
|
|
|
known bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IncomingMessageImpl) SetOriginal(original []byte) {
|
|
|
|
i.original = original
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IncomingMessageImpl) Original() []byte {
|
|
|
|
return i.original
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IncomingMessageImpl) SetIncomingMessage(msg IncomingMessage) {
|
|
|
|
*i = interface{}(msg).(IncomingMessageImpl)
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ msgpack.CustomDecoder = (*IncomingMessageImpl)(nil)
|
|
|
|
var _ IncomingMessage = (*IncomingMessageImpl)(nil)
|
|
|
|
|
|
|
|
func (i *IncomingMessageImpl) GetKind() types.ProtocolMethod {
|
|
|
|
return i.kind
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IncomingMessageImpl) ToMessage() (message []byte, err error) {
|
|
|
|
return msgpack.Marshal(i)
|
|
|
|
}
|
|
|
|
|
2024-01-07 08:58:22 +00:00
|
|
|
func (i *IncomingMessageImpl) HandleMessage(node interfaces.Node, peer *net.Peer, verifyId bool) error {
|
2024-01-06 11:33:46 +00:00
|
|
|
panic("child class should implement this method")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IncomingMessageImpl) Kind() types.ProtocolMethod {
|
|
|
|
return i.kind
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IncomingMessageImpl) Data() msgpack.RawMessage {
|
|
|
|
return i.data
|
|
|
|
}
|
|
|
|
|
|
|
|
type IncomingMessageTypedImpl struct {
|
|
|
|
IncomingMessageImpl
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewIncomingMessageUnknown() *IncomingMessageImpl {
|
|
|
|
return &IncomingMessageImpl{
|
|
|
|
known: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewIncomingMessageKnown(kind types.ProtocolMethod, data msgpack.RawMessage) *IncomingMessageImpl {
|
|
|
|
return &IncomingMessageImpl{
|
|
|
|
kind: kind,
|
|
|
|
data: data,
|
|
|
|
known: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewIncomingMessageTyped(kind types.ProtocolMethod, data msgpack.RawMessage) *IncomingMessageTypedImpl {
|
|
|
|
known := NewIncomingMessageKnown(kind, data)
|
|
|
|
return &IncomingMessageTypedImpl{*known}
|
|
|
|
}
|
|
|
|
|
2024-01-07 09:27:54 +00:00
|
|
|
type IncomingMessageHandler func(node interfaces.Node, peer *net.Peer, u *url.URL, verifyId bool) error
|
2024-01-06 11:33:46 +00:00
|
|
|
|
|
|
|
func (i *IncomingMessageImpl) DecodeMsgpack(dec *msgpack.Decoder) error {
|
|
|
|
if i.known {
|
|
|
|
if msgTyped, ok := interface{}(i).(IncomingMessageTyped); ok {
|
|
|
|
return msgTyped.DecodeMessage(dec)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("type assertion to IncomingMessageTyped failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
kind, err := dec.DecodeInt()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
i.kind = types.ProtocolMethod(kind)
|
|
|
|
|
|
|
|
raw, err := dec.DecodeRaw()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
i.data = raw
|
|
|
|
return nil
|
|
|
|
}
|