feudal/message/envelope.go

50 lines
1021 B
Go
Raw Permalink Normal View History

2024-04-04 19:43:46 +00:00
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package message;
import (
"feudal/identity"
)
type Envelope interface {
Body() interface{}
Sender() Sender
Receiver() Receiver
}
type Handler interface {
Handle(m Envelope)
}
type Receiver interface {
identity.Addresser
Receive(m Envelope)
}
type Sender interface {
identity.Addresser
Send(m Envelope)
}
type AbstractEnvelope struct {
body interface{}
sender Sender
receiver Receiver
}
func (e *AbstractEnvelope) Body() interface{} { return e.body }
func (e *AbstractEnvelope) Sender() Sender { return e.sender }
func (e *AbstractEnvelope) Receiver() Receiver { return e.receiver }
func New(b interface{}, s Sender) Envelope {
return &AbstractEnvelope{ body: b, sender: s }
}
func NewAnonymous(b interface{}) Envelope {
return &AbstractEnvelope{ body: b, sender: nil }
}
func To(b interface{}, from Sender, to Receiver) Envelope {
return &AbstractEnvelope{ body: b, sender: from, receiver: to }
}