work in progress: set up standard (core) processing chain (Step, HandleMessage, ...)

This commit is contained in:
Helmut Merz 2023-06-05 18:33:21 +02:00
parent c182385353
commit 22e8759369
6 changed files with 84 additions and 7 deletions

View file

@ -18,10 +18,12 @@ func Start(ctx lib.Context) {
// definitions
type Base struct {
name string
starter lib.StartProc
actions []lib.ActionConfig
children []lib.Config
name string
starter lib.StartProc
step lib.StepProc
msgHandler lib.MessageHandler
actions []lib.ActionConfig
children []lib.Config
}
func (cfg *Base) Name() string {
@ -32,6 +34,19 @@ func (cfg *Base) Starter() lib.StartProc {
return cfg.starter
}
func (cfg *Base) Step() lib.StepProc {
return cfg.step
}
func (cfg *Base) MessageHandler() lib.MessageHandler {
return cfg.msgHandler
}
func (cfg *Base) WithMessageHandler(hdlr lib.MessageHandler) *Base {
cfg.msgHandler = hdlr
return cfg
}
func (cfg *Base) Actions() []lib.ActionConfig {
return cfg.actions
}

View file

@ -53,6 +53,12 @@ func (ctx *context) Mailbox() chan lib.Message {
return ctx.mailbox
}
func (ctx *context) Send(rcvr lib.Address, msg lib.Message) {
if srv, ok := ctx.Services()[rcvr.Service()]; ok {
srv.Mailbox() <- msg
}
}
func (ctx *context) HandleMsg(msg lib.Message) bool {
fmt.Println("HandleMsg ", msg)
// return ctx.Config().MessageHandler()(ctx, msg)

21
lib/core/core.go Normal file
View file

@ -0,0 +1,21 @@
package core
import "git.sr.ht/~cco/go-scopes/lib"
func Start(ctx lib.Context) {
Listen(ctx)
}
func Listen(ctx lib.Context) {
step := ctx.Config().Step()
for step(ctx) {
}
}
func Step(ctx lib.Context) bool {
return true
}
func HandleMessage(ctx lib.Context, msg lib.Message) bool {
return true
}

View file

@ -20,6 +20,8 @@ type ActionConfig interface {
type Config interface {
Name() string
Starter() StartProc
Step() StepProc
MessageHandler() MessageHandler
Actions() []ActionConfig
Children() []Config
Add(Config)
@ -38,13 +40,19 @@ type Context interface {
State() ContextState
WithState(ContextState) Context
Mailbox() chan Message
Send(Address, Message)
HandleMsg(Message) bool
WaitGroup() *sync.WaitGroup
Stop()
}
type Address interface {
Service() string
}
type Message interface {
Action() string
Sender() Address
}
type Action interface {
@ -55,8 +63,9 @@ type Action interface {
type Proc = func(Context)
type StartProc = Proc
type MessageHandler = func(Context, Message)
type ActionHandler = func(Action)
type StepProc = func(Context) bool
type MessageHandler = func(Context, Message) bool
type ActionHandler = func(Action) bool
// async Runners

View file

@ -1,11 +1,35 @@
package message
import "git.sr.ht/~cco/go-scopes/lib"
// Message implementation(s)
type strMessage string
func (msg strMessage) Action() string {
return string(msg)
}
func (msg strMessage) Sender() lib.Address {
return nil
}
func StrMessage(action string) strMessage {
return strMessage(action)
}
// Address implementation
type address struct {
service string
}
func (addr *address) Service() string {
return addr.service
}
func CreateAddress(srv string) *address {
return &address{
service: srv,
}
}

View file

@ -4,6 +4,7 @@ import (
"git.sr.ht/~cco/go-scopes/app"
"git.sr.ht/~cco/go-scopes/config"
"git.sr.ht/~cco/go-scopes/lib"
"git.sr.ht/~cco/go-scopes/lib/core"
"git.sr.ht/~cco/go-scopes/testing"
)
@ -11,7 +12,8 @@ func Config() lib.Config {
ovr := Overrides().Use
b := config.MakeBase
cfg := app.Cfg{
Base: b("testing", testing.Start),
Base: b("testing", testing.Start).
WithMessageHandler(core.HandleMessage),
Home: ovr(".", HOME),
AppType: "standard",
}