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 {
handler(*server, Message, string) ([]byte, error)
handler(server *server, message Message, node string) ([]byte, error)
}
type methodCommandShellCommand struct{}

View file

@ -7,7 +7,6 @@ import (
"fmt"
"log"
"os"
"os/exec"
"sync"
"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
// method etc.
switch {
case message.CommandOrEvent == Command:
out, err := func(s *server, message Message, node string) ([]byte, error) {
// Since the command to execute is at the first position in the
// slice we need to slice it out. The arguments are at the
// remaining positions.
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)
case message.CommandOrEvent == Command || message.CommandOrEvent == Event:
mf, ok := s.methodsAvailable.CheckIfExists(message.Method)
if !ok {
// TODO: Check how errors should be handled here!!!
log.Printf("*****METHOD MISSING \n")
}
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)
}
// 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)
out, err := mf.handler(s, message, node)
if err != nil {
// 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