From bd85f1b495b8e2e1305fa2f3529b4fb8bfc592f0 Mon Sep 17 00:00:00 2001 From: Othmar Gsenger Date: Wed, 20 Mar 2013 21:52:22 +0000 Subject: [PATCH] door daemon multipley working --- door_daemon_go/run.sh | 2 +- door_daemon_go/unix_socket_server.go | 63 +++++++++++++++++++++++++++++----- 2 files changed, 55 insertions(+), 10 deletions(-) diff --git a/door_daemon_go/run.sh b/door_daemon_go/run.sh index 2d47fd6..049a8ef 100755 --- a/door_daemon_go/run.sh +++ b/door_daemon_go/run.sh @@ -4,4 +4,4 @@ rm /tmp/test.sock 2>/dev/null export GOPATH=`pwd` #go install termios #go build unix_socket_server.go && ./unix_socket_server /dev/ttyACM0 || sleep 5 -go build && ./unix_socket_server /dev/ttyACM0 || sleep 5 +go build && ./door_daemon_go /dev/ttyACM0 || sleep 5 diff --git a/door_daemon_go/unix_socket_server.go b/door_daemon_go/unix_socket_server.go index 61893ff..77a64e6 100644 --- a/door_daemon_go/unix_socket_server.go +++ b/door_daemon_go/unix_socket_server.go @@ -8,6 +8,19 @@ import "io" import "termios" import "flag" import "regexp" +import "sync" +import "time" + +var lock sync.RWMutex + +type DoorConnection struct { + rw * bufio.ReadWriter + c net.Conn + wchan chan string +} + +var DoorConnectionMap = map[uint]DoorConnection { +} var cmdHandler = map[string]func([]string,string,*bufio.ReadWriter ) { "test":handleCmdTest, @@ -50,9 +63,13 @@ func connToReadWriter(c io.Reader,cw io.Writer) (*bufio.ReadWriter) { return bufio.NewReadWriter(client_r,client_w) } -func handleConnection(c net.Conn) () { - client:=connToReadWriter(c,c) +func handleConnection(c net.Conn, client * bufio.ReadWriter, connID uint) () { fmt.Println("new connection") + defer func () { + lock.Lock() + delete(DoorConnectionMap,connID) + lock.Unlock() + }() for { line,err:=readLineSafe(bufio.NewReader(client)) if err != nil { @@ -139,17 +156,37 @@ func openSerial(filename string) (chan string,chan string) { serial:=openTTY(filename) in:=make(chan string) out:=make(chan string) - //go SerialWriter(out,serial) + go SerialWriter(out,serial) go SerialReader(in,bufio.NewReaderSize(serial,128)) return in,out } func SerialHandler(serial_i chan string) { for { - fmt.Printf("Serial Read: %s\n",<-serial_i); + line:=<-serial_i + fmt.Printf("Serial Read: %s\n",line) + lock.RLock() + for _, v := range DoorConnectionMap{ + select { + case v.wchan<-line: + + case <-time.After(2): + + } + } + lock.RUnlock() + } +} + +func chanWriter(c chan string, w io.Writer) { + for { + line := <-c + w.Write([]byte(line)) + w.Write([]byte("\n")) } } func main() { +// lock = make(sync.RWMutex) flag.Usage = usage flag.Parse() @@ -158,6 +195,10 @@ func main() { fmt.Println("Input file is missing."); os.Exit(1); } + serial_i,serial_o:=openSerial(args[0]) + go SerialHandler(serial_i) + serial_o<-"f" + ln, err := net.Listen("unix", "/tmp/test.sock") if err != nil { fmt.Printf("Error: %s\n",err.Error()) @@ -165,16 +206,20 @@ func main() { } fmt.Printf("Listener started\n") - serial_i,serial_o:=openSerial(args[0]) - go SerialHandler(serial_i) - serial_o<-"f" - + var connectionID uint =0 for { conn, err := ln.Accept() if err != nil { // handle error continue } - go handleConnection(conn) + client:=connToReadWriter(conn,conn) + connectionID++ + writeChan := make(chan string) + lock.Lock() + DoorConnectionMap[connectionID]= DoorConnection{ rw:client,c:conn, wchan:writeChan } + lock.Unlock() + go handleConnection(conn,client,connectionID) + go chanWriter(writeChan,conn) } } -- 1.7.10.4