Merge attribute handling from request and response to commen code.

This commit was merged in pull request #7.
This commit is contained in:
2020-12-28 20:05:01 +01:00
parent 04a4b4157f
commit 53aaaf5521
14 changed files with 291 additions and 172 deletions

View File

@@ -3,6 +3,7 @@ package ipp
import (
"encoding/binary"
"fmt"
"io"
)
type ippResponseHeader struct {
@@ -24,92 +25,69 @@ func (h *ippResponseHeader) marshal() []byte {
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 {
operationAttributes []Attribute
jobAttributes []Attribute
printerAttributes []Attribute
header ippResponseHeader
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
r.operationAttributes = make([]Attribute, 0)
r.printerAttributes = make([]Attribute, 0)
r.jobAttributes = make([]Attribute, 0)
return r
}
// func (r *Response) length() int {
// l := 8 + 1
// if len(r.jobAttributes) > 0 {
// l += 1 //
// for _, e := range r.jobAttributes {
// l += e.length()
// }
// }
// for _, e := range r.operationAttributes {
// l += e.length()
// }
// for _, e := range r.printerAttributes {
// l += e.length()
// }
// }
func (r Response) String() string {
s := r.header.String() + "\n" + " OperationAttributes" + "\n"
for _, a := range r.operationAttributes {
s = s + fmt.Sprintf(" %v (%v)\n", a, a.valueTag())
}
s = s + " PrinterAttributes" + "\n"
for _, a := range r.printerAttributes {
s = s + fmt.Sprintf(" %v (%v)\n", a, a.valueTag())
}
s = s + " JobAttributes" + "\n"
for _, a := range r.jobAttributes {
s = s + fmt.Sprintf(" %v (%v)\n", a, a.valueTag())
}
return s
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.operationAttributes) > 0 {
if len(r.a.operation) > 0 {
a = append(a, byte(operationAttributes))
for _, e := range r.operationAttributes {
for _, e := range r.a.operation {
a = append(a, e.marshal()...)
}
}
if len(r.jobAttributes) > 0 {
if len(r.a.job) > 0 {
a = append(a, byte(jobAttributes))
for _, e := range r.jobAttributes {
for _, e := range r.a.job {
a = append(a, e.marshal()...)
}
}
if len(r.printerAttributes) > 0 {
if len(r.a.printer) > 0 {
a = append(a, byte(printerAttributes))
for _, e := range r.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.printerAttributes = append(r.printerAttributes, a)
r.a.addAttribute(printerAttributes, a)
}
func (r *Response) AddOperatonAttribute(a Attribute) {
r.operationAttributes = append(r.operationAttributes, a)
r.a.addAttribute(operationAttributes, a)
}
func (r *Response) AddJobAttribute(a Attribute) {
r.jobAttributes = append(r.jobAttributes, a)
r.a.addAttribute(jobAttributes, a)
}