Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: develop
Fetching contributors…

Cannot retrieve contributors at this time

80 lines (65 sloc) 1.61 kb
package main
//import object
import (
"fmt"
//"./client"
"strings"
//"os"
)
// The CommandFunc type is an adapter to allow the use of
// ordinary functions as Command functions.
type CommandFunc func(client *Client)
var commandMap = map[string] CommandFunc {
"shutdown": Shutdown,
"ping": Ping,
"set": Set,
"get": Get,
"del": Delete,
}
// Strings
// -------
func Set(client *Client) {
fmt.Printf("\n\nSettings: %s %s", client.Request.Argv[1], client.Request.Argv[2])
client.Db.Set(client.Request.Argv[1], client.Request.Argv[2])
// Reply
client.Response.Ok()
}
func Get(client *Client) {
buf := client.Db.Get(client.Request.Argv[1])
if (len(buf) == 0) {
client.Response.Nil()
return
}
fmt.Printf("\n\nGET RESPONSE: %s", string(buf))
client.Response.SendBulk(buf)
//_, _ = client.Conn.Write([]byte("+OK\r\n"))
//client.Conn.Close()
}
// Base
// ----
func Ping(client *Client) {
client.Response.Pong()
}
func Delete(client *Client) {
client.Db.Delete(client.Request.Argv[1])
client.Response.Ok()
}
func Unknown(client *Client) {
// Send err
client.Response.Error(fmt.Sprintf("unknown command '%s'", string(client.Request.Argv[0])))
}
// Server
// ------
func Shutdown(client *Client) {
// @todo Tell server to stop gracefully...
client.Server.Stop()
}
func CommandFromRequest(r *Request) CommandFunc {
key := strings.ToLower(string(r.Argv[0]))
comm, ok := commandMap[key]
if !ok {
// Unknown command.
return Unknown
}
return comm
}
Jump to Line
Something went wrong with that request. Please try again.