Client working and logging to terminal

This commit is contained in:
Fred Boniface 2023-07-19 13:22:55 +01:00
parent efcce8a632
commit 3266b147ee
5 changed files with 50 additions and 10 deletions

View File

@ -7,6 +7,7 @@
*.dll
*.so
*.dylib
*.txt
# Test binary, built with `go test -c`
*.test

1
.gitignore vendored
View File

@ -7,6 +7,7 @@
*.dll
*.so
*.dylib
*.txt
# Test binary, built with `go test -c`
*.test

View File

@ -10,5 +10,6 @@ import (
func main() {
log.Msg.Info("Initialised OwlBoard MQ Client " + helpers.Version)
dbAccess.PrintFromDbPackage()
defer messaging.Disconnect(messaging.Client)
messaging.Listen()
}

View File

@ -14,6 +14,7 @@ type nrodCredential struct {
pass string
}
// Fetches credentials from environment variables and exits if none provided.
func getCredentials() nrodCredential {
var nrod_user string = os.Getenv("OWL_LDB_CORPUSUSER")
var nrod_pass string = os.Getenv("OWL_LDB_CORPUSPASS")
@ -27,8 +28,9 @@ func getCredentials() nrodCredential {
}
}
var client = dial()
var Client = dial()
// Connects the STOMP file to the Network Rail MQ Server
func dial() *stomp.Conn {
var credentials nrodCredential = getCredentials()
conn, err := stomp.Dial("tcp", "publicdatafeeds.networkrail.co.uk:61618",
@ -38,19 +40,26 @@ func dial() *stomp.Conn {
)
if err != nil {
log.Msg.Fatal("Unable to connect to STOMP Client", zap.String("err", err.Error()))
conn.MustDisconnect()
}
//defer disconnect(conn) // This exits as soon as the function has returned
log.Msg.Info("Initialised STOMP Client")
return conn
}
func disconnect(conn *stomp.Conn) {
// Handles graceful disconnection of the STOMP client, falls back to
// a force disconnect if this fails.
func Disconnect(conn *stomp.Conn) {
if conn != nil {
err := conn.Disconnect()
log.Msg.Warn("Disconnected STOMP Client")
if err != nil {
conn.MustDisconnect()
log.Msg.Error("STOMP Disconnection failed, forced disconnect")
}
return
}
log.Msg.Error("STOMP Disconnect failed, next connection attempt may fail")
}
// Register against the MQ Server and log each message for testing purposes

View File

@ -2,6 +2,8 @@ package messaging
import (
"fmt"
"io/ioutil"
"time"
"git.fjla.uk/owlboard/mq-client/log"
@ -9,19 +11,45 @@ import (
)
func Listen() {
sub, err := client.Subscribe("/topic/VSTP_ALL", stomp.AckAuto)
sub, err := Client.Subscribe("/topic/VSTP_ALL", stomp.AckAuto)
if err != nil {
log.Msg.Fatal("Unable to start subscription: " + err.Error())
}
log.Msg.Info("Subscription to VSTP topic successful")
msg := <-sub.C
if msg.Err != nil {
fmt.Println(msg.Err)
}
handle(msg)
go func() {
log.Msg.Debug("Message handler routine started")
for {
msg := <-sub.C
if msg.Err != nil {
fmt.Println(msg.Err)
}
handle(msg)
}
}()
select {}
}
var count uint64 = 0
func handle(msg *stomp.Message) {
log.Msg.Info("STOMP message received")
fmt.Println(msg)
count++
log.Msg.Info("Message count: " + fmt.Sprint(count))
fmt.Println(string(msg.Body))
saveToFile(string(msg.Body))
}
func saveToFile(msg string) {
timestamp := time.Now().Format("2006-01-02T15:04:05")
path := fmt.Sprintf("%s-msg.txt", timestamp)
err := ioutil.WriteFile(path, []byte(msg), 0644)
if err != nil {
log.Msg.Error("Error saving message: " + err.Error())
return
}
log.Msg.Info("Saved message to: " + path)
}