2021-02-24 14:43:31 +00:00
|
|
|
package steward
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/gob"
|
2021-04-09 16:20:04 +00:00
|
|
|
"encoding/json"
|
2021-02-24 14:43:31 +00:00
|
|
|
"fmt"
|
2021-04-03 05:33:03 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
2021-02-24 14:43:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// --- Message
|
|
|
|
|
|
|
|
type Message struct {
|
2021-03-11 16:14:43 +00:00
|
|
|
// The node to send the message to
|
2021-02-24 14:43:31 +00:00
|
|
|
ToNode node `json:"toNode" yaml:"toNode"`
|
|
|
|
// The Unique ID of the message
|
|
|
|
ID int `json:"id" yaml:"id"`
|
|
|
|
// The actual data in the message
|
|
|
|
Data []string `json:"data" yaml:"data"`
|
2021-04-13 11:50:00 +00:00
|
|
|
// Method, what is this message doing, etc. CLI, syslog, etc.
|
|
|
|
Method Method `json:"method" yaml:"method"`
|
|
|
|
// ReplyMethod, is the method to use for the reply message.
|
|
|
|
// By default the reply method will be set to log to file, but
|
|
|
|
// you can override it setting your own here.
|
|
|
|
ReplyMethod Method `json:"replyMethod" yaml:"replyMethod"`
|
|
|
|
// From what node the message originated
|
2021-02-24 14:43:31 +00:00
|
|
|
FromNode node
|
2021-04-15 08:33:44 +00:00
|
|
|
// ACKTimeout for waiting for an ack message
|
|
|
|
ACKTimeout int `json:"ACKTimeout" yaml:"ACKTimeout"`
|
|
|
|
// Resend retries
|
2021-03-11 11:07:09 +00:00
|
|
|
Retries int `json:"retries" yaml:"retries"`
|
2021-04-15 08:33:44 +00:00
|
|
|
// The ACK timeout of the new message created via a request event.
|
2021-04-15 08:52:38 +00:00
|
|
|
ReplyACKTimeout int `json:"replyACKTimeout" yaml:"replyACKTimeout"`
|
2021-03-11 16:14:43 +00:00
|
|
|
// The retries of the new message created via a request event.
|
2021-04-13 12:37:17 +00:00
|
|
|
ReplyRetries int `json:"replyRetries" yaml:"replyRetries"`
|
2021-03-11 16:14:43 +00:00
|
|
|
// Timeout for long a process should be allowed to operate
|
|
|
|
MethodTimeout int `json:"methodTimeout" yaml:"methodTimeout"`
|
2021-04-07 05:00:40 +00:00
|
|
|
// Directory is a string that can be used to create the
|
|
|
|
//directory structure when saving the result of some method.
|
|
|
|
// For example "syslog","metrics", or "metrics/mysensor"
|
|
|
|
// The type is typically used in the handler of a method.
|
|
|
|
Directory string `json:"directory" yaml:"directory"`
|
|
|
|
// FileExtension is used to be able to set a wanted extension
|
|
|
|
// on a file being saved as the result of data being handled
|
|
|
|
// by a method handler.
|
|
|
|
FileExtension string `json:"fileExtension" yaml:"fileExtension"`
|
2021-04-09 16:20:04 +00:00
|
|
|
// operation are used to give an opCmd and opArg's.
|
|
|
|
Operation Operation `json:"operation"`
|
2021-04-03 04:31:48 +00:00
|
|
|
// PreviousMessage are used for example if a reply message is
|
2021-06-17 07:36:42 +00:00
|
|
|
// generated and we also need a copy of the details of the the
|
2021-04-09 16:20:04 +00:00
|
|
|
// initial request message.
|
2021-04-03 04:31:48 +00:00
|
|
|
PreviousMessage *Message
|
|
|
|
|
2021-02-24 14:43:31 +00:00
|
|
|
// done is used to signal when a message is fully processed.
|
2021-04-03 04:31:48 +00:00
|
|
|
// This is used for signaling back to the ringbuffer that we are
|
|
|
|
// done with processing a message, and the message can be removed
|
|
|
|
// from the ringbuffer and into the time series log.
|
2021-03-11 16:14:43 +00:00
|
|
|
done chan struct{}
|
2021-02-24 14:43:31 +00:00
|
|
|
}
|
|
|
|
|
2021-04-09 16:20:04 +00:00
|
|
|
// ---
|
|
|
|
|
|
|
|
type Operation struct {
|
|
|
|
OpCmd string `json:"opCmd"`
|
|
|
|
OpArg json.RawMessage `json:"opArg"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
2021-04-01 08:05:14 +00:00
|
|
|
// gobEncodePayload will encode the message structure into gob
|
|
|
|
// binary format.
|
2021-02-24 14:43:31 +00:00
|
|
|
func gobEncodeMessage(m Message) ([]byte, error) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
gobEnc := gob.NewEncoder(&buf)
|
|
|
|
err := gobEnc.Encode(m)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error: gob.Encode failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Subject
|
|
|
|
|
|
|
|
type node string
|
|
|
|
|
|
|
|
// subject contains the representation of a subject to be used with one
|
|
|
|
// specific process
|
|
|
|
type Subject struct {
|
|
|
|
// node, the name of the node
|
|
|
|
ToNode string `json:"node" yaml:"toNode"`
|
|
|
|
// messageType, command/event
|
|
|
|
CommandOrEvent CommandOrEvent `json:"commandOrEvent" yaml:"commandOrEvent"`
|
2021-03-01 11:16:36 +00:00
|
|
|
// method, what is this message doing, etc. CLICommand, Syslog, etc.
|
2021-02-24 14:43:31 +00:00
|
|
|
Method Method `json:"method" yaml:"method"`
|
2021-03-09 06:43:55 +00:00
|
|
|
// messageCh is used by publisher kind processes to read new messages
|
|
|
|
// to be published. The content on this channel have been routed here
|
|
|
|
// from routeMessagesToPublish in *server.
|
|
|
|
// This channel is only used for publishing processes.
|
2021-02-24 14:43:31 +00:00
|
|
|
messageCh chan Message
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-04-03 05:33:03 +00:00
|
|
|
func newSubject(method Method, node string) Subject {
|
|
|
|
// Get the CommandOrEvent type for the Method.
|
|
|
|
ma := method.GetMethodsAvailable()
|
|
|
|
coe, ok := ma.methodhandlers[method]
|
|
|
|
if !ok {
|
2021-04-07 14:45:51 +00:00
|
|
|
log.Printf("error: no CommandOrEvent type specified for the method: %v\n", method)
|
2021-04-03 05:33:03 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2021-02-24 14:43:31 +00:00
|
|
|
return Subject{
|
|
|
|
ToNode: node,
|
2021-04-03 05:33:03 +00:00
|
|
|
CommandOrEvent: coe.getKind(),
|
2021-02-24 14:43:31 +00:00
|
|
|
Method: method,
|
|
|
|
messageCh: make(chan Message),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// subjectName is the complete representation of a subject
|
|
|
|
type subjectName string
|
|
|
|
|
|
|
|
func (s Subject) name() subjectName {
|
2021-04-02 18:36:21 +00:00
|
|
|
return subjectName(fmt.Sprintf("%s.%s.%s", s.ToNode, s.Method, s.CommandOrEvent))
|
2021-02-24 14:43:31 +00:00
|
|
|
}
|