2023-10-04 20:58:42 +00:00
|
|
|
package ctrl
|
2022-05-19 18:54:33 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-06-22 08:02:56 +00:00
|
|
|
"log"
|
2022-05-19 18:54:33 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// -----
|
|
|
|
|
|
|
|
// Handler for receiving hello messages.
|
2024-11-19 02:48:42 +00:00
|
|
|
func methodHello(proc process, message Message, node string) ([]byte, error) {
|
2022-05-19 18:54:33 +00:00
|
|
|
data := fmt.Sprintf("%v, Received hello from %#v\n", time.Now().Format("Mon Jan _2 15:04:05 2006"), message.FromNode)
|
|
|
|
|
|
|
|
fileName := message.FileName
|
|
|
|
folderTree := filepath.Join(proc.configuration.SubscribersDataFolder, message.Directory, string(message.FromNode))
|
|
|
|
|
|
|
|
// Check if folder structure exist, if not create it.
|
|
|
|
if _, err := os.Stat(folderTree); os.IsNotExist(err) {
|
2023-01-10 05:50:28 +00:00
|
|
|
err := os.MkdirAll(folderTree, 0770)
|
2022-05-19 18:54:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error: failed to create errorLog directory tree %v: %v", folderTree, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
er := fmt.Errorf("info: Creating subscribers data folder at %v", folderTree)
|
2024-03-27 11:48:17 +00:00
|
|
|
proc.errorKernel.logDebug(er)
|
2022-05-19 18:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open file and write data.
|
|
|
|
file := filepath.Join(folderTree, fileName)
|
2023-01-10 05:50:28 +00:00
|
|
|
//f, err := os.OpenFile(file, os.O_APPEND|os.O_RDWR|os.O_CREATE|os.O_SYNC, 0660)
|
|
|
|
f, err := os.OpenFile(file, os.O_TRUNC|os.O_RDWR|os.O_CREATE|os.O_SYNC, 0660)
|
2022-05-19 18:54:33 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2024-11-19 02:48:42 +00:00
|
|
|
er := fmt.Errorf("error: methodHello.handler: failed to open file: %v", err)
|
2022-05-19 18:54:33 +00:00
|
|
|
return nil, er
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
_, err = f.Write([]byte(data))
|
|
|
|
f.Sync()
|
|
|
|
if err != nil {
|
|
|
|
er := fmt.Errorf("error: methodEventTextLogging.handler: failed to write to file: %v", err)
|
2023-01-11 07:38:15 +00:00
|
|
|
proc.errorKernel.errSend(proc, message, er, logWarning)
|
2022-05-19 18:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------
|
|
|
|
|
|
|
|
// send the message to the procFuncCh which is running alongside the process
|
|
|
|
// and can hold registries and handle special things for an individual process.
|
|
|
|
proc.procFuncCh <- message
|
|
|
|
|
|
|
|
ackMsg := []byte("confirmed from: " + node + ": " + fmt.Sprint(message.ID))
|
|
|
|
return ackMsg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
|
|
|
// Handle the writing of error logs.
|
2024-11-19 02:48:42 +00:00
|
|
|
func methodErrorLog(proc process, message Message, node string) ([]byte, error) {
|
2022-05-19 18:54:33 +00:00
|
|
|
proc.metrics.promErrorMessagesReceivedTotal.Inc()
|
|
|
|
|
|
|
|
// 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) {
|
2023-01-10 05:50:28 +00:00
|
|
|
err := os.MkdirAll(folderTree, 0770)
|
2022-05-19 18:54:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error: failed to create errorLog directory tree %v: %v", folderTree, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
er := fmt.Errorf("info: Creating subscribers data folder at %v", folderTree)
|
2024-03-27 11:48:17 +00:00
|
|
|
proc.errorKernel.logDebug(er)
|
2022-05-19 18:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open file and write data.
|
|
|
|
file := filepath.Join(folderTree, fileName)
|
2023-01-10 05:50:28 +00:00
|
|
|
f, err := os.OpenFile(file, os.O_APPEND|os.O_RDWR|os.O_CREATE|os.O_SYNC, 0660)
|
2022-05-19 18:54:33 +00:00
|
|
|
if err != nil {
|
2024-11-19 02:48:42 +00:00
|
|
|
er := fmt.Errorf("error: methodErrorLog.handler: failed to open file: %v", err)
|
2022-05-19 18:54:33 +00:00
|
|
|
return nil, er
|
|
|
|
}
|
|
|
|
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", err)
|
2023-01-11 07:38:15 +00:00
|
|
|
proc.errorKernel.errSend(proc, message, er, logWarning)
|
2022-05-19 18:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ackMsg := []byte("confirmed from: " + node + ": " + fmt.Sprint(message.ID))
|
|
|
|
return ackMsg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
|
|
|
// Handler to write directly to console.
|
2023-10-04 20:58:42 +00:00
|
|
|
// This handler handles the writing to console.
|
2024-11-19 02:48:42 +00:00
|
|
|
func methodConsole(proc process, message Message, node string) ([]byte, error) {
|
2022-05-19 18:54:33 +00:00
|
|
|
|
|
|
|
switch {
|
2022-06-22 08:02:56 +00:00
|
|
|
case len(message.MethodArgs) > 0 && message.MethodArgs[0] == "stderr":
|
|
|
|
log.Printf("* DEBUG: MethodArgs: got stderr \n")
|
|
|
|
fmt.Fprintf(os.Stderr, "%v", string(message.Data))
|
|
|
|
fmt.Println()
|
2022-05-19 18:54:33 +00:00
|
|
|
default:
|
|
|
|
fmt.Fprintf(os.Stdout, "%v", string(message.Data))
|
|
|
|
fmt.Println()
|
|
|
|
}
|
|
|
|
|
|
|
|
ackMsg := []byte("confirmed from: " + node + ": " + fmt.Sprint(message.ID))
|
|
|
|
return ackMsg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
2022-05-22 04:36:02 +00:00
|
|
|
// handler to be used as a reply method when testing requests.
|
|
|
|
// We can then within the test listen on the testCh for received
|
|
|
|
// data and validate it.
|
2022-05-22 13:56:54 +00:00
|
|
|
// If no test is listening the data will be dropped.
|
2024-11-19 02:48:42 +00:00
|
|
|
func methodTest(proc process, message Message, node string) ([]byte, error) {
|
2022-05-22 04:36:02 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
// Try to send the received message data on the test channel. If we
|
|
|
|
// have a test started the data will be read from the testCh.
|
|
|
|
// If no test is reading from the testCh the data will be dropped.
|
|
|
|
select {
|
|
|
|
case proc.errorKernel.testCh <- message.Data:
|
|
|
|
default:
|
|
|
|
// drop.
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
ackMsg := []byte("confirmed from: " + node + ": " + fmt.Sprint(message.ID))
|
|
|
|
return ackMsg, nil
|
|
|
|
}
|