91e21122b82ac5013d7bc90bbbe933d7ade4dbd1
1 //
2 // Copyright (C) 2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions
7 // are met:
8 // 1. Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // 2. Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 //
14 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 // ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
18 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // Package proto provides helper functions for using the SysDB front-end
27 // protocol. That's the protocol used for communication between a client and a
28 // SysDB server instance.
29 package proto
31 import (
32 "encoding/binary"
33 "encoding/json"
34 "fmt"
35 "io"
36 )
38 // Network byte order.
39 var nbo = binary.BigEndian
41 // A Status represents the type of a message. The message type describes the
42 // current status or state of a connection depending on the context.
43 type Status uint32
45 const (
46 // ConnectionOK indicates that a command was successful.
47 ConnectionOK = Status(0)
48 // ConnectionError indicates that a command has failed.
49 ConnectionError = Status(1)
50 // ConnectionLog indicates an (asynchronous) log message.
51 ConnectionLog = Status(2)
53 // ConnectionData indicates a successful query returning data.
54 ConnectionData = Status(100)
55 )
57 const (
58 // ConnectionIdle is the internal state for idle connections.
59 ConnectionIdle = Status(0)
60 // ConnectionPing is the state requesting a connection check.
61 ConnectionPing = Status(1)
62 // ConnectionStartup is the state requesting the setup of a client
63 // connection.
64 ConnectionStartup = Status(2)
66 // ConnectionQuery is the state requesting the execution of a query in the
67 // server.
68 ConnectionQuery = Status(3)
69 // ConnectionFetch is the state requesting the execution of the 'FETCH'
70 // command in the server.
71 ConnectionFetch = Status(4)
72 // ConnectionList is the state requesting the execution of the 'LIST'
73 // command in the server.
74 ConnectionList = Status(5)
75 // ConnectionLookup is the state requesting the execution of the 'LOOKUP'
76 // command in the server.
77 ConnectionLookup = Status(6)
78 // ConnectionTimeseries is the state requesting the execution of the
79 // 'TIMESERIES' command in the server.
80 ConnectionTimeseries = Status(7)
82 // ConnectionExpr is the internal state for expression parsing.
83 ConnectionExpr = Status(100)
84 )
86 // The DataType describes the type of data in a ConnectionData message.
87 type DataType int
89 const (
90 // A HostList can be unmarshaled to []sysdb.Host.
91 HostList DataType = iota
92 // A Host can be unmarshaled to sysdb.Host.
93 Host
94 // A Timeseries can be unmarshaled to sysdb.Timeseries.
95 Timeseries
96 )
98 // A Message represents a raw message of the SysDB front-end protocol.
99 type Message struct {
100 Type Status
101 Raw []byte
102 }
104 // Read reads a raw message encoded in the SysDB wire format from r. The
105 // function parses the header but the raw body of the message will still be
106 // encoded in the wire format.
107 //
108 // The reader has to be in blocking mode. Otherwise, the client and server
109 // will be out of sync after reading a partial message and cannot recover from
110 // that.
111 func Read(r io.Reader) (*Message, error) {
112 var header [8]byte
113 if _, err := io.ReadFull(r, header[:]); err != nil {
114 return nil, err
115 }
117 typ := nbo.Uint32(header[:4])
118 l := nbo.Uint32(header[4:])
119 msg := make([]byte, l)
120 if _, err := io.ReadFull(r, msg); err != nil {
121 return nil, err
122 }
124 return &Message{Status(typ), msg}, nil
125 }
127 // Write writes a raw message to w. The raw body of m has to be encoded in the
128 // SysDB wire format. The function adds the right header to the message.
129 //
130 // The writer has to be in blocking mode. Otherwise, the client and server
131 // will be out of sync after writing a partial message and cannot recover from
132 // that.
133 func Write(w io.Writer, m *Message) error {
134 var header [8]byte
135 nbo.PutUint32(header[:4], uint32(m.Type))
136 nbo.PutUint32(header[4:], uint32(len(m.Raw)))
138 if _, err := io.WriteString(w, string(header[:])); err != nil {
139 return err
140 }
141 if _, err := io.WriteString(w, string(m.Raw)); err != nil {
142 return err
143 }
144 return nil
145 }
147 // DataType determines the type of data in a ConnectionData message.
148 func (m Message) DataType() (DataType, error) {
149 if m.Type != ConnectionData {
150 return 0, fmt.Errorf("message is not of type DATA")
151 }
153 typ := nbo.Uint32(m.Raw[:4])
154 switch Status(typ) {
155 case ConnectionList, ConnectionLookup:
156 return HostList, nil
157 case ConnectionFetch:
158 return Host, nil
159 case ConnectionTimeseries:
160 return Timeseries, nil
161 }
162 return 0, fmt.Errorf("unknown DATA type %d", typ)
163 }
165 // Unmarshal parses the raw body of m and stores the result in the value
166 // pointed to by v which has to match the type of the message and its data.
167 func Unmarshal(m *Message, v interface{}) error {
168 if m.Type != ConnectionData {
169 return fmt.Errorf("unmarshaling message of type %d not supported", m.Type)
170 }
171 if len(m.Raw) == 0 { // empty command
172 return nil
173 } else if len(m.Raw) < 4 {
174 return fmt.Errorf("DATA message body too short")
175 }
176 return json.Unmarshal(m.Raw[4:], v)
177 }
179 // vim: set tw=78 sw=4 sw=4 noexpandtab :