2021-02-02 12:06:37 +00:00
|
|
|
package steward
|
|
|
|
|
|
|
|
import (
|
2021-03-29 11:36:30 +00:00
|
|
|
"bytes"
|
2021-02-03 21:08:28 +00:00
|
|
|
"encoding/json"
|
2021-02-02 12:06:37 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
2021-03-29 11:36:30 +00:00
|
|
|
// readSocket will read the .sock file specified.
|
|
|
|
// It will take a channel of []byte as input, and it is in this
|
|
|
|
// channel the content of a file that has changed is returned.
|
|
|
|
func (s *server) readSocket(toRingbufferCh chan []subjectAndMessage) {
|
2021-02-05 06:25:12 +00:00
|
|
|
|
2021-03-29 11:36:30 +00:00
|
|
|
// Loop, and wait for new connections.
|
|
|
|
for {
|
2021-03-30 08:37:16 +00:00
|
|
|
conn, err := s.netListener.Accept()
|
2021-02-05 06:25:12 +00:00
|
|
|
if err != nil {
|
2021-03-29 11:36:30 +00:00
|
|
|
er := fmt.Errorf("error: failed to accept conn on socket: %v", err)
|
|
|
|
sendErrorLogMessage(toRingbufferCh, node(s.nodeName), er)
|
2021-02-05 06:25:12 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 11:36:30 +00:00
|
|
|
b := make([]byte, 65535)
|
|
|
|
_, err = conn.Read(b)
|
|
|
|
if err != nil {
|
|
|
|
er := fmt.Errorf("error: failed to read data from socket: %v", err)
|
|
|
|
sendErrorLogMessage(toRingbufferCh, node(s.nodeName), er)
|
2021-02-05 09:47:07 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-03-29 11:36:30 +00:00
|
|
|
b = bytes.Trim(b, "\x00")
|
|
|
|
|
2021-02-05 06:25:12 +00:00
|
|
|
// unmarshal the JSON into a struct
|
2021-03-29 11:36:30 +00:00
|
|
|
sam, err := convertBytesToSAM(b)
|
2021-02-05 06:25:12 +00:00
|
|
|
if err != nil {
|
2021-03-12 05:48:48 +00:00
|
|
|
er := fmt.Errorf("error: malformed json: %v", err)
|
2021-03-29 11:36:30 +00:00
|
|
|
sendErrorLogMessage(toRingbufferCh, node(s.nodeName), er)
|
2021-03-12 05:48:48 +00:00
|
|
|
continue
|
2021-02-02 12:06:37 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 11:36:30 +00:00
|
|
|
for i := range sam {
|
|
|
|
|
2021-02-10 09:14:49 +00:00
|
|
|
// Fill in the value for the FromNode field, so the receiver
|
|
|
|
// can check this field to know where it came from.
|
2021-03-29 11:36:30 +00:00
|
|
|
sam[i].Message.FromNode = node(s.nodeName)
|
2021-02-05 06:25:12 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 11:36:30 +00:00
|
|
|
// Send the SAM struct to be picked up by the ring buffer.
|
|
|
|
toRingbufferCh <- sam
|
2021-02-05 06:25:12 +00:00
|
|
|
}
|
2021-02-02 12:06:37 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 09:14:49 +00:00
|
|
|
type subjectAndMessage struct {
|
2021-02-04 10:46:58 +00:00
|
|
|
Subject `json:"subject" yaml:"subject"`
|
|
|
|
Message `json:"message" yaml:"message"`
|
2021-02-03 21:08:28 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 11:36:30 +00:00
|
|
|
// convertBytesToSAM will range over the byte representing a message given in
|
|
|
|
// json format. For each element found the Message type will be converted into
|
|
|
|
// a SubjectAndMessage type value and appended to a slice, and the slice is
|
|
|
|
// returned to the caller.
|
|
|
|
func convertBytesToSAM(b []byte) ([]subjectAndMessage, error) {
|
2021-02-10 09:14:49 +00:00
|
|
|
MsgSlice := []Message{}
|
2021-02-03 21:08:28 +00:00
|
|
|
|
2021-02-10 09:14:49 +00:00
|
|
|
err := json.Unmarshal(b, &MsgSlice)
|
2021-02-03 21:08:28 +00:00
|
|
|
if err != nil {
|
2021-02-04 10:46:58 +00:00
|
|
|
return nil, fmt.Errorf("error: unmarshal of file failed: %#v", err)
|
2021-02-03 21:08:28 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 09:14:49 +00:00
|
|
|
sam := []subjectAndMessage{}
|
|
|
|
|
2021-02-25 10:08:05 +00:00
|
|
|
// Range over all the messages parsed from json, and create a subject for
|
|
|
|
// each message.
|
2021-02-10 09:14:49 +00:00
|
|
|
for _, m := range MsgSlice {
|
2021-03-12 11:08:11 +00:00
|
|
|
sm, err := newSAM(m)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error: jsonFromFileData: %v\n", err)
|
|
|
|
continue
|
|
|
|
}
|
2021-02-10 09:14:49 +00:00
|
|
|
sam = append(sam, sm)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sam, nil
|
2021-02-03 21:08:28 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 05:34:36 +00:00
|
|
|
// newSAM will look up the correct values and value types to
|
2021-03-10 06:11:14 +00:00
|
|
|
// be used in a subject for a Message, and return the a combined structure
|
|
|
|
// of type subjectAndMessage.
|
2021-03-12 11:08:11 +00:00
|
|
|
func newSAM(m Message) (subjectAndMessage, error) {
|
2021-03-10 06:11:14 +00:00
|
|
|
// We need to create a tempory method type to look up the kind for the
|
|
|
|
// real method for the message.
|
|
|
|
var mt Method
|
|
|
|
|
2021-03-12 11:08:11 +00:00
|
|
|
//fmt.Printf("-- \n getKind contains: %v\n\n", mt.getHandler(m.Method).getKind())
|
|
|
|
|
|
|
|
tmpH := mt.getHandler(m.Method)
|
|
|
|
if tmpH == nil {
|
|
|
|
return subjectAndMessage{}, fmt.Errorf("error: method value did not exist in map")
|
|
|
|
}
|
|
|
|
|
2021-03-10 06:11:14 +00:00
|
|
|
sub := Subject{
|
|
|
|
ToNode: string(m.ToNode),
|
2021-03-12 11:08:11 +00:00
|
|
|
CommandOrEvent: tmpH.getKind(),
|
2021-03-10 06:11:14 +00:00
|
|
|
Method: m.Method,
|
|
|
|
}
|
|
|
|
|
|
|
|
sm := subjectAndMessage{
|
|
|
|
Subject: sub,
|
|
|
|
Message: m,
|
|
|
|
}
|
|
|
|
|
2021-03-12 11:08:11 +00:00
|
|
|
return sm, nil
|
2021-03-10 06:11:14 +00:00
|
|
|
}
|