1
0
Fork 0
mirror of https://github.com/postmannen/ctrl.git synced 2024-12-14 12:37:31 +00:00
ctrl/read_socket.go

117 lines
3 KiB
Go
Raw Normal View History

package steward
import (
"bytes"
"encoding/json"
"fmt"
"log"
)
// 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
// 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 {
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
}
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)
continue
}
b = bytes.Trim(b, "\x00")
2021-02-05 06:25:12 +00:00
// unmarshal the JSON into a struct
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)
sendErrorLogMessage(toRingbufferCh, node(s.nodeName), er)
2021-03-12 05:48:48 +00:00
continue
}
for i := range sam {
// Fill in the value for the FromNode field, so the receiver
// can check this field to know where it came from.
sam[i].Message.FromNode = node(s.nodeName)
2021-02-05 06:25:12 +00:00
}
// Send the SAM struct to be picked up by the ring buffer.
toRingbufferCh <- sam
conn.Close()
2021-02-05 06:25:12 +00:00
}
}
type subjectAndMessage struct {
2021-02-04 10:46:58 +00:00
Subject `json:"subject" yaml:"subject"`
Message `json:"message" yaml:"message"`
}
// 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) {
MsgSlice := []Message{}
err := json.Unmarshal(b, &MsgSlice)
if err != nil {
2021-02-04 10:46:58 +00:00
return nil, fmt.Errorf("error: unmarshal of file failed: %#v", err)
}
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.
for _, m := range MsgSlice {
sm, err := newSAM(m)
if err != nil {
log.Printf("error: jsonFromFileData: %v\n", err)
continue
}
sam = append(sam, sm)
}
return sam, nil
}
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.
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
//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),
CommandOrEvent: tmpH.getKind(),
2021-03-10 06:11:14 +00:00
Method: m.Method,
Label: m.Label,
2021-03-10 06:11:14 +00:00
}
sm := subjectAndMessage{
Subject: sub,
Message: m,
}
return sm, nil
2021-03-10 06:11:14 +00:00
}