message forwarding basically working

This commit is contained in:
Helmut Merz 2023-06-07 15:35:02 +02:00
parent 5c6a7c76d8
commit 8b0230da3b
4 changed files with 33 additions and 6 deletions

View file

@ -8,6 +8,7 @@ import (
"git.sr.ht/~cco/go-scopes/config" "git.sr.ht/~cco/go-scopes/config"
"git.sr.ht/~cco/go-scopes/lib" "git.sr.ht/~cco/go-scopes/lib"
"git.sr.ht/~cco/go-scopes/lib/context"
"git.sr.ht/~cco/go-scopes/lib/message" "git.sr.ht/~cco/go-scopes/lib/message"
) )
@ -48,7 +49,7 @@ func step(ctx lib.Context, sig <-chan os.Signal) bool {
//ctx.LogInfo("Dispatcher stopped", m.Map{}) //ctx.LogInfo("Dispatcher stopped", m.Map{})
return false return false
} }
return ctx.HandleMsg(msg) return context.HandleMsg(ctx, msg)
case <-ctx.Done(): case <-ctx.Done():
return false return false
} }

View file

@ -1,6 +1,9 @@
package action package action
import "git.sr.ht/~cco/go-scopes/lib" import (
"git.sr.ht/~cco/go-scopes/lib"
"git.sr.ht/~cco/go-scopes/lib/message"
)
type baseSpec struct { type baseSpec struct {
handler lib.ActionHandler handler lib.ActionHandler
@ -23,11 +26,22 @@ func BaseSpec(hdlr lib.ActionHandler, rcvrs []string) *baseSpec {
func Select(ctx lib.Context, msg lib.Message) []lib.Action { func Select(ctx lib.Context, msg lib.Message) []lib.Action {
var acts []lib.Action var acts []lib.Action
// act := action{ctx, spec, msg} for _, ac := range ctx.Config().Actions() {
// acts = append(acts, &act) if match(ac, msg) {
for _, spec := range ac.Specs() {
act := action{ctx, spec, msg}
acts = append(acts, &act)
}
}
}
return acts return acts
} }
func match(ac lib.ActionConfig, msg lib.Message) bool {
return ac.Pattern() == msg.Action()
//return false
}
// action // action
type action struct { type action struct {
@ -55,6 +69,11 @@ func (act *action) Handle() bool {
// predefined action handlers // predefined action handlers
func Forward(act lib.Action) bool { func Forward(act lib.Action) bool {
// Send(ctx, addr. msg) ctx := act.Context()
msg := act.Message()
for _, rcvr := range act.Spec().Receivers() {
addr := message.SimpleAddress(rcvr)
message.Send(ctx, addr, msg)
}
return true return true
} }

View file

@ -78,6 +78,11 @@ func makeCtx(cfg lib.Config, parent Context) *context {
} }
} }
func HandleMsg(ctx lib.Context, msg lib.Message) bool {
fmt.Println("context.HandleMsg ", msg)
return ctx.Config().MessageHandler()(ctx, msg)
}
// implement interface context.Context from standard library // implement interface context.Context from standard library
func (ctx *context) Deadline() (deadline time.Time, ok bool) { func (ctx *context) Deadline() (deadline time.Time, ok bool) {

View file

@ -14,7 +14,9 @@ func Config() lib.Config {
b := config.MakeBase b := config.MakeBase
cfg := app.Cfg{ cfg := app.Cfg{
Base: b("testing", testing.Start). Base: b("testing", testing.Start).
WithMessageHandler(core.HandleMessage), WithAction("demo",
action.BaseSpec(action.Forward,
[]string{"test-receiver"})).(*config.Base),
Home: ovr(".", HOME), Home: ovr(".", HOME),
AppType: "standard", AppType: "standard",
} }