From 22e8759369cb5f77ed659fc338c12e5464d1d93e Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Mon, 5 Jun 2023 18:33:21 +0200 Subject: [PATCH] work in progress: set up standard (core) processing chain (Step, HandleMessage, ...) --- config/config.go | 23 +++++++++++++++++++---- lib/context/context.go | 6 ++++++ lib/core/core.go | 21 +++++++++++++++++++++ lib/lib.go | 13 +++++++++++-- lib/message/message.go | 24 ++++++++++++++++++++++++ tests/etc/etc.go | 4 +++- 6 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 lib/core/core.go diff --git a/config/config.go b/config/config.go index a963534..b118c6a 100644 --- a/config/config.go +++ b/config/config.go @@ -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 } diff --git a/lib/context/context.go b/lib/context/context.go index 11b3a6e..ce472f9 100644 --- a/lib/context/context.go +++ b/lib/context/context.go @@ -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) diff --git a/lib/core/core.go b/lib/core/core.go new file mode 100644 index 0000000..b9e4939 --- /dev/null +++ b/lib/core/core.go @@ -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 +} diff --git a/lib/lib.go b/lib/lib.go index 2f1ecf5..918433b 100644 --- a/lib/lib.go +++ b/lib/lib.go @@ -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 diff --git a/lib/message/message.go b/lib/message/message.go index 7048aae..6bae981 100644 --- a/lib/message/message.go +++ b/lib/message/message.go @@ -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, + } +} diff --git a/tests/etc/etc.go b/tests/etc/etc.go index 5766d1b..1a5b246 100644 --- a/tests/etc/etc.go +++ b/tests/etc/etc.go @@ -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", }