use newlines in communication protocol

Mentioned in #12 and #47.
This commit is contained in:
Gokcehan committed 2017-07-24 23:55:44 +03:00
1 parent ec5058429c
commit 74078f6111
2 files changed
+25 -18

No files matched your search

+12 -10
View File
@@ -105,15 +105,18 @@ func saveFiles(list []string, copy bool) error {
log.Printf("saving files: %v", list)
fmt.Fprint(c, "save ")
fmt.Fprintln(c, "save")
if copy {
fmt.Fprint(c, "copy ")
fmt.Fprintln(c, "copy")
} else {
fmt.Fprint(c, "move ")
fmt.Fprintln(c, "move")
}
fmt.Fprintln(c, strings.Join(list, ":"))
for _, f := range list {
fmt.Fprintln(c, f)
}
fmt.Fprintln(c)
return nil
}
@@ -132,20 +135,19 @@ func loadFiles() (list []string, copy bool, err error) {
s.Scan()
word, rest := splitWord(s.Text())
log.Printf("load: %s", s.Text())
switch word {
switch s.Text() {
case "copy":
copy = true
case "move":
copy = false
default:
err = fmt.Errorf("unexpected option to copy file(s): %s", word)
err = fmt.Errorf("unexpected option to copy file(s): %s", s.Text())
return
}
list = strings.Split(rest, ":")
for s.Scan() && s.Text() != "" {
list = append(list, s.Text())
}
if s.Err() != nil {
err = fmt.Errorf("scanning file list: %s", s.Err())
+13 -8
View File
@@ -7,7 +7,6 @@ import (
"net"
"os"
"strconv"
"strings"
)
var (
@@ -56,24 +55,30 @@ func handleConn(c net.Conn) {
word, rest := splitWord(s.Text())
switch word {
case "save":
word2, rest2 := splitWord(rest)
switch word2 {
s.Scan()
switch s.Text() {
case "copy":
gCopyFile = true
case "move":
gCopyFile = false
default:
log.Printf("unexpected option to copy file(s): %s", word2)
log.Printf("unexpected option to copy file(s): %s", s.Text())
break
}
gFileList = strings.Split(rest2, ":")
gFileList = nil
for s.Scan() && s.Text() != "" {
gFileList = append(gFileList, s.Text())
}
case "load":
if gCopyFile {
fmt.Fprint(c, "copy ")
fmt.Fprintln(c, "copy")
} else {
fmt.Fprint(c, "move ")
fmt.Fprintln(c, "move")
}
fmt.Fprintln(c, strings.Join(gFileList, ":"))
for _, f := range gFileList {
fmt.Fprintln(c, f)
}
fmt.Fprintln(c)
case "conn":
if rest != "" {
word2, _ := splitWord(rest)