File: fsm.go

package info (click to toggle)
golang-github-katalix-go-l2tp 0.1.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 632 kB
  • sloc: ansic: 127; sh: 10; makefile: 4
file content (35 lines) | stat: -rw-r--r-- 583 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package l2tp

import (
	"fmt"
)

type fsmCallback func(args []interface{})

type eventDesc struct {
	from, to string
	events   []string
	cb       fsmCallback
}

type fsm struct {
	current string
	table   []eventDesc
}

func (f *fsm) handleEvent(e string, args ...interface{}) error {
	for _, t := range f.table {
		if f.current == t.from {
			for _, event := range t.events {
				if e == event {
					f.current = t.to
					if t.cb != nil {
						t.cb(args)
					}
					return nil
				}
			}
		}
	}
	return fmt.Errorf("no transition defined for event %v in state %v", e, f.current)
}