| 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 protocol implements the client-daemon <-> push-server protocol. |
| 18 |
package protocol |
| 19 |
|
| 20 |
import ( |
| 21 |
"bytes" |
| 22 |
"encoding/binary" |
| 23 |
"encoding/json" |
| 24 |
"fmt" |
| 25 |
"io" |
| 26 |
"net" |
| 27 |
"time" |
| 28 |
) |
| 29 |
|
| 30 |
// Protocol is a connection capable of writing and reading the wire format |
| 31 |
// of protocol messages. |
| 32 |
type Protocol interface { |
| 33 |
SetDeadline(t time.Time) |
| 34 |
ReadMessage(msg interface{}) error |
| 35 |
WriteMessage(msg interface{}) error |
| 36 |
} |
| 37 |
|
| 38 |
func ReadWireFormatVersion(conn net.Conn, exchangeTimeout time.Duration) (ver int, err error) { |
| 39 |
var buf1 [1]byte |
| 40 |
err = conn.SetReadDeadline(time.Now().Add(exchangeTimeout)) |
| 41 |
if err != nil { |
| 42 |
panic(fmt.Errorf("can't set deadline: %v", err)) |
| 43 |
} |
| 44 |
_, err = conn.Read(buf1[:]) |
| 45 |
ver = int(buf1[0]) |
| 46 |
return |
| 47 |
} |
| 48 |
|
| 49 |
const ProtocolWireVersion = 0 |
| 50 |
|
| 51 |
// protocol0 handles version 0 of the wire format |
| 52 |
type protocol0 struct { |
| 53 |
buffer *bytes.Buffer |
| 54 |
enc *json.Encoder |
| 55 |
conn net.Conn |
| 56 |
} |
| 57 |
|
| 58 |
// NewProtocol0 creates and initialises a protocol with wire format version 0. |
| 59 |
func NewProtocol0(conn net.Conn) Protocol { |
| 60 |
buf := bytes.NewBuffer(make([]byte, 5000)) |
| 61 |
return &protocol0{ |
| 62 |
buffer: buf, |
| 63 |
enc: json.NewEncoder(buf), |
| 64 |
conn: conn} |
| 65 |
} |
| 66 |
|
| 67 |
// SetDeadline sets the deadline for the subsequent WriteMessage/ReadMessage exchange. |
| 68 |
func (c *protocol0) SetDeadline(t time.Time) { |
| 69 |
err := c.conn.SetDeadline(t) |
| 70 |
if err != nil { |
| 71 |
panic(fmt.Errorf("can't set deadline: %v", err)) |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
// ReadMessage reads from the connection one message with a JSON body |
| 76 |
// preceded by its big-endian uint16 length. |
| 77 |
func (c *protocol0) ReadMessage(msg interface{}) error { |
| 78 |
c.buffer.Reset() |
| 79 |
_, err := io.CopyN(c.buffer, c.conn, 2) |
| 80 |
if err != nil { |
| 81 |
return err |
| 82 |
} |
| 83 |
length := binary.BigEndian.Uint16(c.buffer.Bytes()) |
| 84 |
c.buffer.Reset() |
| 85 |
_, err = io.CopyN(c.buffer, c.conn, int64(length)) |
| 86 |
if err != nil { |
| 87 |
return err |
| 88 |
} |
| 89 |
return json.Unmarshal(c.buffer.Bytes(), msg) |
| 90 |
} |
| 91 |
|
| 92 |
// WriteMessage writes one message to the connection with a JSON body |
| 93 |
// preceding it with its big-endian uint16 length. |
| 94 |
func (c *protocol0) WriteMessage(msg interface{}) error { |
| 95 |
c.buffer.Reset() |
| 96 |
c.buffer.WriteString("\x00\x00") // placeholder for length |
| 97 |
err := c.enc.Encode(msg) |
| 98 |
if err != nil { |
| 99 |
panic(fmt.Errorf("WriteMessage got: %v", err)) |
| 100 |
} |
| 101 |
msgLen := c.buffer.Len() - 3 // length space, extra newline |
| 102 |
toWrite := c.buffer.Bytes() |
| 103 |
binary.BigEndian.PutUint16(toWrite[:2], uint16(msgLen)) |
| 104 |
_, err = c.conn.Write(toWrite[:msgLen+2]) |
| 105 |
return err |
| 106 |
} |