go-ipp/reqest.go

95 lines
2.1 KiB
Go
Raw Normal View History

2019-03-10 17:43:47 +00:00
package ipp
import (
"bytes"
"encoding/binary"
2019-03-13 21:54:08 +00:00
"io"
2019-03-10 17:43:47 +00:00
)
type Request struct {
Operation Operation
RequestID int
OperationAttributes map[string]interface{}
JobAttributes map[string]interface{}
PrinterAttributes map[string]interface{}
2019-03-13 21:54:08 +00:00
File io.Reader
FileSize int
2019-03-10 17:43:47 +00:00
}
func NewRequest(op Operation, reqID int) *Request {
return &Request{
2019-03-13 21:54:08 +00:00
op, reqID, make(map[string]interface{}), make(map[string]interface{}), make(map[string]interface{}), nil, -1,
2019-03-10 17:43:47 +00:00
}
}
func (r *Request) Encode() ([]byte, error) {
buf := new(bytes.Buffer)
enc := NewAttributeEncoder(buf)
if err := binary.Write(buf, binary.BigEndian, ProtocolVersionMajor); err != nil {
return nil, err
}
if err := binary.Write(buf, binary.BigEndian, ProtocolVersionMinor); err != nil {
return nil, err
}
if err := binary.Write(buf, binary.BigEndian, r.Operation); err != nil {
return nil, err
}
2019-03-13 21:54:08 +00:00
if err := binary.Write(buf, binary.BigEndian, int32(r.RequestID)); err != nil {
2019-03-10 17:43:47 +00:00
return nil, err
}
2019-03-13 21:54:08 +00:00
if err := binary.Write(buf, binary.BigEndian, int8(TagOperation)); err != nil {
2019-03-10 17:43:47 +00:00
return nil, err
}
if err := enc.Encode("attributes-charset", Charset); err != nil {
return nil, err
}
if err := enc.Encode("attributes-natural-language", CharsetLanguage); err != nil {
return nil, err
}
if len(r.OperationAttributes) > 0 {
for attr, value := range r.OperationAttributes {
if err := enc.Encode(attr, value); err != nil {
return nil, err
}
}
}
if len(r.JobAttributes) > 0 {
2019-03-13 21:54:08 +00:00
if err := binary.Write(buf, binary.BigEndian, int8(TagJob)); err != nil {
2019-03-10 17:43:47 +00:00
return nil, err
}
for attr, value := range r.JobAttributes {
if err := enc.Encode(attr, value); err != nil {
return nil, err
}
}
}
if len(r.PrinterAttributes) > 0 {
2019-03-13 21:54:08 +00:00
if err := binary.Write(buf, binary.BigEndian, int8(TagPrinter)); err != nil {
2019-03-10 17:43:47 +00:00
return nil, err
}
for attr, value := range r.PrinterAttributes {
if err := enc.Encode(attr, value); err != nil {
return nil, err
}
}
}
2019-03-13 21:54:08 +00:00
if err := binary.Write(buf, binary.BigEndian, int8(TagEnd)); err != nil {
2019-03-10 17:43:47 +00:00
return nil, err
}
return buf.Bytes(), nil
}