From a2cc32c9084152e1982ca7173e66c56a7357561d Mon Sep 17 00:00:00 2001 From: Gokcehan Date: Sun, 6 Nov 2016 17:06:25 +0300 Subject: [PATCH] implement a send command for remove commands Mentioned in #36. --- client.go | 4 ++-- misc.go | 18 ++++++++++++++++++ misc_test.go | 24 ++++++++++++++++++++++++ server.go | 42 +++++++++++++++++++++++++++++++++--------- 4 files changed, 77 insertions(+), 11 deletions(-) diff --git a/client.go b/client.go index d161e014..ab50316d 100644 --- a/client.go +++ b/client.go @@ -61,11 +61,11 @@ func readExpr(c net.Conn) chan Expr { ch := make(chan Expr) go func() { - fmt.Fprintln(c, "conn") + fmt.Fprintf(c, "conn %d\n", gClientId) s := bufio.NewScanner(c) for s.Scan() { - log.Printf("reading expression: %s", s.Text()) + log.Printf("recv: %s", s.Text()) p := newParser(strings.NewReader(s.Text())) if p.parse() { ch <- p.expr diff --git a/misc.go b/misc.go index ccf67f5c..481541a7 100644 --- a/misc.go +++ b/misc.go @@ -5,6 +5,7 @@ import ( "log" "path/filepath" "strconv" + "strings" "unicode" "unicode/utf8" @@ -49,6 +50,23 @@ func runeSliceWidthRange(rs []rune, beg int, end int) []rune { return nil } +// This function splits the first word of a string delimited by whitespace from +// the rest. This is used to tokenize a string one by one without touching the +// rest. Whitespace on the left side of both the word and the rest are trimmed. +func splitWord(s string) (word, rest string) { + s = strings.TrimLeftFunc(s, unicode.IsSpace) + ind := len(s) + for i, c := range s { + if unicode.IsSpace(c) { + ind = i + break + } + } + word = s[0:ind] + rest = strings.TrimLeftFunc(s[ind:], unicode.IsSpace) + return +} + // This function converts a size in bytes to a human readable form. For this // purpose metric suffixes are used (e.g. 1K = 1000). For values less than 10 // the first significant digit is shown, otherwise it is hidden. Numbers are diff --git a/misc_test.go b/misc_test.go index 47714440..7ebc8b54 100644 --- a/misc_test.go +++ b/misc_test.go @@ -91,6 +91,30 @@ func TestRuneSliceWidthRange(t *testing.T) { } } +func TestSplitWord(t *testing.T) { + strs := []struct { + s string + word string + rest string + }{ + {"", "", ""}, + {"foo", "foo", ""}, + {" foo", "foo", ""}, + {"foo ", "foo", ""}, + {" foo ", "foo", ""}, + {"foo bar baz", "foo", "bar baz"}, + {" foo bar baz", "foo", "bar baz"}, + {"foo bar baz", "foo", "bar baz"}, + {" foo bar baz", "foo", "bar baz"}, + } + + for _, str := range strs { + if w, r := splitWord(str.s); w != str.word || r != str.rest { + t.Errorf("at input '%s' expected '%s' and '%s' but got '%s' and '%s'", str.s, str.word, str.rest, w, r) + } + } +} + func TestHumanize(t *testing.T) { nums := []struct { i int64 diff --git a/server.go b/server.go index abff940b..37093074 100644 --- a/server.go +++ b/server.go @@ -6,12 +6,13 @@ import ( "log" "net" "os" + "strconv" ) var ( gKeepFile bool gFileList []string - gConnList []net.Conn + gConnList map[int]net.Conn ) func serve() { @@ -27,9 +28,12 @@ func serve() { l, err := net.Listen("unix", gSocketPath) if err != nil { log.Printf("listening socket: %s", err) + return } defer l.Close() + gConnList = make(map[int]net.Conn) + listen(l) } @@ -47,7 +51,9 @@ func handleConn(c net.Conn) { s := bufio.NewScanner(c) for s.Scan() { - switch s.Text() { + log.Printf("listen: %s", s.Text()) + word, rest := splitWord(s.Text()) + switch word { case "save": saveFilesServer(s) log.Printf("listen: save, list: %v, keep: %t", gFileList, gKeepFile) @@ -55,15 +61,33 @@ func handleConn(c net.Conn) { loadFilesServer(c) log.Printf("listen: load, keep: %t", gKeepFile) case "conn": - gConnList = append(gConnList, c) - log.Print("listen: conn") - case "ping": - for _, c := range gConnList { - fmt.Fprintln(c, "echo ping from server") + if rest != "" { + word2, _ := splitWord(rest) + id, err := strconv.Atoi(word2) + if err != nil { + log.Print("listen: conn: client id should be a number") + } else { + gConnList[id] = c + } + } else { + log.Print("listen: conn: requires a client id") + } + case "send": + if rest != "" { + word2, rest2 := splitWord(rest) + id, err := strconv.Atoi(word2) + if err != nil { + for _, c := range gConnList { + fmt.Fprintln(c, rest) + } + } else { + if c, ok := gConnList[id]; ok { + fmt.Fprintln(c, rest2) + } + } } - log.Print("listen: ping") default: - log.Print("listen: unexpected command") + log.Printf("listen: unexpected command: %s", word) } }