message forwarding basically working
This commit is contained in:
parent
5c6a7c76d8
commit
8b0230da3b
4 changed files with 33 additions and 6 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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",
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue