Skip to content

Commit

Permalink
Allow case-insensitive ok or OK replies from pluggable discoverie…
Browse files Browse the repository at this point in the history
…s and monitors (#1633)

* Allow case-insensitive 'ok'/'OK' replies from discoveries and monitors

* Factor the same messages to reduce translations strings

* Better error messages for discoveries and monitor
  • Loading branch information
cmaglie committed Jan 24, 2022
1 parent 569e194 commit 5f8cb4b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
32 changes: 21 additions & 11 deletions arduino/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,11 @@ func (disc *PluggableDiscovery) Run() (err error) {
if msg, err := disc.waitMessage(time.Second * 10); err != nil {
return fmt.Errorf(tr("calling %[1]s: %[2]w"), "HELLO", err)
} else if msg.EventType != "hello" {
return errors.Errorf(tr("communication out of sync, expected 'hello', received '%s'"), msg.EventType)
} else if msg.Message != "OK" || msg.Error {
return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "hello", msg.EventType)
} else if msg.Error {
return errors.Errorf(tr("command failed: %s"), msg.Message)
} else if strings.ToUpper(msg.Message) != "OK" {
return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "OK", msg.Message)
} else if msg.ProtocolVersion > 1 {
return errors.Errorf(tr("protocol version not supported: requested 1, got %d"), msg.ProtocolVersion)
}
Expand All @@ -332,9 +334,11 @@ func (disc *PluggableDiscovery) Start() error {
if msg, err := disc.waitMessage(time.Second * 10); err != nil {
return fmt.Errorf(tr("calling %[1]s: %[2]w"), "START", err)
} else if msg.EventType != "start" {
return errors.Errorf(tr("communication out of sync, expected 'start', received '%s'"), msg.EventType)
} else if msg.Message != "OK" || msg.Error {
return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "start", msg.EventType)
} else if msg.Error {
return errors.Errorf(tr("command failed: %s"), msg.Message)
} else if strings.ToUpper(msg.Message) != "OK" {
return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "OK", msg.Message)
}
disc.statusMutex.Lock()
defer disc.statusMutex.Unlock()
Expand All @@ -352,9 +356,11 @@ func (disc *PluggableDiscovery) Stop() error {
if msg, err := disc.waitMessage(time.Second * 10); err != nil {
return fmt.Errorf(tr("calling %[1]s: %[2]w"), "STOP", err)
} else if msg.EventType != "stop" {
return errors.Errorf(tr("communication out of sync, expected 'stop', received '%s'"), msg.EventType)
} else if msg.Message != "OK" || msg.Error {
return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "stop", msg.EventType)
} else if msg.Error {
return errors.Errorf(tr("command failed: %s"), msg.Message)
} else if strings.ToUpper(msg.Message) != "OK" {
return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "OK", msg.Message)
}
disc.statusMutex.Lock()
defer disc.statusMutex.Unlock()
Expand All @@ -375,9 +381,11 @@ func (disc *PluggableDiscovery) Quit() error {
if msg, err := disc.waitMessage(time.Second * 10); err != nil {
return fmt.Errorf(tr("calling %[1]s: %[2]w"), "QUIT", err)
} else if msg.EventType != "quit" {
return errors.Errorf(tr("communication out of sync, expected 'quit', received '%s'"), msg.EventType)
} else if msg.Message != "OK" || msg.Error {
return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "quit", msg.EventType)
} else if msg.Error {
return errors.Errorf(tr("command failed: %s"), msg.Message)
} else if strings.ToUpper(msg.Message) != "OK" {
return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "OK", msg.Message)
}
disc.killProcess()
return nil
Expand All @@ -392,7 +400,7 @@ func (disc *PluggableDiscovery) List() ([]*Port, error) {
if msg, err := disc.waitMessage(time.Second * 10); err != nil {
return nil, fmt.Errorf(tr("calling %[1]s: %[2]w"), "LIST", err)
} else if msg.EventType != "list" {
return nil, errors.Errorf(tr("communication out of sync, expected 'list', received '%s'"), msg.EventType)
return nil, errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "list", msg.EventType)
} else if msg.Error {
return nil, errors.Errorf(tr("command failed: %s"), msg.Message)
} else {
Expand All @@ -415,9 +423,11 @@ func (disc *PluggableDiscovery) StartSync(size int) (<-chan *Event, error) {
if msg, err := disc.waitMessage(time.Second * 10); err != nil {
return nil, fmt.Errorf(tr("calling %[1]s: %[2]w"), "START_SYNC", err)
} else if msg.EventType != "start_sync" {
return nil, errors.Errorf(tr("communication out of sync, expected 'start_sync', received '%s'"), msg.EventType)
} else if msg.Message != "OK" || msg.Error {
return nil, errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "start_sync", msg.EventType)
} else if msg.Error {
return nil, errors.Errorf(tr("command failed: %s"), msg.Message)
} else if strings.ToUpper(msg.Message) != "OK" {
return nil, errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "OK", msg.Message)
}

disc.statusMutex.Lock()
Expand Down
5 changes: 4 additions & 1 deletion arduino/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,12 @@ func (mon *PluggableMonitor) waitMessage(timeout time.Duration, expectedEvt stri
if msg.EventType != expectedEvt {
return msg, fmt.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), expectedEvt, msg.EventType)
}
if msg.Message != "OK" || msg.Error {
if msg.Error {
return msg, fmt.Errorf(tr("command '%[1]s' failed: %[2]s"), expectedEvt, msg.Message)
}
if strings.ToUpper(msg.Message) != "OK" {
return msg, fmt.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "OK", msg.Message)
}
return msg, nil
}

Expand Down

0 comments on commit 5f8cb4b

Please sign in to comment.