facedac9f80ef4fedfdc7b7d42657619039e4c95
[svn42.git] / go / door_daemon_zmq / main.go
1 // (c) Bernhard Tittelbach, 2013
2
3 package main
4
5 import (
6     "fmt"
7     "os"
8     "flag"
9     "time"
10 )
11
12 //~ func StringArrayToByteArray(ss []string) [][]byte {
13     //~ bb := make([][]byte, len(ss))
14     //~ for index, s := range(ss) {
15         //~ bb[index] = []byte(s)
16     //~ }
17     //~ return bb
18 //~ }
19
20 // ---------- Main Code -------------
21
22 var (
23     cmd_port_ string
24     pub_port_ string
25 )
26
27 func usage() {
28     fmt.Fprintf(os.Stderr, "Usage: door_daemon_0mq <door tty device>\n")
29     flag.PrintDefaults()
30 }
31
32 func init() {
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")
35     flag.Usage = usage
36     flag.Parse()
37 }
38
39 func main() {
40     args := flag.Args()
41     if len(args) < 1 {
42         fmt.Fprintf(os.Stderr, "Input file is missing!\n");
43         usage()
44         os.Exit(1);
45     }
46
47     zmqctx, cmd_chans, pub_chans := ZmqsInit(cmd_port_, pub_port_)
48     defer cmd_chans.Close()
49     defer pub_chans.Close()
50     defer zmqctx.Close()
51
52     serial_wr, serial_rd, err := OpenAndHandleSerial(args[0])
53     defer close(serial_wr)
54     if err != nil {
55         panic(err)
56     }
57
58     //~ serial_wr <- "f"
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)
64     for {
65         select {
66             case incoming_ser_line, is_notclosed := <- serial_rd:
67                 if is_notclosed {
68                     if next_incoming_serial_is_client_reply {
69                         next_incoming_serial_is_client_reply = false
70                         cmd_chans.Out() <- incoming_ser_line
71                     }
72                     pub_chans.Out() <- incoming_ser_line
73                 } else {
74                     os.Exit(1)
75                 }
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")}
80                 }
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
86                  } else {
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;}()
90                  }
91         }
92     }
93 }