1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
package controlsvc
import (
"context"
"fmt"
)
type (
PingCommandType struct{}
PingCommand struct {
target string
}
)
func (t *PingCommandType) InitFromString(params string) (ControlCommand, error) {
if params == "" {
return nil, fmt.Errorf("no ping target")
}
c := &PingCommand{
target: params,
}
return c, nil
}
func (t *PingCommandType) InitFromJSON(config map[string]interface{}) (ControlCommand, error) {
target, ok := config["target"]
if !ok {
return nil, fmt.Errorf("no ping target")
}
targetStr, ok := target.(string)
if !ok {
return nil, fmt.Errorf("ping target must be string")
}
c := &PingCommand{
target: targetStr,
}
return c, nil
}
func (c *PingCommand) ControlFunc(ctx context.Context, nc NetceptorForControlCommand, _ ControlFuncOperations) (map[string]interface{}, error) {
pingTime, pingRemote, err := nc.Ping(ctx, c.target, nc.MaxForwardingHops())
cfr := make(map[string]interface{})
if err == nil {
cfr["Success"] = true
cfr["From"] = pingRemote
cfr["Time"] = pingTime
cfr["TimeStr"] = fmt.Sprint(pingTime)
} else {
cfr["Success"] = false
cfr["Error"] = err.Error()
}
return cfr, nil
}
|