robo/cmd/rclient/main.go

68 lines
1.1 KiB
Go
Raw Normal View History

2015-02-04 21:08:44 -08:00
package main
import (
"bufio"
2015-02-04 21:24:45 -08:00
"encoding/gob"
2015-02-04 21:08:44 -08:00
"fmt"
"net"
"os"
2015-02-04 21:24:45 -08:00
"strconv"
"strings"
2015-02-04 21:08:44 -08:00
"mcquay.me/robo"
)
const usage = "rsend <hostname>"
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "%s\n", usage)
os.Exit(1)
}
2015-02-04 21:24:45 -08:00
var err error
var x, y float64
2015-02-04 21:08:44 -08:00
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)
}
2015-02-04 21:24:45 -08:00
enc := gob.NewEncoder(conn)
2015-02-04 21:08:44 -08:00
s := bufio.NewScanner(os.Stdin)
2015-02-04 21:24:45 -08:00
point := robo.Point{}
2015-02-04 21:08:44 -08:00
for s.Scan() {
txt := s.Text()
2015-02-04 21:24:45 -08:00
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
2015-02-04 21:08:44 -08:00
fmt.Printf("sending: %s\n", txt)
2015-02-04 21:24:45 -08:00
err = enc.Encode(&point)
2015-02-04 21:08:44 -08:00
if err != nil {
break
}
}
2015-02-04 21:24:45 -08:00
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
2015-02-04 21:08:44 -08:00
if err := s.Err(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}