Debian Codegraph @

message.go

raw symbols SCIP
1 /* Go IPP - IPP core protocol implementation in pure Go
2 *
3 * Copyright (C) 2020 and up by Alexander Pevzner ([email protected])
4 * See LICENSE for license terms and conditions
5 *
6 * IPP protocol messages
7 */
8
9 package goipp
10
11 import (
12 "bytes"
13 "fmt"
14 "io"
15 )
16
17 // Code represents Op(operation) or Status codes
18 type Code uint16
19
20 // Version represents a protocol version. It consist
21 // of Major and Minor version codes, packed into a single
22 // 16-bit word
23 type Version uint16
24
25 // DefaultVersion is the default IPP version
26 const DefaultVersion Version = 0x0200
27
28 // MakeVersion makes version from major and minor parts
29 func MakeVersion(major, minor uint8) Version {
30 return Version(major)<<8 | Version(minor)
31 }
32
33 // Major returns a major part of version
34 func (v Version) Major() uint8 {
35 return uint8(v >> 8)
36 }
37
38 // Minor returns a minor part of version
39 func (v Version) Minor() uint8 {
40 return uint8(v)
41 }
42
43 // String() converts version to string (i.e., 2.0)
44 func (v Version) String() string {
45 return fmt.Sprintf("%d.%d", v.Major(), v.Minor())
46 }
47
48 // Message represents a single IPP message, which may be either
49 // client request or server response
50 type Message struct {
51 // Common header
52 Version Version // Protocol version
53 Code Code // Operation for request, status for response
54 RequestID uint32 // Set in request, returned in response
55
56 // Attributes, by group
57 Operation Attributes // Operation attributes
58 Job Attributes // Job attributes
59 Printer Attributes // Printer attributes
60 Unsupported Attributes // Unsupported attributes
61 Subscription Attributes // Subscription attributes
62 EventNotification Attributes // Event Notification attributes
63 Resource Attributes // Resource attributes
64 Document Attributes // Document attributes
65 System Attributes // System attributes
66 Future11 Attributes // \
67 Future12 Attributes // \
68 Future13 Attributes // | Reserved for future extensions
69 Future14 Attributes // /
70 Future15 Attributes // /
71 }
72
73 // NewRequest creates a new request message
74 //
75 // Use DefaultVersion as a first argument, if you don't
76 // have any specific needs
77 func NewRequest(v Version, op Op, id uint32) *Message {
78 return &Message{
79 Version: v,
80 Code: Code(op),
81 RequestID: id,
82 }
83 }
84
85 // NewResponse creates a new response message
86 //
87 // Use DefaultVersion as a first argument, if you don't
88 func NewResponse(v Version, status Status, id uint32) *Message {
89 return &Message{
90 Version: v,
91 Code: Code(status),
92 RequestID: id,
93 }
94 }
95
96 // Equal checks that two messages are equal
97 func (m Message) Equal(m2 Message) bool {
98 if m.Version != m2.Version ||
99 m.Code != m2.Code ||
100 m.RequestID != m2.RequestID {
101 return false
102 }
103
104 groups1 := m.attrGroups()
105 groups2 := m2.attrGroups()
106
107 if len(groups1) != len(groups2) {
108 return false
109 }
110
111 for i, grp1 := range groups1 {
112 grp2 := groups2[i]
113
114 if grp1.tag != grp2.tag || !grp1.attrs.Equal(grp2.attrs) {
115 return false
116 }
117 }
118
119 return true
120 }
121
122 // Reset the message into initial state
123 func (m *Message) Reset() {
124 *m = Message{}
125 }
126
127 // Encode message
128 func (m *Message) Encode(out io.Writer) error {
129 me := messageEncoder{
130 out: out,
131 }
132
133 return me.encode(m)
134 }
135
136 // EncodeBytes encodes message to byte slice
137 func (m *Message) EncodeBytes() ([]byte, error) {
138 var buf bytes.Buffer
139
140 err := m.Encode(&buf)
141 return buf.Bytes(), err
142 }
143
144 // Decode message
145 func (m *Message) Decode(in io.Reader) error {
146 md := messageDecoder{
147 in: in,
148 }
149
150 m.Reset()
151 return md.decode(m)
152 }
153
154 // DecodeBytes decodes message from byte slice
155 func (m *Message) DecodeBytes(data []byte) error {
156 return m.Decode(bytes.NewBuffer(data))
157 }
158
159 // Print pretty-prints the message. The 'request' parameter affects
160 // interpretation of Message.Code: it is interpreted either
161 // as Op or as Status
162 func (m *Message) Print(out io.Writer, request bool) {
163 out.Write([]byte("{\n"))
164
165 fmt.Fprintf(out, msgPrintIndent+"VERSION %s\n", m.Version)
166
167 if request {
168 fmt.Fprintf(out, msgPrintIndent+"OPERATION %s\n", Op(m.Code))
169 } else {
170 fmt.Fprintf(out, msgPrintIndent+"STATUS %s\n", Status(m.Code))
171 }
172
173 for _, grp := range m.attrGroups() {
174 fmt.Fprintf(out, "\n"+msgPrintIndent+"GROUP %s\n", grp.tag)
175 for _, attr := range grp.attrs {
176 m.printAttribute(out, attr, 1)
177 out.Write([]byte("\n"))
178 }
179 }
180
181 out.Write([]byte("}\n"))
182 }
183
184 // Pretty-print an attribute. Handles Collection attributes
185 // recursively
186 func (m *Message) printAttribute(out io.Writer, attr Attribute, indent int) {
187 m.printIndent(out, indent)
188 fmt.Fprintf(out, "ATTR %q", attr.Name)
189
190 tag := TagZero
191 for _, val := range attr.Values {
192 if val.T != tag {
193 fmt.Fprintf(out, " %s:", val.T)
194 tag = val.T
195 }
196
197 if collection, ok := val.V.(Collection); ok {
198 out.Write([]byte(" {\n"))
199 for _, attr2 := range collection {
200 m.printAttribute(out, attr2, indent+1)
201 out.Write([]byte("\n"))
202 }
203 m.printIndent(out, indent)
204 out.Write([]byte("}"))
205 } else {
206 fmt.Fprintf(out, " %s", val.V)
207 }
208 }
209 }
210
211 // Print indentation
212 func (m *Message) printIndent(out io.Writer, indent int) {
213 for i := 0; i < indent; i++ {
214 out.Write([]byte(msgPrintIndent))
215 }
216 }
217
218 // Get attributes by group. Groups with nil Attributes are skipped,
219 // but groups with non-nil are not, even if len(Attributes) == 0
220 //
221 // This is a helper function for message encoder and pretty-printer
222 func (m *Message) attrGroups() []struct {
223 tag Tag
224 attrs Attributes
225 } {
226 // Initialize slice of groups
227 groups := []struct {
228 tag Tag
229 attrs Attributes
230 }{
231 {TagOperationGroup, m.Operation},
232 {TagJobGroup, m.Job},
233 {TagPrinterGroup, m.Printer},
234 {TagUnsupportedGroup, m.Unsupported},
235 {TagSubscriptionGroup, m.Subscription},
236 {TagEventNotificationGroup, m.EventNotification},
237 {TagResourceGroup, m.Resource},
238 {TagDocumentGroup, m.Document},
239 {TagSystemGroup, m.System},
240 {TagFuture11Group, m.Future11},
241 {TagFuture12Group, m.Future12},
242 {TagFuture13Group, m.Future13},
243 {TagFuture14Group, m.Future14},
244 {TagFuture15Group, m.Future15},
245 }
246
247 // Skip all empty groups
248 out := 0
249 for in := 0; in < len(groups); in++ {
250 if groups[in].attrs != nil {
251 groups[out] = groups[in]
252 out++
253 }
254 }
255
256 return groups[:out]
257 }