1
0
Fork 0

Fix shutdown bug

This commit is contained in:
Chteufleur 2015-11-26 18:59:19 +01:00
parent c7d1d71c95
commit e11f5d3f98
3 changed files with 29 additions and 27 deletions

41
main.go
View File

@ -7,15 +7,15 @@ import (
"github.com/Philipp15b/go-steam/internal/steamlang" "github.com/Philipp15b/go-steam/internal/steamlang"
"github.com/jimlawless/cfg" "github.com/jimlawless/cfg"
"bufio"
"log" "log"
"os" "os"
"strings" "os/signal"
"syscall"
"time" "time"
) )
const ( const (
Version = "go-xmpp4steam v0.1.3" Version = "go-xmpp4steam v0.1.4.0"
configurationFilePath = "xmpp4steam.cfg" configurationFilePath = "xmpp4steam.cfg"
) )
@ -54,13 +54,19 @@ func main() {
go gatewaySteamXmppPresence() go gatewaySteamXmppPresence()
go steam.Run() go steam.Run()
xmpp.Run() go xmpp.Run()
// inputStop() sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, os.Interrupt)
signal.Notify(sigchan, syscall.SIGTERM)
signal.Notify(sigchan, os.Kill)
<-sigchan
steam.Disconnect() steam.Disconnect()
xmpp.Disconnect() xmpp.Disconnect()
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
log.Println("Exit main()")
} }
// XMPP -> Steam gateways // XMPP -> Steam gateways
@ -78,6 +84,9 @@ func gatewayXmppSteamAction() {
if steam.IsConnected() { if steam.IsConnected() {
steam.Disconnect() steam.Disconnect()
} }
case xmpp.ActionMainMethodEnded:
go xmpp.Run()
} }
} }
} }
@ -134,6 +143,9 @@ func gatewaySteamXmppAction() {
disconnectAllSteamUser() disconnectAllSteamUser()
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
go steam.Run() go steam.Run()
case steam.ActionMainMethodEnded:
go steam.Run()
} }
} }
} }
@ -149,7 +161,7 @@ func gatewaySteamXmppMessage() {
func gatewaySteamXmppPresence() { func gatewaySteamXmppPresence() {
for { for {
steamId := <-steam.ChanPresence steamId := <-steam.ChanPresence
name := <- steam.ChanPresence name := <-steam.ChanPresence
stat := <-steam.ChanPresenceSteam stat := <-steam.ChanPresenceSteam
gameName := <-steam.ChanPresence gameName := <-steam.ChanPresence
@ -185,26 +197,11 @@ func gatewaySteamXmppPresence() {
} }
} }
func disconnectAllSteamUser() { func disconnectAllSteamUser() {
for sid, _ := range SetSteamId { for sid, _ := range SetSteamId {
xmpp.SendPresenceFrom(xmpp.Status_offline, xmpp.Type_unavailable, sid+"@"+xmpp.JidStr, "", "") xmpp.SendPresenceFrom(xmpp.Status_offline, xmpp.Type_unavailable, sid+"@"+xmpp.JidStr, "", "")
delete(SetSteamId, sid) delete(SetSteamId, sid)
} }
} }
// /Steam -> XMPP gateways // /Steam -> XMPP gateways
func inputStop() {
for {
in := bufio.NewReader(os.Stdin)
line, err := in.ReadString('\n')
if err != nil {
continue
}
line = strings.TrimRight(line, "\n")
if line == "stop" {
return
}
}
}

View File

@ -25,9 +25,10 @@ const (
State_LookingToPlay = steamlang.EPersonaState_LookingToPlay State_LookingToPlay = steamlang.EPersonaState_LookingToPlay
State_Max = steamlang.EPersonaState_Max State_Max = steamlang.EPersonaState_Max
ActionConnected = "steam_connected" ActionConnected = "steam_connected"
ActionDisconnected = "steam_disconnected" ActionDisconnected = "steam_disconnected"
ActionFatalError = "steam_fatal_error" ActionFatalError = "steam_fatal_error"
ActionMainMethodEnded = "action_steam_main_method_ended"
LogInfo = "\t[STEAM INFO]\t" LogInfo = "\t[STEAM INFO]\t"
LogError = "\t[STEAM ERROR]\t" LogError = "\t[STEAM ERROR]\t"
@ -55,6 +56,7 @@ func Run() {
client.ConnectionTimeout = 10 * time.Second client.ConnectionTimeout = 10 * time.Second
mainSteam() mainSteam()
ChanAction <- ActionMainMethodEnded
} }
func mainSteam() { func mainSteam() {

View File

@ -24,8 +24,9 @@ const (
Type_probe = "probe" Type_probe = "probe"
Type_error = "error" Type_error = "error"
ActionConnexion = "action_xmpp_connexion" ActionConnexion = "action_xmpp_connexion"
ActionDeconnexion = "action_xmpp_deconnexion" ActionDeconnexion = "action_xmpp_deconnexion"
ActionMainMethodEnded = "action_xmpp_main_method_ended"
LogInfo = "\t[XMPP INFO]\t" LogInfo = "\t[XMPP INFO]\t"
LogError = "\t[XMPP ERROR]\t" LogError = "\t[XMPP ERROR]\t"
@ -60,6 +61,7 @@ func Run() {
comp = must(xmpp.NewComponentXMPP(stream, jid, Secret)).(*xmpp.XMPP) comp = must(xmpp.NewComponentXMPP(stream, jid, Secret)).(*xmpp.XMPP)
mainXMPP() mainXMPP()
ChanAction <- ActionMainMethodEnded
} }
func mainXMPP() { func mainXMPP() {
@ -102,6 +104,7 @@ func must(v interface{}, err error) interface{} {
} }
func Disconnect() { func Disconnect() {
log.Printf("%sXMPP disconnect", LogInfo)
SendPresence(Status_offline, Type_unavailable, "") SendPresence(Status_offline, Type_unavailable, "")
} }