Debian Codegraph @

server/session/tracker.go

raw
1 /*
2 Copyright 2013-2014 Canonical Ltd.
3
4 This program is free software: you can redistribute it and/or modify it
5 under the terms of the GNU General Public License version 3, as published
6 by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranties of
10 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along
14 with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 package session
18
19 import (
20 "fmt"
21 "net"
22 "time"
23
24 "gitlab.com/ubports/development/core/lomiri-push-service/logger"
25 "gitlab.com/ubports/development/core/lomiri-push-service/server/broker"
26 )
27
28 // SessionTracker logs session events.
29 type SessionTracker interface {
30 logger.Logger
31 // Session got started.
32 Start(WithRemoteAddr)
33 // SessionId
34 SessionId() string
35 // Session got registered with broker as sess BrokerSession.
36 Registered(sess broker.BrokerSession)
37 // Report effective elapsed ping interval.
38 EffectivePingInterval(time.Duration)
39 // Session got ended with error err (can be nil).
40 End(error) error
41 }
42
43 // WithRemoteAddr can report a remote address.
44 type WithRemoteAddr interface {
45 RemoteAddr() net.Addr
46 }
47
48 var sessionsEpoch = time.Date(2013, 1, 1, 0, 0, 0, 0, time.UTC).UnixNano()
49
50 // Tracker implements SessionTracker simply.
51 type tracker struct {
52 logger.Logger
53 sessionId string
54 }
55
56 func NewTracker(logger logger.Logger) SessionTracker {
57 return &tracker{Logger: logger}
58 }
59
60 func (trk *tracker) Start(conn WithRemoteAddr) {
61 trk.sessionId = fmt.Sprintf("%x", time.Now().UnixNano()-sessionsEpoch)
62 trk.Debugf("session(%s) connected %v", trk.sessionId, conn.RemoteAddr())
63 }
64
65 func (trk *tracker) SessionId() string {
66 return trk.sessionId
67 }
68
69 func (trk *tracker) Registered(sess broker.BrokerSession) {
70 trk.Infof("session(%s) registered %v", trk.sessionId, sess.DeviceIdentifier())
71 }
72
73 func (trk *tracker) EffectivePingInterval(time.Duration) {
74 }
75
76 func (trk *tracker) End(err error) error {
77 trk.Debugf("session(%s) ended with: %v", trk.sessionId, err)
78 return err
79 }