tile38/pkg/controller/client.go

251 lines
5.9 KiB
Go
Raw Normal View History

2016-04-01 02:25:47 +03:00
package controller
import (
"errors"
"fmt"
2016-04-01 02:25:47 +03:00
"net"
"sort"
"strings"
2016-04-01 02:25:47 +03:00
"time"
"github.com/tidwall/resp"
"github.com/tidwall/tile38/pkg/server"
2016-04-01 02:25:47 +03:00
)
2016-04-03 05:16:36 +03:00
// Conn represents a simple resp connection.
2016-04-01 02:25:47 +03:00
type Conn struct {
conn net.Conn
rd *resp.Reader
wr *resp.Writer
}
2017-09-30 18:00:29 +03:00
type clientConn struct {
id int
name astring
opened atime
last atime
conn *server.Conn
}
2016-04-03 05:16:36 +03:00
// DialTimeout dials a resp server.
2016-04-01 02:25:47 +03:00
func DialTimeout(address string, timeout time.Duration) (*Conn, error) {
tcpconn, err := net.DialTimeout("tcp", address, timeout)
if err != nil {
return nil, err
}
conn := &Conn{
conn: tcpconn,
rd: resp.NewReader(tcpconn),
wr: resp.NewWriter(tcpconn),
}
return conn, nil
}
2016-04-03 05:16:36 +03:00
// Close closes the connection.
2016-04-01 02:25:47 +03:00
func (conn *Conn) Close() error {
conn.wr.WriteMultiBulk("quit")
return conn.conn.Close()
}
2016-04-03 05:16:36 +03:00
// Do performs a command and returns a resp value.
2016-04-01 02:25:47 +03:00
func (conn *Conn) Do(commandName string, args ...interface{}) (val resp.Value, err error) {
if err := conn.wr.WriteMultiBulk(commandName, args...); err != nil {
return val, err
}
val, _, err = conn.rd.ReadValue()
return val, err
}
type byID []*clientConn
func (arr byID) Len() int {
return len(arr)
}
func (arr byID) Less(a, b int) bool {
return arr[a].id < arr[b].id
}
func (arr byID) Swap(a, b int) {
arr[a], arr[b] = arr[b], arr[a]
}
2017-10-05 18:20:40 +03:00
func (c *Controller) cmdClient(msg *server.Message, conn *server.Conn) (resp.Value, error) {
start := time.Now()
2017-10-05 18:20:40 +03:00
if len(msg.Values) == 1 {
2017-10-05 18:20:40 +03:00
return server.NOMessage, errInvalidNumberOfArguments
}
switch strings.ToLower(msg.Values[1].String()) {
default:
2017-10-05 18:20:40 +03:00
return server.NOMessage, errors.New("Syntax error, try CLIENT " +
"(LIST | KILL | GETNAME | SETNAME)")
case "list":
if len(msg.Values) != 2 {
2017-10-05 18:20:40 +03:00
return server.NOMessage, errInvalidNumberOfArguments
}
var list []*clientConn
2017-09-30 18:00:29 +03:00
c.connsmu.RLock()
2017-09-30 21:06:10 +03:00
for _, cc := range c.conns {
list = append(list, cc)
}
2017-09-30 18:00:29 +03:00
c.connsmu.RUnlock()
sort.Sort(byID(list))
now := time.Now()
var buf []byte
for _, cc := range list {
buf = append(buf,
fmt.Sprintf("id=%d addr=%s name=%s age=%d idle=%d\n",
2017-09-30 18:00:29 +03:00
cc.id, cc.conn.RemoteAddr().String(), cc.name.get(),
now.Sub(cc.opened.get())/time.Second,
now.Sub(cc.last.get())/time.Second,
)...,
)
}
switch msg.OutputType {
case server.JSON:
2017-10-05 18:20:40 +03:00
return resp.StringValue(`{"ok":true,"list":` + jsonString(string(buf)) + `,"elapsed":"` + time.Now().Sub(start).String() + "\"}"), nil
case server.RESP:
2017-10-05 18:20:40 +03:00
return resp.BytesValue(buf), nil
}
2017-10-05 18:20:40 +03:00
return server.NOMessage, nil
case "getname":
if len(msg.Values) != 2 {
2017-10-05 18:20:40 +03:00
return server.NOMessage, errInvalidNumberOfArguments
}
name := ""
2017-09-30 18:00:29 +03:00
c.connsmu.RLock()
2017-09-30 21:06:10 +03:00
if cc, ok := c.conns[conn]; ok {
2017-09-30 18:00:29 +03:00
name = cc.name.get()
}
2017-09-30 18:00:29 +03:00
c.connsmu.RUnlock()
switch msg.OutputType {
case server.JSON:
2017-10-05 18:20:40 +03:00
return resp.StringValue(`{"ok":true,"name":` + jsonString(name) + `,"elapsed":"` + time.Now().Sub(start).String() + "\"}"), nil
case server.RESP:
2017-10-05 18:20:40 +03:00
return resp.StringValue(name), nil
}
case "setname":
if len(msg.Values) != 3 {
2017-10-05 18:20:40 +03:00
return server.NOMessage, errInvalidNumberOfArguments
}
name := msg.Values[2].String()
for i := 0; i < len(name); i++ {
if name[i] < '!' || name[i] > '~' {
2017-09-30 18:00:29 +03:00
errstr := "Client names cannot contain spaces, newlines or special characters."
2017-10-05 18:20:40 +03:00
return server.NOMessage, errors.New(errstr)
}
}
2017-09-30 18:00:29 +03:00
c.connsmu.RLock()
2017-09-30 21:06:10 +03:00
if cc, ok := c.conns[conn]; ok {
2017-09-30 18:00:29 +03:00
cc.name.set(name)
}
2017-09-30 18:00:29 +03:00
c.connsmu.RUnlock()
switch msg.OutputType {
case server.JSON:
2017-10-05 18:20:40 +03:00
return resp.StringValue(`{"ok":true,"elapsed":"` + time.Now().Sub(start).String() + "\"}"), nil
case server.RESP:
2017-10-05 18:20:40 +03:00
return resp.SimpleStringValue("OK"), nil
}
case "kill":
if len(msg.Values) < 3 {
2017-10-05 18:20:40 +03:00
return server.NOMessage, errInvalidNumberOfArguments
}
var useAddr bool
var addr string
var useID bool
var id string
for i := 2; i < len(msg.Values); i++ {
arg := msg.Values[i].String()
if strings.Contains(arg, ":") {
addr = arg
useAddr = true
break
}
switch strings.ToLower(arg) {
default:
2017-10-05 18:20:40 +03:00
return server.NOMessage, errors.New("No such client")
case "addr":
i++
if i == len(msg.Values) {
2017-10-05 18:20:40 +03:00
return server.NOMessage, errors.New("syntax error")
}
addr = msg.Values[i].String()
useAddr = true
case "id":
i++
if i == len(msg.Values) {
2017-10-05 18:20:40 +03:00
return server.NOMessage, errors.New("syntax error")
}
id = msg.Values[i].String()
useID = true
}
}
var cclose *clientConn
2017-09-30 18:00:29 +03:00
c.connsmu.RLock()
2017-09-30 21:06:10 +03:00
for _, cc := range c.conns {
if useID && fmt.Sprintf("%d", cc.id) == id {
cclose = cc
break
} else if useAddr && cc.conn.RemoteAddr().String() == addr {
cclose = cc
break
}
}
2017-09-30 18:00:29 +03:00
c.connsmu.RUnlock()
if cclose == nil {
2017-10-05 18:20:40 +03:00
return server.NOMessage, errors.New("No such client")
}
2017-10-05 18:20:40 +03:00
var res resp.Value
switch msg.OutputType {
case server.JSON:
2017-10-05 18:20:40 +03:00
res = resp.StringValue(`{"ok":true,"elapsed":"` + time.Now().Sub(start).String() + "\"}")
case server.RESP:
2017-10-05 18:20:40 +03:00
res = resp.SimpleStringValue("OK")
}
if cclose.conn == conn {
// closing self, return response now
2017-10-05 18:20:40 +03:00
// NOTE: This is the only exception where we do convert response to a string
var outBytes []byte
switch msg.OutputType {
case server.JSON:
outBytes = res.Bytes()
case server.RESP:
outBytes, _ = res.MarshalRESP()
}
cclose.conn.Write(outBytes)
}
cclose.conn.Close()
return res, nil
}
2017-10-05 18:20:40 +03:00
return server.NOMessage, errors.New("invalid output type")
}
/*
func (c *Controller) cmdClientList(msg *server.Message) (string, error) {
var ok bool
var key string
if vs, key, ok = tokenval(vs); !ok || key == "" {
return "", errInvalidNumberOfArguments
}
col := c.getCol(key)
if col == nil {
if msg.OutputType == server.RESP {
return "+none\r\n", nil
}
return "", errKeyNotFound
}
typ := "hash"
switch msg.OutputType {
case server.JSON:
return `{"ok":true,"type":` + string(typ) + `,"elapsed":"` + time.Now().Sub(start).String() + "\"}", nil
case server.RESP:
return "+" + typ + "\r\n", nil
}
return "", nil
}
*/