@@ -22,6 +22,8 @@ import (
22
22
"encoding/json"
23
23
"fmt"
24
24
"os"
25
+ "regexp"
26
+ "strconv"
25
27
"strings"
26
28
"sync"
27
29
@@ -42,13 +44,42 @@ func main() {
42
44
43
45
reader := bufio .NewReader (os .Stdin )
44
46
for {
45
- cmd , err := reader .ReadString ('\n' )
47
+ fullCmd , err := reader .ReadString ('\n' )
46
48
if err != nil {
47
49
outputError (err )
48
50
os .Exit (1 )
49
51
}
50
- cmd = strings .ToUpper (strings .TrimSpace (cmd ))
52
+ split := strings .Split (fullCmd , " " )
53
+ cmd := strings .ToUpper (strings .TrimSpace (split [0 ]))
51
54
switch cmd {
55
+ case "HELLO" :
56
+ re := regexp .MustCompile (`(\d+) "([^"]+)"` )
57
+ matches := re .FindStringSubmatch (fullCmd [6 :])
58
+ if len (matches ) != 3 {
59
+ output (& genericMessageJSON {
60
+ EventType : "command_error" ,
61
+ Error : true ,
62
+ Message : "Invalid HELLO command" ,
63
+ })
64
+ continue
65
+ }
66
+ // userAgent := matches[2]
67
+ // reqProtocolVersion, err := strconv.ParseUint(matches[1], 10, 64)
68
+ _ , err := strconv .ParseUint (matches [1 ], 10 , 64 )
69
+ if err != nil {
70
+ output (& genericMessageJSON {
71
+ EventType : "command_error" ,
72
+ Error : true ,
73
+ Message : "Invalid protocol version: " + matches [2 ],
74
+ })
75
+ }
76
+ // fmt.Println("User agent:", userAgent)
77
+ // fmt.Println("Req. Protocol version:", reqProtocolVersion)
78
+ output (& helloMessageJSON {
79
+ EventType : "hello" ,
80
+ ProtocolVersion : "1" , // Protocol version 1 is the only supported for now...
81
+ Message : "OK" ,
82
+ })
52
83
case "START" :
53
84
output (& genericMessageJSON {
54
85
EventType : "start" ,
@@ -147,6 +178,12 @@ func newBoardPortJSON(port *enumerator.PortDetails) *boardPortJSON {
147
178
return portJSON
148
179
}
149
180
181
+ type helloMessageJSON struct {
182
+ EventType string `json:"eventType"`
183
+ ProtocolVersion string `json:"protocolVersion"`
184
+ Message string `json:"message"`
185
+ }
186
+
150
187
type genericMessageJSON struct {
151
188
EventType string `json:"eventType"`
152
189
Error bool `json:"error,omitempty"`
0 commit comments