Add xmpp ping periodically in order to keep connection alive.

This commit is contained in:
Chteufleur 2017-01-14 10:43:00 +01:00
parent aa1259b3dc
commit 8378253574
4 changed files with 59 additions and 37 deletions

View File

@ -2,10 +2,12 @@ appname = DataHouse
httpport = 8080
runmode = dev
mysqluser = "root"
mysqlpass = "toto"
database = mysql
mysqluser = "datahouse_user"
mysqlpass = "datahouse_user_password"
mysqlurls = "127.0.0.1"
mysqldb = "orm_test"
mysqldb = "datahouse"
row_limit = 100000
sessionon = true
SessionProvider = memory
@ -17,4 +19,5 @@ SessionCookieLifeTime = 3600
JID = test@kingpenguin.tk
PWD = test
PORT = 5222
XMPP_DEBUG = false
TIMER_PING = 60
XMPP_DEBUG = true

View File

@ -62,7 +62,7 @@ func main() {
time.Sleep(1 * time.Second)
models.ChanRuns <- watchlog.EndRun
time.Sleep(1 * time.Second)
models.ChanRuns <- xmpp_manager.EndRun
go xmpp_manager.Run()
beego.Run()
}
@ -72,10 +72,6 @@ func reborn() {
action := <-models.ChanRuns
switch action {
case xmpp_manager.EndRun:
go xmpp_manager.Run()
log.Info("XMPP Started")
case watchlog.EndRun:
go watchlog.Run()
log.Info("Watchlog Started")

View File

@ -20,9 +20,9 @@ type TempTable struct {
var (
timezoneName, timezoneOffset = time.Now().Zone()
timezoneLocation = time.FixedZone(timezoneName, timezoneOffset)
deltaTimeCompressData = 1440 * time.Hour
log = logs.NewLogger(10000)
timezoneLocation = time.FixedZone(timezoneName, timezoneOffset)
deltaTimeCompressData = 1440 * time.Hour
log = logs.NewLogger(10000)
)
func init() {
@ -46,19 +46,19 @@ func AddData(sensor, value int64) {
}
func AddDataWithHorodate(id, sensor, value int64, horodateGMT time.Time) {
log.Info("Add Temperature {sensor: %s, value: %d, horodateGNT: %v}", sensor, value, horodateGMT)
o := orm.NewOrm()
o.Using(database.Alias)
log.Info("Add Temperature {sensor: %s, value: %d, horodateGNT: %v}", sensor, value, horodateGMT)
o := orm.NewOrm()
o.Using(database.Alias)
tmpTable := new(TempTable)
tmpTable.Id = id
tmpTable.Value = value
tmpTable.SensorID = sensor
tmpTable.HorodateGMT = horodateGMT
_, err := o.Insert(tmpTable)
if err != nil {
log.Info("Error on insert temperature ", err)
}
tmpTable := new(TempTable)
tmpTable.Id = id
tmpTable.Value = value
tmpTable.SensorID = sensor
tmpTable.HorodateGMT = horodateGMT
_, err := o.Insert(tmpTable)
if err != nil {
log.Info("Error on insert temperature ", err)
}
}
func GetAllTempForSensor(sensorId int64) []TempTable {
@ -112,8 +112,8 @@ func DeleteTemperatureBySenor(sensorId int64) {
}
func DeleteTemperatureById(tempId int64) {
log.Info("Delete temperatures id : %d", tempId)
o := orm.NewOrm()
o.Using(database.Alias)
o.Delete(&TempTable{Id: tempId})
log.Info("Delete temperatures id : %d", tempId)
o := orm.NewOrm()
o.Using(database.Alias)
o.Delete(&TempTable{Id: tempId})
}

View File

@ -1,10 +1,12 @@
package xmpp_manager
import (
"strconv"
"time"
"github.com/astaxie/beego"
"github.com/astaxie/beego/logs"
"git.kingpenguin.tk/chteufleur/datahouse.git/models"
"git.kingpenguin.tk/chteufleur/datahouse.git/models/relay"
"git.kingpenguin.tk/chteufleur/datahouse.git/models/variables"
"git.kingpenguin.tk/chteufleur/go-xmpp.git/src/xmpp"
@ -26,8 +28,6 @@ const (
Type_unsubscribed = "unsubscribed"
Type_probe = "probe"
Type_error = "error"
EndRun = "EndRunXMPP"
)
var (
@ -41,12 +41,20 @@ var (
stream = new(xmpp.Stream)
client = new(xmpp.XMPP)
ping = false
pingId int64 = 0
timerDoPing, _ = beego.AppConfig.Int("TIMER_PING")
Debug = false
)
func init() {
log.SetLogger(variables.LogType, variables.LogParams)
Debug = beego.AppConfig.String("XMPP_DEBUG") == "true"
if timerDoPing < 30 {
timerDoPing = 30
}
go doPing()
}
func must(v interface{}, err error) interface{} {
@ -59,6 +67,7 @@ func must(v interface{}, err error) interface{} {
func Run() {
// Create stream and configure it as a component connection.
log.Info("XMPP Run()")
jid = must(xmpp.ParseJID(JidStr)).(xmpp.JID)
addrs, err := xmpp.HomeServerAddrs(jid)
if err == nil && len(addrs) > 0 {
@ -66,13 +75,19 @@ func Run() {
client = must(xmpp.NewClientXMPP(stream, jid, passwdStr, &xmpp.ClientConfig{InsecureSkipVerify: true})).(*xmpp.XMPP)
mainXMPP()
time.Sleep(1 * time.Second)
}
log.Debug("xmpp.Run Ended")
models.ChanRuns <- EndRun
go Run()
}
func mainXMPP() {
SendPresence(Status_online, Type_available, "")
defer func() {
ping = false
log.Debug("mainXMPP Ended")
}()
SendPresence(Status_online, Type_available, "DataHouse")
ping = true
for x := range client.In {
switch v := x.(type) {
@ -90,14 +105,22 @@ func mainXMPP() {
log.Info("recv: %v", x)
}
}
}
// Send deconnexion
SendPresence(Status_offline, Type_unavailable, "")
log.Debug("mainXMPP Ended")
func doPing() {
for {
if ping {
iq := &xmpp.Iq{Id: strconv.FormatInt(pingId, 10), Type: xmpp.IQTypeGet, To: jid.Domain, From: jid.Full()}
iq.PayloadEncode(&xmpp.Ping{})
client.Out <- iq
pingId++
}
time.Sleep(time.Duration(timerDoPing) * time.Second)
}
}
func SendPresence(status, tpye, message string) {
client.Out <- xmpp.Presence{From: jid.Domain, Show: status, Type: tpye, Status: message}
client.Out <- xmpp.Presence{From: jid.Full(), Show: status, Type: tpye, Status: message}
}
func SendMessage(to, subject, message string) {