| 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 has code handling long-lived connections from devices. |
| 18 |
package session |
| 19 |
|
| 20 |
import ( |
| 21 |
"errors" |
| 22 |
"net" |
| 23 |
"time" |
| 24 |
|
| 25 |
"gitlab.com/ubports/development/core/lomiri-push-service/protocol" |
| 26 |
"gitlab.com/ubports/development/core/lomiri-push-service/server/broker" |
| 27 |
) |
| 28 |
|
| 29 |
// SessionConfig is for carrying the session configuration. |
| 30 |
type SessionConfig interface { |
| 31 |
// pings are emitted each ping interval |
| 32 |
PingInterval() time.Duration |
| 33 |
// send and waiting for response shouldn't take more than exchange |
| 34 |
// timeout |
| 35 |
ExchangeTimeout() time.Duration |
| 36 |
} |
| 37 |
|
| 38 |
// sessionStart manages the start of the protocol session. |
| 39 |
func sessionStart(proto protocol.Protocol, brkr broker.Broker, cfg SessionConfig, track SessionTracker) (broker.BrokerSession, error) { |
| 40 |
var connMsg protocol.ConnectMsg |
| 41 |
proto.SetDeadline(time.Now().Add(cfg.ExchangeTimeout())) |
| 42 |
err := proto.ReadMessage(&connMsg) |
| 43 |
if err != nil { |
| 44 |
return nil, err |
| 45 |
} |
| 46 |
if connMsg.Type != "connect" { |
| 47 |
return nil, &broker.ErrAbort{"expected CONNECT message"} |
| 48 |
} |
| 49 |
err = proto.WriteMessage(&protocol.ConnAckMsg{ |
| 50 |
Type: "connack", |
| 51 |
Params: protocol.ConnAckParams{PingInterval: cfg.PingInterval().String()}, |
| 52 |
}) |
| 53 |
if err != nil { |
| 54 |
return nil, err |
| 55 |
} |
| 56 |
return brkr.Register(&connMsg, track) |
| 57 |
} |
| 58 |
|
| 59 |
var errOneway = errors.New("oneway") |
| 60 |
|
| 61 |
type loop struct { |
| 62 |
// params |
| 63 |
proto protocol.Protocol |
| 64 |
sess broker.BrokerSession |
| 65 |
track SessionTracker |
| 66 |
// exchange timeout |
| 67 |
exchangeTimeout time.Duration |
| 68 |
// ping mgmt |
| 69 |
pingInterval time.Duration |
| 70 |
pingTimer *time.Timer |
| 71 |
intervalStart time.Time |
| 72 |
} |
| 73 |
|
| 74 |
// exchange writes outMsg message, reads answer in inMsg |
| 75 |
func (l *loop) exchange(outMsg, inMsg interface{}) error { |
| 76 |
proto := l.proto |
| 77 |
proto.SetDeadline(time.Now().Add(l.exchangeTimeout)) |
| 78 |
err := proto.WriteMessage(outMsg) |
| 79 |
if err != nil { |
| 80 |
return err |
| 81 |
} |
| 82 |
if inMsg == nil { // no answer expected |
| 83 |
if outMsg.(protocol.OnewayMsg).OnewayContinue() { |
| 84 |
return errOneway |
| 85 |
} |
| 86 |
return &broker.ErrAbort{"session broken for reason"} |
| 87 |
} |
| 88 |
err = proto.ReadMessage(inMsg) |
| 89 |
if err != nil { |
| 90 |
return err |
| 91 |
} |
| 92 |
return nil |
| 93 |
} |
| 94 |
|
| 95 |
func (l *loop) pingTimerReset(totalReset bool) bool { |
| 96 |
interval := l.pingInterval |
| 97 |
if !totalReset { |
| 98 |
interval -= time.Since(l.intervalStart) |
| 99 |
if interval <= 0 { |
| 100 |
return false // late |
| 101 |
} |
| 102 |
} |
| 103 |
l.pingTimer.Reset(interval) |
| 104 |
if totalReset { |
| 105 |
l.intervalStart = time.Now() |
| 106 |
} |
| 107 |
return true |
| 108 |
} |
| 109 |
|
| 110 |
func (l *loop) doPing() error { |
| 111 |
l.track.EffectivePingInterval(time.Since(l.intervalStart)) |
| 112 |
pingMsg := &protocol.PingPongMsg{"ping"} |
| 113 |
var pongMsg protocol.PingPongMsg |
| 114 |
err := l.exchange(pingMsg, &pongMsg) |
| 115 |
if err != nil { |
| 116 |
return err |
| 117 |
} |
| 118 |
if pongMsg.Type != "pong" { |
| 119 |
return &broker.ErrAbort{"expected PONG message"} |
| 120 |
} |
| 121 |
l.pingTimerReset(true) |
| 122 |
return nil |
| 123 |
} |
| 124 |
|
| 125 |
func (l *loop) run() error { |
| 126 |
ch := l.sess.SessionChannel() |
| 127 |
Loop: |
| 128 |
for { |
| 129 |
select { |
| 130 |
case <-l.pingTimer.C: |
| 131 |
err := l.doPing() |
| 132 |
if err != nil { |
| 133 |
return err |
| 134 |
} |
| 135 |
case exchg := <-ch: |
| 136 |
l.pingTimer.Stop() |
| 137 |
if exchg == nil { |
| 138 |
return &broker.ErrAbort{"terminated"} |
| 139 |
} |
| 140 |
outMsg, inMsg, err := exchg.Prepare(l.sess) |
| 141 |
if err == broker.ErrNop { // nothing to do |
| 142 |
if !l.pingTimerReset(false) { |
| 143 |
// we are late, do a ping here |
| 144 |
err := l.doPing() |
| 145 |
if err != nil { |
| 146 |
return err |
| 147 |
} |
| 148 |
} |
| 149 |
continue Loop |
| 150 |
} |
| 151 |
if err != nil { |
| 152 |
return err |
| 153 |
} |
| 154 |
for { |
| 155 |
done := outMsg.Split() |
| 156 |
err = l.exchange(outMsg, inMsg) |
| 157 |
if err == errOneway { |
| 158 |
l.pingTimerReset(true) |
| 159 |
continue Loop |
| 160 |
} |
| 161 |
if err != nil { |
| 162 |
return err |
| 163 |
} |
| 164 |
if done { |
| 165 |
l.pingTimerReset(true) |
| 166 |
} |
| 167 |
err = exchg.Acked(l.sess, done) |
| 168 |
if err != nil { |
| 169 |
return err |
| 170 |
} |
| 171 |
if done { |
| 172 |
break |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
// sessionLoop manages the exchanges of the protocol session. |
| 180 |
func sessionLoop(proto protocol.Protocol, sess broker.BrokerSession, cfg SessionConfig, track SessionTracker) error { |
| 181 |
pingInterval := cfg.PingInterval() |
| 182 |
l := &loop{ |
| 183 |
proto: proto, |
| 184 |
sess: sess, |
| 185 |
track: track, |
| 186 |
// ping setup |
| 187 |
pingInterval: pingInterval, |
| 188 |
pingTimer: time.NewTimer(pingInterval), |
| 189 |
intervalStart: time.Now(), |
| 190 |
exchangeTimeout: cfg.ExchangeTimeout(), |
| 191 |
} |
| 192 |
return l.run() |
| 193 |
} |
| 194 |
|
| 195 |
// Session manages the session with a client. |
| 196 |
func Session(conn net.Conn, brkr broker.Broker, cfg SessionConfig, track SessionTracker) error { |
| 197 |
defer conn.Close() |
| 198 |
track.Start(conn) |
| 199 |
v, err := protocol.ReadWireFormatVersion(conn, cfg.ExchangeTimeout()) |
| 200 |
if err != nil { |
| 201 |
return track.End(err) |
| 202 |
} |
| 203 |
if v != protocol.ProtocolWireVersion { |
| 204 |
return track.End(&broker.ErrAbort{"unexpected wire format version"}) |
| 205 |
} |
| 206 |
proto := protocol.NewProtocol0(conn) |
| 207 |
sess, err := sessionStart(proto, brkr, cfg, track) |
| 208 |
if err != nil { |
| 209 |
return track.End(err) |
| 210 |
} |
| 211 |
track.Registered(sess) |
| 212 |
defer brkr.Unregister(sess) |
| 213 |
return track.End(sessionLoop(proto, sess, cfg, track)) |
| 214 |
} |