From dcefbff670266f3e2294071855132991300a1c67 Mon Sep 17 00:00:00 2001 From: postmannen Date: Wed, 31 May 2023 09:29:00 +0200 Subject: [PATCH] added initial write to socket --- methodREQToFile.go | 41 ---------------------------------- methodREQToFileAppend.go | 38 -------------------------------- requests_file_handling.go | 46 ++++++++++++++++++++++++++++++++------- 3 files changed, 38 insertions(+), 87 deletions(-) delete mode 100644 methodREQToFile.go delete mode 100644 methodREQToFileAppend.go diff --git a/methodREQToFile.go b/methodREQToFile.go deleted file mode 100644 index 715c505..0000000 --- a/methodREQToFile.go +++ /dev/null @@ -1,41 +0,0 @@ -func (m methodREQToFile) handler(proc process, message Message, node string) ([]byte, error) { - - // If it was a request type message we want to check what the initial messages - // method, so we can use that in creating the file name to store the data. - fileName, folderTree := selectFileNaming(message, proc) - - // Check if folder structure exist, if not create it. - if _, err := os.Stat(folderTree); os.IsNotExist(err) { - err := os.MkdirAll(folderTree, 0770) - if err != nil { - er := fmt.Errorf("error: methodREQToFile failed to create toFile directory tree: subject:%v, folderTree: %v, %v", proc.subject, folderTree, err) - proc.errorKernel.errSend(proc, message, er, logWarning) - - return nil, er - } - - er := fmt.Errorf("info: Creating subscribers data folder at %v", folderTree) - proc.errorKernel.logDebug(er, proc.configuration) - } - - // Open file and write data. - file := filepath.Join(folderTree, fileName) - f, err := os.OpenFile(file, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0755) - if err != nil { - er := fmt.Errorf("error: methodREQToFile.handler: failed to open file, check that you've specified a value for fileName in the message: directory: %v, fileName: %v, %v", message.Directory, message.FileName, err) - proc.errorKernel.errSend(proc, message, er, logWarning) - - return nil, err - } - defer f.Close() - - _, err = f.Write(message.Data) - f.Sync() - if err != nil { - er := fmt.Errorf("error: methodEventTextLogging.handler: failed to write to file: file: %v, %v", file, err) - proc.errorKernel.errSend(proc, message, er, logWarning) - } - - ackMsg := []byte("confirmed from: " + node + ": " + fmt.Sprint(message.ID)) - return ackMsg, nil -} \ No newline at end of file diff --git a/methodREQToFileAppend.go b/methodREQToFileAppend.go deleted file mode 100644 index 1df74f5..0000000 --- a/methodREQToFileAppend.go +++ /dev/null @@ -1,38 +0,0 @@ -func (m methodREQToFileAppend) handler(proc process, message Message, node string) ([]byte, error) { - - // If it was a request type message we want to check what the initial messages - // method, so we can use that in creating the file name to store the data. - fileName, folderTree := selectFileNaming(message, proc) - - // Check if folder structure exist, if not create it. - if _, err := os.Stat(folderTree); os.IsNotExist(err) { - err := os.MkdirAll(folderTree, 0770) - if err != nil { - er := fmt.Errorf("error: methodREQToFileAppend: failed to create toFileAppend directory tree:%v, subject: %v, %v", folderTree, proc.subject, err) - proc.errorKernel.errSend(proc, message, er, logWarning) - } - - er := fmt.Errorf("info: Creating subscribers data folder at %v", folderTree) - proc.errorKernel.logDebug(er, proc.configuration) - } - - // Open file and write data. - file := filepath.Join(folderTree, fileName) - f, err := os.OpenFile(file, os.O_APPEND|os.O_RDWR|os.O_CREATE|os.O_SYNC, 0660) - if err != nil { - er := fmt.Errorf("error: methodREQToFileAppend.handler: failed to open file: %v, %v", file, err) - proc.errorKernel.errSend(proc, message, er, logWarning) - return nil, err - } - defer f.Close() - - _, err = f.Write(message.Data) - f.Sync() - if err != nil { - er := fmt.Errorf("error: methodEventTextLogging.handler: failed to write to file : %v, %v", file, err) - proc.errorKernel.errSend(proc, message, er, logWarning) - } - - ackMsg := []byte("confirmed from: " + node + ": " + fmt.Sprint(message.ID)) - return ackMsg, nil -} diff --git a/requests_file_handling.go b/requests_file_handling.go index 441dcd6..887d383 100644 --- a/requests_file_handling.go +++ b/requests_file_handling.go @@ -3,23 +3,52 @@ package steward import ( "fmt" "io" + "io/fs" + "net" "os" "path/filepath" "github.com/hpcloud/tail" ) -func reqWriteFile(isAppend bool, proc process, message Message) error { +func reqWriteFileOrSocket(isAppend bool, proc process, message Message) error { // If it was a request type message we want to check what the initial messages // method, so we can use that in creating the file name to store the data. fileName, folderTree := selectFileNaming(message, proc) + file := filepath.Join(folderTree, fileName) + // Check the file is a unix socket, and if it is we write the + // data to the socket instead of writing it to a normal file. + fi, err := os.Stat(file) + if err != nil { + er := fmt.Errorf("error: methodREQToFile/Append failed to stat filepath: subject:%v, folderTree: %v, %v", proc.subject, folderTree, err) + + return er + } + + if fi.Mode().Type() == fs.ModeSocket { + // TODO: Write to socket + socket, err := net.Dial("unix", file) + if err != nil { + er := fmt.Errorf("error: methodREQToFile/Append could to open socket file for writing: subject:%v, folderTree: %v, %v", proc.subject, folderTree, err) + return er + } + defer socket.Close() + + _, err = socket.Write([]byte(message.Data)) + if err != nil { + er := fmt.Errorf("error: methodREQToFile/Append could not write to socket: subject:%v, folderTree: %v, %v", proc.subject, folderTree, err) + return er + } + + } + + // The file is a normal file and not a socket. // Check if folder structure exist, if not create it. if _, err := os.Stat(folderTree); os.IsNotExist(err) { err := os.MkdirAll(folderTree, 0770) if err != nil { er := fmt.Errorf("error: methodREQToFile/Append failed to create toFile directory tree: subject:%v, folderTree: %v, %v", proc.subject, folderTree, err) - proc.errorKernel.errSend(proc, message, er, logWarning) return er } @@ -37,13 +66,11 @@ func reqWriteFile(isAppend bool, proc process, message Message) error { } // Open file and write data. - file := filepath.Join(folderTree, fileName) f, err := os.OpenFile(file, fileFlag, 0755) if err != nil { er := fmt.Errorf("error: methodREQToFile/Append: failed to open file, check that you've specified a value for fileName in the message: directory: %v, fileName: %v, %v", message.Directory, message.FileName, err) - proc.errorKernel.errSend(proc, message, er, logWarning) - return err + return er } defer f.Close() @@ -51,7 +78,8 @@ func reqWriteFile(isAppend bool, proc process, message Message) error { f.Sync() if err != nil { er := fmt.Errorf("error: methodREQToFile/Append: failed to write to file: file: %v, %v", file, err) - proc.errorKernel.errSend(proc, message, er, logWarning) + + return er } return nil @@ -67,7 +95,8 @@ func (m methodREQToFileAppend) getKind() Event { // Handle appending data to file. func (m methodREQToFileAppend) handler(proc process, message Message, node string) ([]byte, error) { - reqWriteFile(true, proc, message) + err := reqWriteFileOrSocket(true, proc, message) + proc.errorKernel.errSend(proc, message, err, logWarning) ackMsg := []byte("confirmed from: " + node + ": " + fmt.Sprint(message.ID)) return ackMsg, nil @@ -86,7 +115,8 @@ func (m methodREQToFile) getKind() Event { // Handle writing to a file. Will truncate any existing data if the file did already // exist. func (m methodREQToFile) handler(proc process, message Message, node string) ([]byte, error) { - reqWriteFile(false, proc, message) + err := reqWriteFileOrSocket(false, proc, message) + proc.errorKernel.errSend(proc, message, err, logWarning) ackMsg := []byte("confirmed from: " + node + ": " + fmt.Sprint(message.ID)) return ackMsg, nil