use Addressable interface for generic Send() and action receivers
This commit is contained in:
parent
fbff96dcc0
commit
e55ce25190
5 changed files with 33 additions and 22 deletions
|
@ -59,7 +59,7 @@ func (t *T) TearDownApp(name string) {
|
|||
// give actors time to recieve all messages:
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
//t.Check()
|
||||
lib.Send(t.Ctx, message.SimpleAddress(name), message.Quit)
|
||||
message.Send(t.Ctx, message.SimpleAddress(name), message.Quit)
|
||||
t.Ctx.WaitGroup().Wait()
|
||||
//t.AssertNoUncheckedMessages()
|
||||
}
|
||||
|
|
|
@ -10,19 +10,23 @@ type BaseSpec = baseSpec
|
|||
|
||||
type baseSpec struct {
|
||||
handler lib.ActionHandler
|
||||
receivers []string
|
||||
receivers []lib.Addressable
|
||||
}
|
||||
|
||||
func (spec *baseSpec) Handler() lib.ActionHandler {
|
||||
return spec.handler
|
||||
}
|
||||
|
||||
func (spec *baseSpec) Receivers() []string {
|
||||
func (spec *baseSpec) Receivers() []lib.Addressable {
|
||||
return spec.receivers
|
||||
}
|
||||
|
||||
func Base(hdlr lib.ActionHandler, rcvrs ...string) *baseSpec {
|
||||
return &baseSpec{hdlr, rcvrs}
|
||||
func Base(hdlr lib.ActionHandler, rcvs ...string) *baseSpec {
|
||||
spec := baseSpec{handler: hdlr}
|
||||
for _, rcv := range rcvs {
|
||||
spec.receivers = append(spec.receivers, message.SimpleAddress(rcv))
|
||||
}
|
||||
return &spec
|
||||
}
|
||||
|
||||
// action selection
|
||||
|
@ -89,9 +93,8 @@ func (act *action) Handle() bool {
|
|||
func Forward(act lib.Action) bool {
|
||||
ctx := act.Context()
|
||||
msg := act.Message()
|
||||
for _, rcvr := range act.Spec().Receivers() {
|
||||
addr := message.SimpleAddress(rcvr)
|
||||
lib.Send(ctx, addr, msg)
|
||||
for _, rcv := range act.Spec().Receivers() {
|
||||
message.Send(ctx, rcv, msg)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
|
|
@ -2,9 +2,11 @@ package message
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
lib "git.sr.ht/~cco/go-scopes"
|
||||
"git.sr.ht/~cco/go-scopes/logging"
|
||||
"git.sr.ht/~cco/go-scopes/logging/log"
|
||||
)
|
||||
|
||||
|
@ -124,3 +126,20 @@ func PayloadFromJson[T any](s string) lib.Payload {
|
|||
pl := &payload{new(T)}
|
||||
return pl.FromJson(s)
|
||||
}
|
||||
|
||||
// generic send functionality
|
||||
|
||||
func Send(ctx lib.Context, rcv lib.Addressable, msg lib.Message) {
|
||||
if msg.Sender() == nil {
|
||||
msg.WithSender(ctx)
|
||||
}
|
||||
if cell := rcv.Cell(); cell != nil {
|
||||
cell.Send(msg)
|
||||
} else if svc, ok := ctx.Services()[rcv.Address().Service()]; ok {
|
||||
// TODO: check Address for sid, iid
|
||||
svc.Send(msg)
|
||||
} else {
|
||||
logging.WarnM(ctx, msg).
|
||||
Msg(fmt.Sprintf("scopes.Send: invalid address: %+v", rcv))
|
||||
}
|
||||
}
|
||||
|
|
13
scopes.go
13
scopes.go
|
@ -82,7 +82,7 @@ type Action interface {
|
|||
|
||||
type ActionSpec interface {
|
||||
Handler() ActionHandler
|
||||
Receivers() []string
|
||||
Receivers() []Addressable
|
||||
}
|
||||
|
||||
// procedures and handlers
|
||||
|
@ -132,17 +132,6 @@ func HandleMsg(ctx Context, msg Message) bool {
|
|||
return ctx.Config().MessageHandler()(ctx, msg)
|
||||
}
|
||||
|
||||
func Send(ctx Context, addr Address, msg Message) {
|
||||
if msg.Sender() == nil {
|
||||
msg.WithSender(ctx)
|
||||
}
|
||||
if svc, ok := ctx.Services()[addr.Service()]; ok {
|
||||
// TODO: check Address for sid, iid
|
||||
svc.Send(msg)
|
||||
}
|
||||
// TODO: Warn().Msg("receiver not found")
|
||||
}
|
||||
|
||||
func GetState[St ContextState](ctx Context) St {
|
||||
return ctx.State().(St)
|
||||
}
|
||||
|
|
|
@ -38,14 +38,14 @@ func SendTest(t *testing.T) {
|
|||
ctx := t.Ctx
|
||||
rcvr := message.SimpleAddress("testing")
|
||||
msg := message.SimpleMessage("demo")
|
||||
lib.Send(ctx, rcvr, msg)
|
||||
message.Send(ctx, rcvr, msg)
|
||||
}
|
||||
|
||||
func ClientTest(t *testing.T) {
|
||||
ctx := t.Ctx
|
||||
rcvr := message.SimpleAddress("test-client")
|
||||
msg := message.SimpleMessage("demo")
|
||||
lib.Send(ctx, rcvr, msg)
|
||||
message.Send(ctx, rcvr, msg)
|
||||
}
|
||||
|
||||
// action handlers
|
||||
|
|
Loading…
Add table
Reference in a new issue