84db84875bfdaf606e0514493b4dfceac2204426
[svn42.git] / go / door_daemon_zmq / main.go
1 // (c) Bernhard Tittelbach, 2013
2
3 package main
4
5 import (
6     "os"
7     "flag"
8     "time"
9 )
10
11 //~ func StringArrayToByteArray(ss []string) [][]byte {
12     //~ bb := make([][]byte, len(ss))
13     //~ for index, s := range(ss) {
14         //~ bb[index] = []byte(s)
15     //~ }
16     //~ return bb
17 //~ }
18
19 // ---------- Main Code -------------
20
21 var (
22     cmd_port_ string
23     pub_port_ string
24     door_tty_path_ string
25     enable_syslog_ bool
26     enable_debug_ bool
27 )
28
29 func init() {
30     flag.StringVar(&cmd_port_, "cmdport", "ipc:///run/tuer/door_cmd.ipc", "zmq command socket path")
31     flag.StringVar(&pub_port_, "pubport", "tcp://*:4242", "zmq public/listen socket path")
32     flag.StringVar(&door_tty_path_, "device", "/dev/door", "door tty device path")
33     flag.BoolVar(&enable_syslog_, "syslog", false, "enable logging to syslog")
34     flag.BoolVar(&enable_debug_, "debug", false, "enable debug output")
35     flag.Parse()
36 }
37
38 func main() {
39     if enable_syslog_ { LogEnableSyslog()}
40     if enable_debug_ { LogEnableDebuglog()}
41     Syslog_.Print("started")
42     defer Syslog_.Print("exiting")
43
44     zmqctx, cmd_chans, pub_chans := ZmqsInit(cmd_port_, pub_port_)
45     defer cmd_chans.Close()
46     defer pub_chans.Close()
47     defer zmqctx.Close()
48
49     serial_wr, serial_rd, err := OpenAndHandleSerial(door_tty_path_, 0)
50     defer close(serial_wr)
51     if err != nil {
52         panic(err)
53     }
54
55     var next_incoming_serial_is_client_reply bool
56     timeout_chan := make(chan bool)
57     defer close(timeout_chan)
58     for {
59         select {
60             case incoming_ser_line, is_notclosed := <- serial_rd:
61                 if is_notclosed {
62                     //~ if Syslog_ != nil { Syslog_.Print(ByteArrayToString(incoming_ser_line)) }
63                     if Syslog_ != nil { Syslog_.Printf("%s",incoming_ser_line) }
64                     if next_incoming_serial_is_client_reply {
65                         next_incoming_serial_is_client_reply = false
66                         cmd_chans.Out() <- incoming_ser_line
67                     }
68                     pub_chans.Out() <- incoming_ser_line
69                 } else {
70                     Syslog_.Print("serial device disappeared, exiting")
71                     os.Exit(1)
72                 }
73             case tv, timeout_notclosed := <- timeout_chan:
74                 if timeout_notclosed && tv && next_incoming_serial_is_client_reply {
75                         next_incoming_serial_is_client_reply = false
76                         cmd_chans.Out() <- [][]byte{[]byte("ERROR"), []byte("No reply from firmware")}
77                 }
78             case incoming_request, ic_notclosed := <- cmd_chans.In():
79                 if ! ic_notclosed {
80                     Syslog_.Print("zmq socket died, exiting")
81                     os.Exit(2)
82                 }
83                 if string(incoming_request[0]) == "log" {
84                     if len(incoming_request) < 2 {
85                         cmd_chans.Out() <- [][]byte{[]byte("ERROR"), []byte("argument missing")}
86                         continue
87                     }
88                     Syslog_.Printf("Log: %s",incoming_request[1:])
89                     cmd_chans.Out() <- [][]byte{[]byte("Ok")}
90                     continue
91                 }
92                 Syslog_.Printf("%s",incoming_request)
93                  if err := HandleCommand(incoming_request, serial_wr, serial_rd); err != nil {
94                     out_msg := [][]byte{[]byte("ERROR"), []byte(err.Error())}
95                     cmd_chans.Out() <- out_msg
96                  } else {
97                     pub_chans.Out() <- incoming_request
98                     next_incoming_serial_is_client_reply = true
99                     go func(){time.Sleep(3*time.Second); timeout_chan <- true;}()
100                  }
101         }
102     }
103 }