Files
ippserver/packages/ipp/response.go
2021-01-09 12:10:43 +01:00

94 lines
2.2 KiB
Go

package ipp
import (
"encoding/binary"
"fmt"
"io"
)
type ippResponseHeader struct {
versionNumber versionNumber
statusCode statusCode
requestID uint32
}
func (h ippResponseHeader) String() string {
return fmt.Sprintf("Version number: %v Status code: %v Request Id: %v", h.versionNumber, h.statusCode, h.requestID)
}
func (h *ippResponseHeader) marshal() []byte {
a := make([]byte, 8)
binary.BigEndian.PutUint16(a[0:2], uint16(h.versionNumber))
binary.BigEndian.PutUint16(a[2:4], uint16(h.statusCode))
binary.BigEndian.PutUint32(a[4:8], h.requestID)
return a
}
func (h *ippResponseHeader) unmarshal(byteStream io.Reader) {
binary.Read(byteStream, binary.BigEndian, &h.versionNumber)
binary.Read(byteStream, binary.BigEndian, &h.statusCode)
binary.Read(byteStream, binary.BigEndian, &h.requestID)
}
type Response struct {
a *attributes
header ippResponseHeader
}
func NewResponse(code statusCode, requestID uint32) *Response {
r := new(Response)
r.a = new(attributes)
r.header.versionNumber = 0x0101
r.header.requestID = requestID
r.header.statusCode = code
return r
}
func (r Response) String() string {
return r.header.String() + "\n" + r.a.String()
}
func (r *Response) Marshal() []byte {
a := make([]byte, 0, 20)
a = append(a, r.header.marshal()...)
if len(r.a.operation) > 0 {
a = append(a, byte(operationAttributes))
for _, e := range r.a.operation {
a = append(a, e.marshal()...)
}
}
if len(r.a.job) > 0 {
a = append(a, byte(jobAttributes))
for _, e := range r.a.job {
a = append(a, e.marshal()...)
}
}
if len(r.a.printer) > 0 {
a = append(a, byte(printerAttributes))
for _, e := range r.a.printer {
a = append(a, e.marshal()...)
}
}
a = append(a, byte(endOfAttributes))
return a
}
func (r *Response) UnMarshal(body io.Reader) {
r.header.unmarshal(body)
//log.Printf("Header %v", r.header)
r.a = UnMarshalAttributues(body)
}
func (r *Response) AddPrinterAttribute(a Attribute) {
r.a.addAttribute(printerAttributes, a)
}
func (r *Response) AddOperatonAttribute(a Attribute) {
r.a.addAttribute(operationAttributes, a)
}
func (r *Response) AddJobAttribute(a Attribute) {
r.a.addAttribute(jobAttributes, a)
}