8 import "svn.spreadspace.org/realraum/go.svn/termios"
16 type DoorConnection struct {
22 var DoorConnectionMap = map[uint]DoorConnection {
25 var cmdHandler = map[string]func([]string,string,*bufio.ReadWriter ) {
27 "open":handleCmdController,
28 "close":handleCmdController,
29 "toggle":handleCmdController,
33 func readLineSafe(rw *bufio.Reader) (string, error) {
36 for isPrefix:=true;isPrefix; {
39 lineBuf,isPrefix,err = rw.ReadLine()
50 fmt.Println("line too long")
51 //fmt.Fprintf(rw,"line too long\n")
55 reg := regexp.MustCompile("\r")
56 safe := reg.ReplaceAllString(line, "")
60 func connToReadWriter(c io.Reader,cw io.Writer) (*bufio.ReadWriter) {
61 client_r := bufio.NewReaderSize(c,1024)
62 client_w := bufio.NewWriterSize(cw,1024)
63 return bufio.NewReadWriter(client_r,client_w)
66 func handleConnection(c net.Conn, client * bufio.ReadWriter, connID uint) () {
67 fmt.Println("new connection")
70 delete(DoorConnectionMap,connID)
74 line,err:=readLineSafe(bufio.NewReader(client))
76 if err.Error() != "EOF" {
77 fmt.Printf("Error: readLineSafe returned %v\n",err.Error())
79 fmt.Printf("Connection closed by remote host\n");
87 fmt.Printf("Received: %v\n", line)
88 tokens:=strings.Fields(line)
89 remainStr:=strings.Join(tokens[1:]," ")
90 handleCmd(tokens,remainStr,client)
94 func handleCmd(tokens []string, remainStr string,client * bufio.ReadWriter) {
96 func_ptr,present := cmdHandler[cmd]
98 func_ptr(tokens, remainStr,client)
100 fmt.Printf("Error: unknown Cmd: %v\n", cmd)
104 func handleCmdTest(tokens []string, remainStr string, client * bufio.ReadWriter) {
106 fmt.Printf("Test: %v\n", remainStr)
109 func handleCmdController(tokens []string, remainStr string, client * bufio.ReadWriter) {
111 s_r:=strings.NewReader(cmd)
112 char := make([]byte,1)
114 fmt.Println(string(char))
118 func openTTY(name string) *os.File {
119 file, err := os.OpenFile(name,os.O_RDWR ,0600) // For read access.
121 fmt.Println(err.Error())
123 termios.Ttyfd(file.Fd())
128 fmt.Fprintf(os.Stderr, "usage: myprog [inputfile]\n")
133 func SerialWriter(c chan string, serial * os.File ) {
135 serial.WriteString(<-c)
140 func SerialReader(c chan string , serial * bufio.Reader) {
142 s,err := readLineSafe(serial)
147 fmt.Printf("Error in read from serial: %v\n",err.Error())
150 //fmt.Printf("Serial: Read %v\n",s);
155 func openSerial(filename string) (chan string,chan string) {
156 serial:=openTTY(filename)
157 in:=make(chan string)
158 out:=make(chan string)
159 go SerialWriter(out,serial)
160 go SerialReader(in,bufio.NewReaderSize(serial,128))
164 func SerialHandler(serial_i chan string) {
167 fmt.Printf("Serial Read: %s\n",line)
169 for _, v := range DoorConnectionMap{
173 case <-time.After(2):
181 func chanWriter(c chan string, w io.Writer) {
184 w.Write([]byte(line))
185 w.Write([]byte("\n"))
189 // lock = make(sync.RWMutex)
195 fmt.Println("Input file is missing.");
198 serial_i,serial_o:=openSerial(args[0])
199 go SerialHandler(serial_i)
202 ln, err := net.Listen("unix", "/tmp/test.sock")
204 fmt.Printf("Error: %s\n",err.Error())
207 fmt.Printf("Listener started\n")
209 var connectionID uint =0
211 conn, err := ln.Accept()
216 client:=connToReadWriter(conn,conn)
218 writeChan := make(chan string)
220 DoorConnectionMap[connectionID]= DoorConnection{ rw:client,c:conn, wchan:writeChan }
222 go handleConnection(conn,client,connectionID)
223 go chanWriter(writeChan,conn)