Skip to content

Commit

Permalink
start/stop server on fg/bg
Browse files Browse the repository at this point in the history
  • Loading branch information
bitgamma committed Feb 9, 2022
1 parent 1e1c271 commit fddab92
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
10 changes: 10 additions & 0 deletions api/geth_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,16 @@ func (b *GethStatusBackend) AppStateChange(state string) {
b.log.Info("App State changed", "new-state", s)
b.appState = s

wakuext := b.statusNode.WakuExtService()

if wakuext != nil {
if s == appStateForeground {
wakuext.Messenger().ToForeground()
} else {
wakuext.Messenger().ToBackground()
}
}

// TODO: put node in low-power mode if the app is in background (or inactive)
// and normal mode if the app is in foreground.
}
Expand Down
23 changes: 20 additions & 3 deletions protocol/images/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func (s *messageHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

type Server struct {
Port int
run bool
server *http.Server
logger *zap.Logger
db *sql.DB
Expand All @@ -181,16 +182,20 @@ func (s *Server) listenAndServe() {

listener, err := tls.Listen("tcp", addr, cfg)
if err != nil {
s.logger.Error("failed to start server", zap.Error(err))
s.logger.Error("failed to start server, retrying", zap.Error(err))
s.Start()
return
}

s.Port = listener.Addr().(*net.TCPAddr).Port

s.run = true
err = s.server.Serve(listener)
if err != http.ErrServerClosed {
s.logger.Error("server failed unexpectedly, restarting", zap.Error(err))
go s.listenAndServe()
s.Start()
return
} else {
s.run = false
}
}

Expand All @@ -212,3 +217,15 @@ func (s *Server) Stop() error {

return nil
}

func (s *Server) ToForeground() {
if !s.run && (s.server != nil) {
s.Start()
}
}

func (s *Server) ToBackground() {
if s.run {
s.Stop()
}
}
8 changes: 8 additions & 0 deletions protocol/messenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,14 @@ func (m *Messenger) resendExpiredMessages() error {
return nil
}

func (m *Messenger) ToForeground() {
m.imageServer.ToForeground()
}

func (m *Messenger) ToBackground() {
m.imageServer.ToBackground()
}

func (m *Messenger) Start() (*MessengerResponse, error) {
m.logger.Info("starting messenger", zap.String("identity", types.EncodeHex(crypto.FromECDSAPub(&m.identity.PublicKey))))
// Start push notification server
Expand Down

0 comments on commit fddab92

Please sign in to comment.