2021-02-02 12:06:37 +00:00
|
|
|
package steward
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2021-02-03 21:08:28 +00:00
|
|
|
"encoding/json"
|
2021-02-02 12:06:37 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/fsnotify/fsnotify"
|
|
|
|
)
|
|
|
|
|
|
|
|
// getMessagesFromFile will start a file watcher for the given directory
|
|
|
|
// and filename. 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.
|
2021-02-15 10:28:27 +00:00
|
|
|
func (s *server) getMessagesFromFile(directoryToCheck string, fileName string, inputFromFileCh chan []subjectAndMessage) {
|
2021-02-02 12:06:37 +00:00
|
|
|
fileUpdated := make(chan bool)
|
|
|
|
go fileWatcherStart(directoryToCheck, fileUpdated)
|
|
|
|
|
2021-02-05 06:25:12 +00:00
|
|
|
for range fileUpdated {
|
|
|
|
|
|
|
|
//load file, read it's content
|
|
|
|
b, err := readTruncateMessageFile(fileName)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error: reading file: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-02-05 09:47:07 +00:00
|
|
|
// Start on top again if the file did not contain
|
|
|
|
// any data.
|
|
|
|
if len(b) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-02-05 06:25:12 +00:00
|
|
|
// unmarshal the JSON into a struct
|
|
|
|
js, err := jsonFromFileData(b)
|
2021-02-17 09:27:39 +00:00
|
|
|
// fmt.Printf("*** OUTPUT AFTER UNMARSHALING JSON: %#v\n", js)
|
2021-02-05 06:25:12 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("%v\n", err)
|
2021-02-02 12:06:37 +00:00
|
|
|
}
|
|
|
|
|
2021-02-05 09:47:07 +00:00
|
|
|
for i := range js {
|
2021-02-10 06:25:44 +00:00
|
|
|
fmt.Printf("*** Checking message found in file: messageType type: %T, messagetype contains: %#v\n", js[i].Subject.CommandOrEvent, js[i].Subject.CommandOrEvent)
|
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-02-09 14:08:04 +00:00
|
|
|
js[i].Message.FromNode = node(s.nodeName)
|
2021-02-05 06:25:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send the data back to be consumed
|
2021-02-15 10:28:27 +00:00
|
|
|
inputFromFileCh <- js
|
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-02-10 09:14:49 +00:00
|
|
|
func jsonFromFileData(b []byte) ([]subjectAndMessage, error) {
|
|
|
|
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-17 09:27:39 +00:00
|
|
|
//fmt.Printf("*** OUTPUT DIRECTLY AFTER UNMARSHALING JSON: %#v\n", MsgSlice)
|
2021-02-04 10:46:58 +00:00
|
|
|
// TODO: Look into also marshaling from yaml and toml later
|
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-11 05:34:36 +00:00
|
|
|
sm := newSAM(m)
|
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-11 05:34:36 +00:00
|
|
|
func newSAM(m Message) subjectAndMessage {
|
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
|
|
|
|
|
|
|
|
sub := Subject{
|
|
|
|
ToNode: string(m.ToNode),
|
|
|
|
CommandOrEvent: mt.getHandler(m.Method).getKind(),
|
|
|
|
Method: m.Method,
|
|
|
|
}
|
|
|
|
|
|
|
|
sm := subjectAndMessage{
|
|
|
|
Subject: sub,
|
|
|
|
Message: m,
|
|
|
|
}
|
|
|
|
|
|
|
|
return sm
|
|
|
|
}
|
|
|
|
|
2021-02-02 12:06:37 +00:00
|
|
|
// readTruncateMessageFile, will read all the messages in the given
|
|
|
|
// file, and truncate the file after read.
|
|
|
|
// A []byte will be returned with the content read.
|
|
|
|
func readTruncateMessageFile(fileName string) ([]byte, error) {
|
|
|
|
|
|
|
|
f, err := os.OpenFile(fileName, os.O_APPEND|os.O_RDWR, os.ModeAppend)
|
|
|
|
if err != nil {
|
2021-03-02 12:46:02 +00:00
|
|
|
log.Printf("error: readTruncateMessageFile: Failed to open file: %v\n", err)
|
2021-02-02 12:06:37 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(f)
|
|
|
|
|
|
|
|
lines := []byte{}
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
lines = append(lines, scanner.Bytes()...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// empty the file after all is read
|
2021-02-03 21:08:28 +00:00
|
|
|
_, err = f.Seek(0, io.SeekStart)
|
2021-02-02 12:06:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("f.Seek failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = f.Truncate(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("f.Truncate failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return lines, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func fileWatcherStart(directoryToCheck string, fileUpdated chan bool) {
|
|
|
|
watcher, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Failed fsnotify.NewWatcher")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer watcher.Close()
|
|
|
|
|
|
|
|
done := make(chan bool)
|
|
|
|
go func() {
|
|
|
|
//Give a true value to updated so it reads the file the first time.
|
|
|
|
fileUpdated <- true
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event := <-watcher.Events:
|
|
|
|
if event.Op&fsnotify.Write == fsnotify.Write {
|
2021-02-16 03:57:54 +00:00
|
|
|
// log.Println("info: inmsg.txt file updated, processing input: ", event.Name)
|
2021-02-02 12:06:37 +00:00
|
|
|
//testing with an update chan to get updates
|
|
|
|
fileUpdated <- true
|
|
|
|
}
|
|
|
|
case err := <-watcher.Errors:
|
|
|
|
log.Println("error:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = watcher.Add(directoryToCheck)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error: watcher add: %v\n", err)
|
|
|
|
}
|
|
|
|
<-done
|
|
|
|
}
|