Merge attribute handling from request and response to commen code.
This commit was merged in pull request #7.
This commit is contained in:
@@ -5,23 +5,21 @@ import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type ippRequestHeader struct {
|
||||
type ippMessageHeader struct {
|
||||
versionNumber versionNumber
|
||||
operationId OperationId
|
||||
requestId uint32
|
||||
}
|
||||
|
||||
func (h *ippRequestHeader) unmarshal(byteStream io.Reader) {
|
||||
func (h *ippMessageHeader) unmarshal(byteStream io.Reader) {
|
||||
binary.Read(byteStream, binary.BigEndian, &h.versionNumber)
|
||||
binary.Read(byteStream, binary.BigEndian, &h.operationId)
|
||||
binary.Read(byteStream, binary.BigEndian, &h.requestId)
|
||||
}
|
||||
|
||||
func (h *ippRequestHeader) marshal() []byte {
|
||||
func (h *ippMessageHeader) marshal() []byte {
|
||||
b := make([]byte, 0, 8)
|
||||
buf := bytes.NewBuffer(b)
|
||||
binary.Write(buf, binary.BigEndian, h.versionNumber)
|
||||
@@ -30,25 +28,17 @@ func (h *ippRequestHeader) marshal() []byte {
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
func (h ippRequestHeader) String() string {
|
||||
func (h ippMessageHeader) String() string {
|
||||
return fmt.Sprintf("Version number: %v Operation Id: %v Request Id: %v", h.versionNumber, h.operationId, h.requestId)
|
||||
}
|
||||
|
||||
type Attribute interface {
|
||||
valueTag() tag
|
||||
marshal() []byte
|
||||
//size() int
|
||||
}
|
||||
|
||||
type AddValuer interface {
|
||||
addValue(string)
|
||||
}
|
||||
|
||||
type Request struct {
|
||||
operationAttributes map[string]Attribute
|
||||
jobAttributes map[string]Attribute
|
||||
printerAttributes map[string]Attribute
|
||||
header ippRequestHeader
|
||||
a *attributes
|
||||
header ippMessageHeader
|
||||
}
|
||||
|
||||
func NewRequest(op OperationId, requestId uint32) *Request {
|
||||
@@ -56,112 +46,18 @@ func NewRequest(op OperationId, requestId uint32) *Request {
|
||||
r.header.operationId = op
|
||||
r.header.requestId = requestId
|
||||
r.header.versionNumber = 0x0200
|
||||
r.operationAttributes = make(map[string]Attribute)
|
||||
r.jobAttributes = make(map[string]Attribute)
|
||||
r.printerAttributes = make(map[string]Attribute)
|
||||
|
||||
r.a = new(attributes)
|
||||
return r
|
||||
}
|
||||
|
||||
func (r Request) 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 *Request) UnMarshal(body io.Reader) {
|
||||
r.header.unmarshal(body)
|
||||
log.Debugf("Header %v", r.header)
|
||||
var tag tag
|
||||
err := binary.Read(body, binary.BigEndian, &tag)
|
||||
if err != nil {
|
||||
log.Error(err.Error())
|
||||
}
|
||||
log.Debugf("got tag - %v", tag)
|
||||
var currentAttributeGroup map[string]Attribute
|
||||
switch tag {
|
||||
case operationAttributes:
|
||||
currentAttributeGroup = r.operationAttributes
|
||||
case jobAttributes:
|
||||
currentAttributeGroup = r.jobAttributes
|
||||
case printerAttributes:
|
||||
currentAttributeGroup = r.printerAttributes
|
||||
default:
|
||||
log.Errorf("Unknown tag %v", tag)
|
||||
}
|
||||
|
||||
var lastAddValuer AddValuer
|
||||
for {
|
||||
err = binary.Read(body, binary.BigEndian, &tag)
|
||||
if err != nil {
|
||||
log.Errorf("End of input before end of attributes tag (%v)", err.Error())
|
||||
}
|
||||
log.Debugf("Value tag - %v", tag)
|
||||
switch tag {
|
||||
case endOfAttributes:
|
||||
return
|
||||
case charsetValueTag:
|
||||
c := NewCharSetValue("", "")
|
||||
c.unmarshal(body)
|
||||
currentAttributeGroup[c.name] = c
|
||||
log.Debugf("%v %v", c.name, c.value)
|
||||
case uriValueTag:
|
||||
u := NewUriValue("", "")
|
||||
u.unmarshal(body)
|
||||
currentAttributeGroup[u.name] = u
|
||||
log.Debugf("%v %v", u.name, u.value)
|
||||
case naturalLanguageValueTag:
|
||||
n := NewNaturalLanguage("", "")
|
||||
n.unmarshal(body)
|
||||
currentAttributeGroup[n.name] = n
|
||||
log.Debugf("%v %v", n.name, n.value)
|
||||
case keyWordValueTag:
|
||||
name, value := unmarshalSingleValue(body)
|
||||
if name == "" {
|
||||
lastAddValuer.addValue(value)
|
||||
} else {
|
||||
k := NewKeyWord(name, value)
|
||||
currentAttributeGroup[name] = k
|
||||
lastAddValuer = k
|
||||
}
|
||||
log.Debugf("%v : %v", name, value)
|
||||
case nameWithoutLanguageValueTag:
|
||||
n := NewNameWithoutLanguage("", "")
|
||||
n.unmarshal(body)
|
||||
currentAttributeGroup[n.name] = n
|
||||
log.Debugf("%v %v", n.name, n.value)
|
||||
case mimeMediaTypeValueTag:
|
||||
name, value := unmarshalSingleValue(body)
|
||||
if name == "" {
|
||||
lastAddValuer.addValue(value)
|
||||
} else {
|
||||
m := NewMimeMediaType(name, value)
|
||||
currentAttributeGroup[name] = m
|
||||
lastAddValuer = m
|
||||
}
|
||||
log.Debugf("%v : %v", name, value)
|
||||
case jobAttributes:
|
||||
log.Debug("Start job attributes")
|
||||
currentAttributeGroup = r.jobAttributes
|
||||
case resolutionValueTag:
|
||||
res := NewResolution("", 0, 0)
|
||||
res.unmarshal(body)
|
||||
currentAttributeGroup[res.name] = res
|
||||
log.Debugf("Resolution %v", res)
|
||||
default:
|
||||
log.Errorf("Unsupported tag %v", tag)
|
||||
}
|
||||
}
|
||||
//log.Printf("Header %v", r.header)
|
||||
r.a = UnMarshalAttributues(body)
|
||||
}
|
||||
|
||||
func (r *Request) RequestId() uint32 {
|
||||
@@ -173,31 +69,48 @@ func (r *Request) Operation() OperationId {
|
||||
}
|
||||
|
||||
func (r *Request) GetAttribute(name string) Attribute {
|
||||
return r.operationAttributes[name]
|
||||
for _, a := range r.a.operation {
|
||||
if a.Name() == name {
|
||||
return a
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Request) Marshal() []byte {
|
||||
// //s = r.size
|
||||
|
||||
var buf bytes.Buffer
|
||||
buf.Write(r.header.marshal())
|
||||
if len(r.operationAttributes) > 0 {
|
||||
if len(r.a.operation) > 0 {
|
||||
buf.WriteByte(byte(operationAttributes))
|
||||
for _, e := range r.operationAttributes {
|
||||
for _, e := range r.a.operation {
|
||||
buf.Write(e.marshal())
|
||||
}
|
||||
}
|
||||
if len(r.jobAttributes) > 0 {
|
||||
if len(r.a.job) > 0 {
|
||||
buf.WriteByte(byte(jobAttributes))
|
||||
for _, e := range r.jobAttributes {
|
||||
for _, e := range r.a.job {
|
||||
buf.Write(e.marshal())
|
||||
}
|
||||
}
|
||||
if len(r.printerAttributes) > 0 {
|
||||
if len(r.a.printer) > 0 {
|
||||
buf.WriteByte(byte(printerAttributes))
|
||||
for _, e := range r.printerAttributes {
|
||||
for _, e := range r.a.printer {
|
||||
buf.Write(e.marshal())
|
||||
}
|
||||
}
|
||||
buf.WriteByte(byte(endOfAttributes))
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
func (r *Request) AddPrinterAttribute(a Attribute) {
|
||||
r.a.addAttribute(printerAttributes, a)
|
||||
}
|
||||
|
||||
func (r *Request) AddOperatonAttribute(a Attribute) {
|
||||
r.a.addAttribute(operationAttributes, a)
|
||||
}
|
||||
|
||||
func (r *Request) AddJobAttribute(a Attribute) {
|
||||
r.a.addAttribute(jobAttributes, a)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user