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

126 lines
3.4 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-08-09 12:41:31 +00:00
conn, err := s.StewardSocket.Accept()
2021-02-05 06:25:12 +00:00
if err != nil {
er := fmt.Errorf("error: failed to accept conn on socket: %v", err)
2021-06-29 06:21:42 +00:00
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)
2021-06-29 06:21:42 +00:00
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)
2021-06-29 06:21:42 +00:00
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.
2021-06-29 06:21:42 +00:00
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
}
}
2021-07-02 06:38:44 +00:00
// TODO: Create the writer go routine for this socket.
func (s *server) writeStewSocket(toStewSocketCh []byte) {
//s.StewSockListener
}
2021-08-16 11:01:12 +00:00
// The subject are made up of different parts of the message field.
// To make things easier and to avoid figuring out what the subject
// is in all places we've created the concept of subjectAndMessage
// (sam) where we get the subject for the message once, and use the
// sam structure with subject alongside the message instead.
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-08-16 11:01:12 +00:00
// be used in a subject for a Message (sam), and return the a combined structure
2021-03-10 06:11:14 +00:00
// 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,
}
sm := subjectAndMessage{
Subject: sub,
Message: m,
}
return sm, nil
2021-03-10 06:11:14 +00:00
}