1 // (c) Bernhard Tittelbach, 2013
15 // ---------- Main Code -------------
25 flag.StringVar(&cmd_port_, "cmdport", "ipc:///run/tuer/door_cmd.ipc", "zmq command socket path")
26 flag.StringVar(&sub_port_, "pubport", "tcp://torwaechter.realraum.at:4242", "zmq subscribe/listen socket path")
27 flag.StringVar(&cmd_method_, "method", "", "zmq cmd method")
28 flag.StringVar(&cmd_user_, "usernick", "", "zmq cmd user identity")
32 func LineReader(out chan <- [][]byte, stdin * os.File) {
33 linescanner := bufio.NewScanner(stdin)
34 linescanner.Split(bufio.ScanLines)
36 for linescanner.Scan() {
37 if err := linescanner.Err(); err != nil {
41 //text := bytes.Fields(linescanner.Bytes()) //this returns a slice (aka pointer, no array deep-copy here)
42 text := bytes.Fields([]byte(linescanner.Text())) //this allocates a string and slices it -> no race-condition with overwriting any data
50 func ByteArrayToString(bb [][]byte) string {
51 b := bytes.Join(bb, []byte(" "))
56 zmqctx, cmd_chans, sub_chans := ZmqsInit(cmd_port_, sub_port_)
57 defer cmd_chans.Close()
58 defer sub_chans.Close()
61 var ignore_next uint = 0
63 user_input_chan := make(chan [][]byte, 1)
64 go LineReader(user_input_chan, os.Stdin)
65 defer os.Stdin.Close()
69 case input, input_open := (<- user_input_chan):
71 if len(input) == 0 { continue }
72 switch string(input[0]) {
74 fmt.Println("Available Commands: help, listen, quit. Everything else is passed through to door daemon")
77 fmt.Println("Now listening, @ are broadcasts")
82 cmd_chans.Out() <- input
83 reply := <- cmd_chans.In()
84 fmt.Println(">",ByteArrayToString(reply))
89 case pubsubstuff := <- sub_chans.In():
90 if len(pubsubstuff) == 0 { continue}
96 fmt.Println("@",ByteArrayToString(pubsubstuff))