69 lines
1.1 KiB
Go
69 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"encoding/gob"
|
|
"fmt"
|
|
"log"
|
|
"net"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"mcquay.me/robo"
|
|
)
|
|
|
|
const usage = "rsend <hostname>"
|
|
|
|
func main() {
|
|
if len(os.Args) != 2 {
|
|
fmt.Fprintf(os.Stderr, "%s\n", usage)
|
|
os.Exit(1)
|
|
}
|
|
|
|
var err error
|
|
var x, y float64
|
|
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", os.Args[1], robo.Port))
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "%v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
enc := gob.NewEncoder(conn)
|
|
|
|
s := bufio.NewScanner(os.Stdin)
|
|
point := robo.Point{}
|
|
for s.Scan() {
|
|
txt := s.Text()
|
|
parts := strings.Split(txt, ",")
|
|
if len(parts) != 2 {
|
|
err = fmt.Errorf("bad parse from stdin: %q", txt)
|
|
break
|
|
}
|
|
x, err = strconv.ParseFloat(parts[0], 32)
|
|
if err != nil {
|
|
break
|
|
}
|
|
y, err = strconv.ParseFloat(parts[1], 32)
|
|
if err != nil {
|
|
break
|
|
}
|
|
point.X, point.Y = x, y
|
|
|
|
log.Printf("sending: %+v\n", point)
|
|
err = enc.Encode(point)
|
|
if err != nil {
|
|
break
|
|
}
|
|
}
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "%v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
if err := s.Err(); err != nil {
|
|
fmt.Fprintf(os.Stderr, "%v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|