From 8378253574458abe74db88908298d7e9ed0ec7dd Mon Sep 17 00:00:00 2001 From: Chteufleur Date: Sat, 14 Jan 2017 10:43:00 +0100 Subject: [PATCH] Add xmpp ping periodically in order to keep connection alive. --- conf/app.conf | 11 ++++++--- main.go | 6 +---- models/temperature/temperature.go | 38 ++++++++++++++-------------- xmpp/xmpp.go | 41 ++++++++++++++++++++++++------- 4 files changed, 59 insertions(+), 37 deletions(-) diff --git a/conf/app.conf b/conf/app.conf index ee2dffc..49c08c9 100644 --- a/conf/app.conf +++ b/conf/app.conf @@ -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 diff --git a/main.go b/main.go index d475429..13804a1 100644 --- a/main.go +++ b/main.go @@ -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") diff --git a/models/temperature/temperature.go b/models/temperature/temperature.go index f1931f3..51559ec 100644 --- a/models/temperature/temperature.go +++ b/models/temperature/temperature.go @@ -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}) } diff --git a/xmpp/xmpp.go b/xmpp/xmpp.go index 3a476a9..9aafc1d 100644 --- a/xmpp/xmpp.go +++ b/xmpp/xmpp.go @@ -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) {