Skip to content

Commit

Permalink
reformation and add context.TODO, until I find the correct solution, …
Browse files Browse the repository at this point in the history
…most likely another setting
  • Loading branch information
laduchesneau committed Feb 28, 2024
1 parent 56900f5 commit 558f2d2
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 48 deletions.
3 changes: 2 additions & 1 deletion example/transeiver_with_request_window/CustomSubmitSM.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package main

import (
"github.com/linxGnu/gosmpp/pdu"
"math/rand"
"strconv"

"github.com/linxGnu/gosmpp/pdu"
)

// CustomSubmitSM by embedding the PDU interface
Expand Down
64 changes: 54 additions & 10 deletions example/transeiver_with_request_window/CustomWindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,84 @@ package main

import (
"context"
"github.com/linxGnu/gosmpp"
"golang.org/x/exp/maps"
"fmt"
cmap "github.com/orcaman/concurrent-map/v2"
"strconv"

"github.com/orcaman/concurrent-map/v2"
"github.com/linxGnu/gosmpp"
"golang.org/x/exp/maps"
)

type CustomWindow struct {
store cmap.ConcurrentMap[string, gosmpp.Request]
}

func (w CustomWindow) Set(ctx context.Context, request gosmpp.Request) {
w.store.Set(strconv.Itoa(int(request.PDU.GetSequenceNumber())), request)
for {
select {
case <-ctx.Done():
fmt.Println("Task cancelled")
return
default:
w.store.Set(strconv.Itoa(int(request.PDU.GetSequenceNumber())), request)
}
}
}

func (w CustomWindow) Get(ctx context.Context, sequenceNumber int32) (gosmpp.Request, bool) {
return w.store.Get(strconv.Itoa(int(sequenceNumber)))
for {
select {
case <-ctx.Done():
fmt.Println("Task cancelled")
return gosmpp.Request{}, false
default:
return w.store.Get(strconv.Itoa(int(sequenceNumber)))
}
}
}

func (w CustomWindow) List(ctx context.Context) []gosmpp.Request {
return maps.Values(w.store.Items())
//return maps.Values(w.store)
for {
select {
case <-ctx.Done():
return []gosmpp.Request{}
default:
return maps.Values(w.store.Items())
}
}
}

func (w CustomWindow) Delete(ctx context.Context, sequenceNumber int32) {
w.store.Remove(strconv.Itoa(int(sequenceNumber)))
for {
select {
case <-ctx.Done():
return
default:
w.store.Remove(strconv.Itoa(int(sequenceNumber)))
}
}
}

func (w CustomWindow) Clear(ctx context.Context) {
w.store.Clear()
for {
select {
case <-ctx.Done():
return
default:
w.store.Clear()
}
}
}

func (w CustomWindow) Length(ctx context.Context) int {
return w.store.Count()
for {
select {
case <-ctx.Done():
return -1
default:
return w.store.Count()
}
}
}

func NewCustomWindow() gosmpp.RequestWindowStore {
Expand Down
41 changes: 19 additions & 22 deletions receivable.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ func (t *receivable) windowCleanup() {
case <-t.ctx.Done():
return
case <-ticker.C:
for _, request := range t.settings.RequestWindowStore.List(nil) {
for _, request := range t.settings.RequestWindowStore.List(context.TODO()) {
if time.Since(request.TimeSent) > t.settings.PduExpireTimeOut {
t.settings.RequestWindowStore.Delete(nil, request.GetSequenceNumber())
t.settings.RequestWindowStore.Delete(context.TODO(), request.GetSequenceNumber())
if t.settings.OnExpiredPduRequest != nil {
t.settings.OnExpiredPduRequest(request.PDU)
}
Expand Down Expand Up @@ -158,9 +158,9 @@ func (t *receivable) handleWindowPdu(p pdu.PDU) (closing bool) {
*pdu.SubmitMultiResp,
*pdu.SubmitSMResp:
if t.settings.OnExpectedPduResponse != nil {
request, ok := t.settings.RequestWindowStore.Get(nil, p.GetSequenceNumber())
request, ok := t.settings.RequestWindowStore.Get(context.TODO(), p.GetSequenceNumber())
if ok {
t.settings.RequestWindowStore.Delete(nil, p.GetSequenceNumber())
t.settings.RequestWindowStore.Delete(context.TODO(), p.GetSequenceNumber())
response := Response{
PDU: p,
OriginalRequest: request,
Expand All @@ -173,15 +173,13 @@ func (t *receivable) handleWindowPdu(p pdu.PDU) (closing bool) {
case *pdu.EnquireLink:
if t.settings.EnableAutoRespond {
t.settings.response(pp.GetResponse())
} else {
if t.settings.OnReceivedPduRequest != nil {
r, closeBind := t.settings.OnReceivedPduRequest(p)
t.settings.response(r)
if closeBind {
time.Sleep(50 * time.Millisecond)
closing = true
t.closing(UnbindClosing)
}
} else if t.settings.OnReceivedPduRequest != nil {
r, closeBind := t.settings.OnReceivedPduRequest(p)
t.settings.response(r)
if closeBind {
time.Sleep(50 * time.Millisecond)
closing = true
t.closing(UnbindClosing)
}
}
case *pdu.Unbind:
Expand All @@ -192,15 +190,14 @@ func (t *receivable) handleWindowPdu(p pdu.PDU) (closing bool) {
time.Sleep(50 * time.Millisecond)
closing = true
t.closing(UnbindClosing)
} else {
if t.settings.OnReceivedPduRequest != nil {
r, closeBind := t.settings.OnReceivedPduRequest(p)
t.settings.response(r)
if closeBind {
time.Sleep(50 * time.Millisecond)
closing = true
t.closing(UnbindClosing)
}
} else if t.settings.OnReceivedPduRequest != nil {
r, closeBind := t.settings.OnReceivedPduRequest(p)
t.settings.response(r)
if closeBind {
time.Sleep(50 * time.Millisecond)
closing = true
t.closing(UnbindClosing)

}
}
default:
Expand Down
18 changes: 9 additions & 9 deletions transceivable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,36 +409,36 @@ func Test_newTransceivable(t *testing.T) {
})
}

type DefaultWindow struct {
type TestWindowStore struct {
store map[string]Request
}

func (w DefaultWindow) Set(ctx context.Context, request Request) {
func (w TestWindowStore) Set(_ context.Context, request Request) {
w.store[strconv.Itoa(int(request.PDU.GetSequenceNumber()))] = request
}

func (w DefaultWindow) Get(ctx context.Context, sequenceNumber int32) (Request, bool) {
func (w TestWindowStore) Get(_ context.Context, sequenceNumber int32) (Request, bool) {
return w.store[strconv.Itoa(int(sequenceNumber))], true
}

func (w DefaultWindow) List(ctx context.Context) []Request {
func (w TestWindowStore) List(_ context.Context) []Request {
return maps.Values(w.store)
}

func (w DefaultWindow) Delete(ctx context.Context, sequenceNumber int32) {
func (w TestWindowStore) Delete(_ context.Context, sequenceNumber int32) {
delete(w.store, strconv.Itoa(int(sequenceNumber)))
}

func (w DefaultWindow) Clear(ctx context.Context) {
w.store = make(map[string]Request)
func (w TestWindowStore) Clear(_ context.Context) {
maps.Clear(w.store)
}

func (w DefaultWindow) Length(ctx context.Context) int {
func (w TestWindowStore) Length(_ context.Context) int {
return len(w.store)
}

func NewTestWindow() RequestWindowStore {
return &DefaultWindow{
return &TestWindowStore{
store: make(map[string]Request),
}
}
13 changes: 7 additions & 6 deletions transmittable.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,13 @@ func (t *transmittable) close(state State) (err error) {

// concurrent-map has no func to verify initialization
// we need to do the same check in
if t.settings.RequestWindowConfig != nil && t.settings.RequestWindowStore.Length(nil) > 0 {
for _, request := range t.settings.RequestWindowStore.List(context.Background()) {
if t.settings.RequestWindowConfig != nil && t.settings.RequestWindowStore.Length(context.TODO()) > 0 {
for _, request := range t.settings.RequestWindowStore.List(context.TODO()) {
if t.settings.OnClosePduRequest != nil {
t.settings.OnClosePduRequest(request.PDU)
}
t.settings.RequestWindowStore.Delete(nil, request.GetSequenceNumber())
t.settings.RequestWindowStore.Delete(context.TODO(), request.GetSequenceNumber())
t.settings.RequestWindowStore.Delete(context.TODO(), request.GetSequenceNumber())
}
}
}
Expand Down Expand Up @@ -206,7 +207,7 @@ func (t *transmittable) write(p pdu.PDU) (n int, err error) {
}

if t.settings.RequestWindowConfig != nil && t.settings.MaxWindowSize > 0 && isAllowPDU(p) {
if t.settings.RequestWindowStore.Length(nil) < int(t.settings.MaxWindowSize) {
if t.settings.RequestWindowStore.Length(context.TODO()) < int(t.settings.MaxWindowSize) {
n, err = t.conn.WritePDU(p)
if err != nil {
return 0, err
Expand All @@ -215,7 +216,7 @@ func (t *transmittable) write(p pdu.PDU) (n int, err error) {
PDU: p,
TimeSent: time.Now(),
}
t.settings.RequestWindowStore.Set(nil, request)
t.settings.RequestWindowStore.Set(context.TODO(), request)
} else {
return 0, ErrWindowsFull
}
Expand All @@ -238,5 +239,5 @@ func isAllowPDU(p pdu.PDU) bool {
}

func (t *transmittable) GetWindowSize() int {
return t.settings.RequestWindowStore.Length(nil)
return t.settings.RequestWindowStore.Length(context.TODO())
}

0 comments on commit 558f2d2

Please sign in to comment.