mirror of
https://github.com/status-im/status-keycard-go.git
synced 2025-01-18 18:31:56 +00:00
add types, end signal
This commit is contained in:
parent
d776bbb621
commit
decbebd4ff
34
flow.go
34
flow.go
@ -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}
|
||||||
}
|
}
|
||||||
|
@ -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{}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user