move HandleMsg() to lib

This commit is contained in:
Helmut Merz 2023-06-07 17:37:39 +02:00
parent d37db07942
commit 08263df110
3 changed files with 8 additions and 4 deletions

View file

@ -8,7 +8,6 @@ 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"
) )
@ -49,7 +48,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 context.HandleMsg(ctx, msg) return lib.HandleMsg(ctx, msg)
case <-ctx.Done(): case <-ctx.Done():
return false return false
} }

View file

@ -23,7 +23,7 @@ func Step(ctx lib.Context) (loop bool) {
//defer ctx.LogCatch("Step") //defer ctx.LogCatch("Step")
select { select {
case msg := <-ctx.Mailbox(): case msg := <-ctx.Mailbox():
loop = ctx.HandleMsg(msg) loop = lib.HandleMsg(ctx, msg)
case <-ctx.Done(): case <-ctx.Done():
loop = false loop = false
} }

View file

@ -4,6 +4,7 @@ package lib
import ( import (
stdlib_context "context" stdlib_context "context"
"fmt"
"sync" "sync"
) )
@ -41,7 +42,6 @@ type Context interface {
State() ContextState State() ContextState
WithState(ContextState) Context WithState(ContextState) Context
Mailbox() chan Message Mailbox() chan Message
HandleMsg(Message) bool
WaitGroup() *sync.WaitGroup WaitGroup() *sync.WaitGroup
Stop() Stop()
} }
@ -79,6 +79,11 @@ func RunCtx(ctx Context, fct Proc) {
}() }()
} }
func HandleMsg(ctx Context, msg Message) bool {
fmt.Println("context.HandleMsg ", msg)
return ctx.Config().MessageHandler()(ctx, msg)
}
func Send(ctx Context, addr Address, msg Message) { func Send(ctx Context, addr Address, msg Message) {
if srv, ok := ctx.Services()[addr.Service()]; ok { if srv, ok := ctx.Services()[addr.Service()]; ok {
srv.Mailbox() <- msg srv.Mailbox() <- msg