Skip to content

Commit

Permalink
refactor(plc4go/spi): converted WriteBufferByteBased options to prope…
Browse files Browse the repository at this point in the history
…r options
  • Loading branch information
sruehl committed Nov 2, 2022
1 parent 4318f4b commit e383adc
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 24 deletions.
3 changes: 2 additions & 1 deletion plc4go/internal/ads/MessageCodec.go
Expand Up @@ -20,6 +20,7 @@
package ads

import (
"encoding/binary"
"github.com/apache/plc4x/plc4go/protocols/ads/readwrite/model"
"github.com/apache/plc4x/plc4go/spi"
"github.com/apache/plc4x/plc4go/spi/default"
Expand Down Expand Up @@ -48,7 +49,7 @@ func (m *MessageCodec) Send(message spi.Message) error {
// Cast the message to the correct type of struct
tcpPaket := message.(model.AmsTCPPacket)
// Serialize the request
wb := utils.NewLittleEndianWriteBufferByteBased()
wb := utils.NewWriteBufferByteBased(utils.WithByteOrderForByteBasedBuffer(binary.LittleEndian))
err := tcpPaket.Serialize(wb)
if err != nil {
return errors.Wrap(err, "error serializing request")
Expand Down
3 changes: 2 additions & 1 deletion plc4go/internal/ads/Writer.go
Expand Up @@ -21,6 +21,7 @@ package ads

import (
"context"
"encoding/binary"
"github.com/apache/plc4x/plc4go/pkg/api/model"
readWriteModel "github.com/apache/plc4x/plc4go/protocols/ads/readwrite/model"
"github.com/apache/plc4x/plc4go/spi"
Expand Down Expand Up @@ -105,7 +106,7 @@ func (m *Writer) Write(ctx context.Context, writeRequest model.PlcWriteRequest)

// Get the value from the request and serialize it to a byte array
value := writeRequest.GetValue(fieldName)
io := utils.NewLittleEndianWriteBufferByteBased()
io := utils.NewWriteBufferByteBased(utils.WithByteOrderForByteBasedBuffer(binary.LittleEndian))
if err := readWriteModel.DataItemSerialize(io, value, adsField.Datatype.PlcValueType(), adsField.StringLength); err != nil {
result <- &plc4goModel.DefaultPlcWriteRequestResult{
Request: writeRequest,
Expand Down
3 changes: 2 additions & 1 deletion plc4go/internal/eip/MessageCodec.go
Expand Up @@ -20,6 +20,7 @@
package eip

import (
"encoding/binary"
"github.com/apache/plc4x/plc4go/protocols/eip/readwrite/model"
"github.com/apache/plc4x/plc4go/spi"
"github.com/apache/plc4x/plc4go/spi/default"
Expand Down Expand Up @@ -48,7 +49,7 @@ func (m *MessageCodec) Send(message spi.Message) error {
// Cast the message to the correct type of struct
eipPacket := message.(model.EipPacket)
// Serialize the request
wb := utils.NewLittleEndianWriteBufferByteBased()
wb := utils.NewWriteBufferByteBased(utils.WithByteOrderForByteBasedBuffer(binary.LittleEndian))
err := eipPacket.Serialize(wb)
if err != nil {
return errors.Wrap(err, "error serializing request")
Expand Down
3 changes: 2 additions & 1 deletion plc4go/internal/eip/Reader.go
Expand Up @@ -21,6 +21,7 @@ package eip

import (
"context"
"encoding/binary"
"github.com/apache/plc4x/plc4go/pkg/api/model"
"github.com/apache/plc4x/plc4go/pkg/api/values"
readWriteModel "github.com/apache/plc4x/plc4go/protocols/eip/readwrite/model"
Expand Down Expand Up @@ -316,7 +317,7 @@ func toAnsi(tag string) ([]byte, error) {
}
}

buffer := utils.NewLittleEndianWriteBufferByteBased()
buffer := utils.NewWriteBufferByteBased(utils.WithByteOrderForByteBasedBuffer(binary.LittleEndian))

err := buffer.WriteByte("", 0x91)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion plc4go/internal/eip/Writer.go
Expand Up @@ -21,6 +21,7 @@ package eip

import (
"context"
"encoding/binary"
"github.com/apache/plc4x/plc4go/pkg/api/model"
"github.com/apache/plc4x/plc4go/pkg/api/values"
readWriteModel "github.com/apache/plc4x/plc4go/protocols/eip/readwrite/model"
Expand Down Expand Up @@ -276,7 +277,7 @@ func (m Writer) Write(ctx context.Context, writeRequest model.PlcWriteRequest) <
}

func encodeValue(value values.PlcValue, _type readWriteModel.CIPDataTypeCode, elements uint16) ([]byte, error) {
buffer := utils.NewLittleEndianWriteBufferByteBased()
buffer := utils.NewWriteBufferByteBased(utils.WithByteOrderForByteBasedBuffer(binary.LittleEndian))
switch _type {
case readWriteModel.CIPDataTypeCode_SINT:
err := buffer.WriteByte("", value.GetUint8())
Expand Down
2 changes: 1 addition & 1 deletion plc4go/spi/MessageCodec.go
Expand Up @@ -50,7 +50,7 @@ type MessageCodec interface {
ConnectWithContext(ctx context.Context) error
// Disconnect disconnects this codec
Disconnect() error
// IsRunning returns tur if the codec (workers are running)
// IsRunning returns true if the codec (workers are running)
IsRunning() bool

// Send is sending a given message
Expand Down
4 changes: 2 additions & 2 deletions plc4go/spi/testutils/DriverTestRunner.go
Expand Up @@ -285,7 +285,7 @@ func (m DriverTestsuite) ExecuteStep(connection plc4go.PlcConnection, testcase *
if m.byteOrder == binary.BigEndian {
expectedWriteBuffer = utils.NewWriteBufferByteBased()
} else {
expectedWriteBuffer = utils.NewLittleEndianWriteBufferByteBased()
expectedWriteBuffer = utils.NewWriteBufferByteBased(utils.WithByteOrderForByteBasedBuffer(binary.LittleEndian))
}
err = expectedSerializable.Serialize(expectedWriteBuffer)
if err != nil {
Expand Down Expand Up @@ -371,7 +371,7 @@ func (m DriverTestsuite) ExecuteStep(connection plc4go.PlcConnection, testcase *
if m.byteOrder == binary.BigEndian {
wb = utils.NewWriteBufferByteBased()
} else {
wb = utils.NewLittleEndianWriteBufferByteBased()
wb = utils.NewWriteBufferByteBased(utils.WithByteOrderForByteBasedBuffer(binary.LittleEndian))
}
err = expectedSerializable.Serialize(wb)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion plc4go/spi/testutils/ParserSerializerTestRunner.go
Expand Up @@ -214,7 +214,7 @@ func RunParserSerializerTestsuite(t *testing.T, testPath string, skippedTestCase
}
var writeBuffer utils.WriteBufferByteBased
if byteOrder == binary.LittleEndian {
writeBuffer = utils.NewLittleEndianWriteBufferByteBased()
writeBuffer = utils.NewWriteBufferByteBased(utils.WithByteOrderForByteBasedBuffer(binary.LittleEndian))
} else {
writeBuffer = utils.NewWriteBufferByteBased()
}
Expand Down
35 changes: 20 additions & 15 deletions plc4go/spi/utils/WriteBufferByteBased.go
Expand Up @@ -35,32 +35,37 @@ type WriteBufferByteBased interface {
GetTotalBytes() uint64
}

func NewWriteBufferByteBased() WriteBufferByteBased {
func NewWriteBufferByteBased(options ...WriteBufferByteBasedOptions) WriteBufferByteBased {
data := new(bytes.Buffer)
writer := bitio.NewWriter(data)
return &byteWriteBuffer{
b := &byteWriteBuffer{
data: data,
writer: writer,
byteOrder: binary.BigEndian,
}
for _, option := range options {
option(b)
}
return b
}

func NewLittleEndianWriteBufferByteBased() WriteBufferByteBased {
data := new(bytes.Buffer)
writer := bitio.NewWriter(data)
return &byteWriteBuffer{
data: data,
writer: writer,
byteOrder: binary.LittleEndian,
type WriteBufferByteBasedOptions = func(b *byteWriteBuffer)

func WithInitialSizeForByteBasedBuffer(length int) WriteBufferByteBasedOptions {
return func(b *byteWriteBuffer) {
b.data = bytes.NewBuffer(make([]byte, length))
}
}

func NewCustomWriteBufferByteBased(buffer *bytes.Buffer, byteOrder binary.ByteOrder) WriteBufferByteBased {
writer := bitio.NewWriter(buffer)
return &byteWriteBuffer{
data: buffer,
writer: writer,
byteOrder: byteOrder,
func WithByteOrderForByteBasedBuffer(byteOrder binary.ByteOrder) WriteBufferByteBasedOptions {
return func(b *byteWriteBuffer) {
b.byteOrder = byteOrder
}
}

func WithCustomBufferForByteBasedBuffer(buffer *bytes.Buffer) WriteBufferByteBasedOptions {
return func(b *byteWriteBuffer) {
b.data = buffer
}
}

Expand Down

0 comments on commit e383adc

Please sign in to comment.