1
0
Fork 0
mirror of https://github.com/postmannen/ctrl.git synced 2025-03-05 14:56:49 +00:00

methods are being got'n from map, and executed if it exist

This commit is contained in:
postmannen 2021-02-11 15:51:07 +01:00
parent ed3ae1dbc3
commit 63c64aa669
3 changed files with 8 additions and 35 deletions

View file

@ -57,7 +57,7 @@ func (ma MethodsAvailable) CheckIfExists(m Method) (methodHandler, bool) {
// ------------------------------------------------------------ // ------------------------------------------------------------
type methodHandler interface { type methodHandler interface {
handler(*server, Message, string) ([]byte, error) handler(server *server, message Message, node string) ([]byte, error)
} }
type methodCommandShellCommand struct{} type methodCommandShellCommand struct{}

View file

@ -7,7 +7,6 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"os/exec"
"sync" "sync"
"time" "time"
@ -423,40 +422,13 @@ func (s *server) subscriberHandler(natsConn *nats.Conn, node string, msg *nats.M
// that there was a problem like missing method to handle a specific // that there was a problem like missing method to handle a specific
// method etc. // method etc.
switch { switch {
case message.CommandOrEvent == Command: case message.CommandOrEvent == Command || message.CommandOrEvent == Event:
out, err := func(s *server, message Message, node string) ([]byte, error) { mf, ok := s.methodsAvailable.CheckIfExists(message.Method)
// Since the command to execute is at the first position in the if !ok {
// slice we need to slice it out. The arguments are at the // TODO: Check how errors should be handled here!!!
// remaining positions. log.Printf("*****METHOD MISSING \n")
c := message.Data[0]
a := message.Data[1:]
cmd := exec.Command(c, a...)
//cmd.Stdout = os.Stdout
out, err := cmd.CombinedOutput()
if err != nil {
log.Printf("error: execution of command failed: %v\n", err)
}
outMsg := []byte(fmt.Sprintf("confirmed from node: %v: messageID: %v\n---\n%s---", node, message.ID, out))
return outMsg, nil
}(s, message, node)
if err != nil {
// TODO: Send to error kernel ?
log.Printf("error: failed to execute event: %v\n", err)
} }
out, err := mf.handler(s, message, node)
// Send a confirmation message back to the publisher
natsConn.Publish(msg.Reply, out)
case message.CommandOrEvent == Event:
out, err := func(s *server, message Message, node string) ([]byte, error) {
for _, d := range message.Data {
s.logCh <- []byte(d)
}
outMsg := []byte("confirmed from: " + node + ": " + fmt.Sprint(message.ID))
return outMsg, nil
}(s, message, node)
if err != nil { if err != nil {
// TODO: Send to error kernel ? // TODO: Send to error kernel ?

View file

@ -16,3 +16,4 @@ some message sent from a ship for testing
some message sent from a ship for testing some message sent from a ship for testing
some message sent from a ship for testing some message sent from a ship for testing
some message sent from a ship for testing some message sent from a ship for testing
some message sent from a ship for testing