From: Othmar Gsenger Date: Thu, 7 Mar 2013 22:36:05 +0000 (+0000) Subject: added command dispatcher X-Git-Url: https://git.realraum.at/?a=commitdiff_plain;h=bbbad3e45ac405f5511349d4fdf2d8af5240ac44;p=svn42.git added command dispatcher --- diff --git a/door_daemon_go/run.sh b/door_daemon_go/run.sh index 8f8d5bd..0f91b48 100755 --- a/door_daemon_go/run.sh +++ b/door_daemon_go/run.sh @@ -1,4 +1,4 @@ #!/bin/sh clear -rm /tmp/test.sock +rm /tmp/test.sock 2>/dev/null go build unix_socket_server.go && ./unix_socket_server || sleep 5 diff --git a/door_daemon_go/unix_socket_server.go b/door_daemon_go/unix_socket_server.go index eb34e0c..ee5429f 100644 --- a/door_daemon_go/unix_socket_server.go +++ b/door_daemon_go/unix_socket_server.go @@ -4,6 +4,11 @@ import "net" import "bufio" import "strings" +var cmdHandler = map[string]func([]string,string) { + "test":handleCmdTest, +} + + func readLineSafe(rw *bufio.ReadWriter) (string, error) { wasPrefix:=false var line string @@ -41,7 +46,11 @@ func handleConnection(c net.Conn) () { for { line,err:=readLineSafe(client) if err != nil { - fmt.Println(err.Error()) + if err.Error() != "EOF" { + fmt.Printf("Error: readLineSafe returned %v\n",err.Error()) + } else { + fmt.Printf("Connection closed by remote host\n"); + } c.Close() return } @@ -57,8 +66,19 @@ func handleConnection(c net.Conn) () { func handleCmd(tokens []string, remainStr string) { cmd:=tokens[0] - fmt.Printf("Handling Cmd: %v\n", cmd) + func_ptr,present := cmdHandler[cmd] + if present { + func_ptr(tokens, remainStr) + } else { + fmt.Printf("Error: unknown Cmd: %v\n", cmd) + } } + +func handleCmdTest(tokens []string, remainStr string) { + //cmd:=tokens[0] + fmt.Printf("Test: %v\n", remainStr) +} + func main() { ln, err := net.Listen("unix", "/tmp/test.sock") if err != nil {