added basic tty_file support
authorOthmar Gsenger <otti@realraum.at>
Thu, 7 Mar 2013 23:29:45 +0000 (23:29 +0000)
committerOthmar Gsenger <otti@realraum.at>
Thu, 7 Mar 2013 23:29:45 +0000 (23:29 +0000)
door_daemon_go/unix_socket_server.go

index 1d5424b..886cf11 100644 (file)
@@ -3,9 +3,14 @@ import "fmt"
 import "net"
 import "bufio"
 import "strings"
+import "os"
+import "io"
 
 var cmdHandler = map[string]func([]string,string,*bufio.ReadWriter ) {
   "test":handleCmdTest,
+  "open":handleCmdController,
+  "close":handleCmdController,
+  "toggle":handleCmdController,
 }
 
 
@@ -34,14 +39,14 @@ func readLineSafe(rw *bufio.ReadWriter) (string, error) {
   return line,nil
 }
 
-func connToReadWriter(c net.Conn) (*bufio.ReadWriter) {
+func connToReadWriter(c io.Reader,cw io.Writer) (*bufio.ReadWriter) {
     client_r := bufio.NewReaderSize(c,14)
-    client_w := bufio.NewWriterSize(c,14)
+    client_w := bufio.NewWriterSize(cw,14)
     return bufio.NewReadWriter(client_r,client_w)
 }
 
 func handleConnection(c net.Conn) () {
-    client:=connToReadWriter(c)
+    client:=connToReadWriter(c,c)
     fmt.Println("new connection")
     for {
          line,err:=readLineSafe(client)
@@ -79,7 +84,27 @@ func handleCmdTest(tokens []string, remainStr string, client * bufio.ReadWriter)
   fmt.Printf("Test: %v\n", remainStr)
 }
 
+func handleCmdController(tokens []string, remainStr string, client * bufio.ReadWriter) {
+  cmd:=tokens[0]
+  s_r:=strings.NewReader(cmd)
+  char := make([]byte,1)
+  s_r.Read(char)
+  fmt.Println(string(char))
+}
+
+
+func openTTY(name string) *bufio.ReadWriter{
+  file, err := os.OpenFile(name,os.O_RDWR  ,0600) // For read access.
+  if err != nil {
+    fmt.Println(err.Error())
+  }
+  return connToReadWriter(file,file) 
+}
+
 func main() {
+  serial:=openTTY("tty")
+  serial.WriteString("foo")
+  serial.Flush()
   ln, err := net.Listen("unix", "/tmp/test.sock")
   if err != nil {
     fmt.Printf("Error: %s\n",err.Error())