Skip to content

Commit 51184d9

Browse files
committed
Implemented ERROR protocol
1 parent 0b87821 commit 51184d9

File tree

5 files changed

+64
-45
lines changed

5 files changed

+64
-45
lines changed

main.go

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ func main() {
4646
for {
4747
fullCmd, err := reader.ReadString('\n')
4848
if err != nil {
49-
outputError(err)
49+
output(&genericMessageJSON{
50+
EventType: "command_error",
51+
Error: true,
52+
Message: err.Error(),
53+
})
5054
os.Exit(1)
5155
}
5256
split := strings.Split(fullCmd, " ")
@@ -104,18 +108,27 @@ func main() {
104108
os.Exit(0)
105109
case "START_SYNC":
106110
if syncStarted {
111+
// sync already started, just acknowledge again...
112+
output(&genericMessageJSON{
113+
EventType: "start_sync",
114+
Message: "OK",
115+
})
107116
} else if close, err := startSync(); err != nil {
108-
outputError(err)
117+
output(&genericMessageJSON{
118+
EventType: "start_sync",
119+
Error: true,
120+
Message: err.Error(),
121+
})
109122
} else {
110123
syncCloseChan = close
111124
syncStarted = true
112125
}
126+
default:
113127
output(&genericMessageJSON{
114-
EventType: "start_sync",
115-
Message: "OK",
128+
EventType: "command_error",
129+
Error: true,
130+
Message: fmt.Sprintf("Command %s not supported", cmd),
116131
})
117-
default:
118-
outputError(fmt.Errorf("Command %s not supported", cmd))
119132
}
120133
}
121134
}
@@ -137,23 +150,22 @@ type listOutputJSON struct {
137150
func outputList() {
138151
list, err := enumerator.GetDetailedPortsList()
139152
if err != nil {
140-
outputError(err)
153+
output(&genericMessageJSON{
154+
EventType: "list",
155+
Error: true,
156+
Message: err.Error(),
157+
})
141158
return
142159
}
143160
portsJSON := []*boardPortJSON{}
144161
for _, port := range list {
145162
portJSON := newBoardPortJSON(port)
146163
portsJSON = append(portsJSON, portJSON)
147164
}
148-
d, err := json.MarshalIndent(&listOutputJSON{
165+
output(&listOutputJSON{
149166
EventType: "list",
150167
Ports: portsJSON,
151-
}, "", " ")
152-
if err != nil {
153-
outputError(err)
154-
return
155-
}
156-
syncronizedPrintLn(string(d))
168+
})
157169
}
158170

159171
func newBoardPortJSON(port *enumerator.PortDetails) *boardPortJSON {
@@ -193,20 +205,16 @@ type genericMessageJSON struct {
193205
func output(msg interface{}) {
194206
d, err := json.MarshalIndent(msg, "", " ")
195207
if err != nil {
196-
outputError(err)
208+
output(&genericMessageJSON{
209+
EventType: "command_error",
210+
Error: true,
211+
Message: err.Error(),
212+
})
197213
} else {
198214
syncronizedPrintLn(string(d))
199215
}
200216
}
201217

202-
func outputError(err error) {
203-
output(&genericMessageJSON{
204-
EventType: "command_error",
205-
Error: true,
206-
Message: err.Error(),
207-
})
208-
}
209-
210218
var stdoutMutext sync.Mutex
211219

212220
func syncronizedPrintLn(a ...interface{}) {

sync.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,7 @@
1717

1818
package main
1919

20-
import "encoding/json"
21-
2220
type syncOutputJSON struct {
2321
EventType string `json:"eventType"`
2422
Port *boardPortJSON `json:"port"`
2523
}
26-
27-
func outputSyncMessage(message *syncOutputJSON) {
28-
d, err := json.MarshalIndent(message, "", " ")
29-
if err != nil {
30-
outputError(err)
31-
} else {
32-
syncronizedPrintLn(string(d))
33-
}
34-
}

sync_darwin.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,12 @@ func startSync() (chan<- bool, error) {
5252
if err != nil {
5353
return nil, err
5454
}
55+
output(&genericMessageJSON{
56+
EventType: "start_sync",
57+
Message: "OK",
58+
})
5559
for _, port := range current {
56-
outputSyncMessage(&syncOutputJSON{
60+
output(&syncOutputJSON{
5761
EventType: "add",
5862
Port: newBoardPortJSON(port),
5963
})
@@ -100,7 +104,11 @@ func startSync() (chan<- bool, error) {
100104
continue
101105
}
102106
if err != nil {
103-
outputError(fmt.Errorf("error decoding START_SYNC event: %s", err))
107+
output(&genericMessageJSON{
108+
EventType: "start_sync",
109+
Error: true,
110+
Message: fmt.Sprintf("error decoding START_SYNC event: %s", err),
111+
})
104112
}
105113
// if there is an event retry up to 5 times
106114
if n > 0 {
@@ -115,7 +123,7 @@ func startSync() (chan<- bool, error) {
115123

116124
for _, port := range current {
117125
if !portListHas(updates, port) {
118-
outputSyncMessage(&syncOutputJSON{
126+
output(&syncOutputJSON{
119127
EventType: "remove",
120128
Port: &boardPortJSON{Address: port.Name},
121129
})
@@ -124,7 +132,7 @@ func startSync() (chan<- bool, error) {
124132

125133
for _, port := range updates {
126134
if !portListHas(current, port) {
127-
outputSyncMessage(&syncOutputJSON{
135+
output(&syncOutputJSON{
128136
EventType: "add",
129137
Port: newBoardPortJSON(port),
130138
})

sync_linux.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,14 @@ func startSync() (chan<- bool, error) {
4343
syncReader.Close()
4444
}()
4545

46+
output(&genericMessageJSON{
47+
EventType: "start_sync",
48+
Message: "OK",
49+
})
50+
4651
// Ouput initial port state
4752
for _, port := range current {
48-
outputSyncMessage(&syncOutputJSON{
53+
output(&syncOutputJSON{
4954
EventType: "add",
5055
Port: newBoardPortJSON(port),
5156
})
@@ -62,7 +67,12 @@ func startSync() (chan<- bool, error) {
6267
for {
6368
evt, err := dec.Decode()
6469
if err != nil {
65-
outputError(fmt.Errorf("error decoding START_SYNC event: %s", err))
70+
output(&genericMessageJSON{
71+
EventType: "start_sync",
72+
Error: true,
73+
Message: fmt.Sprintf("error decoding START_SYNC event: %s", err),
74+
})
75+
6676
// TODO: output "stop" msg? close?
6777
return
6878
}
@@ -77,7 +87,7 @@ func startSync() (chan<- bool, error) {
7787
}
7888
for _, port := range portList {
7989
if port.IsUSB && port.Name == changedPort {
80-
outputSyncMessage(&syncOutputJSON{
90+
output(&syncOutputJSON{
8191
EventType: "add",
8292
Port: newBoardPortJSON(port),
8393
})
@@ -86,7 +96,7 @@ func startSync() (chan<- bool, error) {
8696
}
8797
}
8898
if evt.Action == "remove" {
89-
outputSyncMessage(&syncOutputJSON{
99+
output(&syncOutputJSON{
90100
EventType: "remove",
91101
Port: &boardPortJSON{Address: changedPort},
92102
})

sync_windows.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,12 @@ func startSync() (chan<- bool, error) {
117117
fmt.Println(err)
118118
return
119119
}
120+
output(&genericMessageJSON{
121+
EventType: "start_sync",
122+
Message: "OK",
123+
})
120124
for _, port := range current {
121-
outputSyncMessage(&syncOutputJSON{
125+
output(&syncOutputJSON{
122126
EventType: "add",
123127
Port: newBoardPortJSON(port),
124128
})
@@ -164,7 +168,7 @@ func startSync() (chan<- bool, error) {
164168

165169
for _, port := range current {
166170
if !portListHas(updates, port) {
167-
outputSyncMessage(&syncOutputJSON{
171+
output(&syncOutputJSON{
168172
EventType: "remove",
169173
Port: &boardPortJSON{Address: port.Name},
170174
})
@@ -173,7 +177,7 @@ func startSync() (chan<- bool, error) {
173177

174178
for _, port := range updates {
175179
if !portListHas(current, port) {
176-
outputSyncMessage(&syncOutputJSON{
180+
output(&syncOutputJSON{
177181
EventType: "add",
178182
Port: newBoardPortJSON(port),
179183
})

0 commit comments

Comments
 (0)