timetable-mgr/src/messaging/client.go

47 lines
1.2 KiB
Go
Raw Normal View History

2023-07-18 00:25:13 +01:00
package messaging
2023-07-18 14:09:28 +01:00
import (
"git.fjla.uk/owlboard/timetable-mgr/helpers"
"git.fjla.uk/owlboard/timetable-mgr/log"
2023-07-18 14:09:28 +01:00
"github.com/go-stomp/stomp/v3"
)
var Client *stomp.Conn
2023-07-18 14:09:28 +01:00
func StompInit(cfg *helpers.Configuration) {
Client = dial(cfg.NrodUser, cfg.NrodPass)
2023-07-18 14:09:28 +01:00
}
2023-07-19 13:22:55 +01:00
// Connects the STOMP file to the Network Rail MQ Server
func dial(user, pass string) *stomp.Conn {
2023-07-18 14:09:28 +01:00
conn, err := stomp.Dial("tcp", "publicdatafeeds.networkrail.co.uk:61618",
stomp.ConnOpt.Login(user, pass),
2023-07-18 14:09:28 +01:00
stomp.ConnOpt.HeartBeat(15000, 15000),
stomp.ConnOpt.Header("client-id", user+"-mq-client"),
2023-07-18 14:09:28 +01:00
)
if err != nil {
log.Msg.Fatal("Unable to connect to STOMP Client: " + err.Error())
2023-07-19 13:22:55 +01:00
conn.MustDisconnect()
2023-07-18 14:09:28 +01:00
}
2023-07-19 13:22:55 +01:00
2023-07-19 01:18:55 +01:00
log.Msg.Info("Initialised STOMP Client")
2023-07-18 14:09:28 +01:00
return conn
}
2023-07-19 13:22:55 +01:00
// Handles graceful disconnection of the STOMP client, falls back to
// a force disconnect if this fails.
func Disconnect(conn *stomp.Conn) {
2023-07-18 14:09:28 +01:00
if conn != nil {
err := conn.Disconnect()
2023-07-19 13:22:55 +01:00
log.Msg.Warn("Disconnected STOMP Client")
2023-07-18 14:09:28 +01:00
if err != nil {
conn.MustDisconnect()
2023-07-19 13:22:55 +01:00
log.Msg.Error("STOMP Disconnection failed, forced disconnect")
2023-07-18 14:09:28 +01:00
}
2023-07-19 13:22:55 +01:00
return
2023-07-18 14:09:28 +01:00
}
2023-07-19 13:22:55 +01:00
log.Msg.Error("STOMP Disconnect failed, next connection attempt may fail")
2023-07-18 14:09:28 +01:00
}
2023-07-18 00:25:13 +01:00
// Register against the MQ Server and log each message for testing purposes