summaryrefslogtreecommitdiffstats
path: root/goctl.go
diff options
context:
space:
mode:
authorBrian Cully <bjc@kublai.com>2015-07-10 19:18:55 +0000
committerBrian Cully <bjc@kublai.com>2015-07-10 19:27:01 +0000
commit2fd292d5f480fd51cdbbff4821b6f09940a005a9 (patch)
treee3b4a2c4bb074133fe7a3625c01d256078f00968 /goctl.go
parent9408742ed1a7eef568e2bf0d8e1aba390ae4d80b (diff)
downloadgoctl-2fd292d5f480fd51cdbbff4821b6f09940a005a9.tar.gz
goctl-2fd292d5f480fd51cdbbff4821b6f09940a005a9.zip
Change Handler type to interface.
Should allow for more flexibility in handlers. Also, move builtin handlers into their own modules
Diffstat (limited to 'goctl.go')
-rw-r--r--goctl.go50
1 files changed, 14 insertions, 36 deletions
diff --git a/goctl.go b/goctl.go
index 12e181a..a94122a 100644
--- a/goctl.go
+++ b/goctl.go
@@ -7,7 +7,6 @@ import (
"io"
"net"
"os"
- "strconv"
"strings"
"sync/atomic"
"time"
@@ -18,19 +17,12 @@ import (
// How long to wait for a response before returning an error.
const timeout = 100 * time.Millisecond
-// Built-in commands.
-const (
- cmdPing = "ping"
- cmdPID = "pid"
-)
-
-var builtinHandlers []*Handler
+var builtinHandlers []Handler
// Error returned if handler already exists.
var HandlerExists error
var (
- pid int
lastid uint64
Logger log15.Logger
)
@@ -39,31 +31,25 @@ type Goctl struct {
logger log15.Logger
path string
listener net.Listener
- handlers map[string]*Handler
+ handlers map[string]Handler
}
-type Handler struct {
- Name string
- Fn func([]string) string
+type Handler interface {
+ Name() string
+ Run([]string) string
}
func init() {
- pid = os.Getpid()
Logger = log15.New()
Logger.SetHandler(log15.DiscardHandler())
HandlerExists = errors.New("handler exists")
-
- builtinHandlers = []*Handler{
- {cmdPing, handlePing},
- {cmdPID, handlePID},
- }
}
func NewGoctl(path string) Goctl {
- handlers := make(map[string]*Handler)
+ handlers := make(map[string]Handler)
for _, h := range builtinHandlers {
- handlers[h.Name] = h
+ handlers[h.Name()] = h
}
return Goctl{
logger: Logger.New("id", atomic.AddUint64(&lastid, 1)),
@@ -102,16 +88,16 @@ func (gc *Goctl) Stop() {
}
}
-func (gc *Goctl) AddHandler(name string, fn func([]string) string) error {
- return gc.AddHandlers([]*Handler{{name, fn}})
+func (gc *Goctl) AddHandler(h Handler) error {
+ return gc.AddHandlers([]Handler{h})
}
-func (gc *Goctl) AddHandlers(handlers []*Handler) error {
+func (gc *Goctl) AddHandlers(handlers []Handler) error {
for _, h := range handlers {
- if gc.handlers[h.Name] != nil {
+ if gc.handlers[h.Name()] != nil {
return HandlerExists
}
- gc.handlers[h.Name] = h
+ gc.handlers[h.Name()] = h
}
return nil
}
@@ -145,14 +131,6 @@ func Write(w io.Writer, p []byte) error {
return nil
}
-func handlePing(args []string) string {
- return "pong"
-}
-
-func handlePID(args []string) string {
- return strconv.Itoa(pid)
-}
-
func (gc *Goctl) acceptor() {
for {
c, err := gc.listener.Accept()
@@ -180,7 +158,7 @@ func (gc *Goctl) reader(c io.ReadWriteCloser) error {
gc.logger.Debug("Got command.", "cmd", cmd)
var resp string
if h := gc.handlers[cmd[0]]; h != nil {
- resp = h.Fn(cmd[1:])
+ resp = h.Run(cmd[1:])
} else {
resp = fmt.Sprintf("ERROR: unknown command: '%s'.", cmd[0])
}
@@ -198,7 +176,7 @@ func (gc *Goctl) isAlreadyRunning() string {
defer c.Close()
dataChan := make(chan []byte, 1)
- c.Write([]byte(cmdPID))
+ c.Write([]byte("pid"))
go func() {
buf := make([]byte, 1024)
n, err := c.Read(buf[:])