implement a send command for remove commands

Mentioned in #36.
This commit is contained in:
Gokcehan committed 2016-11-06 17:06:25 +03:00
1 parent d622e74c51
commit a2cc32c908
4 files changed
+77 -11

No files matched your search

+2 -2
View File
@@ -61,11 +61,11 @@ func readExpr(c net.Conn) chan Expr {
ch := make(chan Expr) ch := make(chan Expr)
go func() { go func() {
fmt.Fprintln(c, "conn") fmt.Fprintf(c, "conn %d\n", gClientId)
s := bufio.NewScanner(c) s := bufio.NewScanner(c)
for s.Scan() { for s.Scan() {
log.Printf("reading expression: %s", s.Text()) log.Printf("recv: %s", s.Text())
p := newParser(strings.NewReader(s.Text())) p := newParser(strings.NewReader(s.Text()))
if p.parse() { if p.parse() {
ch <- p.expr ch <- p.expr
+18
View File
@@ -5,6 +5,7 @@ import (
"log" "log"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings"
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
@@ -49,6 +50,23 @@ func runeSliceWidthRange(rs []rune, beg int, end int) []rune {
return nil 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 // 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 // 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 // the first significant digit is shown, otherwise it is hidden. Numbers are
+24
View File
@@ -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) { func TestHumanize(t *testing.T) {
nums := []struct { nums := []struct {
i int64 i int64
+33 -9
View File
@@ -6,12 +6,13 @@ import (
"log" "log"
"net" "net"
"os" "os"
"strconv"
) )
var ( var (
gKeepFile bool gKeepFile bool
gFileList []string gFileList []string
gConnList []net.Conn gConnList map[int]net.Conn
) )
func serve() { func serve() {
@@ -27,9 +28,12 @@ func serve() {
l, err := net.Listen("unix", gSocketPath) l, err := net.Listen("unix", gSocketPath)
if err != nil { if err != nil {
log.Printf("listening socket: %s", err) log.Printf("listening socket: %s", err)
return
} }
defer l.Close() defer l.Close()
gConnList = make(map[int]net.Conn)
listen(l) listen(l)
} }
@@ -47,7 +51,9 @@ func handleConn(c net.Conn) {
s := bufio.NewScanner(c) s := bufio.NewScanner(c)
for s.Scan() { for s.Scan() {
switch s.Text() { log.Printf("listen: %s", s.Text())
word, rest := splitWord(s.Text())
switch word {
case "save": case "save":
saveFilesServer(s) saveFilesServer(s)
log.Printf("listen: save, list: %v, keep: %t", gFileList, gKeepFile) log.Printf("listen: save, list: %v, keep: %t", gFileList, gKeepFile)
@@ -55,15 +61,33 @@ func handleConn(c net.Conn) {
loadFilesServer(c) loadFilesServer(c)
log.Printf("listen: load, keep: %t", gKeepFile) log.Printf("listen: load, keep: %t", gKeepFile)
case "conn": case "conn":
gConnList = append(gConnList, c) if rest != "" {
log.Print("listen: conn") word2, _ := splitWord(rest)
case "ping": id, err := strconv.Atoi(word2)
for _, c := range gConnList { if err != nil {
fmt.Fprintln(c, "echo ping from server") 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: default:
log.Print("listen: unexpected command") log.Printf("listen: unexpected command: %s", word)
} }
} }