2016-08-13 12:49:04 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os"
|
2016-11-06 14:06:25 +00:00
|
|
|
"strconv"
|
2016-08-13 12:49:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-11-06 18:32:14 +00:00
|
|
|
gCopyFile bool
|
2016-08-13 12:49:04 +00:00
|
|
|
gFileList []string
|
2018-03-22 14:54:24 +00:00
|
|
|
gConnList = make(map[int]net.Conn)
|
2018-07-22 17:44:56 +00:00
|
|
|
gQuitChan = make(chan bool, 1)
|
|
|
|
gListener net.Listener
|
2016-08-13 12:49:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func serve() {
|
2018-05-20 17:30:41 +00:00
|
|
|
f, err := os.Create(gServerLogPath)
|
2016-08-13 12:49:04 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-05-20 17:30:41 +00:00
|
|
|
defer f.Close()
|
|
|
|
log.SetOutput(f)
|
2016-08-13 12:49:04 +00:00
|
|
|
|
|
|
|
log.Print("hi!")
|
|
|
|
|
2017-07-20 22:26:36 +00:00
|
|
|
l, err := net.Listen(gSocketProt, gSocketPath)
|
2016-08-13 12:49:04 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("listening socket: %s", err)
|
2016-11-06 14:06:25 +00:00
|
|
|
return
|
2016-08-13 12:49:04 +00:00
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
2018-07-22 17:44:56 +00:00
|
|
|
gListener = l
|
|
|
|
|
2016-08-13 12:49:04 +00:00
|
|
|
listen(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
func listen(l net.Listener) {
|
|
|
|
for {
|
|
|
|
c, err := l.Accept()
|
|
|
|
if err != nil {
|
2018-07-22 17:44:56 +00:00
|
|
|
select {
|
|
|
|
case <-gQuitChan:
|
|
|
|
log.Printf("bye!")
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
log.Printf("accepting connection: %s", err)
|
|
|
|
}
|
2016-08-13 12:49:04 +00:00
|
|
|
}
|
2016-10-29 23:20:35 +00:00
|
|
|
go handleConn(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleConn(c net.Conn) {
|
|
|
|
s := bufio.NewScanner(c)
|
|
|
|
|
|
|
|
for s.Scan() {
|
2016-11-06 14:06:25 +00:00
|
|
|
log.Printf("listen: %s", s.Text())
|
|
|
|
word, rest := splitWord(s.Text())
|
|
|
|
switch word {
|
2016-10-29 23:20:35 +00:00
|
|
|
case "save":
|
2017-07-24 20:55:44 +00:00
|
|
|
s.Scan()
|
|
|
|
switch s.Text() {
|
2016-11-06 18:32:14 +00:00
|
|
|
case "copy":
|
|
|
|
gCopyFile = true
|
|
|
|
case "move":
|
|
|
|
gCopyFile = false
|
|
|
|
default:
|
2017-07-24 20:55:44 +00:00
|
|
|
log.Printf("unexpected option to copy file(s): %s", s.Text())
|
2016-11-06 18:32:14 +00:00
|
|
|
break
|
|
|
|
}
|
2017-07-24 20:55:44 +00:00
|
|
|
gFileList = nil
|
|
|
|
for s.Scan() && s.Text() != "" {
|
|
|
|
gFileList = append(gFileList, s.Text())
|
|
|
|
}
|
2016-10-29 23:20:35 +00:00
|
|
|
case "load":
|
2016-11-06 18:32:14 +00:00
|
|
|
if gCopyFile {
|
2017-07-24 20:55:44 +00:00
|
|
|
fmt.Fprintln(c, "copy")
|
2016-11-06 18:32:14 +00:00
|
|
|
} else {
|
2017-07-24 20:55:44 +00:00
|
|
|
fmt.Fprintln(c, "move")
|
|
|
|
}
|
|
|
|
for _, f := range gFileList {
|
|
|
|
fmt.Fprintln(c, f)
|
2016-11-06 18:32:14 +00:00
|
|
|
}
|
2017-07-24 20:55:44 +00:00
|
|
|
fmt.Fprintln(c)
|
2016-10-29 23:20:35 +00:00
|
|
|
case "conn":
|
2016-11-06 14:06:25 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2016-08-13 12:49:04 +00:00
|
|
|
}
|
2018-07-22 17:44:56 +00:00
|
|
|
case "quit":
|
|
|
|
gQuitChan <- true
|
|
|
|
for _, c := range gConnList {
|
|
|
|
fmt.Fprintln(c, "echo server is quitting...")
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
gListener.Close()
|
|
|
|
break
|
2016-10-29 23:20:35 +00:00
|
|
|
default:
|
2016-11-06 14:06:25 +00:00
|
|
|
log.Printf("listen: unexpected command: %s", word)
|
2016-08-13 12:49:04 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-29 23:20:35 +00:00
|
|
|
|
2016-08-13 12:49:04 +00:00
|
|
|
if s.Err() != nil {
|
2016-11-06 18:32:14 +00:00
|
|
|
log.Printf("listening: %s", s.Err())
|
2016-08-13 12:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Close()
|
|
|
|
}
|