1 // (c) Bernhard Tittelbach, 2013
12 //~ func StringArrayToByteArray(ss []string) [][]byte {
13 //~ bb := make([][]byte, len(ss))
14 //~ for index, s := range(ss) {
15 //~ bb[index] = []byte(s)
20 // ---------- Main Code -------------
28 fmt.Fprintf(os.Stderr, "Usage: door_daemon_0mq <door tty device>\n")
33 flag.StringVar(&cmd_port_, "cmdport", "tcp://127.0.01:3232", "zmq command socket path")
34 flag.StringVar(&pub_port_, "pubport", "pgm://233.252.1.42:4242", "zmq public/listen socket path")
42 fmt.Fprintf(os.Stderr, "Input file is missing!\n");
47 zmqctx, cmd_chans, pub_chans := ZmqsInit(cmd_port_, pub_port_)
48 defer cmd_chans.Close()
49 defer pub_chans.Close()
52 serial_wr, serial_rd, err := OpenAndHandleSerial(args[0])
53 defer close(serial_wr)
59 //~ firmware_version := <- serial_rd
60 //~ log.Print("Firmware version:", firmware_version)
61 var next_incoming_serial_is_client_reply bool
62 timeout_chan := make(chan bool)
63 defer close(timeout_chan)
66 case incoming_ser_line, is_notclosed := <- serial_rd:
68 if next_incoming_serial_is_client_reply {
69 next_incoming_serial_is_client_reply = false
70 cmd_chans.Out() <- incoming_ser_line
72 pub_chans.Out() <- incoming_ser_line
76 case tv, timeout_notclosed := <- timeout_chan:
77 if timeout_notclosed && tv && next_incoming_serial_is_client_reply {
78 next_incoming_serial_is_client_reply = false
79 cmd_chans.Out() <- [][]byte{[]byte("ERROR"), []byte("No reply from firmware")}
81 case incoming_request, ic_notclosed := <- cmd_chans.In():
82 if ! ic_notclosed {os.Exit(2)}
83 if err := HandleCommand(incoming_request, serial_wr, serial_rd); err != nil {
84 out_msg := [][]byte{[]byte("ERROR"), []byte(err.Error())}
85 cmd_chans.Out() <- out_msg
87 pub_chans.Out() <- incoming_request
88 next_incoming_serial_is_client_reply = true
89 go func(){time.Sleep(3*time.Second); timeout_chan <- true;}()