mirror of
https://github.com/postmannen/ctrl.git
synced 2025-01-09 21:49:16 +00:00
79 lines
2.3 KiB
Go
79 lines
2.3 KiB
Go
|
package steward
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/gob"
|
||
|
"fmt"
|
||
|
)
|
||
|
|
||
|
// --- Message
|
||
|
|
||
|
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
|
||
|
// 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{}
|
||
|
}
|
||
|
|
||
|
// gobEncodePayload will encode the message structure along with its
|
||
|
// valued in gob binary format.
|
||
|
// TODO: Check if it adds value to compress with gzip.
|
||
|
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"`
|
||
|
// method, what is this message doing, etc. shellCommand, syslog, etc.
|
||
|
Method Method `json:"method" yaml:"method"`
|
||
|
// messageCh is the channel for receiving new content to be sent
|
||
|
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.
|
||
|
func newSubject(method Method, commandOrEvent CommandOrEvent, node string) Subject {
|
||
|
return Subject{
|
||
|
ToNode: node,
|
||
|
CommandOrEvent: commandOrEvent,
|
||
|
Method: method,
|
||
|
messageCh: make(chan Message),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// subjectName is the complete representation of a subject
|
||
|
type subjectName string
|
||
|
|
||
|
func (s Subject) name() subjectName {
|
||
|
return subjectName(fmt.Sprintf("%s.%s.%s", s.Method, s.CommandOrEvent, s.ToNode))
|
||
|
}
|