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 // definitions
type Base struct { type Base struct {
name string name string
starter lib.StartProc starter lib.StartProc
actions []lib.ActionConfig step lib.StepProc
children []lib.Config msgHandler lib.MessageHandler
actions []lib.ActionConfig
children []lib.Config
} }
func (cfg *Base) Name() string { func (cfg *Base) Name() string {
@ -32,6 +34,19 @@ func (cfg *Base) Starter() lib.StartProc {
return cfg.starter 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 { func (cfg *Base) Actions() []lib.ActionConfig {
return cfg.actions return cfg.actions
} }

View file

@ -53,6 +53,12 @@ func (ctx *context) Mailbox() chan lib.Message {
return ctx.mailbox 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 { func (ctx *context) HandleMsg(msg lib.Message) bool {
fmt.Println("HandleMsg ", msg) fmt.Println("HandleMsg ", msg)
// return ctx.Config().MessageHandler()(ctx, 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 { type Config interface {
Name() string Name() string
Starter() StartProc Starter() StartProc
Step() StepProc
MessageHandler() MessageHandler
Actions() []ActionConfig Actions() []ActionConfig
Children() []Config Children() []Config
Add(Config) Add(Config)
@ -38,13 +40,19 @@ type Context interface {
State() ContextState State() ContextState
WithState(ContextState) Context WithState(ContextState) Context
Mailbox() chan Message Mailbox() chan Message
Send(Address, Message)
HandleMsg(Message) bool HandleMsg(Message) bool
WaitGroup() *sync.WaitGroup WaitGroup() *sync.WaitGroup
Stop() Stop()
} }
type Address interface {
Service() string
}
type Message interface { type Message interface {
Action() string Action() string
Sender() Address
} }
type Action interface { type Action interface {
@ -55,8 +63,9 @@ type Action interface {
type Proc = func(Context) type Proc = func(Context)
type StartProc = Proc type StartProc = Proc
type MessageHandler = func(Context, Message) type StepProc = func(Context) bool
type ActionHandler = func(Action) type MessageHandler = func(Context, Message) bool
type ActionHandler = func(Action) bool
// async Runners // async Runners

View file

@ -1,11 +1,35 @@
package message package message
import "git.sr.ht/~cco/go-scopes/lib"
// Message implementation(s)
type strMessage string type strMessage string
func (msg strMessage) Action() string { func (msg strMessage) Action() string {
return string(msg) return string(msg)
} }
func (msg strMessage) Sender() lib.Address {
return nil
}
func StrMessage(action string) strMessage { func StrMessage(action string) strMessage {
return strMessage(action) 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/app"
"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/core"
"git.sr.ht/~cco/go-scopes/testing" "git.sr.ht/~cco/go-scopes/testing"
) )
@ -11,7 +12,8 @@ func Config() lib.Config {
ovr := Overrides().Use ovr := Overrides().Use
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),
Home: ovr(".", HOME), Home: ovr(".", HOME),
AppType: "standard", AppType: "standard",
} }