Compare commits

...

2 Commits

Author SHA1 Message Date
2453d639ff Ensure all pdus use pointer to header
All checks were successful
Run Tests / Test (push) Successful in 20s
Benchmark BufferPool / RunBenchmarks (push) Successful in 23s
2024-07-24 19:22:41 +02:00
14c861ccb4 Implement submit encode 2024-07-24 19:20:08 +02:00
8 changed files with 96 additions and 78 deletions

View File

@@ -2,7 +2,7 @@ package pdu
type ( type (
BIND struct { BIND struct {
header PDU_HEADER header *PDU_HEADER
system_id string system_id string
password string password string
system_type string system_type string
@@ -12,7 +12,7 @@ type (
address_range string address_range string
} }
BIND_RESP struct { BIND_RESP struct {
header PDU_HEADER header *PDU_HEADER
system_id string system_id string
sc_interface_version byte sc_interface_version byte
} }
@@ -36,9 +36,9 @@ type (
} }
UNBIND struct { UNBIND struct {
PDU_HEADER header *PDU_HEADER
} }
UNBIND_RESP struct { UNBIND_RESP struct {
PDU_HEADER header *PDU_HEADER
} }
) )

View File

@@ -2,7 +2,7 @@ package pdu
type ( type (
CANCEL_SM struct { CANCEL_SM struct {
header PDU_HEADER header *PDU_HEADER
service_type string service_type string
message_id string message_id string
source_addr_ton byte source_addr_ton byte
@@ -13,6 +13,6 @@ type (
destination_addr string destination_addr string
} }
CANCEL_SM_RESP struct { CANCEL_SM_RESP struct {
header PDU_HEADER header *PDU_HEADER
} }
) )

View File

@@ -2,11 +2,11 @@ package pdu
type ( type (
DELIVER_SM struct { DELIVER_SM struct {
header PDU_HEADER header *PDU_HEADER
SUBMIT_SM SUBMIT_SM
} }
DELIVER_SM_RESP struct { DELIVER_SM_RESP struct {
header PDU_HEADER header *PDU_HEADER
SUBMIT_SM_RESP SUBMIT_SM_RESP
} }
) )

View File

@@ -2,9 +2,9 @@ package pdu
type ( type (
ENQUIRE_LINK struct { ENQUIRE_LINK struct {
header PDU_HEADER header *PDU_HEADER
} }
ENQUIRE_LINK_RESP struct { ENQUIRE_LINK_RESP struct {
header PDU_HEADER header *PDU_HEADER
} }
) )

View File

@@ -22,7 +22,7 @@ type (
} }
GENERIC_NACK struct { GENERIC_NACK struct {
header PDU_HEADER header *PDU_HEADER
} }
) )
@@ -58,6 +58,9 @@ type (
// Don't know // Don't know
func (p *PDU_HEADER) Encode(buf *bytes.Buffer) error { func (p *PDU_HEADER) Encode(buf *bytes.Buffer) error {
if buf == nil {
return fmt.Errorf("cannot encode into nil buffer")
}
binary.Write(buf, binary.BigEndian, p.command_length) binary.Write(buf, binary.BigEndian, p.command_length)
binary.Write(buf, binary.BigEndian, p.command_id) binary.Write(buf, binary.BigEndian, p.command_id)
binary.Write(buf, binary.BigEndian, p.command_status) binary.Write(buf, binary.BigEndian, p.command_status)

View File

@@ -283,50 +283,6 @@ func TestEncodeIntoWithBoundaryValues(t *testing.T) {
} }
} }
// func TestRealScenario(t *testing.T) {
// expected := []byte{0, 0, 0, 54, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 1, 67, 77, 84, 0, 1, 1, 49, 50, 51, 52, 53, 0, 1, 1, 54, 55, 56, 57, 48, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 12, 72, 101, 108, 108, 111, 44, 32, 83, 77, 80, 80, 33}
// p := &SUBMIT_SM{
// header: PDU_HEADER{
// command_length: 0,
// command_id: 4,
// command_status: 0,
// sequence_number: 1,
// },
// service_type: "CMT",
// source_addr_ton: 1,
// source_addr_npi: 1,
// source_addr: "12345",
// dest_addr_ton: 1,
// dest_addr_npi: 1,
// destination_addr: "67890",
// esm_class: 0,
// protocol_id: 0,
// priority_flag: 0,
// schedule_delivery_time: "",
// validity_period: "",
// registered_delivery: 1,
// data_coding: 0,
// sm_default_msg_id: 0,
// short_message: "Hello, SMPP!",
// }
// p.header.command_length = uint32(p.Size())
// p.sm_length = byte(len(p.short_message))
// buf := make([]byte, p.Size())
// err := p.EncodeInto(&buf)
// if err != nil {
// t.Errorf("Expected no error, got %v", err)
// }
// if len(buf) != len(expected) {
// t.Errorf("Expected byte slice of length %d, got %d", len(expected), len(buf))
// }
// for i, v := range buf {
// if v != expected[i] {
// t.Errorf("Expected byte slice with values %v, got %v", expected, buf)
// break
// }
// }
// }
// region decode // region decode
func TestDecodeHandlesShortByteSlice(t *testing.T) { func TestDecodeHandlesShortByteSlice(t *testing.T) {
var p PDU_HEADER var p PDU_HEADER

View File

@@ -4,11 +4,12 @@ import (
"bytes" "bytes"
"encoding/ascii85" "encoding/ascii85"
"encoding/binary" "encoding/binary"
"fmt"
) )
type ( type (
SUBMIT_SM struct { SUBMIT_SM struct {
header PDU_HEADER header *PDU_HEADER
service_type string service_type string
source_addr_ton byte source_addr_ton byte
source_addr_npi byte source_addr_npi byte
@@ -57,7 +58,7 @@ type (
// ussd_service_op byte // ussd_service_op byte
} }
SUBMIT_SM_RESP struct { SUBMIT_SM_RESP struct {
header PDU_HEADER header *PDU_HEADER
message_id string message_id string
} }
SUBMIT_MULTI struct{} SUBMIT_MULTI struct{}
@@ -65,33 +66,41 @@ type (
) )
func (p *SUBMIT_SM) Encode(buf *bytes.Buffer) error { func (p *SUBMIT_SM) Encode(buf *bytes.Buffer) error {
if buf == nil {
return fmt.Errorf("cannot encode into nil buffer")
}
p.header.Encode(buf) p.header.Encode(buf)
n := ascii85.Encode(buf.Bytes(), []byte(p.service_type)) n := ascii85.Encode(buf.Bytes(), []byte(p.service_type))
buf.Truncate(n) buf.Truncate(n)
binary.Write(buf, binary.BigEndian, byte(len(p.service_type))) binary.Write(buf, binary.BigEndian, p.source_addr_ton)
binary.Write(buf, binary.BigEndian, p.source_addr_npi)
// service_type string n = ascii85.Encode(buf.Bytes(), []byte(p.source_addr))
// source_addr_ton byte buf.Truncate(n)
// source_addr_npi byte binary.Write(buf, binary.BigEndian, p.dest_addr_ton)
// source_addr string binary.Write(buf, binary.BigEndian, p.dest_addr_npi)
// dest_addr_ton byte n = ascii85.Encode(buf.Bytes(), []byte(p.destination_addr))
// dest_addr_npi byte buf.Truncate(n)
// destination_addr string binary.Write(buf, binary.BigEndian, p.esm_class)
// esm_class byte binary.Write(buf, binary.BigEndian, p.protocol_id)
// protocol_id byte binary.Write(buf, binary.BigEndian, p.priority_flag)
// priority_flag byte n = ascii85.Encode(buf.Bytes(), []byte(p.schedule_delivery_time))
// schedule_delivery_time string buf.Truncate(n)
// validity_period string n = ascii85.Encode(buf.Bytes(), []byte(p.validity_period))
// registered_delivery byte buf.Truncate(n)
// replace_if_present byte binary.Write(buf, binary.BigEndian, p.registered_delivery)
// data_coding byte binary.Write(buf, binary.BigEndian, p.replace_if_present)
// sm_default_msg_id byte binary.Write(buf, binary.BigEndian, p.data_coding)
// sm_length byte binary.Write(buf, binary.BigEndian, p.sm_default_msg_id)
// short_message string binary.Write(buf, binary.BigEndian, p.sm_length)
// TODO: Implement encodings bsed on p.data_coding
n = ascii85.Encode(buf.Bytes(), []byte(p.short_message))
buf.Truncate(n)
return nil return nil
} }
func (p *SUBMIT_SM) Decode(buf *bytes.Buffer) error { func (p *SUBMIT_SM) Decode(buf *bytes.Buffer) error {
if buf == nil {
return fmt.Errorf("cannot decode nil buffer")
}
return nil return nil
} }
func (p *SUBMIT_SM) Size() int { func (p *SUBMIT_SM) Size() int {

View File

@@ -4,6 +4,54 @@ import (
"testing" "testing"
) )
// region encode
// func TestRealScenario(t *testing.T) {
// expected := []byte{0, 0, 0, 54, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 1, 67, 77, 84, 0, 1, 1, 49, 50, 51, 52, 53, 0, 1, 1, 54, 55, 56, 57, 48, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 12, 72, 101, 108, 108, 111, 44, 32, 83, 77, 80, 80, 33}
// p := &SUBMIT_SM{
// header: PDU_HEADER{
// command_length: 0,
// command_id: 4,
// command_status: 0,
// sequence_number: 1,
// },
// service_type: "CMT",
// source_addr_ton: 1,
// source_addr_npi: 1,
// source_addr: "12345",
// dest_addr_ton: 1,
// dest_addr_npi: 1,
// destination_addr: "67890",
// esm_class: 0,
// protocol_id: 0,
// priority_flag: 0,
// schedule_delivery_time: "",
// validity_period: "",
// registered_delivery: 1,
// data_coding: 0,
// sm_default_msg_id: 0,
// short_message: "Hello, SMPP!",
// }
// p.header.command_length = uint32(p.Size())
// p.sm_length = byte(len(p.short_message))
// buf := make([]byte, p.Size())
// err := p.EncodeInto(&buf)
// if err != nil {
// t.Errorf("Expected no error, got %v", err)
// }
// if len(buf) != len(expected) {
// t.Errorf("Expected byte slice of length %d, got %d", len(expected), len(buf))
// }
// for i, v := range buf {
// if v != expected[i] {
// t.Errorf("Expected byte slice with values %v, got %v", expected, buf)
// break
// }
// }
// }
// region decode
// region size // region size
func TestCalculateSizeTypicalInstance(t *testing.T) { func TestCalculateSizeTypicalInstance(t *testing.T) {
p := &SUBMIT_SM{ p := &SUBMIT_SM{
@@ -53,3 +101,5 @@ func TestHandlesEmptyStringsForAllStringFields(t *testing.T) {
t.Errorf("Expected size %d, but got %d", expectedSize, actualSize) t.Errorf("Expected size %d, but got %d", expectedSize, actualSize)
} }
} }
// region benchmark