From 8b0230da3b8cd50f70abe89e58084d1d4e525b5d Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Wed, 7 Jun 2023 15:35:02 +0200 Subject: [PATCH] message forwarding basically working --- app/app.go | 3 ++- lib/action/action.go | 27 +++++++++++++++++++++++---- lib/context/context.go | 5 +++++ tests/etc/etc.go | 4 +++- 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/app/app.go b/app/app.go index 0018a52..089a600 100644 --- a/app/app.go +++ b/app/app.go @@ -8,6 +8,7 @@ import ( "git.sr.ht/~cco/go-scopes/config" "git.sr.ht/~cco/go-scopes/lib" + "git.sr.ht/~cco/go-scopes/lib/context" "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{}) return false } - return ctx.HandleMsg(msg) + return context.HandleMsg(ctx, msg) case <-ctx.Done(): return false } diff --git a/lib/action/action.go b/lib/action/action.go index 7191b3d..cd0392d 100644 --- a/lib/action/action.go +++ b/lib/action/action.go @@ -1,6 +1,9 @@ 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 { 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 { var acts []lib.Action - // act := action{ctx, spec, msg} - // acts = append(acts, &act) + for _, ac := range ctx.Config().Actions() { + if match(ac, msg) { + for _, spec := range ac.Specs() { + act := action{ctx, spec, msg} + acts = append(acts, &act) + } + } + } return acts } +func match(ac lib.ActionConfig, msg lib.Message) bool { + return ac.Pattern() == msg.Action() + //return false +} + // action type action struct { @@ -55,6 +69,11 @@ func (act *action) Handle() bool { // predefined action handlers 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 } diff --git a/lib/context/context.go b/lib/context/context.go index 1a97c5f..dc4001e 100644 --- a/lib/context/context.go +++ b/lib/context/context.go @@ -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 func (ctx *context) Deadline() (deadline time.Time, ok bool) { diff --git a/tests/etc/etc.go b/tests/etc/etc.go index b62704d..19ae1e1 100644 --- a/tests/etc/etc.go +++ b/tests/etc/etc.go @@ -14,7 +14,9 @@ func Config() lib.Config { b := config.MakeBase cfg := app.Cfg{ Base: b("testing", testing.Start). - WithMessageHandler(core.HandleMessage), + WithAction("demo", + action.BaseSpec(action.Forward, + []string{"test-receiver"})).(*config.Base), Home: ovr(".", HOME), AppType: "standard", }