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

+33 -9
View File
@@ -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)
}
}