2021-01-28 10:17:54 +00:00
|
|
|
// Notes:
|
2021-02-01 10:13:38 +00:00
|
|
|
package steward
|
2021-01-25 14:23:00 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/gob"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2021-02-10 09:14:49 +00:00
|
|
|
"os"
|
2021-01-28 13:58:16 +00:00
|
|
|
"sync"
|
2021-01-25 14:23:00 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/nats-io/nats.go"
|
|
|
|
)
|
|
|
|
|
2021-02-11 14:39:19 +00:00
|
|
|
type Message struct {
|
|
|
|
ToNode node `json:"toNode" yaml:"toNode"`
|
|
|
|
// The Unique ID of the message
|
|
|
|
ID int `json:"id" yaml:"id"`
|
|
|
|
// The actual data in the message
|
|
|
|
// TODO: Change this to a slice instead...or maybe use an
|
|
|
|
// interface type here to handle several data types ?
|
|
|
|
Data []string `json:"data" yaml:"data"`
|
|
|
|
// The type of the message being sent
|
|
|
|
CommandOrEvent CommandOrEvent `json:"commandOrEvent" yaml:"commandOrEvent"`
|
|
|
|
// method, what is this message doing, etc. shellCommand, syslog, etc.
|
|
|
|
Method Method `json:"method" yaml:"method"`
|
|
|
|
FromNode node
|
2021-02-16 11:29:15 +00:00
|
|
|
// done is used to signal when a message is fully processed.
|
|
|
|
// This is used when choosing when to move the message from
|
|
|
|
// the ringbuffer into the time series log.
|
|
|
|
done chan struct{}
|
2021-02-11 14:39:19 +00:00
|
|
|
}
|
|
|
|
|
2021-01-28 10:17:54 +00:00
|
|
|
// server is the structure that will hold the state about spawned
|
|
|
|
// processes on a local instance.
|
2021-01-27 13:02:57 +00:00
|
|
|
type server struct {
|
|
|
|
natsConn *nats.Conn
|
2021-01-28 10:17:54 +00:00
|
|
|
// TODO: sessions should probably hold a slice/map of processes ?
|
2021-02-03 11:55:02 +00:00
|
|
|
processes map[subjectName]process
|
2021-01-28 10:17:54 +00:00
|
|
|
// The last processID created
|
|
|
|
lastProcessID int
|
2021-02-04 12:26:10 +00:00
|
|
|
// The name of the node
|
|
|
|
nodeName string
|
|
|
|
mu sync.Mutex
|
|
|
|
// The channel where we receive new messages from the outside to
|
|
|
|
// insert into the system for being processed
|
2021-02-15 10:28:27 +00:00
|
|
|
inputFromFileCh chan []subjectAndMessage
|
2021-02-05 09:47:07 +00:00
|
|
|
// errorCh is used to report errors from a process
|
|
|
|
// NB: Implementing this as an int to report for testing
|
2021-02-08 05:02:54 +00:00
|
|
|
errorCh chan errProcess
|
2021-02-05 12:56:42 +00:00
|
|
|
// errorKernel
|
|
|
|
errorKernel *errorKernel
|
2021-02-10 04:11:48 +00:00
|
|
|
// TODO: replace this with some structure to hold the logCh value
|
|
|
|
logCh chan []byte
|
2021-02-10 13:29:17 +00:00
|
|
|
// used to check if the methods specified in message is valid
|
|
|
|
methodsAvailable MethodsAvailable
|
|
|
|
// used to check if the commandOrEvent specified in message is valid
|
|
|
|
commandOrEventAvailable CommandOrEventAvailable
|
2021-01-27 13:02:57 +00:00
|
|
|
}
|
|
|
|
|
2021-01-28 10:17:54 +00:00
|
|
|
// newServer will prepare and return a server type
|
2021-02-01 10:13:38 +00:00
|
|
|
func NewServer(brokerAddress string, nodeName string) (*server, error) {
|
|
|
|
conn, err := nats.Connect(brokerAddress, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error: nats.Connect failed: %v\n", err)
|
|
|
|
}
|
|
|
|
|
2021-02-10 13:29:17 +00:00
|
|
|
var m Method
|
|
|
|
var co CommandOrEvent
|
|
|
|
|
2021-02-01 12:41:04 +00:00
|
|
|
s := &server{
|
2021-02-10 13:29:17 +00:00
|
|
|
nodeName: nodeName,
|
|
|
|
natsConn: conn,
|
|
|
|
processes: make(map[subjectName]process),
|
2021-02-15 10:28:27 +00:00
|
|
|
inputFromFileCh: make(chan []subjectAndMessage),
|
2021-02-10 13:29:17 +00:00
|
|
|
errorCh: make(chan errProcess, 2),
|
|
|
|
logCh: make(chan []byte),
|
|
|
|
methodsAvailable: m.GetMethodsAvailable(),
|
|
|
|
commandOrEventAvailable: co.GetCommandOrEventAvailable(),
|
2021-02-01 12:41:04 +00:00
|
|
|
}
|
2021-01-29 05:09:48 +00:00
|
|
|
|
2021-02-05 12:56:42 +00:00
|
|
|
// Start the error kernel that will do all the error handling
|
|
|
|
// not done within a process.
|
|
|
|
s.errorKernel = newErrorKernel()
|
|
|
|
s.errorKernel.startErrorKernel(s.errorCh)
|
|
|
|
|
2021-02-05 06:25:12 +00:00
|
|
|
return s, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-02-10 06:25:44 +00:00
|
|
|
// Start will spawn up all the defined subscriber processes.
|
|
|
|
// Spawning of publisher processes is done on the fly by checking
|
|
|
|
// if there is publisher process for a given message subject. This
|
|
|
|
// checking is also started here in Start by calling handleMessagesToPublish.
|
2021-02-10 04:11:48 +00:00
|
|
|
func (s *server) Start() {
|
2021-02-05 09:47:07 +00:00
|
|
|
// Start the checking the input file for new messages from operator.
|
2021-02-15 10:28:27 +00:00
|
|
|
go s.getMessagesFromFile("./", "inmsg.txt", s.inputFromFileCh)
|
2021-02-02 12:06:37 +00:00
|
|
|
|
2021-02-10 09:14:49 +00:00
|
|
|
// Start the textLogging service that will run on the subscribers
|
2021-02-10 04:11:48 +00:00
|
|
|
// TODO: Figure out how to structure event services like these
|
|
|
|
go s.startTextLogging(s.logCh)
|
|
|
|
|
|
|
|
// Start a subscriber for shellCommand messages
|
|
|
|
{
|
|
|
|
fmt.Printf("nodeName: %#v\n", s.nodeName)
|
2021-02-10 09:14:49 +00:00
|
|
|
sub := newSubject(s.nodeName, "command", "shellCommand")
|
2021-02-10 04:11:48 +00:00
|
|
|
proc := s.processPrepareNew(sub, s.errorCh, processKindSubscriber)
|
|
|
|
// fmt.Printf("*** %#v\n", proc)
|
|
|
|
go s.processSpawnWorker(proc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start a subscriber for textLogging messages
|
|
|
|
{
|
|
|
|
fmt.Printf("nodeName: %#v\n", s.nodeName)
|
2021-02-10 09:14:49 +00:00
|
|
|
sub := newSubject(s.nodeName, "event", "textLogging")
|
2021-02-10 04:11:48 +00:00
|
|
|
proc := s.processPrepareNew(sub, s.errorCh, processKindSubscriber)
|
|
|
|
// fmt.Printf("*** %#v\n", proc)
|
|
|
|
go s.processSpawnWorker(proc)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Second * 2)
|
2021-02-10 06:25:44 +00:00
|
|
|
s.printProcessesMap()
|
|
|
|
|
2021-02-16 11:29:15 +00:00
|
|
|
s.handleMessagesInRingbuffer()
|
2021-02-05 09:47:07 +00:00
|
|
|
|
|
|
|
select {}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-02-10 06:25:44 +00:00
|
|
|
func (s *server) printProcessesMap() {
|
|
|
|
fmt.Println("--------------------------------------------------------------------------------------------")
|
|
|
|
fmt.Printf("*** Output of processes map :\n")
|
|
|
|
for _, v := range s.processes {
|
|
|
|
fmt.Printf("*** - : %v\n", v)
|
|
|
|
}
|
|
|
|
fmt.Println("--------------------------------------------------------------------------------------------")
|
|
|
|
}
|
|
|
|
|
2021-02-05 09:47:07 +00:00
|
|
|
// handleNewOperatorMessages will handle all the new operator messages
|
|
|
|
// given to the system, and route them to the correct subject queue.
|
2021-02-16 11:29:15 +00:00
|
|
|
func (s *server) handleMessagesInRingbuffer() {
|
2021-02-12 10:21:51 +00:00
|
|
|
// Prepare and start a new ring buffer
|
|
|
|
const bufferSize int = 100
|
|
|
|
rb := newringBuffer(bufferSize)
|
|
|
|
inCh := make(chan subjectAndMessage)
|
2021-02-16 13:29:32 +00:00
|
|
|
outCh := make(chan samDBValue)
|
2021-02-12 10:21:51 +00:00
|
|
|
rb.start(inCh, outCh)
|
|
|
|
|
|
|
|
// Start reading new messages received on the incomming message
|
|
|
|
// pipe requested by operator, and fill them into the buffer.
|
2021-02-03 13:53:25 +00:00
|
|
|
go func() {
|
2021-02-15 10:28:27 +00:00
|
|
|
for samSlice := range s.inputFromFileCh {
|
2021-02-12 10:21:51 +00:00
|
|
|
for _, sam := range samSlice {
|
|
|
|
inCh <- sam
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(inCh)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Process the messages that are in the ring buffer. Check and
|
|
|
|
// send if there are a specific subject for it, and no subject
|
|
|
|
// exist throw an error.
|
|
|
|
go func() {
|
2021-02-16 13:29:32 +00:00
|
|
|
for samTmp := range outCh {
|
|
|
|
sam := samTmp.Data
|
2021-02-12 10:21:51 +00:00
|
|
|
// Check if the format of the message is correct.
|
|
|
|
// TODO: Send a message to the error kernel here that
|
|
|
|
// it was unable to process the message with the reason
|
|
|
|
// why ?
|
|
|
|
if _, ok := s.methodsAvailable.CheckIfExists(sam.Message.Method); !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !s.commandOrEventAvailable.CheckIfExists(sam.Message.CommandOrEvent) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adding a label here so we are able to redo the sending
|
|
|
|
// of the last message if a process with specified subject
|
|
|
|
// is not present. The process will then be created, and
|
|
|
|
// the code will loop back to the redo: label.
|
|
|
|
|
|
|
|
redo:
|
|
|
|
m := sam.Message
|
|
|
|
subjName := sam.Subject.name()
|
2021-02-17 09:27:39 +00:00
|
|
|
// DEBUG: fmt.Printf("** handleNewOperatorMessages: message: %v, ** subject: %#v\n", m, sam.Subject)
|
2021-02-12 10:21:51 +00:00
|
|
|
_, ok := s.processes[subjName]
|
|
|
|
if ok {
|
|
|
|
log.Printf("info: found the specific subject: %v\n", subjName)
|
|
|
|
// Put the message on the correct process's messageCh
|
|
|
|
s.processes[subjName].subject.messageCh <- m
|
|
|
|
} else {
|
|
|
|
// If a publisher do not exist for the given subject, create it, and
|
|
|
|
// by using the goto at the end redo the process for this specific message.
|
|
|
|
log.Printf("info: did not find that specific subject, starting new process for subject: %v\n", subjName)
|
|
|
|
|
|
|
|
sub := newSubject(sam.Subject.Node, sam.Subject.CommandOrEvent, sam.Subject.Method)
|
|
|
|
proc := s.processPrepareNew(sub, s.errorCh, processKindPublisher)
|
|
|
|
// fmt.Printf("*** %#v\n", proc)
|
|
|
|
go s.processSpawnWorker(proc)
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 500)
|
|
|
|
s.printProcessesMap()
|
|
|
|
// Now when the process is spawned we jump back to the redo: label.
|
|
|
|
goto redo
|
2021-02-03 13:53:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2021-01-28 13:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type node string
|
|
|
|
|
2021-02-02 12:39:24 +00:00
|
|
|
// subject contains the representation of a subject to be used with one
|
|
|
|
// specific process
|
2021-02-03 21:08:28 +00:00
|
|
|
type Subject struct {
|
2021-02-02 12:39:24 +00:00
|
|
|
// node, the name of the node
|
2021-02-04 10:46:58 +00:00
|
|
|
Node string `json:"node" yaml:"node"`
|
2021-02-02 12:39:24 +00:00
|
|
|
// messageType, command/event
|
2021-02-10 06:25:44 +00:00
|
|
|
CommandOrEvent CommandOrEvent `json:"commandOrEvent" yaml:"commandOrEvent"`
|
2021-02-10 09:14:49 +00:00
|
|
|
// method, what is this message doing, etc. shellCommand, syslog, etc.
|
2021-02-10 13:29:17 +00:00
|
|
|
Method Method `json:"method" yaml:"method"`
|
2021-02-03 13:53:25 +00:00
|
|
|
// messageCh is the channel for receiving new content to be sent
|
|
|
|
messageCh chan Message
|
2021-02-02 12:39:24 +00:00
|
|
|
}
|
|
|
|
|
2021-02-03 21:08:28 +00:00
|
|
|
// newSubject will return a new variable of the type subject, and insert
|
|
|
|
// all the values given as arguments. It will also create the channel
|
|
|
|
// to receive new messages on the specific subject.
|
2021-02-10 13:29:17 +00:00
|
|
|
func newSubject(node string, commandOrEvent CommandOrEvent, method Method) Subject {
|
2021-02-03 21:08:28 +00:00
|
|
|
return Subject{
|
2021-02-10 06:25:44 +00:00
|
|
|
Node: node,
|
|
|
|
CommandOrEvent: commandOrEvent,
|
|
|
|
Method: method,
|
|
|
|
messageCh: make(chan Message),
|
2021-02-03 21:08:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-09 10:16:02 +00:00
|
|
|
// subjectName is the complete representation of a subject
|
2021-02-03 11:55:02 +00:00
|
|
|
type subjectName string
|
|
|
|
|
2021-02-03 21:08:28 +00:00
|
|
|
func (s Subject) name() subjectName {
|
2021-02-10 06:25:44 +00:00
|
|
|
return subjectName(fmt.Sprintf("%s.%s.%s", s.Node, s.CommandOrEvent, s.Method))
|
2021-02-03 11:55:02 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 10:52:08 +00:00
|
|
|
// processKind are either kindSubscriber or kindPublisher, and are
|
|
|
|
// used to distinguish the kind of process to spawn and to know
|
|
|
|
// the process kind put in the process map.
|
2021-02-09 10:16:02 +00:00
|
|
|
type processKind string
|
|
|
|
|
|
|
|
const (
|
2021-02-09 10:52:08 +00:00
|
|
|
processKindSubscriber processKind = "subscriber"
|
|
|
|
processKindPublisher processKind = "publisher"
|
2021-02-09 10:16:02 +00:00
|
|
|
)
|
|
|
|
|
2021-01-28 13:58:16 +00:00
|
|
|
// process are represent the communication to one individual host
|
|
|
|
type process struct {
|
|
|
|
messageID int
|
2021-02-03 07:28:21 +00:00
|
|
|
// the subject used for the specific process. One process
|
|
|
|
// can contain only one sender on a message bus, hence
|
|
|
|
// also one subject
|
2021-02-03 21:08:28 +00:00
|
|
|
subject Subject
|
2021-01-28 13:58:16 +00:00
|
|
|
// Put a node here to be able know the node a process is at.
|
|
|
|
// NB: Might not be needed later on.
|
|
|
|
node node
|
|
|
|
// The processID for the current process
|
|
|
|
processID int
|
|
|
|
// errorCh is used to report errors from a process
|
|
|
|
// NB: Implementing this as an int to report for testing
|
2021-02-09 10:52:08 +00:00
|
|
|
errorCh chan errProcess
|
|
|
|
processKind processKind
|
2021-01-28 10:17:54 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 12:41:04 +00:00
|
|
|
// prepareNewProcess will set the the provided values and the default
|
|
|
|
// values for a process.
|
2021-02-09 10:52:08 +00:00
|
|
|
func (s *server) processPrepareNew(subject Subject, errCh chan errProcess, processKind processKind) process {
|
2021-02-03 08:06:37 +00:00
|
|
|
// create the initial configuration for a sessions communicating with 1 host process.
|
2021-01-28 10:17:54 +00:00
|
|
|
s.lastProcessID++
|
|
|
|
proc := process{
|
2021-02-09 10:52:08 +00:00
|
|
|
messageID: 0,
|
|
|
|
subject: subject,
|
|
|
|
node: node(subject.Node),
|
|
|
|
processID: s.lastProcessID,
|
|
|
|
errorCh: errCh,
|
|
|
|
processKind: processKind,
|
2021-02-03 13:53:25 +00:00
|
|
|
//messageCh: make(chan Message),
|
2021-01-27 13:02:57 +00:00
|
|
|
}
|
2021-01-28 10:17:54 +00:00
|
|
|
|
|
|
|
return proc
|
|
|
|
}
|
|
|
|
|
2021-02-01 12:41:04 +00:00
|
|
|
// spawnProcess will spawn a new process. It will give the process
|
|
|
|
// the next available ID, and also add the process to the processes
|
|
|
|
// map.
|
2021-02-08 05:02:54 +00:00
|
|
|
func (s *server) processSpawnWorker(proc process) {
|
2021-02-03 21:08:28 +00:00
|
|
|
s.mu.Lock()
|
2021-02-03 13:53:25 +00:00
|
|
|
// We use the full name of the subject to identify a unique
|
|
|
|
// process. We can do that since a process can only handle
|
|
|
|
// one message queue.
|
2021-02-03 11:55:02 +00:00
|
|
|
s.processes[proc.subject.name()] = proc
|
2021-02-03 21:08:28 +00:00
|
|
|
s.mu.Unlock()
|
2021-01-27 13:02:57 +00:00
|
|
|
|
2021-02-02 12:06:37 +00:00
|
|
|
// TODO: I think it makes most sense that the messages would come to
|
|
|
|
// here from some other message-pickup-process, and that process will
|
|
|
|
// give the message to the correct publisher process. A channel that
|
|
|
|
// is listened on in the for loop below could be used to receive the
|
|
|
|
// messages from the message-pickup-process.
|
2021-02-10 06:25:44 +00:00
|
|
|
//
|
|
|
|
// Handle publisher workers
|
2021-02-09 10:52:08 +00:00
|
|
|
if proc.processKind == processKindPublisher {
|
|
|
|
for {
|
|
|
|
// Wait and read the next message on the message channel
|
|
|
|
m := <-proc.subject.messageCh
|
|
|
|
m.ID = s.processes[proc.subject.name()].messageID
|
|
|
|
messageDeliver(proc, m, s.natsConn)
|
2021-02-16 11:29:15 +00:00
|
|
|
m.done <- struct{}{}
|
2021-02-09 10:52:08 +00:00
|
|
|
|
|
|
|
// Increment the counter for the next message to be sent.
|
|
|
|
proc.messageID++
|
|
|
|
s.processes[proc.subject.name()] = proc
|
|
|
|
time.Sleep(time.Second * 1)
|
|
|
|
|
|
|
|
// NB: simulate that we get an error, and that we can send that
|
|
|
|
// out of the process and receive it in another thread.
|
|
|
|
ep := errProcess{
|
|
|
|
infoText: "process failed",
|
|
|
|
process: proc,
|
|
|
|
message: m,
|
|
|
|
errorActionCh: make(chan errorAction),
|
|
|
|
}
|
|
|
|
s.errorCh <- ep
|
2021-02-08 05:02:54 +00:00
|
|
|
|
2021-02-09 10:52:08 +00:00
|
|
|
// Wait for the response action back from the error kernel, and
|
|
|
|
// decide what to do. Should we continue, quit, or .... ?
|
|
|
|
switch <-ep.errorActionCh {
|
|
|
|
case errActionContinue:
|
|
|
|
log.Printf("The errAction was continue...so we're continuing\n")
|
|
|
|
}
|
2021-02-08 05:02:54 +00:00
|
|
|
}
|
2021-01-27 08:45:52 +00:00
|
|
|
}
|
2021-02-09 12:48:02 +00:00
|
|
|
|
2021-02-10 06:25:44 +00:00
|
|
|
// handle subscriber workers
|
2021-02-09 12:48:02 +00:00
|
|
|
if proc.processKind == processKindSubscriber {
|
|
|
|
subject := string(proc.subject.name())
|
|
|
|
|
|
|
|
// Subscribe will start up a Go routine under the hood calling the
|
|
|
|
// callback function specified when a new message is received.
|
|
|
|
_, err := s.natsConn.Subscribe(subject, func(msg *nats.Msg) {
|
|
|
|
// We start one handler per message received by using go routines here.
|
|
|
|
// This is for being able to reply back the current publisher who sent
|
|
|
|
// the message.
|
2021-02-10 06:25:44 +00:00
|
|
|
go s.subscriberHandler(s.natsConn, s.nodeName, msg)
|
2021-02-09 12:48:02 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error: Subscribe failed: %v\n", err)
|
|
|
|
}
|
|
|
|
}
|
2021-01-27 08:45:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 05:09:48 +00:00
|
|
|
func messageDeliver(proc process, message Message, natsConn *nats.Conn) {
|
2021-01-27 08:45:52 +00:00
|
|
|
for {
|
2021-02-15 10:28:27 +00:00
|
|
|
dataPayload, err := gobEncodeMessage(message)
|
2021-01-27 08:45:52 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error: createDataPayload: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := &nats.Msg{
|
2021-02-03 11:55:02 +00:00
|
|
|
Subject: string(proc.subject.name()),
|
2021-02-10 09:14:49 +00:00
|
|
|
// Subject: fmt.Sprintf("%s.%s.%s", proc.node, "command", "shellCommand"),
|
2021-01-29 05:09:48 +00:00
|
|
|
// Structure of the reply message are:
|
2021-01-29 13:22:36 +00:00
|
|
|
// reply.<nodename>.<message type>.<method>
|
2021-02-03 11:55:02 +00:00
|
|
|
Reply: fmt.Sprintf("reply.%s", proc.subject.name()),
|
2021-01-29 05:09:48 +00:00
|
|
|
Data: dataPayload,
|
2021-01-25 14:23:00 +00:00
|
|
|
}
|
|
|
|
|
2021-01-27 08:45:52 +00:00
|
|
|
// The SubscribeSync used in the subscriber, will get messages that
|
|
|
|
// are sent after it started subscribing, so we start a publisher
|
|
|
|
// that sends out a message every second.
|
|
|
|
//
|
|
|
|
// Create a subscriber for the reply message.
|
|
|
|
subReply, err := natsConn.SubscribeSync(msg.Reply)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error: nc.SubscribeSync failed: %v\n", err)
|
2021-02-10 09:14:49 +00:00
|
|
|
os.Exit(1)
|
2021-01-27 08:45:52 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Publish message
|
|
|
|
err = natsConn.PublishMsg(msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error: publish failed: %v\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait up until 10 seconds for a reply,
|
|
|
|
// continue and resend if to reply received.
|
|
|
|
msgReply, err := subReply.NextMsg(time.Second * 10)
|
|
|
|
if err != nil {
|
2021-02-03 11:55:02 +00:00
|
|
|
log.Printf("error: subRepl.NextMsg failed for node=%v, subject=%v: %v\n", proc.node, proc.subject.name(), err)
|
2021-01-27 08:45:52 +00:00
|
|
|
// did not receive a reply, continuing from top again
|
|
|
|
continue
|
|
|
|
}
|
2021-02-05 09:47:07 +00:00
|
|
|
log.Printf("publisher: received ACK: %s\n", msgReply.Data)
|
2021-01-27 08:45:52 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-27 13:02:57 +00:00
|
|
|
// gobEncodePayload will encode the message structure along with its
|
|
|
|
// valued in gob binary format.
|
2021-01-28 10:17:54 +00:00
|
|
|
// TODO: Check if it adds value to compress with gzip.
|
2021-02-15 10:28:27 +00:00
|
|
|
func gobEncodeMessage(m Message) ([]byte, error) {
|
2021-01-27 08:45:52 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
gobEnc := gob.NewEncoder(&buf)
|
|
|
|
err := gobEnc.Encode(m)
|
|
|
|
if err != nil {
|
2021-02-15 10:28:27 +00:00
|
|
|
return nil, fmt.Errorf("error: gob.Encode failed: %v", err)
|
2021-01-27 08:45:52 +00:00
|
|
|
}
|
2021-01-25 14:23:00 +00:00
|
|
|
|
2021-01-27 08:45:52 +00:00
|
|
|
return buf.Bytes(), nil
|
2021-01-25 14:23:00 +00:00
|
|
|
}
|
2021-02-10 06:25:44 +00:00
|
|
|
|
|
|
|
// handler will deserialize the message when a new message is received,
|
|
|
|
// check the MessageType field in the message to decide what kind of
|
|
|
|
// message it is and then it will check how to handle that message type,
|
|
|
|
// and handle it.
|
|
|
|
// This handler function should be started in it's own go routine,so
|
|
|
|
// one individual handler is started per message received so we can keep
|
|
|
|
// the state of the message being processed, and then reply back to the
|
|
|
|
// correct sending process's reply, meaning so we ACK back to the correct
|
|
|
|
// publisher.
|
|
|
|
func (s *server) subscriberHandler(natsConn *nats.Conn, node string, msg *nats.Msg) {
|
|
|
|
|
|
|
|
message := Message{}
|
|
|
|
|
|
|
|
// Create a buffer to decode the gob encoded binary data back
|
|
|
|
// to it's original structure.
|
|
|
|
buf := bytes.NewBuffer(msg.Data)
|
|
|
|
gobDec := gob.NewDecoder(buf)
|
|
|
|
err := gobDec.Decode(&message)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error: gob decoding failed: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
//fmt.Printf("%v\n", msg)
|
|
|
|
// TODO: Maybe the handling of the errors within the subscriber
|
|
|
|
// should also involve the error-kernel to report back centrally
|
|
|
|
// that there was a problem like missing method to handle a specific
|
|
|
|
// method etc.
|
|
|
|
switch {
|
2021-02-11 14:51:07 +00:00
|
|
|
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")
|
2021-02-10 06:25:44 +00:00
|
|
|
}
|
2021-02-11 14:51:07 +00:00
|
|
|
out, err := mf.handler(s, message, node)
|
2021-02-11 14:07:03 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
// TODO: Send to error kernel ?
|
|
|
|
log.Printf("error: failed to execute event: %v\n", err)
|
2021-02-10 06:25:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send a confirmation message back to the publisher
|
2021-02-11 14:07:03 +00:00
|
|
|
natsConn.Publish(msg.Reply, out)
|
2021-02-10 06:25:44 +00:00
|
|
|
default:
|
|
|
|
log.Printf("info: did not find that specific type of command: %#v\n", message.CommandOrEvent)
|
|
|
|
}
|
|
|
|
}
|