From 3f39a23f132070719d57c2ddc1b84ca0eabc8bf9 Mon Sep 17 00:00:00 2001 From: Kelvin Clement Mwinuka Date: Tue, 4 Jul 2023 04:59:14 +0800 Subject: [PATCH] Created plugin for PING command --- server/Makefile | 1 + server/plugins/commands/ping/ping.go | 48 ++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 server/plugins/commands/ping/ping.go diff --git a/server/Makefile b/server/Makefile index 151e202..2430143 100644 --- a/server/Makefile +++ b/server/Makefile @@ -1,4 +1,5 @@ build-plugins: + go build -buildmode=plugin -o bin/plugins/commands/command_ping.so plugins/commands/ping/ping.go go build -buildmode=plugin -o bin/plugins/commands/command_setget.so plugins/commands/setget/setget.go build-server: diff --git a/server/plugins/commands/ping/ping.go b/server/plugins/commands/ping/ping.go new file mode 100644 index 0000000..9173b51 --- /dev/null +++ b/server/plugins/commands/ping/ping.go @@ -0,0 +1,48 @@ +package main + +import "bufio" + +type Server interface { + GetData(key string) interface{} + SetData(key string, value interface{}) +} + +type plugin struct { + name string + commands []string + description string +} + +var Plugin plugin + +func (p *plugin) Name() string { + return p.name +} + +func (p *plugin) Commands() []string { + return p.commands +} + +func (p *plugin) Description() string { + return p.description +} + +func (p *plugin) HandleCommand(cmd []string, server interface{}, conn *bufio.Writer) { + switch len(cmd) { + default: + conn.Write([]byte("-Error wrong number of arguments for PING command\r\n\n")) + conn.Flush() + case 1: + conn.Write([]byte("+PONG\r\n\n")) + conn.Flush() + case 2: + conn.Write([]byte("+" + cmd[1] + "\r\n\n")) + conn.Flush() + } +} + +func init() { + Plugin.name = "PingCommand" + Plugin.commands = []string{"ping"} + Plugin.description = "Handle PING command" +}