2014-12-14 19:55:56 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-12-19 16:18:25 +00:00
|
|
|
"github.com/dballard/warren/lib/warren"
|
2014-12-14 19:55:56 +00:00
|
|
|
"log"
|
|
|
|
"net"
|
2014-12-19 16:18:25 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2014-12-14 19:55:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func cmdHandle(c net.Conn) {
|
|
|
|
defer c.Close()
|
2014-12-30 17:32:11 +00:00
|
|
|
fmt.Println("cmdHandle")
|
|
|
|
data, err := warren.ReadStringz(c)
|
2014-12-14 19:55:56 +00:00
|
|
|
if err != nil {
|
2014-12-30 17:32:11 +00:00
|
|
|
fmt.Println(err)
|
2014-12-14 19:55:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Println("cmdHandle got command:", string(data))
|
2014-12-30 17:32:11 +00:00
|
|
|
c.Write([]byte("echo: " + data))
|
2014-12-14 19:55:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2014-12-19 16:18:25 +00:00
|
|
|
|
|
|
|
cmdSock, err := net.Listen(warren.CmdSockType(), warren.CmdSockAddr())
|
|
|
|
defer cmdSock.Close()
|
2014-12-14 19:55:56 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Listen on sock error:", err)
|
|
|
|
}
|
|
|
|
|
2014-12-19 16:18:25 +00:00
|
|
|
// Handle signals
|
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(c,
|
|
|
|
syscall.SIGINT,
|
|
|
|
syscall.SIGTERM,
|
|
|
|
syscall.SIGQUIT,
|
|
|
|
syscall.SIGHUP,
|
|
|
|
)
|
|
|
|
go func() {
|
|
|
|
_ = <-c
|
|
|
|
cmdSock.Close()
|
|
|
|
}()
|
|
|
|
|
2014-12-14 19:55:56 +00:00
|
|
|
for {
|
|
|
|
fd, err := cmdSock.Accept()
|
|
|
|
fmt.Println("accept")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("accpet error:", err)
|
|
|
|
}
|
|
|
|
go cmdHandle(fd)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|