Skip to content

Commit 0b87821

Browse files
committed
Implemented HELLO cmd
1 parent adaf607 commit 0b87821

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

main.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"encoding/json"
2323
"fmt"
2424
"os"
25+
"regexp"
26+
"strconv"
2527
"strings"
2628
"sync"
2729

@@ -42,13 +44,42 @@ func main() {
4244

4345
reader := bufio.NewReader(os.Stdin)
4446
for {
45-
cmd, err := reader.ReadString('\n')
47+
fullCmd, err := reader.ReadString('\n')
4648
if err != nil {
4749
outputError(err)
4850
os.Exit(1)
4951
}
50-
cmd = strings.ToUpper(strings.TrimSpace(cmd))
52+
split := strings.Split(fullCmd, " ")
53+
cmd := strings.ToUpper(strings.TrimSpace(split[0]))
5154
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+
})
5283
case "START":
5384
output(&genericMessageJSON{
5485
EventType: "start",
@@ -147,6 +178,12 @@ func newBoardPortJSON(port *enumerator.PortDetails) *boardPortJSON {
147178
return portJSON
148179
}
149180

181+
type helloMessageJSON struct {
182+
EventType string `json:"eventType"`
183+
ProtocolVersion string `json:"protocolVersion"`
184+
Message string `json:"message"`
185+
}
186+
150187
type genericMessageJSON struct {
151188
EventType string `json:"eventType"`
152189
Error bool `json:"error,omitempty"`

0 commit comments

Comments
 (0)