directly use the persistedEngine

This commit is contained in:
Alfred Kamanda 2024-08-15 13:49:36 +03:00
parent 87c8e83f59
commit fb7b35af74
7 changed files with 55 additions and 17 deletions

4
.gitignore vendored
View File

@ -1,4 +1,6 @@
**/*.env **/*.env
covprofile covprofile
go.work* go.work*
services/registration/*.bin **/*/*.bin
**/*/.state/
cmd/.state/

View File

@ -6,8 +6,10 @@ import (
"fmt" "fmt"
"os" "os"
"path" "path"
"git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/cache"
"git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/persist"
"git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/resource"
"git.defalsify.org/vise.git/state" "git.defalsify.org/vise.git/state"
) )
@ -17,7 +19,7 @@ const (
) )
type fsData struct { type fsData struct {
path string path string
} }
func (fsd *fsData) SetLanguageSelected(ctx context.Context, sym string, input []byte) (resource.Result, error) { func (fsd *fsData) SetLanguageSelected(ctx context.Context, sym string, input []byte) (resource.Result, error) {
@ -36,33 +38,54 @@ func (fsd *fsData) SetLanguageSelected(ctx context.Context, sym string, input []
return res, nil return res, nil
} }
var (
scriptDir = path.Join("services", "registration")
)
func main() { func main() {
var dir string var dir string
var root string var root string
var size uint var size uint
var sessionId string var sessionId string
var persist bool
flag.StringVar(&dir, "d", ".", "resource dir to read from") flag.StringVar(&dir, "d", ".", "resource dir to read from")
flag.UintVar(&size, "s", 0, "max size of output") flag.UintVar(&size, "s", 0, "max size of output")
flag.StringVar(&root, "root", "root", "entry point symbol") flag.StringVar(&root, "root", "root", "entry point symbol")
flag.StringVar(&sessionId, "session-id", "default", "session id") flag.StringVar(&sessionId, "session-id", "default", "session id")
flag.BoolVar(&persist, "persist", false, "use state persistence")
flag.Parse() flag.Parse()
fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, dir) fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, dir)
fp := path.Join(dir, sessionId) ctx := context.Background()
st := state.NewState(3)
rfs := resource.NewFsResource(scriptDir)
ca := cache.NewCache()
cfg := engine.Config{
Root: "root",
SessionId: sessionId,
}
dp := path.Join(scriptDir, ".state")
err := os.MkdirAll(dp, 0700)
if err != nil {
fmt.Fprintf(os.Stderr, "state dir create exited with error: %v\n", err)
os.Exit(1)
}
pr := persist.NewFsPersister(dp)
en, err := engine.NewPersistedEngine(ctx, cfg, pr, rfs)
if err != nil {
pr = pr.WithContent(&st, ca)
err = pr.Save(cfg.SessionId)
en, err = engine.NewPersistedEngine(ctx, cfg, pr, rfs)
if err != nil {
fmt.Fprintf(os.Stderr, "engine create exited with error: %v\n", err)
os.Exit(1)
}
}
fp := path.Join(dp, sessionId)
fs := &fsData{ fs := &fsData{
path: fp, path: fp,
} }
rfs.AddLocalFunc("select_language", fs.SetLanguageSelected)
ctx := context.Background()
en, rs, err := engine.NewSizedEnginee(dir, uint32(size), persist, &sessionId)
rs.AddLocalFunc("select_language", fs.SetLanguageSelected)
if err != nil {
fmt.Fprintf(os.Stderr, "engine create error: %v", err)
os.Exit(1)
}
cont, err := en.Init(ctx) cont, err := en.Init(ctx)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "engine init exited with error: %v\n", err) fmt.Fprintf(os.Stderr, "engine init exited with error: %v\n", err)
@ -74,14 +97,17 @@ func main() {
fmt.Fprintf(os.Stderr, "dead init write error: %v\n", err) fmt.Fprintf(os.Stderr, "dead init write error: %v\n", err)
os.Exit(1) os.Exit(1)
} }
err = en.Finish()
if err != nil {
fmt.Fprintf(os.Stderr, "engine finish error: %v\n", err)
os.Exit(1)
}
os.Stdout.Write([]byte{0x0a}) os.Stdout.Write([]byte{0x0a})
os.Exit(0) os.Exit(0)
} }
err = engine.Loop(ctx, en, os.Stdin, os.Stdout) err = engine.Loop(ctx, en, os.Stdin, os.Stdout)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "loop exited with error: %v\n", err) fmt.Fprintf(os.Stderr, "loop exited with error: %v\n", err)
os.Exit(1) os.Exit(1)
} }
} }

1
go-vise Submodule

@ -0,0 +1 @@
Subproject commit ef9a4c3073e7e878846693d4aa7fd0cf1cc977a7

View File

@ -0,0 +1 @@
Welcome to Sarafu Network!

View File

@ -0,0 +1,5 @@
MOUT english 0
MOUT kiswahili 1
HALT
INCMP terms 0
INCMP terms 1

View File

@ -0,0 +1 @@
Karibu Sarafu Network!

View File

@ -1,2 +1,4 @@
LOAD select_language 0
RELOAD select_language
MOUT yes 0 MOUT yes 0
MOUT no 1 MOUT no 1