-
Notifications
You must be signed in to change notification settings - Fork 1
/
session.go
140 lines (130 loc) · 3.96 KB
/
session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
Copyright 2013-2014 Canonical Ltd.
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License version 3, as published
by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranties of
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Package session has code handling long-lived connections from devices.
package session
import (
"launchpad.net/ubuntu-push/protocol"
"launchpad.net/ubuntu-push/server/broker"
"net"
"time"
)
// SessionConfig is for carrying the session configuration.
type SessionConfig interface {
// pings are emitted each ping interval
PingInterval() time.Duration
// send and waiting for response shouldn't take more than exchange
// timeout
ExchangeTimeout() time.Duration
}
// sessionStart manages the start of the protocol session.
func sessionStart(proto protocol.Protocol, brkr broker.Broker, cfg SessionConfig) (broker.BrokerSession, error) {
var connMsg protocol.ConnectMsg
proto.SetDeadline(time.Now().Add(cfg.ExchangeTimeout()))
err := proto.ReadMessage(&connMsg)
if err != nil {
return nil, err
}
if connMsg.Type != "connect" {
return nil, &broker.ErrAbort{"expected CONNECT message"}
}
err = proto.WriteMessage(&protocol.ConnAckMsg{
Type: "connack",
Params: protocol.ConnAckParams{PingInterval: cfg.PingInterval().String()},
})
if err != nil {
return nil, err
}
return brkr.Register(&connMsg)
}
// exchange writes outMsg message, reads answer in inMsg
func exchange(proto protocol.Protocol, outMsg, inMsg interface{}, exchangeTimeout time.Duration) error {
proto.SetDeadline(time.Now().Add(exchangeTimeout))
err := proto.WriteMessage(outMsg)
if err != nil {
return err
}
err = proto.ReadMessage(inMsg)
if err != nil {
return err
}
return nil
}
// sessionLoop manages the exchanges of the protocol session.
func sessionLoop(proto protocol.Protocol, sess broker.BrokerSession, cfg SessionConfig, track SessionTracker) error {
pingInterval := cfg.PingInterval()
exchangeTimeout := cfg.ExchangeTimeout()
pingTimer := time.NewTimer(pingInterval)
intervalStart := time.Now()
ch := sess.SessionChannel()
for {
select {
case <-pingTimer.C:
track.EffectivePingInterval(time.Since(intervalStart))
pingMsg := &protocol.PingPongMsg{"ping"}
var pongMsg protocol.PingPongMsg
err := exchange(proto, pingMsg, &pongMsg, exchangeTimeout)
if err != nil {
return err
}
if pongMsg.Type != "pong" {
return &broker.ErrAbort{"expected PONG message"}
}
pingTimer.Reset(pingInterval)
case exchg := <-ch:
// xxx later can use ch closing for shutdown/reset
pingTimer.Stop()
outMsg, inMsg, err := exchg.Prepare(sess)
if err != nil {
return err
}
for {
done := outMsg.Split()
err = exchange(proto, outMsg, inMsg, exchangeTimeout)
if err != nil {
return err
}
if done {
pingTimer.Reset(pingInterval)
intervalStart = time.Now()
}
err = exchg.Acked(sess, done)
if err != nil {
return err
}
if done {
break
}
}
}
}
}
// Session manages the session with a client.
func Session(conn net.Conn, brkr broker.Broker, cfg SessionConfig, track SessionTracker) error {
defer conn.Close()
track.Start(conn)
v, err := protocol.ReadWireFormatVersion(conn, cfg.ExchangeTimeout())
if err != nil {
return track.End(err)
}
if v != protocol.ProtocolWireVersion {
return track.End(&broker.ErrAbort{"unexpected wire format version"})
}
proto := protocol.NewProtocol0(conn)
sess, err := sessionStart(proto, brkr, cfg)
if err != nil {
return track.End(err)
}
track.Registered(sess)
defer brkr.Unregister(sess)
return track.End(sessionLoop(proto, sess, cfg, track))
}