Skip to main content

cmd_ping.go

cmd_ping.go - Overview

This file defines the PING command, which returns "PONG" if no argument is provided, or "PONG" followed by the provided argument.

Detailed Documentation

cPING

var cPING = &CommandMeta{
Name: "PING",
Syntax: "PING",
HelpShort: "PING returns PONG if no argument is provided, otherwise it returns PONG with the message.",
HelpLong: `
PING returns PONG if no argument is provided, otherwise it returns PONG with the message argument.
`,
Examples: `
localhost:7379> PING
PONG
localhost:7379> PING Hello
PONG Hello
`,
Eval: evalPING,
Execute: executePING,
}
  • Purpose: Defines the metadata for the PING command, including its name, syntax, help text, examples, and the functions to evaluate and execute it.
  • Properties:
    • Name: The name of the command ("PING").
    • Syntax: The syntax of the command ("PING").
    • HelpShort: A short description of the command.
    • HelpLong: A longer description of the command.
    • Examples: Example usages of the command.
    • Eval: The function to evaluate the command (evalPING).
    • Execute: The function to execute the command (executePING).

init

func init() {
CommandRegistry.AddCommand(cPING)
}
  • Purpose: Registers the PING command with the CommandRegistry.
  • Parameters: None
  • Returns: None

evalPING

func evalPING(c *Cmd, s *dstore.Store) (*CmdRes, error) {
if len(c.C.Args) >= 2 {
return cmdResNil, errors.ErrWrongArgumentCount("PING")
}
if len(c.C.Args) == 0 {
return &CmdRes{R: &wire.Response{
Value: &wire.Response_VStr{VStr: "PONG"},
}}, nil
}
return &CmdRes{R: &wire.Response{
Value: &wire.Response_VStr{VStr: "PONG " + c.C.Args[0]},
}}, nil
}
  • Purpose: Evaluates the PING command. It checks the number of arguments and returns "PONG" or "PONG" with the provided message.
  • Parameters:
    • c: A pointer to the Cmd struct, representing the command.
    • s: A pointer to a dstore.Store struct.
  • Returns:
    • A pointer to a CmdRes struct, representing the command result.
    • An error, if any.

executePING

func executePING(c *Cmd, sm *shardmanager.ShardManager) (*CmdRes, error) {
shard := sm.GetShardForKey("-")
return evalPING(c, shard.Thread.Store())
}
  • Purpose: Executes the PING command. It retrieves a shard and calls evalPING.
  • Parameters:
    • c: A pointer to the Cmd struct, representing the command.
    • sm: A pointer to a shardmanager.ShardManager struct.
  • Returns:
    • A pointer to a CmdRes struct, representing the command result.
    • An error, if any.

Getting Started Relevance