add types, end signal

This commit is contained in:
Michele Balistreri 2021-10-18 10:19:09 +03:00
parent d776bbb621
commit decbebd4ff
No known key found for this signature in database
GPG Key ID: E9567DA33A4F791A
2 changed files with 23 additions and 15 deletions

34
flow.go
View File

@ -15,7 +15,7 @@ func NewFlow(storageDir string) (*keycardFlow, error) {
return flow, nil return flow, nil
} }
func (f *keycardFlow) Start(flowType FlowType, params map[string]interface{}) error { func (f *keycardFlow) Start(flowType FlowType, params FlowParams) error {
if f.state != Idle { if f.state != Idle {
return errors.New("already running") return errors.New("already running")
} }
@ -23,12 +23,13 @@ func (f *keycardFlow) Start(flowType FlowType, params map[string]interface{}) er
f.flowType = flowType f.flowType = flowType
f.params = params f.params = params
f.state = Running f.state = Running
f.active = make(chan (struct{}))
go f.runFlow() go f.runFlow()
return nil return nil
} }
func (f *keycardFlow) Resume(params map[string]interface{}) error { func (f *keycardFlow) Resume(params FlowParams) error {
if f.state != Paused { if f.state != Paused {
return errors.New("only paused flows can be resumed") return errors.New("only paused flows can be resumed")
} }
@ -58,9 +59,13 @@ func (f *keycardFlow) Cancel() error {
return nil return nil
} }
func (f *keycardFlow) Wait() {
<-f.active
}
func (f *keycardFlow) runFlow() { func (f *keycardFlow) runFlow() {
repeat := true repeat := true
var result map[string]interface{} var result FlowStatus
for repeat { for repeat {
switch f.flowType { switch f.flowType {
@ -74,30 +79,31 @@ func (f *keycardFlow) runFlow() {
} }
f.state = Idle f.state = Idle
close(f.active)
} }
func (f *keycardFlow) switchFlow() (bool, map[string]interface{}) { func (f *keycardFlow) switchFlow() (bool, FlowStatus) {
kc := f.connect() kc := f.connect()
defer f.closeKeycard(kc) defer f.closeKeycard(kc)
if kc == nil { if kc == nil {
return false, map[string]interface{}{ErrorKey: ErrorConnection} return false, FlowStatus{ErrorKey: ErrorConnection}
} }
switch f.flowType { switch f.flowType {
case GetAppInfo: case GetAppInfo:
return f.getAppInfoFlow(kc) return f.getAppInfoFlow(kc)
default: default:
return false, map[string]interface{}{ErrorKey: ErrorUnknownFlow} return false, FlowStatus{ErrorKey: ErrorUnknownFlow}
} }
} }
func (f *keycardFlow) pause(action string, status map[string]interface{}) { func (f *keycardFlow) pause(action string, status FlowStatus) {
signal.SendEvent(action, status) signal.SendEvent(action, status)
f.state = Paused f.state = Paused
} }
func (f *keycardFlow) pauseAndWait(action string, status map[string]interface{}) bool { func (f *keycardFlow) pauseAndWait(action string, status FlowStatus) bool {
f.pause(action, status) f.pause(action, status)
<-f.wakeUp <-f.wakeUp
@ -122,7 +128,7 @@ func (f *keycardFlow) connect() *keycardContext {
return nil return nil
} }
f.pause(InsertCard, map[string]interface{}{}) f.pause(InsertCard, FlowStatus{})
select { select {
case <-f.wakeUp: case <-f.wakeUp:
if f.state != Cancelling { if f.state != Cancelling {
@ -147,7 +153,7 @@ func (f *keycardFlow) selectKeycard(kc *keycardContext) (bool, error) {
if requiredInstanceUID, ok := f.params[InstanceUID]; ok { if requiredInstanceUID, ok := f.params[InstanceUID]; ok {
if instanceUID := tox(appInfo.InstanceUID); instanceUID != requiredInstanceUID { if instanceUID := tox(appInfo.InstanceUID); instanceUID != requiredInstanceUID {
if f.pauseAndWait(SwapCard, map[string]interface{}{ErrorKey: InstanceUID, InstanceUID: instanceUID}) { if f.pauseAndWait(SwapCard, FlowStatus{ErrorKey: InstanceUID, InstanceUID: instanceUID}) {
return true, nil return true, nil
} else { } else {
return false, errors.New(ErrorCancel) return false, errors.New(ErrorCancel)
@ -157,7 +163,7 @@ func (f *keycardFlow) selectKeycard(kc *keycardContext) (bool, error) {
if requiredKeyUID, ok := f.params[KeyUID]; ok { if requiredKeyUID, ok := f.params[KeyUID]; ok {
if keyUID := tox(appInfo.KeyUID); keyUID != requiredKeyUID { if keyUID := tox(appInfo.KeyUID); keyUID != requiredKeyUID {
if f.pauseAndWait(SwapCard, map[string]interface{}{ErrorKey: KeyUID, KeyUID: keyUID}) { if f.pauseAndWait(SwapCard, FlowStatus{ErrorKey: KeyUID, KeyUID: keyUID}) {
return true, nil return true, nil
} else { } else {
return false, errors.New(ErrorCancel) return false, errors.New(ErrorCancel)
@ -168,14 +174,14 @@ func (f *keycardFlow) selectKeycard(kc *keycardContext) (bool, error) {
return false, nil return false, nil
} }
func (f *keycardFlow) getAppInfoFlow(kc *keycardContext) (bool, map[string]interface{}) { func (f *keycardFlow) getAppInfoFlow(kc *keycardContext) (bool, FlowStatus) {
restart, err := f.selectKeycard(kc) restart, err := f.selectKeycard(kc)
if err != nil { if err != nil {
return false, map[string]interface{}{ErrorKey: err.Error()} return false, FlowStatus{ErrorKey: err.Error()}
} else if restart { } else if restart {
return true, nil return true, nil
} }
return false, map[string]interface{}{ErrorKey: ErrorOK, AppInfo: kc.cmdSet.ApplicationInfo} return false, FlowStatus{ErrorKey: ErrorOK, AppInfo: kc.cmdSet.ApplicationInfo}
} }

View File

@ -1,8 +1,9 @@
package statuskeycardgo package statuskeycardgo
type FlowType int type FlowType int
type FlowParams map[string]interface{}
type FlowStatus map[string]interface{}
type runState int type runState int
type stepState int
const ( const (
GetAppInfo FlowType = iota GetAppInfo FlowType = iota
@ -48,6 +49,7 @@ const (
type keycardFlow struct { type keycardFlow struct {
flowType FlowType flowType FlowType
state runState state runState
active chan (struct{})
wakeUp chan (struct{}) wakeUp chan (struct{})
storage string storage string
params map[string]interface{} params map[string]interface{}